From 1575454ae671858789768368c3f9fb29f08e416c Mon Sep 17 00:00:00 2001 From: Hridoy Roy Date: Fri, 3 Jun 2022 10:34:54 -0700 Subject: [PATCH] =?UTF-8?q?refactor=20some=20code=20in=20modifyResponseMon?= =?UTF-8?q?ths=20and=20ensure=20that=20the=20last=20mo=E2=80=A6=20(#15767)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactr some code in modifyResponseMonths and ensure that the last month comparison with end is comparing end of month with end of month * calibrate end of month apropriately and fix parens issue for lastmonth --- vault/activity_log.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/vault/activity_log.go b/vault/activity_log.go index 1aa044cd6ae2..8ec8e81635d9 100644 --- a/vault/activity_log.go +++ b/vault/activity_log.go @@ -1719,14 +1719,13 @@ func modifyResponseMonths(months []*ResponseMonth, start time.Time, end time.Tim return months } start = timeutil.StartOfMonth(start) + end = timeutil.EndOfMonth(end) if timeutil.IsCurrentMonth(end, time.Now().UTC()) { - end = timeutil.StartOfMonth(end).AddDate(0, -1, 0) + end = timeutil.EndOfMonth(timeutil.StartOfMonth(end).AddDate(0, -1, 0)) } - 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 { + if err != nil { return months } for start.Before(firstMonth) { @@ -1735,8 +1734,13 @@ func modifyResponseMonths(months []*ResponseMonth, start time.Time, end time.Tim start = timeutil.StartOfMonth(start.AddDate(0, 1, 0)) } modifiedResponseMonths = append(modifiedResponseMonths, months...) + lastMonthStart, err := time.Parse(time.RFC3339, modifiedResponseMonths[len(modifiedResponseMonths)-1].Timestamp) + if err != nil { + return modifiedResponseMonths + } + lastMonth := timeutil.EndOfMonth(lastMonthStart) for lastMonth.Before(end) { - lastMonth = timeutil.StartOfMonth(lastMonth.AddDate(0, 1, 0)) + lastMonth = timeutil.StartOfMonth(lastMonth).AddDate(0, 1, 0) monthPlaceholder := &ResponseMonth{Timestamp: lastMonth.UTC().Format(time.RFC3339)} modifiedResponseMonths = append(modifiedResponseMonths, monthPlaceholder)