Skip to content

Commit 6394e47

Browse files
committed
Aggregate alerts
1 parent b24a1e9 commit 6394e47

15 files changed

+711
-5
lines changed

aggregate-alert-backup.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: aggregate-alert
2+
description: asdakmsdasd-2
3+
queryString: foo=* bar=* | count()
4+
searchIntervalSeconds: 7200
5+
actionNames:
6+
- testing
7+
labels:
8+
- label2
9+
enabled: false
10+
throttleField: asd
11+
throttleTimeSeconds: 7200
12+
queryOwnershipType: User
13+
triggerMode: ImmediateMode
14+
queryTimestampType: EventTimestamp
15+
runAsUserId: pMeYeiIlF3NljnMAIcEcRnUK

api/aggregate-alerts.go

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
graphql "github.com/cli/shurcooL-graphql"
6+
"github.com/humio/cli/api/internal/humiographql"
7+
)
8+
9+
type AggregateAlert struct {
10+
ID string `graphql:"id" yaml:"-" json:"id"`
11+
Name string `graphql:"name" yaml:"name" json:"name"`
12+
Description string `graphql:"description" yaml:"description,omitempty" json:"description,omitempty"`
13+
QueryString string `graphql:"queryString" yaml:"queryString" json:"queryString"`
14+
SearchIntervalSeconds int `graphql:"searchIntervalSeconds" yaml:"searchIntervalSeconds" json:"searchIntervalSeconds"`
15+
ActionNames []string `graphql:"actionNames" yaml:"actionNames" json:"actionNames"`
16+
Labels []string `graphql:"labels" yaml:"labels" json:"labels"`
17+
Enabled bool `graphql:"enabled" yaml:"enabled" json:"enabled"`
18+
ThrottleField string `graphql:"throttleField" yaml:"throttleField,omitempty" json:"throttleField,omitempty"`
19+
ThrottleTimeSeconds int `graphql:"throttleTimeSeconds" yaml:"throttleTimeSeconds" json:"throttleTimeSeconds"`
20+
QueryOwnershipType string `graphql:"queryOwnership" yaml:"queryOwnershipType" json:"queryOwnershipType"`
21+
TriggerMode string `graphql:"triggerMode" yaml:"triggerMode" json:"triggerMode"`
22+
QueryTimestampType string `graphql:"queryTimestampType" yaml:"queryTimestampType" json:"queryTimestampType"`
23+
RunAsUserID string `graphql:"runAsUserId" yaml:"runAsUserId,omitempty" json:"runAsUserId,omitempty"`
24+
}
25+
26+
type AggregateAlerts struct {
27+
client *Client
28+
}
29+
30+
func (c *Client) AggregateAlerts() *AggregateAlerts { return &AggregateAlerts{client: c} }
31+
32+
func (a *AggregateAlerts) List(viewName string) ([]AggregateAlert, error) {
33+
var query struct {
34+
SearchDomain struct {
35+
AggregateAlerts []humiographql.AggregateAlert `graphql:"aggregateAlerts"`
36+
} `graphql:"searchDomain(name: $viewName)"`
37+
}
38+
39+
variables := map[string]any{
40+
"viewName": graphql.String(viewName),
41+
}
42+
43+
err := a.client.Query(&query, variables)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
var aggregateAlerts = make([]AggregateAlert, len(query.SearchDomain.AggregateAlerts))
49+
for i := range query.SearchDomain.AggregateAlerts {
50+
aggregateAlerts[i] = mapHumioGraphqlAggregateAlertToAggregateAlert(query.SearchDomain.AggregateAlerts[i])
51+
}
52+
53+
return aggregateAlerts, err
54+
}
55+
56+
func (a *AggregateAlerts) Update(viewName string, updatedAggregateAlert *AggregateAlert) (*AggregateAlert, error) {
57+
if updatedAggregateAlert == nil {
58+
return nil, fmt.Errorf("updatedAggregateAlert must not be nil")
59+
}
60+
61+
if updatedAggregateAlert.ID == "" {
62+
return nil, fmt.Errorf("updatedAggregateAlert must have non-empty ID")
63+
}
64+
65+
var mutation struct {
66+
humiographql.AggregateAlert `graphql:"updateAggregateAlert(input: $input)"`
67+
}
68+
69+
actionNames := make([]graphql.String, len(updatedAggregateAlert.ActionNames))
70+
for i, actionName := range updatedAggregateAlert.ActionNames {
71+
actionNames[i] = graphql.String(actionName)
72+
}
73+
74+
labels := make([]graphql.String, len(updatedAggregateAlert.Labels))
75+
for i, label := range updatedAggregateAlert.Labels {
76+
labels[i] = graphql.String(label)
77+
}
78+
79+
updateAlert := humiographql.UpdateAggregateAlert{
80+
ViewName: humiographql.RepoOrViewName(viewName),
81+
ID: graphql.String(updatedAggregateAlert.ID),
82+
Name: graphql.String(updatedAggregateAlert.Name),
83+
Description: graphql.String(updatedAggregateAlert.Description),
84+
QueryString: graphql.String(updatedAggregateAlert.QueryString),
85+
SearchIntervalSeconds: humiographql.Long(updatedAggregateAlert.SearchIntervalSeconds),
86+
ActionIdsOrNames: actionNames,
87+
Labels: labels,
88+
Enabled: graphql.Boolean(updatedAggregateAlert.Enabled),
89+
RunAsUserID: graphql.String(updatedAggregateAlert.RunAsUserID),
90+
ThrottleField: graphql.String(updatedAggregateAlert.ThrottleField),
91+
ThrottleTimeSeconds: humiographql.Long(updatedAggregateAlert.ThrottleTimeSeconds),
92+
TriggerMode: humiographql.TriggerMode(updatedAggregateAlert.TriggerMode),
93+
QueryTimestampType: humiographql.QueryTimestampType(updatedAggregateAlert.QueryTimestampType),
94+
QueryOwnershipType: humiographql.QueryOwnershipType(updatedAggregateAlert.QueryOwnershipType),
95+
}
96+
97+
variables := map[string]any{
98+
"input": updateAlert,
99+
}
100+
101+
err := a.client.Mutate(&mutation, variables)
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(mutation.AggregateAlert)
107+
108+
return &aggregateAlert, nil
109+
}
110+
111+
func (a *AggregateAlerts) Create(viewName string, newAggregateAlert *AggregateAlert) (*AggregateAlert, error) {
112+
if newAggregateAlert == nil {
113+
return nil, fmt.Errorf("newAggregateAlert must not be nil")
114+
}
115+
116+
var mutation struct {
117+
humiographql.AggregateAlert `graphql:"createAggregateAlert(input: $input)"`
118+
}
119+
120+
actionNames := make([]graphql.String, len(newAggregateAlert.ActionNames))
121+
for i, actionName := range newAggregateAlert.ActionNames {
122+
actionNames[i] = graphql.String(actionName)
123+
}
124+
125+
labels := make([]graphql.String, len(newAggregateAlert.Labels))
126+
for i, label := range newAggregateAlert.Labels {
127+
labels[i] = graphql.String(label)
128+
}
129+
130+
createAggregateAlert := humiographql.CreateAggregateAlert{
131+
ViewName: humiographql.RepoOrViewName(viewName),
132+
Name: graphql.String(newAggregateAlert.Name),
133+
Description: graphql.String(newAggregateAlert.Description),
134+
QueryString: graphql.String(newAggregateAlert.QueryString),
135+
SearchIntervalSeconds: humiographql.Long(newAggregateAlert.SearchIntervalSeconds),
136+
ActionIdsOrNames: actionNames,
137+
Labels: labels,
138+
Enabled: graphql.Boolean(newAggregateAlert.Enabled),
139+
ThrottleField: graphql.String(newAggregateAlert.ThrottleField),
140+
ThrottleTimeSeconds: humiographql.Long(newAggregateAlert.ThrottleTimeSeconds),
141+
RunAsUserID: graphql.String(newAggregateAlert.RunAsUserID),
142+
TriggerMode: humiographql.TriggerMode(newAggregateAlert.TriggerMode),
143+
QueryTimestampType: humiographql.QueryTimestampType(newAggregateAlert.QueryTimestampType),
144+
QueryOwnershipType: humiographql.QueryOwnershipType(newAggregateAlert.QueryOwnershipType),
145+
}
146+
147+
variables := map[string]any{
148+
"input": createAggregateAlert,
149+
}
150+
151+
err := a.client.Mutate(&mutation, variables)
152+
if err != nil {
153+
return nil, err
154+
}
155+
156+
aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(mutation.AggregateAlert)
157+
158+
return &aggregateAlert, nil
159+
}
160+
161+
func (a *AggregateAlerts) Delete(viewName, aggregateAlertID string) error {
162+
if aggregateAlertID == "" {
163+
return fmt.Errorf("aggregateAlertID is empty")
164+
}
165+
166+
var mutation struct {
167+
DidDelete bool `graphql:"deleteAggregateAlert(input: { viewName: $viewName, id: $id })"`
168+
}
169+
170+
variables := map[string]any{
171+
"viewName": humiographql.RepoOrViewName(viewName),
172+
"id": graphql.String(aggregateAlertID),
173+
}
174+
175+
err := a.client.Mutate(&mutation, variables)
176+
177+
if !mutation.DidDelete {
178+
return fmt.Errorf("unable to remove aggregate alert in repo/view '%s' with id '%s'", viewName, aggregateAlertID)
179+
}
180+
181+
return err
182+
}
183+
184+
func (a *AggregateAlerts) Get(viewName string, aggregateAlertID string) (*AggregateAlert, error) {
185+
var query struct {
186+
SearchDomain struct {
187+
AggregateAlert humiographql.AggregateAlert `graphql:"aggregateAlert(id: $aggregateAlertId)"`
188+
} `graphql:"searchDomain(name: $viewName) "`
189+
}
190+
191+
variables := map[string]any{
192+
"viewName": graphql.String(viewName),
193+
"aggregateAlertId": graphql.String(aggregateAlertID),
194+
}
195+
196+
err := a.client.Query(&query, variables)
197+
if err != nil {
198+
return nil, err
199+
}
200+
201+
aggregateAlert := mapHumioGraphqlAggregateAlertToAggregateAlert(query.SearchDomain.AggregateAlert)
202+
203+
return &aggregateAlert, nil
204+
}
205+
206+
func mapHumioGraphqlAggregateAlertToAggregateAlert(input humiographql.AggregateAlert) AggregateAlert {
207+
var queryOwnershipType, runAsUserID string
208+
switch input.QueryOwnership.QueryOwnershipTypeName {
209+
case humiographql.QueryOwnershipTypeNameOrganization:
210+
queryOwnershipType = QueryOwnershipTypeOrganization
211+
case humiographql.QueryOwnershipTypeNameUser:
212+
queryOwnershipType = QueryOwnershipTypeUser
213+
runAsUserID = string(input.QueryOwnership.ID)
214+
}
215+
216+
var actionNames = make([]string, len(input.Actions))
217+
for i := range input.Actions {
218+
actionNames[i] = string(input.Actions[i].Name)
219+
}
220+
221+
var labels = make([]string, len(input.Labels))
222+
for i := range input.Labels {
223+
labels[i] = string(input.Labels[i])
224+
}
225+
226+
return AggregateAlert{
227+
ID: string(input.ID),
228+
Name: string(input.Name),
229+
Description: string(input.Description),
230+
QueryString: string(input.QueryString),
231+
SearchIntervalSeconds: int(input.SearchIntervalSeconds),
232+
ActionNames: actionNames,
233+
Labels: labels,
234+
Enabled: bool(input.Enabled),
235+
ThrottleField: string(input.ThrottleField),
236+
ThrottleTimeSeconds: int(input.ThrottleTimeSeconds),
237+
QueryOwnershipType: queryOwnershipType,
238+
TriggerMode: string(input.TriggerMode),
239+
QueryTimestampType: string(input.QueryTimestampType),
240+
RunAsUserID: runAsUserID,
241+
}
242+
}

