Skip to content

Commit

Permalink
feat(index): colors, remove checks, degraded
Browse files Browse the repository at this point in the history
  • Loading branch information
mentos1386 committed May 28, 2024
1 parent 3e88327 commit dc314a4
Show file tree
Hide file tree
Showing 52 changed files with 2,504 additions and 665 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
dist/

# NPM
package-lock.json
package.json
# Node
node_modules/

# Database
Expand Down
26 changes: 25 additions & 1 deletion build/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,29 @@ module.exports = {
},
extend: {},
},
plugins: [],
plugins: [
// https://gist.github.com/Merott/d2a19b32db07565e94f10d13d11a8574
function ({ addBase, theme }) {
function extractColorVars(colorObj, colorGroup = "") {
return Object.keys(colorObj).reduce((vars, colorKey) => {
const value = colorObj[colorKey];
const cssVariable =
colorKey === "DEFAULT"
? `--color${colorGroup}`
: `--color${colorGroup}-${colorKey}`;

const newVars =
typeof value === "string"
? { [cssVariable]: value }
: extractColorVars(value, `-${colorKey}`);

return { ...vars, ...newVars };
}, {});
}

addBase({
":root": extractColorVars(theme("colors")),
});
},
],
};
20 changes: 20 additions & 0 deletions deploy.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Deploy the application to fly.io
deploy-fly:
fly deploy --ha=false -c deploy/fly.toml -i {{DOCKER_IMAGE}}

# Read local jwt key and set it as fly secret
deploy-fly-set-jwt-key-secrets:
#!/bin/bash
# https://github.com/superfly/flyctl/issues/589
cat <<EOF | fly secrets import -c deploy/fly.toml
JWT_PRIVATE_KEY="""{{JWT_PRIVATE_KEY}}"""
JWT_PUBLIC_KEY="""{{JWT_PUBLIC_KEY}}"""
EOF

# Deploy locally with docker compose
deploy-docker:
cd deploy && docker compose up

