233 lines
6.4 KiB
Go
233 lines
6.4 KiB
Go
package v1
|
|
|
|
import (
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/controller"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/device"
|
|
devc "git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/device"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/errors"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/request"
|
|
"github.com/gin-gonic/gin"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func GetDeviceOnlineStatistic(c *gin.Context) {
|
|
duration := c.Query("duration")
|
|
if len(duration) <= 1 {
|
|
controller.FailCode(c, errors.InvalidParameter, nil, "请检查参数!")
|
|
return
|
|
}
|
|
|
|
var timeDuration time.Duration
|
|
if strings.HasSuffix(duration, "h") {
|
|
timeDuration = time.Hour
|
|
} else if strings.HasSuffix(duration, "m") {
|
|
timeDuration = time.Minute
|
|
} else {
|
|
controller.FailCode(c, errors.InvalidParameter, nil, "请检查参数!")
|
|
return
|
|
}
|
|
|
|
timeInt, err := strconv.ParseInt(duration[0:len(duration)-1], 10, 64)
|
|
if err != nil || timeInt <= 0 {
|
|
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
|
return
|
|
}
|
|
|
|
if timeInt > 1440 || (timeDuration == time.Hour && timeInt > 24) {
|
|
controller.FailCode(c, errors.InvalidParameter, nil, "请检查参数!")
|
|
return
|
|
}
|
|
|
|
result := make(map[string]int)
|
|
|
|
now := time.Now()
|
|
todayBegin := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
timeInterval := time.Duration((timeInt - 1) * timeDuration.Nanoseconds())
|
|
timeIntervalStart := todayBegin
|
|
var timeIntervalEnd time.Time
|
|
sql := "SELECT CASE "
|
|
for timeIntervalStart.Before(now) {
|
|
timeIntervalEnd = timeIntervalStart.Add(timeInterval)
|
|
timeStartStr := timeIntervalStart.Format("200601021504")
|
|
timeEndStr := timeIntervalEnd.Format("200601021504")
|
|
sql += "WHEN id BETWEEN '" + timeStartStr + "' AND '" + timeEndStr + "' THEN '" + timeStartStr + "' "
|
|
result[timeStartStr] = 0
|
|
timeIntervalStart = timeIntervalEnd.Add(timeDuration)
|
|
}
|
|
|
|
sql += "END AS time, MAX(count) AS count FROM perc_device_online_count WHERE id >= '" + todayBegin.Format("200601021504") + "' AND id <= '" + now.Format("200601021504") + "' GROUP BY time;"
|
|
|
|
d := device.DeviceOnlineCount{}
|
|
tResult, err := d.Query(sql)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务异常!")
|
|
return
|
|
}
|
|
|
|
for _, item := range tResult {
|
|
result[item.Time] = item.Count
|
|
}
|
|
|
|
delete(result, "")
|
|
|
|
controller.Success(c, result)
|
|
}
|
|
|
|
func GetDeviceUsageRate(c *gin.Context) {
|
|
d := &devc.DeviceInfo{}
|
|
|
|
result, err := d.GetHistoryUsageRate()
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务异常!")
|
|
return
|
|
}
|
|
|
|
result1, err := d.GetTodayUsageRate()
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务异常!")
|
|
return
|
|
}
|
|
|
|
result["today"] = result1["today"]
|
|
|
|
controller.Success(c, result)
|
|
}
|
|
|
|
type DeviceOperateLogCondition struct {
|
|
PageInfo *request.PageInfo `json:"pageInfo" binding:"required"`
|
|
StartTime *time.Time `json:"startTime"`
|
|
EndTime *time.Time `json:"endTime"`
|
|
Operate string `json:"operate"`
|
|
}
|
|
|
|
func GetDeviceOperateLog(c *gin.Context) {
|
|
serialNo := c.Param("serialNo")
|
|
|
|
var body DeviceOperateLogCondition
|
|
err := c.ShouldBindJSON(&body)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
|
return
|
|
}
|
|
|
|
op := &devc.DeviceOperateLog{}
|
|
fields := make(map[string]interface{})
|
|
fields["serial_no"] = serialNo
|
|
if body.StartTime != nil {
|
|
fields["operate_time >"] = body.StartTime
|
|
} else {
|
|
now := time.Now()
|
|
todayBegin := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
fields["operate_time >"] = todayBegin
|
|
}
|
|
|
|
if body.EndTime != nil {
|
|
fields["operate_time <"] = body.EndTime
|
|
}
|
|
|
|
if len(body.Operate) > 0 {
|
|
fields["operate"] = body.Operate
|
|
}
|
|
|
|
page := &model.Page[devc.DeviceOperateLog]{
|
|
CurrentPage: body.PageInfo.CurrentPage,
|
|
PageSize: body.PageInfo.PageSize,
|
|
Order: body.PageInfo.Order,
|
|
}
|
|
|
|
err = op.Page(page, fields)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务异常!")
|
|
return
|
|
}
|
|
|
|
if page.Data == nil {
|
|
page.Data = []devc.DeviceOperateLog{}
|
|
}
|
|
|
|
controller.Success(c, page)
|
|
}
|
|
|
|
func GetDeviceRunTime(c *gin.Context) {
|
|
serialNo := c.Param("serialNo")
|
|
duration := c.Query("duration")
|
|
|
|
result := make(map[string]int)
|
|
d := &devc.DeviceRunTime{}
|
|
now := time.Now()
|
|
var startDate string
|
|
if duration == "day" {
|
|
nowStr := now.Format("20060102")
|
|
tResult, err := d.GetRunTimeByDay(serialNo, nowStr)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务异常!")
|
|
return
|
|
}
|
|
result[nowStr] = tResult.RunTime
|
|
} else if duration == "month" {
|
|
startDate = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).Format("20060102")
|
|
endDate := now.Format("20060102")
|
|
tmp, err := d.StatisticRunTimeByDuration(serialNo, startDate, endDate)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务异常!")
|
|
return
|
|
}
|
|
|
|
tDate := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
|
for tDate.Before(now) {
|
|
result[tDate.Format("20060102")] = 0
|
|
tDate = tDate.Add(24 * time.Hour)
|
|
}
|
|
|
|
for _, runTime := range tmp {
|
|
result[runTime.Date] = runTime.RunTime
|
|
}
|
|
} else {
|
|
controller.FailCode(c, errors.InvalidParameter, nil, "请检查参数!")
|
|
return
|
|
}
|
|
|
|
controller.Success(c, result)
|
|
}
|
|
|
|
func GetAllDeviceRunTime(c *gin.Context) {
|
|
d := &devc.DeviceRunTime{}
|
|
result, err := d.StatisticRunTimeByAllDevice()
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务异常!")
|
|
return
|
|
}
|
|
controller.Success(c, result)
|
|
}
|
|
|
|
func GetDeviceBootCount(c *gin.Context) {
|
|
serialNo := c.Param("serialNo")
|
|
|
|
d := &devc.DeviceOperateLog{}
|
|
now := time.Now()
|
|
monthBegin := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
|
startDate := monthBegin.Format("2006-01-02 15:04:05.999999999 -0700")
|
|
endDate := now.Format("2006-01-02 15:04:05.999999999 -0700")
|
|
tResult, err := d.StatisticDeviceBootCountByDuration(serialNo, startDate, endDate)
|
|
if err != nil {
|
|
controller.FailCode(c, errors.ServerError, err, "服务异常!")
|
|
return
|
|
}
|
|
|
|
result := make(map[string]int)
|
|
tDate := monthBegin
|
|
for tDate.Before(now) {
|
|
result[tDate.Format("20060102")] = 0
|
|
tDate = tDate.Add(24 * time.Hour)
|
|
}
|
|
|
|
for _, dateCount := range tResult {
|
|
result[dateCount.Date] = dateCount.Count
|
|
}
|
|
|
|
controller.Success(c, result)
|
|
}
|