現在來要做報表第一支的API
它是一個圖餅圖,外面第一層是各群組的總時間,第二層就是底下的任務執行時間
目前都先固定搜尋7天
[GET] /api/report/pie
回傅值
{
"data": {
"report": [
{
"id": 1,
"name": "A",
"total_spend_time": 10, // 群組執行總時間
"tasks": [
{
"id": 2,
"name": "AA",
"total_spend_time": 5 // 任務執行總時間
},
{
"id": 2,
"name": "AB",
"total_spend_time": 5 // 任務執行總時間
}
]
}
]
},
"result": true
}
handler/api.go
// GetReportPie GetReportPie
func GetReportPie(c *gin.Context) {
userID := c.GetString("user_id")
nowTime := time.Now()
endDate := nowTime.Format(config.Val.TimeFormat)
startDate := nowTime.AddDate(0, 0, -7).Format(config.Val.TimeFormat)
// 取得記錄的時間加總
records, err := model.RecordsModel.GetByUserIDAndTimeBetween(userID, startDate, endDate)
if err != nil {
log.WithFields(log.Fields{
"origin_err": err.Error(),
}).Error("GetByUserIDAndTimeBetween error")
res.SystemError(c, res.ErrSystemCode, gin.H{})
return
}
var tasksReport []*TaskReport
var groupsReport []*GroupReport
groups, tasks, err := findGroupsAndTasks(userID)
if err != nil {
log.WithFields(log.Fields{
"user_id": userID,
"origin_err": err.Error(),
}).Error("findGroup error")
res.SystemError(c, res.ErrSystemCode, gin.H{})
return
}
for _, g := range groups {
groupsReport = append(groupsReport, &GroupReport{
ID: g.ID,
Name: g.Name,
})
}
for _, t := range tasks {
tasksReport = append(tasksReport, &TaskReport{
ID: t.ID,
ParentID: t.ParentID,
Name: t.Name,
})
}
// 找到記錄對應的任務
for _, r := range records {
for _, t := range tasksReport {
if t.ID == r.TaskID {
t.TotalSpendTime += r.SpendTime
break
}
}
}
// 找到任務對應的群組
for _, t := range tasksReport {
for _, g := range groupsReport {
if t.ParentID == g.ID {
g.TotalSpendTime += t.TotalSpendTime
g.Tasks = append(g.Tasks, t)
break
}
}
}
res.Success(c, gin.H{
"report": groupsReport,
})
}
試打一下
每一天執行總時間的加總
沒有資料的就回傳0
[GET] /api/report/line
回傅值
{
"data": {
"report": [
{
"date": "2020-09-15",
"total_spend_time": 0
}
]
},
"result": true
}
// GetReportLine GetReportLine
func GetReportLine(c *gin.Context) {
userID := c.GetString("user_id")
nowTime := time.Now()
endDate := nowTime.Format(config.Val.TimeFormat)
startDate := nowTime.AddDate(0, 0, -7).Format(config.Val.TimeFormat)
dateStatistics := rangeDate(nowTime, 7)
daysReport, err := sumDateReport(userID, startDate, endDate, dateStatistics)
if err != nil {
log.WithFields(log.Fields{
"origin_err": err.Error(),
}).Error("db error")
res.SystemError(c, res.ErrSystemCode, gin.H{})
return
}
res.Success(c, gin.H{
"report": daysReport,
})
}
func sumDateReport(userID, startDate, endDate string, dateStatistics []string) ([]*DateReport, error) {
var daysReport []*DateReport
daySum, err := model.RecordsModel.SumByDate(userID, startDate, endDate)
if err != nil {
return daysReport, err
}
// 初始化7天的資料
for _, ds := range dateStatistics {
daysReport = append(daysReport, &DateReport{
Date: ds,
})
}
for _, d := range daySum {
// 轉換DB日期的型態
t1, err := time.Parse(time.RFC3339, d.Date)
if err != nil {
return daysReport, err
}
parsedDate := t1.Format("2006-01-02")
// 把值加總在對應的日期上
for _, dr := range daysReport {
if dr.Date == parsedDate {
dr.TotalSpendTime = d.SpendTime
break
}
}
}
return daysReport, nil
}
試打一下
今天的commit
謝謝大家~