aboutsummaryrefslogtreecommitdiff
path: root/cmd/mimi/modules/discord/flyio/statuspage.go
blob: f23890260e0fcedc980e6c7517abb16548925860 (plain)
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
package flyio

import (
	"context"
	"flag"
	"fmt"
	"log/slog"
	"net/http"
	"strings"

	"github.com/bwmarrin/discordgo"
	"google.golang.org/protobuf/types/known/emptypb"
	"within.website/x/proto/mimi/statuspage"
)

var (
	statusChannelID = flag.String("fly-discord-status-channel", "1194719777625751573", "fly discord status channel ID")
)

func (m *Module) RegisterHTTP(mux *http.ServeMux) {
	mux.Handle(statuspage.UpdatePathPrefix, statuspage.NewUpdateServer(&UpdateService{Module: m}))
}

type UpdateService struct {
	*Module
}

func (u *UpdateService) Poke(ctx context.Context, sp *statuspage.StatusUpdate) (*emptypb.Empty, error) {
	go u.UpdateStatus(context.Background(), sp)

	return &emptypb.Empty{}, nil
}

func (u *UpdateService) UpdateStatus(ctx context.Context, sp *statuspage.StatusUpdate) {
	if sp.GetIncident() != nil {
		u.UpdateIncident(ctx, sp.GetIncident())
		return
	}

	u.UpdateComponent(ctx, sp)
}

func (u *UpdateService) UpdateIncident(ctx context.Context, incident *statuspage.Incident) {
	var sb strings.Builder

	fmt.Fprintf(&sb, "# %s\nimpact: %s\n", incident.GetName(), incident.GetImpact())
	fmt.Fprintf(&sb, "Follow the incident at %s\n", incident.GetShortlink())
	fmt.Fprintf(&sb, "Status: %s\n", incident.GetStatus())
	fmt.Fprintf(&sb, "Latest update:\n")
	fmt.Fprintf(&sb, "At %s: \n%s\n\n", incident.GetIncidentUpdates()[0].GetCreatedAt(), incident.GetIncidentUpdates()[0].GetBody())

	if _, err := u.sess.ChannelMessageSendEmbed(*statusChannelID, &discordgo.MessageEmbed{
		URL:         incident.GetShortlink(),
		Title:       fmt.Sprintf("Incident %s: %s", incident.GetId(), incident.GetName()),
		Description: sb.String(),
	}, discordgo.WithContext(ctx)); err != nil {
		slog.Error("failed to send incident update", "err", err, "incident", incident)
		return
	}
}

func (u *UpdateService) UpdateComponent(ctx context.Context, sp *statuspage.StatusUpdate) {
	var sb strings.Builder

	fmt.Fprintf(&sb, "# %s\nstatus: %s\n", sp.GetComponent().GetName(), sp.GetComponent().GetStatus())

	if _, err := u.sess.ChannelMessageSendEmbed(*statusChannelID, &discordgo.MessageEmbed{
		URL:         "https://status.flyio.net/",
		Title:       fmt.Sprintf("Component %s: %s", sp.GetComponent().GetId(), sp.GetComponent().GetName()),
		Description: sb.String(),
	}); err != nil {
		slog.Error("failed to send component update", "err", err, "component", sp.GetComponent())
		return
	}
}