Skip to content

Commit

Permalink
feat: added ability to group services together
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywhitaker3 committed Apr 19, 2024
1 parent 872e86a commit 445ac8b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ The status page is configured using a yaml config file:
prometheus: http://prometheus:9090
services:
- name: Postgres
# Optional. Group services together
group: Infra
query:
# If multiple values are returned, only the first one is used
query: sum(pg_up)
# The expressions uses https://expr-lang.org and must evaluate to true/false
expression: float(pg_up) == 1
# The time range to calculate the uptime percentage with (default: 24h)
# Optional. The time range to calculate the uptime percentage with (default: 24h)
range: 24h
# The resoltuion of the range query (default: 5m)
# Optional. The resoltuion of the range query (default: 5m)
step: 5m
```
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Query struct {
type Service struct {
Name string `yaml:"name"`
Query Query `yaml:"query"`
Group string `yaml:"group"`
// Extras []Query `yaml:"extras"`
}

Expand Down Expand Up @@ -73,6 +74,9 @@ func setDefaults(conf *Config) {
if svc.Query.Step == 0 {
svc.Query.Step = time.Minute * 5
}
if svc.Group == "" {
svc.Group = "default"
}
conf.Services[i] = svc
}

Expand Down
33 changes: 31 additions & 2 deletions internal/http/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ var (
OutageFull = "Full"
)

type group struct {
Name string
Results []collector.Result
}

type statusData struct {
Config config.Config
Results []collector.Result
Results []group
Age time.Duration
Outage string
BannerClasses string
Expand All @@ -49,7 +54,7 @@ func NewStatusPageHandler(app *app.App, cache *ResultCache) echo.HandlerFunc {

data := statusData{
Config: *app.Config,
Results: res,
Results: groupResults(res),
Age: age.Round(time.Second),
Outage: op,
BannerClasses: bannerClasses(op),
Expand Down Expand Up @@ -95,3 +100,27 @@ func bannerClasses(outage string) string {
return "bg-orange-400"
}
}

func groupResults(res []collector.Result) []group {
grouped := []group{}
// Add the default group at index 0
grouped = append(grouped, group{Name: "default"})

for _, r := range res {
added := false
for i, g := range grouped {
if g.Name == r.Service.Group {
g.Results = append(g.Results, r)
grouped[i] = g
added = true
}
}
if !added {
grouped = append(grouped, group{
Name: r.Service.Group,
Results: []collector.Result{r},
})
}
}
return grouped
}
41 changes: 25 additions & 16 deletions internal/resources/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,34 @@
{{- end }}
</div>

<div class="flex flex-col items-center justify-center rounded-md shadow-md border">
{{- range .Results }}
<div class="flex justify-between items-center w-full py-6 px-6 md:px-8 lg:px-12 border-b">
<div>
<h2 class="text-lg font-bold inline-block mr-2">{{ .Service.Name }}</h2>
<span class="text-gray-500 text-sm">{{ $.Sprintf "%.2f" .Uptime }}% over the last {{ $.PrettyDuration .Service.Query.Range }}</span>
</div>
<div>
{{- if not .Success }}
<img src="/static/unknown.svg" class="h-auto max-w-10" alt="unknown icon">
{{- else if .Status }}
<img src="/static/up.svg" class="h-auto max-w-10" alt="up icon">
{{- else }}
<img src="/static/down.svg" class="h-auto max-w-10" alt="down icon">
{{- end }}
{{- range .Results }}
{{- if .Results }}
<div class="mb-5">
{{- if not (eq .Name "default") }}
<h2 class="text-xl mb-2 pl-1">{{ .Name }}</h2>
{{- end }}
<div class="flex flex-col items-center justify-center rounded-md shadow-md border">
{{- range .Results }}
<div class="flex justify-between items-center w-full py-6 px-6 md:px-8 lg:px-12 border-b">
<div>
<h3 class="text-lg font-bold inline-block mr-2">{{ .Service.Name }}</h2>
<span class="text-gray-500 text-sm">{{ $.Sprintf "%.2f" .Uptime }}% over the last {{ $.PrettyDuration .Service.Query.Range }}</span>
</div>
<div>
{{- if not .Success }}
<img src="/static/unknown.svg" class="h-auto max-w-10" alt="unknown icon">
{{- else if .Status }}
<img src="/static/up.svg" class="h-auto max-w-10" alt="up icon">
{{- else }}
<img src="/static/down.svg" class="h-auto max-w-10" alt="down icon">
{{- end }}
</div>
</div>
{{- end }}
</div>
{{- end }}
</div>
{{- end }}
{{- end }}
</div>

{{/* Footer */}}
Expand Down

0 comments on commit 445ac8b

Please sign in to comment.