Skip to content

Commit 81eed6f

Browse files
committed
feat: timespan query file support
1 parent d1583a5 commit 81eed6f

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

core/controller/dashboard.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"gorm.io/gorm"
1616
)
1717

18-
func getDashboardTime(t string, startTimestamp int64, endTimestamp int64, timezoneLocation *time.Location) (time.Time, time.Time, model.TimeSpanType) {
18+
func getDashboardTime(t string, timespan string, startTimestamp int64, endTimestamp int64, timezoneLocation *time.Location) (time.Time, time.Time, model.TimeSpanType) {
1919
end := time.Now()
2020
if endTimestamp != 0 {
2121
end = time.Unix(endTimestamp, 0)
@@ -47,6 +47,10 @@ func getDashboardTime(t string, startTimestamp int64, endTimestamp int64, timezo
4747
if startTimestamp != 0 {
4848
start = time.Unix(startTimestamp, 0)
4949
}
50+
switch model.TimeSpanType(timespan) {
51+
case model.TimeSpanDay, model.TimeSpanHour:
52+
timeSpan = model.TimeSpanType(timespan)
53+
}
5054
return start, end, timeSpan
5155
}
5256

@@ -150,13 +154,15 @@ func fillGaps(data []*model.ChartData, start, end time.Time, t model.TimeSpanTyp
150154
// @Param start_timestamp query int64 false "Start second timestamp"
151155
// @Param end_timestamp query int64 false "End second timestamp"
152156
// @Param timezone query string false "Timezone, default is Local"
157+
// @Param timespan query string false "Time span type (day, hour)"
153158
// @Success 200 {object} middleware.APIResponse{data=model.DashboardResponse}
154159
// @Router /api/dashboard/ [get]
155160
func GetDashboard(c *gin.Context) {
156161
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
157162
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
158163
timezoneLocation, _ := time.LoadLocation(c.DefaultQuery("timezone", "Local"))
159-
start, end, timeSpan := getDashboardTime(c.Query("type"), startTimestamp, endTimestamp, timezoneLocation)
164+
timespan := c.Query("timespan")
165+
start, end, timeSpan := getDashboardTime(c.Query("type"), timespan, startTimestamp, endTimestamp, timezoneLocation)
160166
modelName := c.Query("model")
161167
channelStr := c.Query("channel")
162168
channelID, _ := strconv.Atoi(channelStr)
@@ -194,6 +200,7 @@ func GetDashboard(c *gin.Context) {
194200
// @Param start_timestamp query int64 false "Start second timestamp"
195201
// @Param end_timestamp query int64 false "End second timestamp"
196202
// @Param timezone query string false "Timezone, default is Local"
203+
// @Param timespan query string false "Time span type (day, hour)"
197204
// @Success 200 {object} middleware.APIResponse{data=model.GroupDashboardResponse}
198205
// @Router /api/dashboard/{group} [get]
199206
func GetGroupDashboard(c *gin.Context) {
@@ -206,7 +213,8 @@ func GetGroupDashboard(c *gin.Context) {
206213
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
207214
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
208215
timezoneLocation, _ := time.LoadLocation(c.DefaultQuery("timezone", "Local"))
209-
start, end, timeSpan := getDashboardTime(c.Query("type"), startTimestamp, endTimestamp, timezoneLocation)
216+
timespan := c.Query("timespan")
217+
start, end, timeSpan := getDashboardTime(c.Query("type"), timespan, startTimestamp, endTimestamp, timezoneLocation)
210218
tokenName := c.Query("token_name")
211219
modelName := c.Query("model")
212220

core/docs/docs.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,12 @@ const docTemplate = `{
972972
"description": "Timezone, default is Local",
973973
"name": "timezone",
974974
"in": "query"
975+
},
976+
{
977+
"type": "string",
978+
"description": "Time span type (day, hour)",
979+
"name": "timespan",
980+
"in": "query"
975981
}
976982
],
977983
"responses": {
@@ -1054,6 +1060,12 @@ const docTemplate = `{
10541060
"description": "Timezone, default is Local",
10551061
"name": "timezone",
10561062
"in": "query"
1063+
},
1064+
{
1065+
"type": "string",
1066+
"description": "Time span type (day, hour)",
1067+
"name": "timespan",
1068+
"in": "query"
10571069
}
10581070
],
10591071
"responses": {

core/docs/swagger.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,12 @@
963963
"description": "Timezone, default is Local",
964964
"name": "timezone",
965965
"in": "query"
966+
},
967+
{
968+
"type": "string",
969+
"description": "Time span type (day, hour)",
970+
"name": "timespan",
971+
"in": "query"
966972
}
967973
],
968974
"responses": {
@@ -1045,6 +1051,12 @@
10451051
"description": "Timezone, default is Local",
10461052
"name": "timezone",
10471053
"in": "query"
1054+
},
1055+
{
1056+
"type": "string",
1057+
"description": "Time span type (day, hour)",
1058+
"name": "timespan",
1059+
"in": "query"
10481060
}
10491061
],
10501062
"responses": {

core/docs/swagger.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,6 +2296,10 @@ paths:
22962296
in: query
22972297
name: timezone
22982298
type: string
2299+
- description: Time span type (day, hour)
2300+
in: query
2301+
name: timespan
2302+
type: string
22992303
produces:
23002304
- application/json
23012305
responses:
@@ -2346,6 +2350,10 @@ paths:
23462350
in: query
23472351
name: timezone
23482352
type: string
2353+
- description: Time span type (day, hour)
2354+
in: query
2355+
name: timespan
2356+
type: string
23492357
produces:
23502358
- application/json
23512359
responses:

0 commit comments

Comments
 (0)