Skip to content

Commit

Permalink
fix(HMS-2002): postpone db stats 10 minutes
Browse files Browse the repository at this point in the history
Signed-off-by: Lukáš Zapletal <[email protected]>
  • Loading branch information
lzap authored and adiabramovitch committed Jul 26, 2023
1 parent 88430bf commit cfcaecb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config/api.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
# STATS_JOBQUEUE_INTERVAL int64
# how often to pull job queue statistics (default "1m")
# STATS_RESERVATIONS_INTERVAL int64
# how often to pull reservation statistics (default "30m")
# how often to pull reservation statistics (default "10m")
# TELEMETRY_ENABLED bool
# open telemetry collecting (default "false")
# TELEMETRY_JAEGER_ENABLED bool
Expand Down
2 changes: 1 addition & 1 deletion internal/background/db_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func dbStatsObserveTick(ctx context.Context) {
func dbStatsTick(ctx context.Context) error {
logger := zerolog.Ctx(ctx)
sdao := dao.GetStatDao(ctx)
stats, err := sdao.Get(ctx)
stats, err := sdao.Get(ctx, 10)
if err != nil {
return fmt.Errorf("stats error: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var config struct {
} `env-prefix:"APP_"`
Stats struct {
JobQueue time.Duration `env:"JOBQUEUE_INTERVAL" env-default:"1m" env-description:"how often to pull job queue statistics"`
ReservationsInterval time.Duration `env:"RESERVATIONS_INTERVAL" env-default:"30m" env-description:"how often to pull reservation statistics"`
ReservationsInterval time.Duration `env:"RESERVATIONS_INTERVAL" env-default:"10m" env-description:"how often to pull reservation statistics"`
} `env-prefix:"STATS_"`
Database struct {
Host string `env:"HOST" env-default:"localhost" env-description:"main database hostname"`
Expand Down
2 changes: 1 addition & 1 deletion internal/dao/dao_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ var GetStatDao func(ctx context.Context) StatDao

// StatDao represents an account (tenant)
type StatDao interface {
Get(ctx context.Context) (*models.Statistics, error)
Get(ctx context.Context, delayMin int) (*models.Statistics, error)
}
17 changes: 9 additions & 8 deletions internal/dao/pgx/stat_pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ func getStatDao(ctx context.Context) dao.StatDao {
return &statDao{}
}

func (x *statDao) getUsage(ctx context.Context, interval string) ([]*models.UsageStat, error) {
func (x *statDao) getUsage(ctx context.Context, iStart, iEnd string) ([]*models.UsageStat, error) {
query := `select provider, 'success' as result, count(provider) as count
from reservations
where created_at >= now() - cast($1 as interval)
where created_at between now() - cast($1 as interval) and now() - cast($2 as interval)
and success = true
group by provider
union all
select provider, 'failure' as result, count(provider) as count
from reservations
where created_at >= now() - cast($1 as interval)
where created_at between now() - cast($1 as interval) and now() - cast($2 as interval)
and success = false
group by provider
union all
select provider, 'pending' as result, count(provider) as count
from reservations
where created_at >= now() - cast($1 as interval)
where created_at between now() - cast($1 as interval) and now() - cast($2 as interval)
and success is null
group by provider`

var result []*models.UsageStat
rows, err := db.Pool.Query(ctx, query, interval)
rows, err := db.Pool.Query(ctx, query, iStart, iEnd)
if err != nil {
return nil, fmt.Errorf("pgx error: %w", err)
}
Expand All @@ -57,13 +57,14 @@ func (x *statDao) getUsage(ctx context.Context, interval string) ([]*models.Usag
return result, nil
}

func (x *statDao) Get(ctx context.Context) (*models.Statistics, error) {
usage24h, err := x.getUsage(ctx, "24 hours")
func (x *statDao) Get(ctx context.Context, delayMin int) (*models.Statistics, error) {
delay := fmt.Sprintf("%d minutes", delayMin)
usage24h, err := x.getUsage(ctx, "24 hours "+delay, delay)
if err != nil {
return nil, fmt.Errorf("get usage error: %w", err)
}

usage28d, err := x.getUsage(ctx, "28 days")
usage28d, err := x.getUsage(ctx, "28 days "+delay, delay)
if err != nil {
return nil, fmt.Errorf("get usage error: %w", err)
}
Expand Down
29 changes: 29 additions & 0 deletions internal/dao/tests/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build integration
// +build integration

package tests

import (
"testing"

"github.com/RHEnVision/provisioning-backend/internal/dao"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestStats(t *testing.T) {
reservationDao, ctx := setupReservation(t)
defer reset()

t.Run("success", func(t *testing.T) {
res := newNoopReservation()
err := reservationDao.CreateNoop(ctx, res)
require.NoError(t, err)

statDao := dao.GetStatDao(ctx)
stats, err := statDao.Get(ctx, 0)

require.NoError(t, err)
assert.Equal(t, int64(1), stats.Usage24h[0].Count)
})
}

0 comments on commit cfcaecb

Please sign in to comment.