dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/models/alarm.go (about)

     1  package models
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/google/uuid"
     8  )
     9  
    10  // Constants for NotificationSeverity
    11  type AlarmSeverity string
    12  
    13  const (
    14  	AlarmNone     AlarmSeverity = "None"
    15  	AlarmCritical AlarmSeverity = "Critical"
    16  	AlarmMajor    AlarmSeverity = "Major"
    17  	AlarmMinor    AlarmSeverity = "Minor"
    18  	AlarmWarning  AlarmSeverity = "Warning"
    19  	AlarmOffline  AlarmSeverity = "Offline" //for backward compatity
    20  )
    21  
    22  // SeverityOrder defines the order of severity levels.
    23  var SeverityOrder = map[AlarmSeverity]int{
    24  	AlarmOffline:  5,
    25  	AlarmCritical: 4,
    26  	AlarmMajor:    3,
    27  	AlarmMinor:    2,
    28  	AlarmWarning:  1,
    29  	AlarmNone:     0,
    30  	// For backward compatibility
    31  }
    32  
    33  func (a *AlarmSeverity) UnmarshalText(text []byte) error {
    34  	switch string(text) {
    35  	case "":
    36  	case "None":
    37  		*a = AlarmNone
    38  	case "Critical":
    39  		*a = AlarmCritical
    40  	case "Major":
    41  		*a = AlarmMajor
    42  	case "Minor":
    43  		*a = AlarmMinor
    44  	case "Warning":
    45  		*a = AlarmWarning
    46  	case "Offline":
    47  		*a = AlarmOffline
    48  	default:
    49  		return errors.New("unknown AlarmSeverity")
    50  	}
    51  	return nil
    52  }
    53  
    54  // Function to convert a string representation to AlarmSeverity.
    55  func ToAlarmSeverity(str string) AlarmSeverity {
    56  	severityMap := map[string]AlarmSeverity{
    57  		"Critical": AlarmCritical,
    58  		"Major":    AlarmMajor,
    59  		"Minor":    AlarmMinor,
    60  		"Warning":  AlarmWarning,
    61  		"Offline":  AlarmOffline, // For backward compatibility
    62  		"None":     AlarmNone,
    63  	}
    64  
    65  	if severity, ok := severityMap[str]; ok {
    66  		return severity
    67  	}
    68  
    69  	// Return some default value or handle the unknown severity case.
    70  	return ""
    71  }
    72  
    73  type DeviceAlarm struct {
    74  	Id         string      // Unique identifier for the update.
    75  	DeviceName string      // The name or identifier of the device.
    76  	Origin     int64       // The origin or source of the status update.
    77  	Alarm      Alarm       // The alarm information (nil if it's a status).
    78  	Status     *StatusKind // status update triggered by the alarm (optional)
    79  }
    80  
    81  // Alarm represents an alarm message from a device.
    82  type Alarm struct {
    83  	Message     string        // The message or description of the alarm.
    84  	Severity    AlarmSeverity // The severity level of the alarm.
    85  	Job         string
    86  	Resolved    bool   // Indicates if the alarm is resolved.
    87  	Description string // Additional description of the alarm.
    88  	Annotations map[string]string
    89  }
    90  
    91  type DeviceAlarmHistory struct {
    92  	DeviceAlarm
    93  	StartAt int64
    94  	EndAt   *int64
    95  }
    96  
    97  func NewDeviceAlarm(deviceName string, msg string, severity AlarmSeverity, job string) DeviceAlarm {
    98  	return DeviceAlarm{
    99  		Id:         uuid.New().String(),
   100  		DeviceName: deviceName,
   101  		Origin:     time.Now().UnixMilli(),
   102  		Alarm: Alarm{
   103  			Message:  msg,
   104  			Severity: severity,
   105  			Job:      job,
   106  		},
   107  	}
   108  }