Skip to content

Commit

Permalink
fix(ui): panics when no data due to laziness
Browse files Browse the repository at this point in the history
  • Loading branch information
philwinder committed May 5, 2023
1 parent 3e40530 commit a60e98d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ func (r *analyticsRepository) QueryMostRecentResultsByKey(ctx context.Context, p
if err != nil {
return nil, err
}
if len(rows) == 0 {
return &QueryResults{
Results: []QueryResult{},
Total: 0,
}, nil
}
results := make([]QueryResult, len(rows))
for i, row := range rows {
results[i] = QueryResult{
Expand Down
8 changes: 8 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (a *amplifyAPI) GetV0AnalyticsRecentResultsResultMetadataKey(w http.Respons
sendError(r.Context(), w, http.StatusInternalServerError, "Could not query analytics", err.Error())
return
}
if results == nil {
sendError(r.Context(), w, http.StatusBadRequest, "No results in database", err.Error())
return
}
resultDatum := make([]ResultDatum, 0, len(results.Results))
for _, v := range results.Results {
if v.Key != "" {
Expand Down Expand Up @@ -187,6 +191,10 @@ func (a *amplifyAPI) GetV0AnalyticsMetricsOverTimeKind(w http.ResponseWriter, r
sendError(r.Context(), w, http.StatusInternalServerError, "Could not query analytics", err.Error())
return
}
if results == nil {
sendError(r.Context(), w, http.StatusBadRequest, "No results in database", err.Error())
return
}
resultDatum := make([]ResultDatum, len(results.Results))
for i, v := range results.Results {
resultDatum[i] = ResultDatum{
Expand Down
39 changes: 33 additions & 6 deletions pkg/db/in_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"
"sync"
"time"

"github.com/bacalhau-project/amplify/pkg/util"
"github.com/google/uuid"
Expand Down Expand Up @@ -293,16 +294,42 @@ func (db *inMemDB) CountQueryTopResultsByKey(ctx context.Context, key string) (i
return int64(len(res)), nil
}

func (*inMemDB) QueryMostRecentResultsByKey(ctx context.Context, arg QueryMostRecentResultsByKeyParams) ([]QueryMostRecentResultsByKeyRow, error) {
panic("unimplemented")
func (r *inMemDB) QueryMostRecentResultsByKey(ctx context.Context, arg QueryMostRecentResultsByKeyParams) ([]QueryMostRecentResultsByKeyRow, error) {
arg.Key = strings.ToLower(arg.Key)
results := make(map[string]int)
rows := make([]QueryMostRecentResultsByKeyRow, 0, len(results))
for _, v := range r.resultMeta {
v.Type = strings.ToLower(v.Type)
if v.Type == strings.ToLower(arg.Key) {
if _, ok := results[v.Value]; !ok {
rows = append(rows, QueryMostRecentResultsByKeyRow{
Value: v.Value,
FullCount: 0,
})
}
}
}
return rows, nil
}

func (*inMemDB) NumResultsOverTime(ctx context.Context, arg NumResultsOverTimeParams) ([]NumResultsOverTimeRow, error) {
panic("unimplemented")
func (r *inMemDB) NumResultsOverTime(ctx context.Context, arg NumResultsOverTimeParams) ([]NumResultsOverTimeRow, error) {
return []NumResultsOverTimeRow{
{
TbTimestamp: time.Now(),
Count: int64(len(r.results)),
FullCount: int64(len(r.results)),
},
}, nil
}

func (*inMemDB) NumSubmissionsOverTime(ctx context.Context, arg NumSubmissionsOverTimeParams) ([]NumSubmissionsOverTimeRow, error) {
panic("unimplemented")
func (r *inMemDB) NumSubmissionsOverTime(ctx context.Context, arg NumSubmissionsOverTimeParams) ([]NumSubmissionsOverTimeRow, error) {
return []NumSubmissionsOverTimeRow{
{
TbTimestamp: time.Now(),
Count: int64(len(r.queueItems)),
FullCount: int64(len(r.queueItems)),
},
}, nil
}

func dedupAndSort(s []int32) []int32 {
Expand Down

0 comments on commit a60e98d

Please sign in to comment.