1+ /*
2+ * Copyright (c) 2024. Devtron Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
117package utils
218
319import (
@@ -9,7 +25,7 @@ import (
925type TimeRangeRequest struct {
1026 From * time.Time `json:"from" schema:"from"`
1127 To * time.Time `json:"to" schema:"to"`
12- TimeWindow * TimeWindows `json:"timeWindow" schema:"timeWindow" validate:"omitempty,oneof=today yesterday week month quarter lastWeek lastMonth"`
28+ TimeWindow * TimeWindows `json:"timeWindow" schema:"timeWindow" validate:"omitempty,oneof=today yesterday week month quarter lastWeek lastMonth lastQuarter last7Days last30Days last90Days "`
1329}
1430
1531func NewTimeRangeRequest (from * time.Time , to * time.Time ) * TimeRangeRequest {
@@ -34,14 +50,18 @@ func (timeRange TimeWindows) String() string {
3450
3551// Define constants for different time windows
3652const (
37- Today TimeWindows = "today"
38- Yesterday TimeWindows = "yesterday"
39- Week TimeWindows = "week"
40- Month TimeWindows = "month"
41- Quarter TimeWindows = "quarter"
42- LastWeek TimeWindows = "lastWeek"
43- LastMonth TimeWindows = "lastMonth"
44- Year TimeWindows = "year"
53+ Today TimeWindows = "today"
54+ Yesterday TimeWindows = "yesterday"
55+ Week TimeWindows = "week"
56+ Month TimeWindows = "month"
57+ Quarter TimeWindows = "quarter"
58+ LastWeek TimeWindows = "lastWeek"
59+ LastMonth TimeWindows = "lastMonth"
60+ Year TimeWindows = "year"
61+ LastQuarter TimeWindows = "lastQuarter"
62+ Last7Days TimeWindows = "last7Days"
63+ Last30Days TimeWindows = "last30Days"
64+ Last90Days TimeWindows = "last90Days"
4565)
4666
4767func (timeRange * TimeRangeRequest ) ParseAndValidateTimeRange () (* TimeRangeRequest , error ) {
@@ -89,9 +109,49 @@ func (timeRange *TimeRangeRequest) ParseAndValidateTimeRange() (*TimeRangeReques
89109 lastMonthStart := thisMonthStart .AddDate (0 , - 1 , 0 )
90110 lastMonthEnd := thisMonthStart .Add (- time .Second )
91111 return NewTimeRangeRequest (& lastMonthStart , & lastMonthEnd ), nil
112+ case LastQuarter :
113+ // Calculate current quarter
114+ currentQuarter := ((int (now .Month ()) - 1 ) / 3 ) + 1
115+
116+ // Calculate previous quarter
117+ var prevQuarter int
118+ var prevYear int
119+ if currentQuarter == 1 {
120+ // If current quarter is Q1, previous quarter is Q4 of previous year
121+ prevQuarter = 4
122+ prevYear = now .Year () - 1
123+ } else {
124+ // Otherwise, previous quarter is in the same year
125+ prevQuarter = currentQuarter - 1
126+ prevYear = now .Year ()
127+ }
128+
129+ // Calculate start and end of previous quarter
130+ prevQuarterStartMonth := time .Month ((prevQuarter - 1 )* 3 + 1 )
131+ prevQuarterStart := time .Date (prevYear , prevQuarterStartMonth , 1 , 0 , 0 , 0 , 0 , now .Location ())
132+
133+ // End of previous quarter is the start of current quarter minus 1 second
134+ currentQuarterStartMonth := time .Month ((currentQuarter - 1 )* 3 + 1 )
135+ currentQuarterStart := time .Date (now .Year (), currentQuarterStartMonth , 1 , 0 , 0 , 0 , 0 , now .Location ())
136+ if currentQuarter == 1 {
137+ // If current quarter is Q1, we need to calculate Q4 end of previous year
138+ currentQuarterStart = time .Date (now .Year (), time .January , 1 , 0 , 0 , 0 , 0 , now .Location ())
139+ }
140+ prevQuarterEnd := currentQuarterStart .Add (- time .Second )
141+
142+ return NewTimeRangeRequest (& prevQuarterStart , & prevQuarterEnd ), nil
92143 case Year :
93144 start := time .Date (now .Year (), 1 , 1 , 0 , 0 , 0 , 0 , now .Location ())
94145 return NewTimeRangeRequest (& start , & now ), nil
146+ case Last7Days :
147+ start := now .AddDate (0 , 0 , - 7 )
148+ return NewTimeRangeRequest (& start , & now ), nil
149+ case Last30Days :
150+ start := now .AddDate (0 , 0 , - 30 )
151+ return NewTimeRangeRequest (& start , & now ), nil
152+ case Last90Days :
153+ start := now .AddDate (0 , 0 , - 90 )
154+ return NewTimeRangeRequest (& start , & now ), nil
95155 default :
96156 return NewTimeRangeRequest (& time.Time {}, & time.Time {}), fmt .Errorf ("unsupported time window: %q" , * timeRange .TimeWindow )
97157 }
0 commit comments