Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ func NewApiServer(config config.Config) *ApiServer {
panic(err)
}

totalWalletsCache, err := otter.MustBuilder[string, int64](16).
WithTTL(totalWalletsCacheTTL).
CollectStats().
Build()
if err != nil {
panic(err)
}

privateKey, err := crypto.HexToECDSA(config.DelegatePrivateKey)
if err != nil {
panic(err)
Expand Down Expand Up @@ -268,6 +276,7 @@ func NewApiServer(config config.Config) *ApiServer {
oauthTokenCache: &oauthTokenCache,
qualifiedPlaylistsCache: &qualifiedPlaylistsCache,
relatedUsersCache: &relatedUsersCache,
totalWalletsCache: &totalWalletsCache,
requestValidator: requestValidator,
rewardAttester: rewardAttester,
transactionSender: transactionSender,
Expand Down Expand Up @@ -822,6 +831,7 @@ type ApiServer struct {
oauthTokenCache *otter.Cache[string, oauthTokenCacheEntry]
qualifiedPlaylistsCache *otter.Cache[string, []int32]
relatedUsersCache *otter.Cache[string, []int32]
totalWalletsCache *otter.Cache[string, int64]
requestValidator *RequestValidator
rewardManagerClient *reward_manager.RewardManagerClient
claimableTokensClient *claimable_tokens.ClaimableTokensClient
Expand Down
10 changes: 10 additions & 0 deletions api/v1_metrics_total_wallets.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package api

import (
"time"

"github.com/gofiber/fiber/v2"
)

const totalWalletsCacheTTL = 5 * time.Minute
const totalWalletsCacheKey = "total"

func (app *ApiServer) v1MetricsTotalWallets(c *fiber.Ctx) error {
if total, ok := app.totalWalletsCache.Get(totalWalletsCacheKey); ok {
return c.JSON(fiber.Map{"data": map[string]int64{"total": total}})
}

var total int64
if err := app.pool.QueryRow(c.Context(), `
SELECT SUM(count)::bigint AS total
Expand All @@ -17,5 +26,6 @@ func (app *ApiServer) v1MetricsTotalWallets(c *fiber.Ctx) error {
return err
}

app.totalWalletsCache.Set(totalWalletsCacheKey, total)
return c.JSON(fiber.Map{"data": map[string]int64{"total": total}})
}
16 changes: 16 additions & 0 deletions api/v1_metrics_total_wallets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"api.audius.co/database"
"github.com/stretchr/testify/require"
)

func TestMetricsTotalWallets_Empty(t *testing.T) {
Expand Down Expand Up @@ -46,3 +47,18 @@ func TestMetricsTotalWallets_WithFixtures(t *testing.T) {
"data.total": 7,
})
}

func TestMetricsTotalWallets_Cache(t *testing.T) {
app := emptyTestApp(t)

_, ok := app.totalWalletsCache.Get(totalWalletsCacheKey)
require.False(t, ok)

app.totalWalletsCache.Set(totalWalletsCacheKey, 42)

status, body := testGet(t, app, "/v1/metrics/total_wallets")
require.Equal(t, 200, status)
jsonAssert(t, body, map[string]any{
"data.total": 42,
})
}
Loading