api/error.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
EntityTypeAlert EntityType = "alert"
1313
EntityTypeFilterAlert EntityType = "filter-alert"
1414
EntityTypeScheduledSearch EntityType = "scheduled-search"
15+
EntityTypeAggregateAlert EntityType = "aggregate-alert"
1516
)
1617

1718
func (e EntityType) String() string {
@@ -68,4 +69,11 @@ func ScheduledSearchNotFound(name string) error {
6869
entityType: EntityTypeScheduledSearch,
6970
key: name,
7071
}
71-
}
72+
}
73+
74+
func AggregateAlertNotFound(name string) error {
75+
return EntityNotFound{
76+
entityType: EntityTypeAggregateAlert,
77+
key: name,
78+
}
79+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package humiographql
2+
3+
import graphql "github.com/cli/shurcooL-graphql"
4+
5+
type Action struct {
6+
Name graphql.String `graphql:"name"`
7+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package humiographql
2+
3+
import graphql "github.com/cli/shurcooL-graphql"
4+
5+
type AggregateAlert struct {
6+
ID graphql.String `graphql:"id"`
7+
Name graphql.String `graphql:"name"`
8+
Description graphql.String `graphql:"description"`
9+
QueryString graphql.String `graphql:"queryString"`
10+
SearchIntervalSeconds Long `graphql:"searchIntervalSeconds"`
11+
ThrottleTimeSeconds Long `graphql:"throttleTimeSeconds"`
12+
ThrottleField graphql.String `graphql:"throttleField"`
13+
Actions []Action `graphql:"actions"`
14+
Labels []graphql.String `graphql:"labels"`
15+
Enabled graphql.Boolean `graphql:"enabled"`
16+
QueryOwnership QueryOwnership `graphql:"queryOwnership"`
17+
TriggerMode TriggerMode `graphql:"triggerMode"`
18+
QueryTimestampType QueryTimestampType `graphql:"queryTimestampType"`
19+
}
20+
21+
type CreateAggregateAlert struct {
22+
ViewName RepoOrViewName `json:"viewName"`
23+
Name graphql.String `json:"name"`
24+
Description graphql.String `json:"description,omitempty"`
25+
QueryString graphql.String `json:"queryString"`
26+
SearchIntervalSeconds Long `json:"searchIntervalSeconds"`
27+
ThrottleTimeSeconds Long `json:"throttleTimeSeconds"`
28+
ThrottleField graphql.String `json:"throttleField,omitempty"`
29+
ActionIdsOrNames []graphql.String `json:"actionIdsOrNames"`
30+
Labels []graphql.String `json:"labels"`
31+
Enabled graphql.Boolean `json:"enabled"`
32+
RunAsUserID graphql.String `json:"runAsUserId,omitempty"`
33+
QueryOwnershipType QueryOwnershipType `json:"queryOwnershipType"`
34+
TriggerMode TriggerMode `json:"triggerMode,omitempty"`
35+
QueryTimestampType QueryTimestampType `json:"queryTimestampType"`
36+
}
37+
38+
type UpdateAggregateAlert struct {
39+
ViewName RepoOrViewName `json:"viewName"`
40+
ID graphql.String `json:"id"`
41+
Name graphql.String `json:"name"`
42+
Description graphql.String `json:"description,omitempty"`
43+
QueryString graphql.String `json:"queryString"`
44+
SearchIntervalSeconds Long `json:"searchIntervalSeconds"`
45+
ThrottleTimeSeconds Long `json:"throttleTimeSeconds"`
46+
ThrottleField graphql.String `json:"throttleField,omitempty"`
47+
ActionIdsOrNames []graphql.String `json:"actionIdsOrNames"`
48+
Labels []graphql.String `json:"labels"`
49+
Enabled graphql.Boolean `json:"enabled"`
50+
RunAsUserID graphql.String `json:"runAsUserId,omitempty"`
51+
QueryOwnershipType QueryOwnershipType `json:"queryOwnershipType"`
52+
TriggerMode TriggerMode `json:"triggerMode"`
53+
QueryTimestampType QueryTimestampType `json:"queryTimestampType"`
54+
}

api/internal/humiographql/filter-alerts.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ type FilterAlert struct {
1717
QueryOwnership QueryOwnership `graphql:"queryOwnership"`
1818
}
1919

20-
type Action struct {
21-
Name graphql.String `graphql:"name"`
22-
}
23-
2420
type CreateFilterAlert struct {
2521
ViewName RepoOrViewName `json:"viewName"`
2622
Name graphql.String `json:"name"`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package humiographql
2+
3+
type QueryTimestampType string
4+
5+
const (
6+
QueryTimestampTypeIngestTimestamp QueryTimestampType = "IngestTimestamp"
7+
QueryTimestampTypeEventTimestamp QueryTimestampType = "EventTimestamp"
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package humiographql
2+
3+
type TriggerMode string
4+
5+
const (
6+
TriggerModeCompleteMode TriggerMode = "CompleteMode"
7+
TriggerModeImmediateMode TriggerMode = "ImmediateMode"
8+
)

0 commit comments

Comments
 (0)