Skip to content

Commit

Permalink
refactor(alarm): 优化告警历史和实时告警数据结构
Browse files Browse the repository at this point in the history
- 将 Annotations 字段从值类型改为指针类型,避免不必要的复制
- 在处理告警历史和实时告警时,使用 NewAnnotations 函数初始化 Annotations
- 更新 Time 类型的 MarshalJSON 和 Value 方法,使其以指针形式调用
  • Loading branch information
aide-cloud committed Nov 9, 2024
1 parent d396469 commit 0b867f2
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/server/palace/internal/data/repoimpl/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (a *alarmHistoryRepositoryImpl) createAlarmHistoryToModels(param *bo.Create
strategy := param.Strategy
historyList := types.SliceTo(param.Alerts, func(alarmParam *bo.AlertItemRawParams) *alarmmodel.AlarmHistory {
labels := vobj.NewLabels(alarmParam.Labels)
annotations := alarmParam.Annotations
annotations := vobj.NewAnnotations(alarmParam.Annotations)
alertStatus := vobj.ToAlertStatus(alarmParam.Status)
return &alarmmodel.AlarmHistory{
Summary: annotations.GetSummary(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/palace/internal/data/repoimpl/realtime_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (r *realtimeAlarmRepositoryImpl) createRealTimeAlarmToModels(param *bo.Crea

alarms := types.SliceTo(param.Alerts, func(alarmParam *bo.AlertItemRawParams) *alarmmodel.RealtimeAlarm {
labels := vobj.NewLabels(alarmParam.Labels)
annotations := alarmParam.Annotations
annotations := vobj.NewAnnotations(alarmParam.Annotations)
return &alarmmodel.RealtimeAlarm{
Status: vobj.ToAlertStatus(alarmParam.Status),
StartsAt: alarmParam.StartsAt,
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/palace/internal/service/builder/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (a *doAlarmHistoryBuilder) ToAPI(history *alarmmodel.AlarmHistory) *admin.A
Fingerprint: history.Fingerprint,
RawInfo: "",
Labels: history.Labels.Map(),
Annotations: history.Annotations,
Annotations: history.Annotations.Map(),
Summary: history.Summary,
Duration: types.NewTimeByString(endAt).Sub(types.NewTimeByString(history.StartsAt).Time).String(),
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/palace/model/alarmmodel/alarm_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type AlarmHistory struct {
// 标签
Labels *vobj.Labels `gorm:"column:labels;type:JSON;not null;comment:标签" json:"labels"`
// 注解
Annotations vobj.Annotations `gorm:"column:annotations;type:JSON;not null;comment:注解" json:"annotations"`
Annotations *vobj.Annotations `gorm:"column:annotations;type:JSON;not null;comment:注解" json:"annotations"`
// 告警原始数据ID
RawInfoID uint32 `gorm:"column:raw_info_id;type:int;comment:告警原始数据id;uniqueIndex:idx__history__notice__raw_info_id,priority:1" json:"rawInfoId"`
// 附加信息
Expand Down
2 changes: 1 addition & 1 deletion pkg/palace/model/alarmmodel/realtime_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type RealtimeAlarm struct {
// 标签
Labels *vobj.Labels `gorm:"column:labels;type:JSON;not null;comment:标签" json:"labels"`
// 注解
Annotations vobj.Annotations `gorm:"column:annotations;type:JSON;not null;comment:注解" json:"annotations"`
Annotations *vobj.Annotations `gorm:"column:annotations;type:JSON;not null;comment:注解" json:"annotations"`
// 告警原始数据ID
RawInfoID uint32 `gorm:"column:raw_info_id;type:int;comment:告警原始数据id;uniqueIndex:idx__realtime__notice__raw_info_id,priority:1" json:"rawInfoId"`
// 实时告警详情
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Time struct {
}

// MarshalJSON 实现 json.Marshaler 接口
func (t Time) MarshalJSON() ([]byte, error) {
func (t *Time) MarshalJSON() ([]byte, error) {
// 返回字符串形式的时间
return TextJoinToBytes(`"`, t.String(), `"`), nil
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (t *Time) Scan(value interface{}) error {
}

// Value 实现 driver.Valuer 接口,Value
func (t Time) Value() (driver.Value, error) {
func (t *Time) Value() (driver.Value, error) {
return t.Time, nil
}

Expand Down

0 comments on commit 0b867f2

Please sign in to comment.