Skip to content

Commit db6f7d8

Browse files
committed
Add incident update stream
This could also be nested inside the incident - but for now, lets push to it's own table.
1 parent 42bf39b commit db6f7d8

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

model/incident_update_v2.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package model
2+
3+
import "github.com/incident-io/singer-tap/client"
4+
5+
type incidentUpdateV2 struct{}
6+
7+
var IncidentUpdateV2 incidentUpdateV2
8+
9+
func (incidentUpdateV2) Schema() Property {
10+
return Property{
11+
Types: []string{"object"},
12+
Properties: map[string]Property{
13+
"id": {
14+
Types: []string{"string"},
15+
},
16+
"incident_id": {
17+
Types: []string{"string"},
18+
},
19+
"message": {
20+
Types: []string{"string", "null"},
21+
},
22+
"new_incident_status": IncidentStatusV1.Schema(),
23+
"new_severity": Optional(SeverityV2.Schema()),
24+
"updater": ActorV2.Schema(),
25+
"created_at": DateTime.Schema(),
26+
},
27+
}
28+
}
29+
30+
func (incidentUpdateV2) Serialize(input client.IncidentUpdateV2) map[string]any {
31+
var severity map[string]any
32+
if input.NewSeverity != nil {
33+
severity = SeverityV2.Serialize(input.NewSeverity)
34+
}
35+
36+
return map[string]any{
37+
"id": input.Id,
38+
"incident_id": input.IncidentId,
39+
"message": input.Message,
40+
"new_incident_status": IncidentStatusV1.Serialize(input.NewIncidentStatus),
41+
"new_severity": severity,
42+
"updater": ActorV2.Serialize(input.Updater),
43+
"created_at": input.CreatedAt,
44+
}
45+
}

tap/stream_incident_updates.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package tap
2+
3+
import (
4+
"context"
5+
6+
kitlog "github.com/go-kit/log"
7+
"github.com/incident-io/singer-tap/client"
8+
"github.com/incident-io/singer-tap/model"
9+
"github.com/pkg/errors"
10+
"github.com/samber/lo"
11+
)
12+
13+
func init() {
14+
register(&StreamIncidentUpdates{})
15+
}
16+
17+
type StreamIncidentUpdates struct {
18+
}
19+
20+
func (s *StreamIncidentUpdates) Output() *Output {
21+
return &Output{
22+
Type: OutputTypeSchema,
23+
Stream: "incident_updates",
24+
Schema: &model.Schema{
25+
HasAdditionalProperties: false,
26+
Type: []string{"object"},
27+
Properties: model.IncidentUpdateV2.Schema().Properties,
28+
},
29+
KeyProperties: []string{"id"},
30+
BookmarkProperties: []string{},
31+
}
32+
}
33+
34+
func (s *StreamIncidentUpdates) GetRecords(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]map[string]any, error) {
35+
var (
36+
after *string
37+
pageSize = 250
38+
results = []map[string]any{}
39+
)
40+
41+
for {
42+
logger.Log("msg", "loading page", "page_size", pageSize, "after", after)
43+
page, err := cl.IncidentUpdatesV2ListWithResponse(ctx, &client.IncidentUpdatesV2ListParams{
44+
PageSize: &pageSize,
45+
After: after,
46+
})
47+
if err != nil {
48+
return nil, errors.Wrap(err, "listing incident updates")
49+
}
50+
51+
for _, element := range page.JSON200.IncidentUpdates {
52+
results = append(results, model.IncidentUpdateV2.Serialize(element))
53+
}
54+
if count := len(page.JSON200.IncidentUpdates); count == 0 {
55+
return results, nil // end pagination
56+
} else {
57+
after = lo.ToPtr(page.JSON200.IncidentUpdates[count-1].Id)
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)