# Build the application
build:
docker build -f build/Dockerfile -t {{DOCKER_IMAGE}} .
2 changes: 0 additions & 2 deletions devbox.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"packages": [
"[email protected]",
"temporal-cli@latest",
"watchexec@latest",
"tailwindcss@latest",
"flyctl@latest",
"just@latest",
"nodePackages.npm@latest",
Expand Down
40 changes: 0 additions & 40 deletions devbox.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,46 +115,6 @@
}
}
},
"tailwindcss@latest": {
"last_modified": "2024-01-27T14:55:31Z",
"resolved": "github:NixOS/nixpkgs/160b762eda6d139ac10ae081f8f78d640dd523eb#tailwindcss",
"source": "devbox-search",
"version": "3.4.1",
"systems": {
"aarch64-darwin": {
"store_path": "/nix/store/dii6r8cwfav7r8kd779f3ma5bs71ny54-tailwindcss-3.4.1"
},
"aarch64-linux": {
"store_path": "/nix/store/b83zqgln5z5wpxiiccrlcmzgvgqj24wq-tailwindcss-3.4.1"
},
"x86_64-darwin": {
"store_path": "/nix/store/lpgbpk0lpiqp8my34l0agcz26qcdlshq-tailwindcss-3.4.1"
},
"x86_64-linux": {
"store_path": "/nix/store/xag8bx7ld2k2ixg52yw5c9knbqw5a3bs-tailwindcss-3.4.1"
}
}
},
"temporal-cli@latest": {
"last_modified": "2024-02-05T02:15:44Z",
"resolved": "github:NixOS/nixpkgs/0a254180b4cad6be45aa46dce896bdb8db5d2930#temporal-cli",
"source": "devbox-search",
"version": "1.18.0",
"systems": {
"aarch64-darwin": {
"store_path": "/nix/store/6897s8ww6fhkiqvqdwfiji7497gvcxj6-temporal-cli-1.18.0"
},
"aarch64-linux": {
"store_path": "/nix/store/d0lvc4fq5y9cql0fshy32285ldzyvhjz-temporal-cli-1.18.0"
},
"x86_64-darwin": {
"store_path": "/nix/store/dxwlx019wah1pj0hr3j50skrbdlb3x9g-temporal-cli-1.18.0"
},
"x86_64-linux": {
"store_path": "/nix/store/v6z5knwsyxymnsk1gfmlnm51j22gf1df-temporal-cli-1.18.0"
}
}
},
"watchexec@latest": {
"last_modified": "2024-01-27T14:55:31Z",
"resolved": "github:NixOS/nixpkgs/160b762eda6d139ac10ae081f8f78d640dd523eb#watchexec",
Expand Down
77 changes: 48 additions & 29 deletions internal/server/handlers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ import (
"github.com/mentos1386/zdravko/web/templates/components"
)

type HistoryOutcome string

const (
HistoryOutcomeHealthy HistoryOutcome = "HEALTHY"
HistoryOutcomeDegraded HistoryOutcome = "DEGRADED"
HistoryOutcomeUnknown HistoryOutcome = "UNKNOWN"
HistoryOutcomeDown HistoryOutcome = "DOWN"
)

type IndexData struct {
*components.Base
Targets map[string]TargetsAndStatus
TargetsLength int
TimeRange string
Status models.TargetStatus
Outcome HistoryOutcome
}

type Target struct {
Name string
Visibility models.TargetVisibility
Group string
Status models.TargetStatus
Outcome HistoryOutcome
History []*HistoryItem
Uptime float64
}
Expand All @@ -35,7 +44,7 @@ type History struct {
}

type HistoryItem struct {
Status models.TargetStatus
Outcome HistoryOutcome
StatusCounts map[models.TargetStatus]int
Counts int
Date time.Time
Expand All @@ -53,14 +62,25 @@ type HistoryItemCheck struct {
}

type TargetsAndStatus struct {
Status models.TargetStatus
Outcome HistoryOutcome
Targets []*Target
}

func getDateString(date time.Time) string {
return date.UTC().Format("2006-01-02T15:04:05")
}

func TargetStatusToHistoryOutcome(status models.TargetStatus) HistoryOutcome {
switch status {
case models.TargetStatusSuccess:
return HistoryOutcomeHealthy
case models.TargetStatusFailure:
return HistoryOutcomeDown
default:
return HistoryOutcomeUnknown
}
}

func getHistory(history []*services.TargetHistory, period time.Duration, buckets int) *History {
historyMap := map[string]*HistoryItem{}
numOfSuccess := 0.0
Expand All @@ -74,7 +94,7 @@ func getHistory(history []*services.TargetHistory, period time.Duration, buckets
mapKeys[i] = dateString

historyMap[dateString] = &HistoryItem{
Status: models.TargetStatusUnknown,
Outcome: HistoryOutcomeUnknown,
StatusCounts: map[models.TargetStatus]int{},
Date: date,
Checks: []*HistoryItemCheck{},
Expand All @@ -95,15 +115,6 @@ func getHistory(history []*services.TargetHistory, period time.Duration, buckets
numOfSuccess++
}

if entry.Status == models.TargetStatusUnknown {
entry.Status = _history.Status
}

// If not yet failure, and failing check. Mark as failing.
if _history.Status == models.TargetStatusFailure && entry.Status != models.TargetStatusFailure {
entry.Status = models.TargetStatusFailure
}

entry.StatusCounts[_history.Status]++
entry.Counts++
entry.SuccessRate = 100.0 * float64(entry.StatusCounts[models.TargetStatusSuccess]) / float64(entry.Counts)
Expand Down Expand Up @@ -146,6 +157,14 @@ func getHistory(history []*services.TargetHistory, period time.Duration, buckets
})
}

if entry.SuccessRate == 100.0 {
entry.Outcome = HistoryOutcomeHealthy
} else if entry.SuccessRate == 0.0 {
entry.Outcome = HistoryOutcomeDown
} else {
entry.Outcome = HistoryOutcomeDegraded
}

historyMap[dateString] = entry
}

Expand Down Expand Up @@ -196,8 +215,8 @@ func (h *BaseHandler) Index(c echo.Context) error {
timeBuckets = 90
}

overallStatus := models.TargetStatusUnknown
statusByGroup := make(map[string]models.TargetStatus)
overallOutcome := HistoryOutcomeUnknown
outcomeByGroup := make(map[string]HistoryOutcome)

targetsWithHistory := make([]*Target, len(targets))
for i, target := range targets {
Expand All @@ -208,29 +227,29 @@ func (h *BaseHandler) Index(c echo.Context) error {

historyResult := getHistory(history, timeInterval, timeBuckets)

if statusByGroup[target.Group] == "" {
statusByGroup[target.Group] = models.TargetStatusUnknown
if outcomeByGroup[target.Group] == "" {
outcomeByGroup[target.Group] = HistoryOutcomeUnknown
}

status := historyResult.List[len(historyResult.List)-1]
if status.Status == models.TargetStatusSuccess {
if overallStatus == models.TargetStatusUnknown {
overallStatus = status.Status
if status.Outcome == HistoryOutcomeHealthy {
if overallOutcome == HistoryOutcomeUnknown {
overallOutcome = status.Outcome
}
if statusByGroup[target.Group] == models.TargetStatusUnknown {
statusByGroup[target.Group] = status.Status
if outcomeByGroup[target.Group] == HistoryOutcomeUnknown {
outcomeByGroup[target.Group] = status.Outcome
}
}
if status.Status != models.TargetStatusSuccess && status.Status != models.TargetStatusUnknown {
overallStatus = status.Status
statusByGroup[target.Group] = status.Status
if status.Outcome != HistoryOutcomeHealthy && status.Outcome != HistoryOutcomeUnknown {
overallOutcome = status.Outcome
outcomeByGroup[target.Group] = status.Outcome
}

targetsWithHistory[i] = &Target{
Name: target.Name,
Visibility: target.Visibility,
Group: target.Group,
Status: status.Status,
Outcome: status.Outcome,
History: historyResult.List,
Uptime: historyResult.Uptime,
}
Expand All @@ -239,7 +258,7 @@ func (h *BaseHandler) Index(c echo.Context) error {
targetsByGroup := map[string]TargetsAndStatus{}
for _, target := range targetsWithHistory {
targetsByGroup[target.Group] = TargetsAndStatus{
Status: statusByGroup[target.Group],
Outcome: outcomeByGroup[target.Group],
Targets: append(targetsByGroup[target.Group].Targets, target),
}
}
Expand All @@ -253,6 +272,6 @@ func (h *BaseHandler) Index(c echo.Context) error {
},
Targets: targetsByGroup,
TimeRange: timeRangeQuery,
Status: overallStatus,
Outcome: overallOutcome,
})
}
Loading

0 comments on commit dc314a4

Please sign in to comment.