One gRPC service per domain in state.v1 (tasks, executions, accounts, identities, packages, captures, bindings, access, orgs, alerts and so on), each with its own server type in staterpc. RPC and message names and numbers are unchanged.
66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
// Alerts: the client half of the operator alert RPCs.
|
|
package staterpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/alert"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
var _ alert.Store = (*Client)(nil)
|
|
|
|
func (c *Client) RaiseAlert(ctx context.Context, in alert.Input) (alert.Alert, error) {
|
|
r, err := c.alert.RaiseAlert(ctx, &pb.RaiseAlertRequest{
|
|
OrgId: string(in.Org), Kind: in.Kind, Subject: in.Subject, Severity: string(in.Severity),
|
|
Title: in.Title, Body: in.Body, Link: in.Link, Condition: in.Condition,
|
|
})
|
|
if err != nil {
|
|
return alert.Alert{}, unmapError(err)
|
|
}
|
|
return alertValue(r.GetAlert()), nil
|
|
}
|
|
|
|
func (c *Client) ResolveAlert(ctx context.Context, key alert.Key, _ string) error {
|
|
_, err := c.alert.ResolveAlert(ctx, &pb.ResolveAlertRequest{OrgId: string(key.Org), Kind: key.Kind, Subject: key.Subject})
|
|
return unmapError(err)
|
|
}
|
|
|
|
func (c *Client) DismissAlert(ctx context.Context, orgID org.OrgID, id int64, _ string) error {
|
|
_, err := c.alert.DismissAlert(ctx, &pb.DismissAlertRequest{OrgId: string(orgID), Id: id})
|
|
return unmapError(err)
|
|
}
|
|
|
|
func (c *Client) ListAlerts(ctx context.Context, orgID org.OrgID, filter alert.Filter) ([]alert.Alert, error) {
|
|
states := make([]string, len(filter.States))
|
|
for i, st := range filter.States {
|
|
states[i] = string(st)
|
|
}
|
|
r, err := c.alert.ListAlerts(ctx, &pb.ListAlertsRequest{OrgId: string(orgID), States: states, Limit: int32(filter.Limit)})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
return mapValues(r.GetAlerts(), alertValue), nil
|
|
}
|
|
|
|
func (c *Client) ClaimAlertDeliveries(ctx context.Context, limit int) ([]alert.Due, error) {
|
|
r, err := c.alert.ClaimAlertDeliveries(ctx, &pb.ClaimAlertDeliveriesRequest{Limit: int32(limit)})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
return mapValues(r.GetDue(), func(d *pb.DueAlertDelivery) alert.Due {
|
|
delivery := alertDeliveryValue(d.GetDelivery())
|
|
a := alertValue(d.GetAlert())
|
|
delivery.AlertID = a.ID
|
|
return alert.Due{Delivery: delivery, Alert: a, Attempt: d.GetAttempt()}
|
|
}), nil
|
|
}
|
|
|
|
func (c *Client) RecordAlertAttempt(ctx context.Context, o alert.Outcome) error {
|
|
_, err := c.alert.RecordAlertAttempt(ctx, &pb.RecordAlertAttemptRequest{
|
|
DeliveryId: o.DeliveryID, Skipped: o.Skipped, StatusCode: int64(o.StatusCode), Error: o.Error,
|
|
})
|
|
return unmapError(err)
|
|
}
|