Skip to content

Commit

Permalink
fix error in stats page when instance has no posts
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 30, 2023
1 parent f5a843c commit abc9d59
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
20 changes: 15 additions & 5 deletions front/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package front

import (
"database/sql"
"fmt"
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/fed"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
45 changes: 45 additions & 0 deletions test/stats_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit abc9d59

Please sign in to comment.