Merge pull request '[API] Add : /activity' (#15) from wonderful/gitea-1156:develop into develop

This commit is contained in:
yystopf 2021-11-23 10:04:54 +08:00
commit 022b9108de
5 changed files with 296 additions and 1 deletions

130
models/action_ext.go Normal file
View File

@ -0,0 +1,130 @@
package models
import "strings"
// GetFeedsOptions options for retrieving feeds
type GetContributorsOptionsExt struct {
RepoId int64
UserId int64
}
type ContributorsDto struct {
Contributions int64 `json:"contributions"`
ID int64 `json:"id"`
Login string `json:"login"`
Email string `json:"email"`
//Type string `json:"type"`
}
func GetContributors(opt GetContributorsOptionsExt) (interface{}, error) {
sql :=
`select a.act_user_id as id ,
u.name as login,
u.email as email,
count(act_user_id) as contributions
from action a
left join user u on a.act_user_id=u.id
where repo_id=? and user_id=?
group by repo_id,act_user_id `
result := make([]ContributorsDto, 0, 0)
err := x.SQL(sql, opt.RepoId, opt.UserId).Find(&result)
return result, err
}
type GetGetActivityOptions struct {
FromDateUnix int64 `json:"-"`
ToDateUnix int64 `json:"-"`
FromDate string `json:"from_date"`
ToDate string `json:"to_date"`
Top int64 `json:"-"`
}
type PlatformDTO struct {
Id int64 `json:"-"`
Name string `json:"-"`
TotalCount int64 `json:"total_count"`
ActiveCount int64 `json:"active_count"`
}
//平台所需数据;
func GetActivity(opt *GetGetActivityOptions) (interface{}, error) {
sql := `select a.id,a.name,ifNull(b.active_count,0) as active_count,b.total_count
from ( select 11 as id ,'PullRequest' name
union
select 5 as id, 'Commit' name
) a
left join (
select op_type,count(op_type) as total_count,
sum(case when a.created_unix>=? and a.created_unix<=?
then 1 else 0 end
) as active_count
from action a
where (a.op_type=11 or a.op_type=5)
group by a.op_type
) b on a.id=b.op_type`
datalist := make([]PlatformDTO, 0, 0)
err := x.SQL(sql, opt.FromDateUnix, opt.ToDateUnix).Find(&datalist)
if err != nil {
return nil, err
}
convertMap := make(map[string]interface{})
for i := 0; i <= len(datalist)-1; i++ {
convertMap[strings.ToLower(datalist[i].Name)] = datalist[i]
}
return convertMap, err
}
type ProjectDTO struct {
Id int64 `json:"-"`
Name string `json:"name"`
TotalCount int64 `json:"total_count"`
ActiveCount int64 `json:"active_count"`
}
//项目所需数据-按项目统计 top 5
func GetActivityProject(opt *GetGetActivityOptions) (interface{}, error) {
sql :=
`select repo_id as id,r.name,
count(op_type) as total_count,
sum(case when a.created_unix>=? and a.created_unix<=?
then 1 else 0 end
) as active_count
from action a
left join repository r on a.repo_id=r.id
where (a.op_type=5)
group by a.repo_id
order by total_count desc
limit ?
`
datalist := make([]ProjectDTO, 0, 0)
err := x.SQL(sql, opt.FromDateUnix, opt.ToDateUnix, opt.Top).Find(&datalist)
return datalist, err
}
//项目所需数据-按开发者统计 top 5
func GetActivityDevelop(opt *GetGetActivityOptions) (interface{}, error) {
sql :=
`select u.name as develop_name,
count(op_type) as total_count,
sum(case when (a.created_unix>=? and a.created_unix<=?) then 1 else 0 end ) as active_count
from action a
left join user u on a.act_user_id=u.id
where (a.op_type=5)
group by a.act_user_id
order by total_count desc
limit ? `
datalist := make([]DevelopDTO, 0, 0)
err := x.SQL(sql, opt.FromDateUnix, opt.ToDateUnix, opt.Top).Find(&datalist)
return datalist, err
}
type DevelopDTO struct {
DevelopName string `json:"develop_name"`
TotalCount int64 `json:"total_count"`
ActiveCount int64 `json:"active_count"`
}

View File

@ -80,6 +80,7 @@ import (
"code.gitea.io/gitea/routers/api/v1/notify"
"code.gitea.io/gitea/routers/api/v1/org"
"code.gitea.io/gitea/routers/api/v1/repo"
report "code.gitea.io/gitea/routers/api/v1/reporter"
"code.gitea.io/gitea/routers/api/v1/settings"
_ "code.gitea.io/gitea/routers/api/v1/swagger" // for swagger generation
"code.gitea.io/gitea/routers/api/v1/user"
@ -600,6 +601,9 @@ func Routes() *web.Route {
m.Get("/repository", settings.GetGeneralRepoSettings)
})
m.Group("/activity", func() {
m.Get("", report.GetActivity)
})
// Notifications
m.Group("/notifications", func() {
m.Combo("").

View File

@ -0,0 +1,97 @@
package reporter
import (
"fmt"
"net/http"
"strconv"
"time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
)
func GetActivity(ctx *context.APIContext) {
// swagger:operation GET /activity activity activity
// ---
// summary: Statistics of commit and pull request data,Platform required data **
// produces:
// - application/json
// parameters:
// - name: from
// in: query
// description: Query begin timestamp
// type: string
// required: false
// - name: to
// in: query
// description: Query end timestamp
// type: string
// required: false
// responses:
// "200":
// "$ref": "#/responses/PlatformDTO"
// "404":
// "$ref": "#/responses/notFound"
opt := GetParamOption(ctx)
if opt == nil {
return
}
list, err := models.GetActivity(opt)
fmt.Println("-==========list====\n", list)
if err != nil {
ctx.ServerError("GetActivity", err)
return
}
ctx.JSON(http.StatusOK, list)
}
func GetParamOption(ctx *context.APIContext) (opt *models.GetGetActivityOptions) {
Layout := "2006-01-02 15:04:05"
//##top
top := ctx.QueryInt64("top")
if top <= 0 {
top = 5
} else if top >= 20 {
top = 20
}
//##from
FromDate, err := strconv.ParseInt(ctx.QueryTrim("from"), 10, 64)
if err != nil {
ctx.Error(http.StatusBadRequest, "param.from", err)
return
}
if FromDate <= 0 {
ctx.Error(http.StatusBadRequest, "param.from", fmt.Errorf("请指定from参数"))
return
//from=time.Now().Format("2006-01-02")
}
from := time.Unix(FromDate, 0).Format(Layout)
//fmt.Println("********from:",from," ", FromDate," convert:",time.Unix( FromDate,0).Format("2006-01-02 15:04:05"))
//##to
ToDate, err := strconv.ParseInt(ctx.QueryTrim("to"), 10, 64)
if err != nil {
ctx.Error(http.StatusBadRequest, "param.to ", err)
return
}
if ToDate <= 0 {
ctx.Error(http.StatusBadRequest, "param.to", fmt.Errorf("请指定to参数"))
return
}
to := time.Unix(ToDate, 0).Format(Layout)
//fmt.Println("********to:",to ," ", ToDate," convert:",time.Unix( ToDate,0).Format(Layout))
opt = &models.GetGetActivityOptions{
FromDateUnix: FromDate,
ToDateUnix: ToDate,
FromDate: from,
ToDate: to,
Top: top,
}
return opt
}

View File

@ -5,6 +5,7 @@
package swagger
import (
"code.gitea.io/gitea/models"
api "code.gitea.io/gitea/modules/structs"
)
@ -21,3 +22,10 @@ type swaggerResponseAccessToken struct {
// in:body
Body api.AccessToken `json:"body"`
}
// PlatformDTO
// swagger:response PlatformDTO
type swaggerReponsePlatformDTO struct {
//in:body
Body models.PlatformDTO `json:"body"`
}

View File

@ -23,6 +23,40 @@
},
"basePath": "{{AppSubUrl | JSEscape | Safe}}/api/v1",
"paths": {
"/activity": {
"get": {
"produces": [
"application/json"
],
"tags": [
"activity"
],
"summary": "Statistics of commit and pull request data,Platform required data **",
"operationId": "activity",
"parameters": [
{
"type": "string",
"description": "Query begin timestamp",
"name": "from",
"in": "query"
},
{
"type": "string",
"description": "Query end timestamp",
"name": "to",
"in": "query"
}
],
"responses": {
"200": {
"$ref": "#/responses/PlatformDTO"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/admin/cron": {
"get": {
"produces": [
@ -4266,7 +4300,7 @@
"tags": [
"repository"
],
"summary": "Get a hooktasks",
"summary": "Get a hooktasks ***",
"operationId": "repoGetHookTasks",
"parameters": [
{
@ -16328,6 +16362,22 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"PlatformDTO": {
"type": "object",
"properties": {
"active_count": {
"type": "integer",
"format": "int64",
"x-go-name": "ActiveCount"
},
"total_count": {
"type": "integer",
"format": "int64",
"x-go-name": "TotalCount"
}
},
"x-go-package": "code.gitea.io/gitea/models"
},
"PublicKey": {
"description": "PublicKey publickey is a user key to push code to repository",
"type": "object",
@ -18199,6 +18249,12 @@
}
}
},
"PlatformDTO": {
"description": "PlatformDTO",
"schema": {
"$ref": "#/definitions/PlatformDTO"
}
},
"PublicKey": {
"description": "PublicKey",
"schema": {