作者 yangfu

fix: 非法数据过滤(启动、通讯异常数据 、当次生产数据异常(超10000))

... ... @@ -122,6 +122,7 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD
}
//计算区间的产能
if v, ok := newDeviceCollection.Values["Count"]; ok {
newDeviceCollection.Values["total"] = v // 记录原始值
newDeviceCollection.Values["Count"] = 0
... ... @@ -142,6 +143,7 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD
newDeviceCollection.ProductCount = count
}
}
_, valErr := newDeviceCollection.Valid()
// TODO:测试假数据,后期注释掉
//if createDeviceCollectionCommand.DeviceType == domain.DeviceTypeChuanChuanJi {
// newDeviceCollection.Values["Count"] = rand.Intn(300)
... ... @@ -150,7 +152,9 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if valErr == nil {
err = domainService.SendWorkshopDeviceData(deviceCollection)
}
if err != nil {
log.Logger.Error("车间设备数据加入redis失败:" + err.Error())
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ...
... ... @@ -97,3 +97,21 @@ func (deviceCollection *DeviceCollection) Update(data map[string]interface{}) er
func TaskDeviceCollection() string {
return fmt.Sprintf("%v:task:device-collection:report", constant.CACHE_PREFIX)
}
func (data *DeviceCollection) Valid() (bool, error) {
var (
result = false
)
if data.ProductCount > DeviceMaxSingleProductCount {
data.ProductCount = 0
data.Values["Count"] = 0
return result, fmt.Errorf("设备数据异常: 生产数量超过:%v", DeviceMaxSingleProductCount)
}
if data.StartupStatus == 0 {
return result, fmt.Errorf("设备数据异常: 启动0")
}
if data.ComStatus == 0 {
return result, fmt.Errorf("设备数据异常: 通讯0")
}
return true, nil
}
... ...
... ... @@ -53,14 +53,18 @@ func (deviceDailyRunningRecord *DeviceDailyRunningRecord) Update(data map[string
return nil
}
func (deviceDailyRunningRecord *DeviceDailyRunningRecord) AddDeviceRunningData(t time.Time, data *DeviceRunningData) {
func (deviceDailyRunningRecord *DeviceDailyRunningRecord) AddDeviceRunningData(t time.Time, data *DeviceRunningData) bool {
deviceDailyRunningRecord.DeviceRunningRecordInfo.AddDeviceRunningData(t, data)
now := time.Now().Unix()
if ok, _ := data.Valid(); !ok {
return false
}
if t.Unix() > (now-DefaultCollectionTimeSpan) && t.Unix() < (now+DefaultCollectionTimeSpan) {
deviceDailyRunningRecord.UpdatedAt = t
return
return false
}
deviceDailyRunningRecord.UpdatedAt = time.Now()
return true
}
func (deviceDailyRunningRecord *DeviceDailyRunningRecord) String() string {
... ...
package domain
import "time"
import (
"fmt"
"time"
)
const DeviceMaxSingleProductCount = 10000
// 设备运行数据
type DeviceRunningData struct {
... ... @@ -41,3 +46,20 @@ type DeviceRunningData struct {
// 单位数据 比如:1串/0.1kg weight = count * unitQuantity
UnitQuantity float64 `json:"unitQuantity"`
}
func (data *DeviceRunningData) Valid() (bool, error) {
var (
result = false
)
if data.Count > DeviceMaxSingleProductCount {
data.Count = 0
return result, fmt.Errorf("设备数据异常: 生产数量超过:%v", DeviceMaxSingleProductCount)
}
if data.StartupStatus == 0 {
return result, fmt.Errorf("设备数据异常: 启动0")
}
if data.ComStatus == 0 {
return result, fmt.Errorf("设备数据异常: 通讯0")
}
return true, nil
}
... ...
... ... @@ -92,7 +92,10 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
if _, err = deviceRunningRecordRepository.Save(deviceRunningRecord); err != nil {
return nil, err
}
// 2.保存设备生产记录 (统计车间、员工产能) 批次跟数量不为空
// 2.更新 设备每日运行记录(汇总) - redis更新 十分钟异步刷库
if addSuccess := deviceDailyRecord.AddDeviceRunningData(deviceRunningData.CollectionTime, deviceRunningData); addSuccess {
// 3.保存设备生产记录 (统计车间、员工产能) 批次跟数量不为空
if record.DeviceType == domain.DeviceTypeChuanChuanJi && plan != nil && deviceRunningData.Count > 0 {
productRecord, _ := ptr.newProductRecord(companyId, orgId, workStation, device, deviceRunningData, plan)
... ... @@ -102,9 +105,7 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
productRecordService.EmployeeProductStatics(productRecord)
productRecordService.WorkshopProductStatics(productRecord)
}
// 3.更新 设备每日运行记录(汇总) - redis更新 十分钟异步刷库
deviceDailyRecord.AddDeviceRunningData(deviceRunningData.CollectionTime, deviceRunningData)
}
if err = redis.SaveDeviceDailyRunningRecord(deviceDailyRecord); err != nil {
//log.Logger.Error(err.Error())
return nil, err
... ...