iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 15
0

熱圖API

資料會回傳各群組的日期跟時間加總

[GET] api/report/group_heatmap
回傳值:

    "data": {
        "report": [
            {
                "name": "A",
                "date": "2020-09-16",
                "total_spend_time": 0
            }
    },
    "result": true
}

handler/api.go

// HeatMapReport HeatMapReport
type HeatMapReport struct {
	ID             int    `json:"-"`
	Name           string `json:"name"`
	Date           string `json:"date"`
	TotalSpendTime int    `json:"total_spend_time"`
}

// GetTaskHeatMap GetTaskHeatMap
func GetTaskHeatMap(c *gin.Context) {
	var heatmap []*HeatMapReport
	userID := c.GetString("user_id")
	groupIDstr, _ := c.GetQuery("group_id")

	groupID, err := strconv.Atoi(groupIDstr)
	if err != nil || groupID == 0 {
		res.BadRequest(c, res.ErrParamsCode, gin.H{"msg": "group_id not found"})
		return
	}

	nowTime := time.Now()
	endDate := nowTime.Format(config.Val.TimeFormat)
	startDate := nowTime.AddDate(0, 0, -7).Format(config.Val.TimeFormat)
	dateStatistics := rangeDate(nowTime, 7)

	tasks, err := model.TaskModel.GetTasksByParentID([]int{groupID}, userID, []int{model.TaskAble})
	if err != nil {
		log.WithFields(log.Fields{
			"user_id":    userID,
			"origin_err": err.Error(),
		}).Error("GetTasksByParentID error")
		res.SystemError(c, res.ErrSystemCode, gin.H{})
		return
	}

	data, err := model.RecordsModel.SumByDateByParentID(userID, groupID, startDate, endDate)
	if err != nil {
		log.WithFields(log.Fields{
			"user_id":    userID,
			"origin_err": err.Error(),
		}).Error("SumByDateByParentID error")
		res.SystemError(c, res.ErrSystemCode, gin.H{})
		return
	}

	for _, t := range tasks {
		for _, ds := range dateStatistics {
			heatmap = append(heatmap, &HeatMapReport{
				ID:   t.ID,
				Name: t.Name,
				Date: ds,
			})
		}
	}

	for _, d := range data {
		t1, err := time.Parse(time.RFC3339, d.Date)
		if err != nil {
			log.WithFields(log.Fields{
				"date":       d.Date,
				"origin_err": err.Error(),
			}).Error("GetAllGroupHeatMap Parse error")
			res.SystemError(c, res.ErrSystemCode, gin.H{})
			return
		}

		parsedDate := t1.Format("2006-01-02")

		for _, h := range heatmap {
			if h.ID == d.TaskID && h.Date == parsedDate {
				h.TotalSpendTime = d.SpendTime
				break
			}
		}
	}

	res.Success(c, gin.H{
		"report": heatmap,
	})
}

試打一下
https://ithelp.ithome.com.tw/upload/images/20200922/20129767tL7rUn7x4m.png

今天的commit

謝謝大家~


上一篇
Day14 統計報表API-1
下一篇
Day16 任務記錄API
系列文
Golang & Vue.js 30天從0打造服務30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言