fix: gold kpi add granularity

This commit is contained in:
2024-01-22 17:08:41 +08:00
parent cb175145f9
commit a72bc037f3
4 changed files with 48 additions and 13 deletions

View File

@@ -142,4 +142,7 @@ ALTER TABLE
`omc_db`.`sys_dept` MODIFY COLUMN `dept_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '部门名称' AFTER `ancestors`;
ALTER TABLE `omc_db`.`sys_dict_data`
MODIFY COLUMN `dict_code` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '字典编码' FIRST;
MODIFY COLUMN `dict_code` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '字典编码' FIRST;
ALTER TABLE `omc_db`.`gold_kpi`
ADD COLUMN IF NOT EXISTS `granularity` tinyint NOT NULL DEFAULT 60 COMMENT '时间颗粒度: 5/10/.../60/300 (秒)' AFTER `index`;

View File

@@ -142,4 +142,7 @@ ALTER TABLE
`omc_db`.`sys_dept` MODIFY COLUMN `dept_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '部门名称' AFTER `ancestors`;
ALTER TABLE `omc_db`.`sys_dict_data`
MODIFY COLUMN `dict_code` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '字典编码' FIRST;
MODIFY COLUMN `dict_code` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '字典编码' FIRST;
ALTER TABLE `omc_db`.`gold_kpi`
ADD COLUMN IF NOT EXISTS `granularity` tinyint NOT NULL DEFAULT 60 COMMENT '时间颗粒度: 5/10/.../60/300 (秒)' AFTER `index`;

View File

@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"math"
"net/http"
"strconv"
"time"
@@ -47,17 +48,18 @@ type KpiReport struct {
type GoldKpi struct {
// Id int `json:"-" xorm:"pk 'id' autoincr"`
Date string `json:"date" xorm:"date"`
Index int `json:"index"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
NEName string `json:"neName" xorm:"ne_name"`
RmUid string `json:"rmUid" xorm:"rm_uid"`
NEType string `json:"neType" xorm:"ne_type"`
KpiId string `json:"kpiId" xorm:"kpi_id"`
Value int `json:"value"`
Error string `json:"error"`
Timestamp string `json:"timestamp"`
Date string `json:"date" xorm:"date"`
Index int `json:"index"`
Granularity int8 `json:"granularity"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
NEName string `json:"neName" xorm:"ne_name"`
RmUid string `json:"rmUid" xorm:"rm_uid"`
NEType string `json:"neType" xorm:"ne_type"`
KpiId string `json:"kpiId" xorm:"kpi_id"`
Value int `json:"value"`
Error string `json:"error"`
Timestamp string `json:"timestamp"`
}
var (
@@ -182,6 +184,12 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
goldKpi.Index, _ = strconv.Atoi(vars["index"])
goldKpi.StartTime = global.GetFmtTimeString(layout, kpiReport.Task.Period.StartTime, time.DateTime)
goldKpi.EndTime = global.GetFmtTimeString(layout, kpiReport.Task.Period.EndTime, time.DateTime)
// get time granularity from startTime and endTime
seconds, _ := global.GetSecondDuration(goldKpi.StartTime, goldKpi.EndTime)
goldKpi.Granularity = 60
if seconds != 0 && seconds <= math.MaxInt8 && seconds >= math.MinInt8 {
goldKpi.Granularity = int8(seconds)
}
goldKpi.NEName = kpiReport.Task.NE.NEName
goldKpi.RmUid = kpiReport.Task.NE.RmUID
goldKpi.NEType = kpiReport.Task.NE.NeType

View File

@@ -391,6 +391,27 @@ func GetSecondsSinceDatetime(datetimeStr string) (int64, error) {
return seconds, nil
}
func GetSecondDuration(time1, time2 string) (int64, error) {
loc1, _ := time.LoadLocation("Local")
// 解析日期时间字符串为时间对象
t1, err := time.ParseInLocation(time.DateTime, time1, loc1)
if err != nil {
return 0, err
}
t2, err := time.ParseInLocation(time.DateTime, time2, loc1)
if err != nil {
return 0, err
}
// 计算时间差
duration := t2.Sub(t1)
// 获取时间差的秒数
seconds := int64(duration.Seconds())
return seconds, nil
}
// 0: invalid ip
// 4: IPv4
// 6: IPv6