Ver Fonte

运营统计-告警统计

sixian há 2 anos atrás
pai
commit
2de1dba47c

+ 38 - 0
app/operation/controller/alarmController.go

@@ -0,0 +1,38 @@
+package controller
+
+import (
+	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/middleware"
+	"iot_manager_service/app/operation/model"
+	"iot_manager_service/app/operation/service"
+	"iot_manager_service/util/common"
+	"net/http"
+)
+
+// alarm 告警统计
+var Alarm = new(alarmCtl)
+
+type alarmCtl struct{}
+
+func (c alarmCtl) List(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	var req model.RequestAlarmFilter
+	err := ctx.ShouldBindQuery(&req)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	var records interface{}
+	err = nil
+	if req.QueryType == "day" {
+		records, err = service.AlarmService.GetDayList(claims.TenantId, req)
+	} else {
+		records, err = service.AlarmService.GetMonthList(claims.TenantId, req)
+	}
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
+}

app/operation/controller/lightingRate.go → app/operation/controller/lightingRateController.go


+ 70 - 0
app/operation/dao/AlarmDao.go

@@ -0,0 +1,70 @@
+package dao
+
+import "iot_manager_service/app/operation/model"
+
+type Alarm struct {
+	TenantId        string
+	DeviceType      int    `gorm:"column:arm_device_type"` //设备类型
+	Count           int    //总数
+	NotHandlerCount int    //未处理报警
+	CountTime       string `json:"countTime"` //统一时间
+}
+
+func (a Alarm) TableName() string {
+	return "warn_platform_alarm"
+}
+
+func (a Alarm) Gets(req model.RequestAlarmFilter) ([]Alarm, error) {
+	var list []Alarm
+	countTimeFormat := "%Y-%m-%d"
+	if req.QueryType == "month" {
+		countTimeFormat = "%Y-%m"
+	}
+	deviceType := ""
+	if a.rewirteDeviceType(req.QueryDeviceType) != "" {
+		deviceType = " arm_device_type as device_type,"
+	}
+	sql := "SELECT count(*) AS count, date_format( arm_time, '" + countTimeFormat + "' ) AS count_time, " + deviceType + " sum(if(arm_handle=1, 1,0)) as not_handler_count FROM warn_platform_alarm WHERE "
+	where := " arm_time>=? and arm_time<=?"
+	if a.rewirteDeviceType(req.QueryDeviceType) != "" {
+		where = " arm_time>=? and arm_time<=? and arm_device_type=" + a.rewirteDeviceType(req.QueryDeviceType)
+	}
+	sql += where
+	group := " group by count_time"
+	if a.rewirteDeviceType(req.QueryDeviceType) != "" {
+		group = " GROUP BY count_time,device_type"
+	}
+	sql += group
+	sql += " order by count_time desc"
+	err := Db.Debug().Raw(sql, req.StartTime, req.EndTime).Scan(&list).Error
+	return list, err
+}
+
+//重写前端传过来的数据,由于前端修改工程大,先这样处理了
+func (a Alarm) rewirteDeviceType(queryDeviceType string) string {
+	switch queryDeviceType {
+	case "count":
+		return ""
+	case "camera":
+		return "1" //摄像机
+	case "gateway":
+		return "2" //网关
+	case "lightcontrol":
+		return "3" //灯控
+	case "infoboard":
+		return "5" //信息屏
+	case "sensor":
+		return "6" //环境监测
+	case "zigbee":
+		return "7" //集控器
+	case "terminal":
+		return "8" //一键告警终端
+	case "capture":
+		return "12" //抓拍单元
+	case "bridgesensor": //雪水传感器
+		return "13"
+	case "ipcast":
+		return "14" //IP音柱
+	}
+	return ""
+}

+ 22 - 0
app/operation/model/Alarm.go

@@ -0,0 +1,22 @@
+package model
+
+type RequestAlarmFilter struct {
+	StartTime       string `form:"queryStartDate"`
+	EndTime         string `form:"queryEndDate"`
+	QueryType       string `form:"queryType"`       //类型 month月 day日
+	QueryDeviceType string `form:"queryDeviceType"` //设备类型
+}
+
+type ResponseAlarm struct {
+	ID           int    `json:"id"`
+	CountTime    string `json:"countTime"`           //统一时间
+	MonthTime    string `json:"monthTime,omitempty"` //月份 与 日期 2选 一
+	DayTime      string `json:"dayTime,omitempty"`   //日期  与 月份  2选 一
+	DeviceType   string `json:"deviceType"`          //设备类型
+	CountUnsolve int    `json:"countUnsolve"`        //未处理报警数量
+	CountSolve   int    `json:"countSolve"`          //已完结工单
+	CountOpts    int    `json:"countOpts"`           //生成工单数量
+	CountT       int    `json:"countT"`              //总报警
+	CreateTime   string `json:"createTime"`
+	TenantID     string `json:"tenantId"`
+}

+ 62 - 0
app/operation/service/AlarmService.go

