Skip to content

Commit

Permalink
Resource Usage: Do not error on missing data when fetching current
Browse files Browse the repository at this point in the history
After fetching range, we error when there's no data matching the next
query.

The query fetches data for the timestamp `To` returned from the range
query. But the query has `request > usage`, so when that doesn't match
the query might return no rows.

In that case, for now, let's return empty data
  • Loading branch information
thokra-nav committed Apr 15, 2024
1 parent 2a79e95 commit 0a5cd99
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions internal/resourceusage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package resourceusage

import (
"context"
"errors"
"fmt"
"sort"
"time"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/nais/api/internal/database"
"github.com/nais/api/internal/database/gensql"
Expand Down Expand Up @@ -171,27 +173,32 @@ func (c *client) CurrentResourceUtilizationForApp(ctx context.Context, env strin
func (c *client) CurrentResourceUtilizationForTeam(ctx context.Context, team slug.Slug) (*model.CurrentResourceUtilization, error) {
timeRange, err := c.db.ResourceUtilizationRangeForTeam(ctx, team)
if err != nil {
return nil, err
return nil, fmt.Errorf("fetching resource utilization range: %w", err)
}

if timeRange.To.Time.Before(time.Now().UTC().Add(-3 * time.Hour)) {
return nil, nil
}

ts := pgtype.Timestamptz{}
err = ts.Scan(timeRange.To.Time)
if err != nil {
return nil, err
ts := pgtype.Timestamptz{
Time: timeRange.To.Time,
Valid: true,
}

currentCpu, err := c.db.SpecificResourceUtilizationForTeam(ctx, team, gensql.ResourceTypeCpu, ts)
if err != nil {
return nil, err
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("fetching current cpu: %w", err)
}

currentMemory, err := c.db.SpecificResourceUtilizationForTeam(ctx, team, gensql.ResourceTypeMemory, ts)
if err != nil {
return nil, err
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("fetching current memory: %w", err)
}

return &model.CurrentResourceUtilization{
Expand Down

0 comments on commit 0a5cd99

Please sign in to comment.