diff --git a/front/stats.go b/front/stats.go index c1d93259..4be4266f 100644 --- a/front/stats.go +++ b/front/stats.go @@ -17,6 +17,7 @@ limitations under the License. package front import ( + "database/sql" "fmt" "github.com/dimkr/tootik/cfg" "github.com/dimkr/tootik/fed" @@ -85,7 +86,8 @@ func getActiveUsersGraph(r *request) string { func stats(w text.Writer, r *request) { prefix := fmt.Sprintf("https://%s/%%", cfg.Domain) - var usersCount, postsCount, postsToday, federatedPostsCount, federatedPostsToday, lastPost, lastFederatedPost, lastRegister, lastFederatedUser int64 + var usersCount, postsCount, postsToday, federatedPostsCount, federatedPostsToday int64 + var lastPost, lastFederatedPost, lastRegister, lastFederatedUser sql.NullInt64 var outboxSize, inboxSize int if err := r.QueryRow(`select count(*) from persons where id like ?`, prefix).Scan(&usersCount); err != nil { @@ -202,15 +204,23 @@ func stats(w text.Writer, r *request) { } w.Subtitle("Other Statistics") - w.Itemf("Latest local post: %s", time.Unix(lastPost, 0).Format(time.UnixDate)) - w.Itemf("Latest federated post: %s", time.Unix(lastFederatedPost, 0).Format(time.UnixDate)) + if lastPost.Valid { + w.Itemf("Latest local post: %s", time.Unix(lastPost.Int64, 0).Format(time.UnixDate)) + } + if lastFederatedPost.Valid { + w.Itemf("Latest federated post: %s", time.Unix(lastFederatedPost.Int64, 0).Format(time.UnixDate)) + } w.Itemf("Local posts today: %d", postsToday) w.Itemf("Federated posts today: %d", federatedPostsToday) w.Itemf("Local users: %d", usersCount) w.Itemf("Local posts: %d", postsCount) w.Itemf("Federated posts: %d", federatedPostsCount) - w.Itemf("Newest user: %s", time.Unix(lastRegister, 0).Format(time.UnixDate)) - w.Itemf("Latest federated user update: %s", time.Unix(lastFederatedUser, 0).Format(time.UnixDate)) + if lastRegister.Valid { + w.Itemf("Newest user: %s", time.Unix(lastRegister.Int64, 0).Format(time.UnixDate)) + } + if lastFederatedUser.Valid { + w.Itemf("Latest federated user update: %s", time.Unix(lastFederatedUser.Int64, 0).Format(time.UnixDate)) + } w.Itemf("Incoming activities queue size: %d", inboxSize) w.Itemf("Outgoing activities queue size: %d", outboxSize) } diff --git a/test/stats_test.go b/test/stats_test.go new file mode 100644 index 00000000..36055fc0 --- /dev/null +++ b/test/stats_test.go @@ -0,0 +1,45 @@ +/* +Copyright 2023 Dima Krasner + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package test + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestStats_NewInstance(t *testing.T) { + server := newTestServer() + defer server.Shutdown() + + assert := assert.New(t) + + stats := server.Handle("/stats", server.Alice) + assert.Regexp("^20 text/gemini\r\n", stats) +} + +func TestStats_WithPosts(t *testing.T) { + server := newTestServer() + defer server.Shutdown() + + assert := assert.New(t) + + whisper := server.Handle("/users/whisper?Hello%20world", server.Alice) + assert.Regexp("^30 /users/view/[0-9a-f]{64}\r\n$", whisper) + + stats := server.Handle("/stats", server.Alice) + assert.Regexp("^20 text/gemini\r\n", stats) +}