-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_impl.go
108 lines (82 loc) · 3.41 KB
/
api_impl.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
package api
import (
"context"
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/sinkingpoint/kiora/internal/clustering"
"github.com/sinkingpoint/kiora/internal/services"
"github.com/sinkingpoint/kiora/lib/kiora/kioradb"
"github.com/sinkingpoint/kiora/lib/kiora/kioradb/query"
"github.com/sinkingpoint/kiora/lib/kiora/model"
)
var _ = API(&APIImpl{})
// API defines an interface that represents all the operations that can be performed on the kiora API.
type API interface {
// GetAlerts returns a list of alerts matching the given query.
GetAlerts(ctx context.Context, q query.AlertQuery) ([]model.Alert, error)
// PostAlerts stores the given alerts in the database, updating any existing alerts with the same labels.
PostAlerts(ctx context.Context, alerts []model.Alert) error
// QueryAlertStats executes the given stats query, returning the resulting frames.
QueryAlertStats(ctx context.Context, q query.AlertStatsQuery) ([]query.StatsResult, error)
// GetSilences returns a list of silences matching the given query.
GetSilences(ctx context.Context, query query.SilenceQuery) ([]model.Silence, error)
// PostSilences stores the given silences in the database, updating any existing silences with the same ID.
PostSilence(ctx context.Context, silences model.Silence) error
// AckAlert acknowledges the given alert with the given acknowledgement.
AckAlert(ctx context.Context, alertID string, alertAck model.AlertAcknowledgement) error
// GetClusterStatus returns the status of the nodes in the cluster.
GetClusterStatus(ctx context.Context) ([]any, error)
}
type APIImpl struct {
bus services.Bus
clusterer clustering.Clusterer
}
func NewAPIImpl(bus services.Bus, clusterer clustering.Clusterer) *APIImpl {
return &APIImpl{
bus: bus,
clusterer: clusterer,
}
}
func (a *APIImpl) GetAlerts(ctx context.Context, q query.AlertQuery) ([]model.Alert, error) {
return a.bus.DB().QueryAlerts(ctx, q), nil
}
func (a *APIImpl) PostAlerts(ctx context.Context, alerts []model.Alert) error {
var postErr error
for i := range alerts {
if err := a.bus.Config().ValidateData(ctx, &alerts[i]); err != nil {
postErr = multierror.Append(postErr, err)
}
}
if postErr != nil {
return postErr
}
return a.bus.Broadcaster().BroadcastAlerts(ctx, alerts...)
}
func (a *APIImpl) QueryAlertStats(ctx context.Context, q query.AlertStatsQuery) ([]query.StatsResult, error) {
return kioradb.QueryAlertStats(ctx, a.bus.DB(), q)
}
func (a *APIImpl) GetSilences(ctx context.Context, q query.SilenceQuery) ([]model.Silence, error) {
return a.bus.DB().QuerySilences(ctx, q), nil
}
func (a *APIImpl) PostSilence(ctx context.Context, silence model.Silence) error {
if err := a.bus.Config().ValidateData(ctx, &silence); err != nil {
return err
}
return a.bus.Broadcaster().BroadcastSilences(ctx, silence)
}
func (a *APIImpl) AckAlert(ctx context.Context, alertID string, alertAck model.AlertAcknowledgement) error {
if err := a.bus.Config().ValidateData(ctx, &alertAck); err != nil {
return err
}
if len(a.bus.DB().QueryAlerts(ctx, query.NewAlertQuery(query.ID(alertID)))) == 0 {
return fmt.Errorf("alert %q not found", alertID)
}
return a.bus.Broadcaster().BroadcastAlertAcknowledgement(ctx, alertID, alertAck)
}
func (a *APIImpl) GetClusterStatus(ctx context.Context) ([]any, error) {
if a.clusterer == nil {
return nil, errors.New("no clusterer configured")
}
return a.clusterer.Nodes(), nil
}