append nil months to query get to cover all requested months (OSS) (#15420)

* fill out nil response months in activity log query handle response based on requested month data

* changelog

* reverse month ordering for nil end months

* typo caught in ent test
This commit is contained in:
Hridoy Roy 2022-05-16 13:01:28 -07:00 committed by GitHub
parent 390310409e
commit 90538739bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 4 deletions

3
changelog/15420.txt Normal file
View file

@ -0,0 +1,3 @@
```release-note:improvement
activity: return nil response months in activity log API when no month data exists
```

View file

@ -510,7 +510,8 @@ func (a *ActivityLog) getLastEntitySegmentNumber(ctx context.Context, startTime
// WalkEntitySegments loads each of the entity segments for a particular start time // WalkEntitySegments loads each of the entity segments for a particular start time
func (a *ActivityLog) WalkEntitySegments(ctx context.Context, func (a *ActivityLog) WalkEntitySegments(ctx context.Context,
startTime time.Time, startTime time.Time,
walkFn func(*activity.EntityActivityLog, time.Time)) error { walkFn func(*activity.EntityActivityLog, time.Time),
) error {
basePath := activityEntityBasePath + fmt.Sprint(startTime.Unix()) + "/" basePath := activityEntityBasePath + fmt.Sprint(startTime.Unix()) + "/"
pathList, err := a.view.List(ctx, basePath) pathList, err := a.view.List(ctx, basePath)
if err != nil { if err != nil {
@ -540,7 +541,8 @@ func (a *ActivityLog) WalkEntitySegments(ctx context.Context,
// WalkTokenSegments loads each of the token segments (expected 1) for a particular start time // WalkTokenSegments loads each of the token segments (expected 1) for a particular start time
func (a *ActivityLog) WalkTokenSegments(ctx context.Context, func (a *ActivityLog) WalkTokenSegments(ctx context.Context,
startTime time.Time, startTime time.Time,
walkFn func(*activity.TokenCount)) error { walkFn func(*activity.TokenCount),
) error {
basePath := activityTokenBasePath + fmt.Sprint(startTime.Unix()) + "/" basePath := activityTokenBasePath + fmt.Sprint(startTime.Unix()) + "/"
pathList, err := a.view.List(ctx, basePath) pathList, err := a.view.List(ctx, basePath)
if err != nil { if err != nil {
@ -1646,7 +1648,6 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
monthResponse := &ResponseMonth{ monthResponse := &ResponseMonth{
Timestamp: time.Unix(monthsRecord.Timestamp, 0).UTC().Format(time.RFC3339), Timestamp: time.Unix(monthsRecord.Timestamp, 0).UTC().Format(time.RFC3339),
} }
if int(monthsRecord.Counts.EntityClients+monthsRecord.Counts.NonEntityClients) != 0 { if int(monthsRecord.Counts.EntityClients+monthsRecord.Counts.NonEntityClients) != 0 {
nsResponse, err := prepareNSResponse(monthsRecord.Namespaces) nsResponse, err := prepareNSResponse(monthsRecord.Namespaces)
if err != nil { if err != nil {
@ -1698,12 +1699,40 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
}) })
} }
} }
months = modifyResponseMonths(months, startTime, endTime)
responseData["months"] = months responseData["months"] = months
return responseData, nil return responseData, nil
} }
// modifyResponseMonths fills out various parts of the query structure to help
// activity log clients parse the returned query.
func modifyResponseMonths(months []*ResponseMonth, start time.Time, end time.Time) []*ResponseMonth {
if len(months) == 0 {
return months
}
start = timeutil.StartOfMonth(start)
end = timeutil.EndOfMonth(end)
modifiedResponseMonths := make([]*ResponseMonth, 0)
firstMonth, err := time.Parse(time.RFC3339, months[0].Timestamp)
lastMonth, err2 := time.Parse(time.RFC3339, months[len(months)-1].Timestamp)
if err != nil || err2 != nil {
return months
}
for start.Before(firstMonth) {
monthPlaceholder := &ResponseMonth{Timestamp: start.UTC().Format(time.RFC3339)}
modifiedResponseMonths = append(modifiedResponseMonths, monthPlaceholder)
start = timeutil.StartOfMonth(start.AddDate(0, 1, 0))
}
modifiedResponseMonths = append(modifiedResponseMonths, months...)
for lastMonth.Before(end) {
lastMonth = timeutil.StartOfMonth(lastMonth.AddDate(0, 1, 0))
monthPlaceholder := &ResponseMonth{Timestamp: lastMonth.UTC().Format(time.RFC3339)}
modifiedResponseMonths = append(modifiedResponseMonths, monthPlaceholder)
}
return modifiedResponseMonths
}
type activityConfig struct { type activityConfig struct {
// DefaultReportMonths are the default number of months that are returned on // DefaultReportMonths are the default number of months that are returned on
// a report. The zero value uses the system default of 12. // a report. The zero value uses the system default of 12.