Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Current Month When Future Month Start Date is Provided #28042

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/28042.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
activity: The sys/internal/counters/activity endpoint will return current month data when the start_date parameter is set to a future date.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
activity: The sys/internal/counters/activity endpoint will return current month data when the start_date parameter is set to a future date.
activity: Fixed issue where sys/internal/counters/activity endpoint would not return current month data when the end_date parameter is set to a future date.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of this is subjective wording, but it should be end_date, not start_date. Feel free to take just the end_date fix

```
18 changes: 13 additions & 5 deletions vault/activity_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -1782,12 +1782,20 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
startTime = timeutil.StartOfMonth(startTime)
endTime = timeutil.EndOfMonth(endTime)

// At the max, we only want to return data up until the end of the current month.
// Adjust the end time be the current month if a future date has been provided.
endOfCurrentMonth := timeutil.EndOfMonth(a.clock.Now().UTC())
adjustedEndTime := endTime
if endTime.After(endOfCurrentMonth) {
adjustedEndTime = endOfCurrentMonth
}

// If the endTime of the query is the current month, request data from the queryStore
// with the endTime equal to the end of the last month, and add in the current month
// data.
precomputedQueryEndTime := endTime
if timeutil.IsCurrentMonth(endTime, a.clock.Now().UTC()) {
precomputedQueryEndTime = timeutil.EndOfMonth(timeutil.MonthsPreviousTo(1, timeutil.StartOfMonth(endTime)))
precomputedQueryEndTime := adjustedEndTime
if timeutil.IsCurrentMonth(adjustedEndTime, a.clock.Now().UTC()) {
precomputedQueryEndTime = timeutil.EndOfMonth(timeutil.MonthsPreviousTo(1, timeutil.StartOfMonth(adjustedEndTime)))
computePartial = true
}

Expand Down Expand Up @@ -1830,7 +1838,7 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T

// Estimate the current month totals. These record contains is complete with all the
// current month data, grouped by namespace and mounts
currentMonth, err := a.computeCurrentMonthForBillingPeriod(ctx, partialByMonth, startTime, endTime)
currentMonth, err := a.computeCurrentMonthForBillingPeriod(ctx, partialByMonth, startTime, adjustedEndTime)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1880,7 +1888,7 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
a.sortActivityLogMonthsResponse(months)

// Modify the final month output to make response more consumable based on API request
months = a.modifyResponseMonths(months, startTime, endTime)
months = a.modifyResponseMonths(months, startTime, adjustedEndTime)
responseData["months"] = months

return responseData, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,45 @@ func Test_ActivityLog_EmptyDataMonths(t *testing.T) {
}
}

// Test_ActivityLog_FutureEndDate queries a start time from the past
// and an end date in the future. The test
// verifies that the current month is returned in the response.
func Test_ActivityLog_FutureEndDate(t *testing.T) {
t.Parallel()
cluster := minimal.NewTestSoloCluster(t, nil)
client := cluster.Cores[0].Client
_, err := client.Logical().Write("sys/internal/counters/config", map[string]interface{}{
"enabled": "enable",
})
require.NoError(t, err)
_, err = clientcountutil.NewActivityLogData(client).
NewPreviousMonthData(1).
NewClientsSeen(10).
NewCurrentMonthData().
NewClientsSeen(10).
Write(context.Background(), generation.WriteOptions_WRITE_PRECOMPUTED_QUERIES, generation.WriteOptions_WRITE_ENTITIES)
require.NoError(t, err)

now := time.Now().UTC()
// query from the beginning of 3 months ago to beginning of next month
resp, err := client.Logical().ReadWithData("sys/internal/counters/activity", map[string][]string{
"end_time": {timeutil.StartOfNextMonth(now).Format(time.RFC3339)},
"start_time": {timeutil.StartOfMonth(timeutil.MonthsPreviousTo(3, now)).Format(time.RFC3339)},
})
require.NoError(t, err)
monthsResponse := getMonthsData(t, resp)

require.Len(t, monthsResponse, 4)

// Get the last month of data in the slice
expectedCurrentMonthData := monthsResponse[3]
expectedTime, err := time.Parse(time.RFC3339, expectedCurrentMonthData.Timestamp)
require.NoError(t, err)
if !timeutil.IsCurrentMonth(expectedTime, now) {
t.Fatalf("final month data is not current month")
}
}

func getMonthsData(t *testing.T, resp *api.Secret) []vault.ResponseMonth {
t.Helper()
monthsRaw, ok := resp.Data["months"]
Expand Down
Loading