Skip to content

Commit

Permalink
feat: add empty status even if not executed yet
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 12, 2021
1 parent 413b895 commit f593576
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
14 changes: 10 additions & 4 deletions exporter/freeze.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type frozenProbeHistory struct {
Target string `json:"target"`
Status string `json:"status"`
History []frozenRecord `json:"history"`
Updated string `json:"updated"`
Updated string `json:"updated,omitempty"`
}

func freezeProbeHistory(h *store.ProbeHistory) frozenProbeHistory {
Expand All @@ -36,13 +36,19 @@ func freezeProbeHistory(h *store.ProbeHistory) frozenProbeHistory {
})
}

last := h.Results[len(h.Results)-1]
status := "NA"
updated := ""
if len(h.Results) > 0 {
last := h.Results[len(h.Results)-1]
status = last.Status.String()
updated = last.CheckedAt.Format(time.RFC3339)
}

return frozenProbeHistory{
Target: h.Target.String(),
Status: last.Status.String(),
Status: status,
History: hs,
Updated: last.CheckedAt.Format(time.RFC3339),
Updated: updated,
}
}

Expand Down
2 changes: 1 addition & 1 deletion exporter/templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ <h2>{{ .Target }}</h2>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><use xlink:href="#ok-icon" /></svg>
</span>
{{ end }}{{ end }}</div>
<span class="updated">updated: <time>{{ .Updated }}</time></span>
<span class="updated">updated: <time>{{ if .Updated }}{{ .Updated }}{{ else }}(not yet){{ end }}</time></span>
</section>{{ end }}
</article>

Expand Down
2 changes: 1 addition & 1 deletion exporter/templates/status.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
if eq .Status "NA" }}─{{ end }}{{
if eq .Status "????" }}━{{ end }}{{
if eq .Status "FAIL" }}!{{ end }}{{
if eq .Status "OK" }}✓{{ end }}{{ end }}┤ updated: {{ .Updated }}
if eq .Status "OK" }}✓{{ end }}{{ end }}┤ updated: {{ if .Updated }}{{ .Updated }}{{ else }}(not yet){{ end }}
{{ end }}

──────────────────────────────┤ Current Incident ├──────────────────────────────
Expand Down
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,16 @@ func RunServer(tasks []Task) {
scheduler := cron.New()
store := store.New(*storePath)

if err := store.Restore(); err != nil {
fmt.Fprintf(os.Stderr, "failed to create or open log file: %s\n", err)
os.Exit(1)
}

for _, t := range tasks {
fmt.Printf("%s\t%s\n", t.Schedule, t.Probe.Target())

store.AddTarget(t.Probe.Target())

f := t.Probe.Check
job := func() {
store.Append(f())
Expand All @@ -172,11 +179,6 @@ func RunServer(tasks []Task) {
}
fmt.Println()

if err := store.Restore(); err != nil {
fmt.Fprintf(os.Stderr, "failed to create or open log file: %s\n", err)
os.Exit(1)
}

scheduler.Start()
defer scheduler.Stop()

Expand Down
11 changes: 11 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,14 @@ func (s *Store) Restore() error {

return nil
}

func (s *Store) AddTarget(target *url.URL) {
s.Lock()
defer s.Unlock()

if _, ok := s.ProbeHistory[target.String()]; !ok {
s.ProbeHistory[target.String()] = &ProbeHistory{
Target: target,
}
}
}

0 comments on commit f593576

Please sign in to comment.