xjy-gitea/models/action_ext.go

158 lines
4.1 KiB
Go

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"`
//Type string `json:"type"`
}
func GetContributors(opt GetContributorsOptionsExt) (interface{}, error) {
sql:=
`select a.act_user_id as id ,
u.name as login,
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,t.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 active_count
// from action a
// where (a.op_type=11 or a.op_type=5)
// and (created_unix>=? and created_unix<=?)
// group by a.op_type
// ) b on a.id=b.op_type
// left join (
// select op_type, count(op_type) as total_count
// from action a
// where (a.op_type=11 or a.op_type=5)
// group by a.op_type
// ) t on a.id=t.op_type
//`
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]
}
//convertMap["param"]=opt
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"`
}