generated from shoriwe/base-go-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.go
145 lines (137 loc) · 3.4 KB
/
alerts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package models
import (
"errors"
"github.com/hidromatologia-v2/models/tables"
uuid "github.com/satori/go.uuid"
"gorm.io/gorm"
)
func (c *Controller) CreateAlert(session *tables.User, alert *tables.Alert) error {
return c.DB.Create(&tables.Alert{
UserUUID: session.UUID,
Name: alert.Name,
SensorUUID: alert.SensorUUID,
Condition: alert.Condition,
Value: alert.Value,
Enabled: alert.Enabled,
}).Error
}
func (c *Controller) DeleteAlert(session *tables.User, alert *tables.Alert) error {
query := c.DB.
Where("uuid = ?", alert.UUID).
Where("user_uuid = ?", session.UUID).
Delete(&tables.Alert{})
if err := query.Error; err != nil {
return err
}
if query.RowsAffected != 1 {
return ErrUnauthorized
}
return nil
}
func (c *Controller) UpdateAlert(session *tables.User, alert *tables.Alert) error {
query := c.DB.
Where("user_uuid = ?", session.UUID).
Where("uuid = ?", alert.UUID).
Limit(1).
Updates(
&tables.Alert{
Model: tables.Model{UUID: alert.UUID},
Name: alert.Name,
Condition: alert.Condition,
Value: alert.Value,
Enabled: alert.Enabled,
},
)
if err := query.Error; err != nil {
return err
}
if query.RowsAffected != 1 {
return ErrUnauthorized
}
return nil
}
func (c *Controller) QueryOneAlert(session *tables.User, alert *tables.Alert) (*tables.Alert, error) {
result := new(tables.Alert)
query := c.DB.
Where("user_uuid = ?", session.UUID).
Where("uuid = ?", alert.UUID).
First(result)
err := query.Error
if err == nil {
return result, nil
}
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrUnauthorized
}
return nil, err
}
func (c *Controller) QueryManyAlert(session *tables.User, filter *Filter[tables.Alert]) (*Results[tables.Alert], error) {
if filter.Page == 0 {
filter.Page = 1
}
if filter.PageSize == 0 {
filter.PageSize = 10
}
if filter.PageSize > 100 {
filter.PageSize = 100
}
query := c.DB.Where("user_uuid = ?", session.UUID)
if filter.Target.SensorUUID != uuid.Nil {
query = query.Where("sensor_uuid = ?", filter.Target.SensorUUID)
}
if filter.Target.Name != nil {
query = query.Where("name LIKE ?", *filter.Target.Name)
}
if filter.Target.Condition != nil {
query = query.Where("condition = ?", *filter.Target.Condition)
}
if filter.Target.Value != nil {
query = query.Where("value = ?", *filter.Target.Value)
}
if filter.Target.Enabled != nil {
query = query.Where("enabled = ?", *filter.Target.Enabled)
}
result := &Results[tables.Alert]{
Page: filter.Page,
PageSize: filter.PageSize,
}
query = query.
Order("uuid DESC").
Offset(filter.PageSize * (filter.Page - 1)).
Limit(filter.PageSize).
Find(&result.Entries)
if err := query.Error; err != nil {
return nil, err
}
result.Count = len(result.Entries)
return result, nil
}
func (c *Controller) CheckAlert(registry *tables.SensorRegistry) ([]tables.Alert, error) {
var alerts []tables.Alert
aErr := c.DB.
Preload("User").
Where(`
(
enabled = true AND sensor_uuid = ?
) AND
(
(condition = ? AND ? < value) OR
(condition = ? AND ? > value) OR
(condition = ? AND ? <= value) OR
(condition = ? AND ? >= value)
)
`,
registry.SensorUUID,
tables.Lt, registry.Value,
tables.Gt, registry.Value,
tables.Le, registry.Value,
tables.Ge, registry.Value,
).
Find(&alerts).
Update("enabled", false).
Error
if aErr != nil {
return nil, aErr
}
return alerts, nil
}