github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/alerts/alertutils/alertutils.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package alertutils
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/go-co-op/gocron"
    23  )
    24  
    25  type AlertDetails struct {
    26  	AlertId         string              `json:"alert_id" gorm:"primaryKey"`
    27  	AlertName       string              `json:"alert_name" gorm:"not null;unique"`
    28  	State           AlertState          `json:"state"`
    29  	CreateTimestamp time.Time           `json:"create_timestamp" gorm:"autoCreateTime:milli" `
    30  	ContactID       string              `json:"contact_id" gorm:"foreignKey:ContactId;"`
    31  	ContactName     string              `json:"contact_name"`
    32  	Labels          []AlertLabel        `json:"labels" gorm:"many2many:label_alerts"`
    33  	SilenceMinutes  uint64              `json:"silence_minutes"`
    34  	QueryParams     QueryParams         `json:"queryParams" gorm:"embedded"`
    35  	Condition       AlertQueryCondition `json:"condition"`
    36  	Value           float32             `json:"value"`
    37  	EvalFor         uint64              `json:"eval_for"`
    38  	EvalInterval    uint64              `json:"eval_interval"`
    39  	Message         string              `json:"message"`
    40  	CronJob         gocron.Job          `json:"cron_job" gorm:"embedded"`
    41  	NodeId          uint64              `json:"node_id"`
    42  	NotificationID  string              `json:"notification_id" gorm:"foreignKey:NotificationId;"`
    43  	OrgId           uint64              `json:"org_id"`
    44  }
    45  
    46  func (AlertDetails) TableName() string {
    47  	return "all_alerts"
    48  }
    49  
    50  type AlertLabel struct {
    51  	LabelName  string `json:"label_name" gorm:"primaryKey;size:256;not null;"` //unique
    52  	LabelValue string `json:"label_value"`
    53  }
    54  
    55  // TableName overrides the default tablename generated by GORM
    56  func (AlertLabel) TableName() string {
    57  	return "alert_labels"
    58  }
    59  
    60  type AlertHistoryDetails struct {
    61  	ID               uint      `gorm:"primaryKey;autoIncrement:true"`
    62  	AlertId          string    `json:"alert_id"`
    63  	EventDescription string    `json:"event_description"`
    64  	UserName         string    `json:"user_name"`
    65  	EventTriggeredAt time.Time `json:"event_triggered_at"`
    66  }
    67  
    68  func (AlertHistoryDetails) TableName() string {
    69  	return "alert_history_details"
    70  }
    71  
    72  type QueryParams struct {
    73  	DataSource    string `json:"data_source"`
    74  	QueryLanguage string `json:"queryLanguage"`
    75  	QueryText     string `json:"queryText"`
    76  	StartTime     string `json:"startTime"`
    77  	EndTime       string `json:"endTime"`
    78  }
    79  type Alert struct {
    80  	Status string
    81  }
    82  
    83  type WebhookBody struct {
    84  	Receiver string
    85  	Status   string
    86  	Title    string
    87  	Body     string
    88  	Alerts   []Alert
    89  }
    90  
    91  type Contact struct {
    92  	ContactId   string             `json:"contact_id" gorm:"primaryKey"`
    93  	ContactName string             `json:"contact_name" gorm:"not null;unique"`
    94  	Email       []string           `json:"email" gorm:"type:text[]"`
    95  	Slack       []SlackTokenConfig `json:"slack" gorm:"many2many:slack_contact;auto_preload"`
    96  	PagerDuty   string             `json:"pager_duty"`
    97  	Webhook     []string           `json:"webhook" gorm:"type:text[]"`
    98  	OrgId       uint64             `json:"org_id"`
    99  }
   100  
   101  type SlackTokenConfig struct {
   102  	ID        uint   `gorm:"primaryKey;autoIncrement:true"`
   103  	ChannelId string `json:"channel_id"`
   104  	SlToken   string `json:"slack_token"`
   105  }
   106  
   107  func (SlackTokenConfig) TableName() string {
   108  	return "slack_token"
   109  }
   110  
   111  type Notification struct {
   112  	NotificationId string    `json:"notification_id" gorm:"primaryKey"`
   113  	CooldownPeriod uint64    `json:"cooldown_period"`
   114  	LastSentTime   time.Time `json:"last_sent_time"`
   115  	AlertId        string    `json:"-"`
   116  }
   117  
   118  func (Notification) TableName() string {
   119  	return "notification_details"
   120  }
   121  
   122  type AlertQueryCondition uint8 // condition for the alert queries
   123  const (
   124  	IsAbove AlertQueryCondition = iota
   125  	IsBelow
   126  	IsEqualTo
   127  	IsNotEqualTo
   128  	HasNoValue
   129  )
   130  
   131  type AlertState uint8 // state of the alerts
   132  const (
   133  	Inactive AlertState = iota
   134  	Pending
   135  	Firing
   136  	SystemGeneratedAlert = "System Generated"
   137  	UserModified         = "User Modified"
   138  	AlertFiring          = "Alert Firing"
   139  	AlertNormal          = "Alert Normal"
   140  	ConfigChange         = "Config Modified"
   141  )
   142  
   143  // This MUST be synced with how https://github.com/sigscalr/logminion structures
   144  // its output JSON.
   145  type LogLinesFile struct {
   146  	Version   string          `json:"version,omitempty"`
   147  	LogAlerts []LogLinesEntry `json:"log_alerts,omitempty"`
   148  }
   149  
   150  // This MUST be synced with how https://github.com/sigscalr/logminion structures
   151  // its output JSON.
   152  type LogLinesEntry struct {
   153  	Repository    string `json:"repository,omitempty"`
   154  	Filename      string `json:"filename,omitempty"`
   155  	LineNumber    int    `json:"line_number,omitempty"`
   156  	LogText       string `json:"log_text,omitempty"`
   157  	LogTextHash   string `json:"log_text_hash,omitempty"`
   158  	QueryLanguage string `json:"query_language,omitempty"`
   159  	Query         string `json:"query,omitempty"`
   160  	Condition     string `json:"condition,omitempty"`
   161  	Value         int    `json:"value,omitempty"`
   162  	LogLevel      string `json:"log_level,omitempty"`
   163  }
   164  
   165  type MinionSearch struct {
   166  	AlertId         string              `json:"alert_id" gorm:"primaryKey"`
   167  	AlertName       string              `json:"alert_name" gorm:"not null;unique"`
   168  	State           AlertState          `json:"state"`
   169  	CreateTimestamp time.Time           `json:"create_timestamp" gorm:"autoCreateTime:milli" `
   170  	ContactID       string              `json:"contact_id" gorm:"foreignKey:ContactId;"`
   171  	ContactName     string              `json:"contact_name"`
   172  	Labels          []AlertLabel        `json:"labels" gorm:"many2many:label_alerts"`
   173  	SilenceMinutes  uint64              `json:"silence_minutes"`
   174  	QueryParams     QueryParams         `json:"queryParams" gorm:"embedded"`
   175  	Condition       AlertQueryCondition `json:"condition"`
   176  	Value           float32             `json:"value"`
   177  	EvalFor         uint64              `json:"eval_for"`
   178  	EvalInterval    uint64              `json:"eval_interval"`
   179  	Message         string              `json:"message"`
   180  	CronJob         gocron.Job          `json:"cron_job" gorm:"embedded"`
   181  	NodeId          uint64              `json:"node_id"`
   182  	Repository      string              `json:"repository,omitempty"`
   183  	Filename        string              `json:"filename,omitempty"`
   184  	LineNumber      int                 `json:"line_number,omitempty"`
   185  	LogText         string              `json:"log_text,omitempty"`
   186  	LogTextHash     string              `json:"log_text_hash,omitempty"`
   187  	LogLevel        string              `json:"log_level,omitempty"`
   188  	OrgId           uint64              `json:"org_id"`
   189  }
   190  
   191  func (MinionSearch) TableName() string {
   192  	return "minion_searches"
   193  }