You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.4 KiB

package common_models
import (
"encoding/json"
"strconv"
"time"
)
// ThresholdItem 阈值项模型
type ThresholdItem struct {
Item int `json:"item"` // item -> t_factor_proto_item(id)
FieldName string `json:"field_name"`
Name string `json:"name"`
Level int `json:"level"`
Lower float64 `json:"lower"`
Upper float64 `json:"upper"`
Begin *int `json:"begin"` // 分时阈值起始时间(24小时制 0~24)
End *int `json:"end"` // 分时阈值结束时间(24小时制 0~24)
AggCategory *int `json:"agg_category,omitempty"` // 在JSON编码时,如果此字段为空,则忽略该字段
}
func (t *ThresholdItem) IsTimeSegmented() bool {
return t.Begin != nil && t.End != nil
}
func (t *ThresholdItem) RangeText() string {
lowerText := "-"
if t.Lower > -100000.0 {
lowerText = strconv.FormatFloat(t.Lower, 'f', -1, 64)
}
upperText := "+"
if t.Upper < 100000.0 {
upperText = strconv.FormatFloat(t.Upper, 'f', -1, 64)
}
return lowerText + "~" + upperText
}
// ThresholdItems 自定义类型,[]ThresholdItem的别名
type ThresholdItems []ThresholdItem
func (t *ThresholdItems) MarshalBinary() (data []byte, err error) {
return json.Marshal(t)
}
func (t *ThresholdItems) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, t)
}
// Threshold 阈值模型
type Threshold struct {
Items ThresholdItems
}
// FindThresholdInRange 阈值判断
func (t *Threshold) FindThresholdInRange(items []ThresholdItem, m float64) *ThresholdItem {
for _, th := range items {
if m > th.Lower && m <= th.Upper {
return &th
}
}
return nil
}
// GetThresholdsByItem 根据监测项ID获取阈值
func (t *Threshold) GetThresholdsByItem(items []ThresholdItem, itemID int) []ThresholdItem {
var filtered []ThresholdItem
for _, th := range items {
if th.Item == itemID {
filtered = append(filtered, th)
}
}
return filtered
}
// GetThresholdsByTime 根据时间获取阈值
func (t *Threshold) GetThresholdsByTime(date time.Time) []ThresholdItem {
if t.Items == nil || len(t.Items) == 0 {
return nil
}
if !t.Items[0].IsTimeSegmented() {
return t.Items
}
hour := date.Hour()
minute := date.Minute()
sec := date.Second()
if hour == 0 && minute == 0 && sec == 0 {
return t.filterByZero(t.Items, hour)
} else if minute == 0 && sec == 0 {
return t.filterByHour(t.Items, hour)
} else {
return t.filterByNormal(t.Items, hour)
}
}
// 0点时间的阈值模型
func (t *Threshold) filterByZero(items []ThresholdItem, hour int) []ThresholdItem {
var filteredItems []ThresholdItem
for _, item := range items {
if item.Begin != nil && *item.Begin == hour {
filteredItems = append(filteredItems, item)
}
}
return filteredItems
}
// 整点时间的阈值模型
func (t *Threshold) filterByHour(items []ThresholdItem, hour int) []ThresholdItem {
var filteredItems []ThresholdItem
for _, item := range items {
if item.Begin != nil && item.End != nil && *item.Begin < hour && *item.End >= hour {
filteredItems = append(filteredItems, item)
}
}
return filteredItems
}
// 常规时间的阈值模型
func (t *Threshold) filterByNormal(items []ThresholdItem, hour int) []ThresholdItem {
var filteredItems []ThresholdItem
for _, item := range items {
if item.Begin != nil && item.End != nil && *item.Begin <= hour && *item.End > hour {
filteredItems = append(filteredItems, item)
}
}
return filteredItems
}