From a72bc037f380f656963796126f46eccbf89f7ebc Mon Sep 17 00:00:00 2001 From: simonzhangsz Date: Mon, 22 Jan 2024 17:08:41 +0800 Subject: [PATCH] fix: gold kpi add granularity --- database/upgrade/zupgrade.sql | 5 ++++- database/upgvue3/zupgrade.sql | 5 ++++- features/pm/performance.go | 30 +++++++++++++++++++----------- lib/global/kits.go | 21 +++++++++++++++++++++ 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/database/upgrade/zupgrade.sql b/database/upgrade/zupgrade.sql index cae6d185..646b7adb 100644 --- a/database/upgrade/zupgrade.sql +++ b/database/upgrade/zupgrade.sql @@ -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; \ No newline at end of file +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`; \ No newline at end of file diff --git a/database/upgvue3/zupgrade.sql b/database/upgvue3/zupgrade.sql index 3e1f9455..1eda589b 100644 --- a/database/upgvue3/zupgrade.sql +++ b/database/upgvue3/zupgrade.sql @@ -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; \ No newline at end of file +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`; \ No newline at end of file diff --git a/features/pm/performance.go b/features/pm/performance.go index 57ae1784..6e91bda6 100644 --- a/features/pm/performance.go +++ b/features/pm/performance.go @@ -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 diff --git a/lib/global/kits.go b/lib/global/kits.go index b36da021..593f18c8 100644 --- a/lib/global/kits.go +++ b/lib/global/kits.go @@ -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