add: user headmap timestamp range

This commit is contained in:
vilet.yy 2021-05-26 14:14:09 +08:00
parent 90f228fd3e
commit b8fd0c3f98
5 changed files with 92 additions and 8 deletions

View File

@ -0,0 +1,18 @@
package models
import "code.gitea.io/gitea/modules/timeutil"
type TimestampOptions struct {
Start timeutil.TimeStamp
End timeutil.TimeStamp
}
func (opts *TimestampOptions) setDefaultValues() {
if opts.Start <= 0 {
opts.Start = timeutil.TimeStampNow() - 365*24*60*60
}
if opts.End <= 0 {
opts.End = timeutil.TimeStampNow()
}
}

View File

@ -56,3 +56,42 @@ func GetUserHeatmapDataByUser(user *User) ([]*UserHeatmapData, error) {
return hdata, err
}
func GetUserHeatMapDataByUserWithTimeStamp(user *User, opts TimestampOptions) ([]*UserHeatmapData, error) {
opts.setDefaultValues()
hdata := make([]*UserHeatmapData, 0)
if user.KeepActivityPrivate {
return hdata, nil
}
var groupBy string
var groupByName = "timestamp"
switch {
case setting.Database.UseSQLite3:
groupBy = "strftime('%s', strftime('%Y-%m-%d', created_unix, 'unixepoch'))"
case setting.Database.UseMySQL:
groupBy = "UNIX_TIMESTAMP(DATE(FROM_UNIXTIME(created_unix)))"
case setting.Database.UsePostgreSQL:
groupBy = "extract(epoch from data_trunc('day', to_timestamp(created_unix)))"
case setting.Database.UseMSSQL:
groupBy = "datediff(SECOND, '19700101', dateadd(DAY, 0, datediff(day, 0, dateadd(s, created_unix, '19700101'))))"
groupByName = groupBy
}
sess := x.Select(groupBy+" AS timestamp, count(user_id) as contributions").
Table("action").
Where("user_id = ?", user.ID).
And("created_unix > ?", opts.Start).
And("created_unix < ?", opts.End)
if user.Type == UserTypeIndividual {
sess = sess.And("act_user_id = ?", user.ID)
}
err := sess.GroupBy(groupByName).
OrderBy("timestamp").
Find(&hdata)
return hdata, err
}

View File

@ -148,12 +148,24 @@ func GetUserHeatmapData(ctx *context.APIContext) {
// description: username of user to get
// type: string
// required: true
// - name: start
// in: query
// description: Query start timestamp
// type: string
// required: false
// - name: end
// in: query
// description: Query end timestamp
// type: string
// required: false
// responses:
// "200":
// "$ref": "#/responses/UserHeatmapData"
// "404":
// "$ref": "#/responses/notFound"
timeStampOptions := utils.GetTimestampOptions(ctx)
// Get the user to throw an error if it does not exist
user, err := models.GetUserByName(ctx.Params(":username"))
if err != nil {
@ -164,8 +176,8 @@ func GetUserHeatmapData(ctx *context.APIContext) {
}
return
}
heatmap, err := models.GetUserHeatmapDataByUser(user)
heatmap, err := models.GetUserHeatMapDataByUserWithTimeStamp(user, timeStampOptions)
// heatmap, err := models.GetUserHeatmapDataByUser(user)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
return

View File

@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/timeutil"
)
// GetQueryBeforeSince return parsed time (unix format) from URL query's before and since
@ -46,3 +47,10 @@ func GetListOptions(ctx *context.APIContext) models.ListOptions {
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
}
}
func GetTimestampOptions(ctx *context.APIContext) models.TimestampOptions {
return models.TimestampOptions{
Start: timeutil.TimeStamp(ctx.QueryInt("start")),
End: timeutil.TimeStamp(ctx.QueryInt("end")),
}
}

View File

@ -21,7 +21,7 @@
},
"version": "1.1.1"
},
"basePath": "/api/v1",
"basePath": "{{AppSubUrl}}/api/v1",
"paths": {
"/activity": {
"get": {
@ -10529,6 +10529,18 @@
"name": "username",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Query start timestamp",
"name": "start",
"in": "query"
},
{
"type": "string",
"description": "Query end timestamp",
"name": "end",
"in": "query"
}
],
"responses": {
@ -11346,11 +11358,6 @@
"_links": {
"$ref": "#/definitions/FileLinksResponse"
},
"commit_count": {
"type": "integer",
"format": "int64",
"x-go-name": "CommitCount"
},
"content": {
"description": "`content` is populated when `type` is `file`, otherwise null",
"type": "string",