async queries for /totals function

Change-Id: If9f1267fc713e547657fbd60e7390ed59f5b9d03
This commit is contained in:
justinschuldt 2021-10-29 03:59:41 -05:00 committed by Justin Schuldt
parent 90f7015b93
commit 82b4ed5843
1 changed files with 46 additions and 35 deletions

View File

@ -57,15 +57,19 @@ func fetchRowsInInterval(tbl *bigtable.Table, ctx context.Context, prefix string
}
func createCountsOfInterval(tbl *bigtable.Table, ctx context.Context, prefix string, numPrevDays int, keySegments int) (map[string]map[string]int, error) {
var mu sync.RWMutex
results := map[string]map[string]int{}
// key track of all the keys seen, to ensure the result objects all have the same keys
seenKeySet := map[string]bool{}
now := time.Now()
for daysAgo := 0; daysAgo <= numPrevDays; daysAgo++ {
var intervalsWG sync.WaitGroup
// there will be a query for each previous day, plus today
intervalsWG.Add(numPrevDays + 1)
for daysAgo := 0; daysAgo <= numPrevDays; daysAgo++ {
go func(tbl *bigtable.Table, ctx context.Context, prefix string, daysAgo int) {
// start is the SOD, end is EOD
// "0 daysAgo start" is 00:00:00 AM of the current day
// "0 daysAgo end" is 23:59:59 of the current day (the future)
@ -82,14 +86,18 @@ func createCountsOfInterval(tbl *bigtable.Table, ctx context.Context, prefix str
start := time.Date(year, month, day, 0, 0, 0, 0, loc)
end := time.Date(year, month, day, 23, 59, 59, maxNano, loc)
result, fetchErr := fetchRowsInInterval(tbl, ctx, prefix, start, end)
var result []bigtable.Row
var fetchErr error
defer intervalsWG.Done()
result, fetchErr = fetchRowsInInterval(tbl, ctx, prefix, start, end)
if fetchErr != nil {
log.Printf("fetchRowsInInterval returned an error: %v", fetchErr)
return nil, fetchErr
log.Fatalf("fetchRowsInInterval returned an error: %v", fetchErr)
}
dateStr := start.Format("2006-01-02")
mu.Lock()
// initialize the map for this date in the result set
if results[dateStr] == nil {
results[dateStr] = map[string]int{"*": 0}
@ -106,6 +114,8 @@ func createCountsOfInterval(tbl *bigtable.Table, ctx context.Context, prefix str
// add this key to the set
seenKeySet[countBy] = true
}
mu.Unlock()
}(tbl, ctx, prefix, daysAgo)
}
// ensure each date object has the same keys:
@ -117,6 +127,7 @@ func createCountsOfInterval(tbl *bigtable.Table, ctx context.Context, prefix str
}
}
}
intervalsWG.Wait()
return results, nil
}