@@ -0,0 +1,62 @@
+package service
+
+import (
+	"iot_manager_service/app/operation/dao"
+	"iot_manager_service/app/operation/model"
+	"iot_manager_service/util/common"
+	"strconv"
+)
+
+var AlarmService = new(alarmService)
+
+type alarmService struct{}
+
+func (s alarmService) GetDayList(tenantId int, req model.RequestAlarmFilter) ([]model.ResponseAlarm, error) {
+	return s.getData("day", tenantId, req)
+}
+
+func (s alarmService) GetMonthList(tenantId int, req model.RequestAlarmFilter) ([]model.ResponseAlarm, error) {
+	return s.getData("month", tenantId, req)
+}
+
+func (s alarmService) getData(method string, tenantId int, req model.RequestAlarmFilter) ([]model.ResponseAlarm, error) {
+	alarmDao := dao.Alarm{TenantId: strconv.Itoa(tenantId)}
+	alarms, err := alarmDao.Gets(req) //取报警数据
+
+	var countDates []string
+	if method == "month" {
+		countDates = common.GetTimeMonths(req.StartTime, req.EndTime) //时间范围 月
+	} else {
+		countDates = common.GetTimeDays(req.StartTime, req.EndTime) //时间范围 天
+	}
+	//fmt.Printf("alarms = %v \n", alarms)
+	//数据按时间范围 填充
+	var list []model.ResponseAlarm
+	for _, countDate := range countDates {
+		//初始化所需数据
+		var deviceType, count, notHandlerCount int
+		for _, alarm := range alarms {
+			date := alarm.CountTime
+			if method == "month" {
+				date = alarm.CountTime + "-01"
+			}
+			//如果是当当天数据,则赋值
+			if date == countDate {
+				deviceType = alarm.DeviceType
+				count = alarm.Count
+				notHandlerCount = alarm.NotHandlerCount
+			}
+		}
+		list = append(list, model.ResponseAlarm{
+			CountTime:    countDate,
+			DeviceType:   strconv.Itoa(deviceType),
+			CountT:       count,
+			CountUnsolve: notHandlerCount,
+			CountSolve:   count - notHandlerCount,
+		})
+	}
+	if err != nil {
+		return nil, err
+	}
+	return list, nil
+}

+ 1 - 1
app/warn/dao/platformAlarmDao.go

@@ -75,7 +75,7 @@ func (a PlatformAlarm) Gets(filter model.RequestPlatFormAlartFilter) ([]Platform
 
 func (a PlatformAlarm) GetMaxIdAndUpDateTime() (int64, string) {
 	var platformAlarm PlatformAlarm
-	Db.Debug().Where(&PlatformAlarm{TenantId: a.TenantId}).Order("id desc").First(&platformAlarm)
+	Db.Debug().Where(&PlatformAlarm{TenantId: a.TenantId}).Last(&platformAlarm)
 	return int64(platformAlarm.ID), platformAlarm.ArmTime.String()
 }
 

+ 1 - 1
app/warn/edge_service/syncAlarmService.go

@@ -38,7 +38,7 @@ func (r *RecordAlarmRecord) SyncAlartRecord(maxId int64, maxUpDateTime string) (
 	cfg := config.Instance()
 	api := cfg.Foreign.IotEdgeUrl + "/data/v1/alarm/sync"
 	url := fmt.Sprintf("%v?id=%d&&updatedat=%v", api, maxId, url2.QueryEscape(maxUpDateTime))
-	//fmt.Printf("url = %v", url)
+	fmt.Printf("url = %v", url)
 	method := "GET"
 	client := &http.Client{}
 	req, err := http.NewRequest(method, url, nil)

+ 2 - 1
app/warn/service/platformAlarmService.go

@@ -60,7 +60,8 @@ func (s platformAlarmService) Refresh(tenantId int) error {
 	//fmt.Printf("newAllCode = %v", newAllCode)
 	edgeService := edgeService.RecordAlarmRecord{}
 	maxId, maxUpDateTime := dao.PlatformAlarm{}.GetMaxIdAndUpDateTime()
-	//fmt.Printf("maxId = %v", maxId)
+	//fmt.Printf("maxId = %v\n", maxId)
+	//fmt.Printf("maxUpDateTime = %v\n", maxUpDateTime)
 
 	list, err := edgeService.SyncAlartRecord(maxId, maxUpDateTime)
 	if err != nil {

+ 3 - 1
router/router.go

@@ -564,8 +564,10 @@ func InitRouter(engine *gin.Engine) {
 		operationDeviceGroup.POST("list", operation.Device.MonthList)
 		operationDeviceGroup.POST("year-list", operation.Device.YearList)
 	}
-	//灯控能耗统计
+	//运营统计-灯控能耗统计
 	operationGroup.POST("/energy/list", operation.LightingRate.List)
+	//运营统计-告警统计
+	operationGroup.POST("/alarm/list", operation.Alarm.List)
 
 	warnGroup := engine.Group("/api/longchi/alarm")
 	{