Skip to content

Commit

Permalink
Retrieve services from redis
Browse files Browse the repository at this point in the history
  • Loading branch information
eirsyl committed Sep 30, 2017
1 parent 7bd6050 commit 5161289
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 84 deletions.
7 changes: 7 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '2'

services:
redis:
image: redis:latest
ports:
- '127.0.0.1:6379:6379'
17 changes: 13 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"github.com/gin-gonic/gin"
"net/http"
"github.com/eirsyl/statuspage/statuspage"
"github.com/eirsyl/statuspage/src"
"github.com/go-redis/redis"
"os"
"strconv"
Expand All @@ -22,24 +22,33 @@ func main() {
redisDBInt = 0
}


db := redis.NewClient(&redis.Options{
Addr: redisAddr,
Password: redisPassword,
DB: redisDBInt,
})

services := statuspage.Services{}
services := src.Services{}
services.Initialize(*db)

incidents := src.Incidents{}
incidents.Initialize(*db)

router := gin.Default()
router.Static("/static", "./static")
router.LoadHTMLGlob("templates/*")

router.GET("/", func(c *gin.Context) {

res, err := services.GetServices(true)
if err != nil {
panic(err)
}

c.HTML(http.StatusOK, "index.tmpl", gin.H{
"owner": "Abakus",
"services": services.GetServices(),
"services": src.AggregateServices(res),
"mostCriticalStatus": src.MostCriticalStatus(res),
})
})

Expand Down
36 changes: 36 additions & 0 deletions src/incidents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package src

import (
"github.com/go-redis/redis"
"time"
)

type Incident struct {
Id string
Time time.Time
Title string
Resolved time.Time
}

type IncidentUpdate struct {
Id string
Incident string
Status string
Message string
}

type Incidents struct {
db redis.Client
}

func (i *Incidents) Initialize(db redis.Client) {
i.db = db
}

func (i *Incidents) CreateIncident(incident Incident) error {
return nil
}

func (i *Incidents) CreateIncidentUpdate(incident string, update IncidentUpdate) error {
return nil
}
80 changes: 80 additions & 0 deletions src/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package src

import (
"github.com/go-redis/redis"
"encoding/json"
)

const REDIS_MAP = "statuspage_services"

type Service struct {
ID string
Name string
Status string
Description string
Group string
Link string
Tags []string
Enabled bool
}

type Services struct {
db redis.Client
}

func (s *Services) Initialize(db redis.Client) {
s.db = db
}

func (s *Services) InsertService(id string, service Service) error {
serialized, err := json.Marshal(service)
if err != nil { return err }

if err := s.db.HSet(REDIS_MAP, id, serialized).Err(); err != nil {
return err
}

return nil
}

func (s *Services) RemoveService(id string) error {
err := s.db.HDel(REDIS_MAP, id).Err()
return err
}

func (s *Services) GetServices(enabled bool) ([]Service, error) {
services := []Service{}

results, err := s.db.HGetAll(REDIS_MAP).Result()
if err != err {
return nil, err
}

for id, result := range results {
var service Service
if err := json.Unmarshal([]byte(result), &service); err != nil {
return nil, err
}
service.ID = id

if service.Enabled == enabled {
services = append(services, service)
}
}

return services, nil
}

func (s *Services) GetService(id string) (Service, error){
result, err := s.db.HGet(REDIS_MAP, id).Result()
if err != err {
return Service{}, err
}

var service Service
if err := json.Unmarshal([]byte(result), &service); err != nil {
return Service{}, err
}

return service, nil
}
50 changes: 50 additions & 0 deletions src/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package src


/*
* Aggregate and group services by service group.
*/

type AggregatedServices map[string][]Service

func AggregateServices (services []Service) AggregatedServices {
aggregated := AggregatedServices{}

for _, service := range services {
groupName := service.Group

group := aggregated[groupName]
if group != nil {
group = append(group, service)
} else {
group = []Service{service}
}
aggregated[groupName] = group
}

return aggregated
}

/*
* Extract the most critical service.
*/

func MostCriticalStatus(services []Service) int {
statusValues := map[string]int{
"Operational": 0,
"Performance Issues": 1,
"Partial Outage": 2,
"Major Outage": 3,
}

mostCritical := 0

for _, service := range services {
serviceStatus := statusValues[service.Status]
if serviceStatus > mostCritical {
mostCritical = serviceStatus
}
}

return mostCritical
}
50 changes: 0 additions & 50 deletions statuspage/services.go

This file was deleted.

83 changes: 53 additions & 30 deletions templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,24 @@

<div class="info-header">

<div class="notification success">
<h1>All Systems Operational</h1>
</div>

{{ if eq .mostCriticalStatus 0 }}
<div class="notification success">
<h1>All Systems Operational</h1>
</div>
{{ else if eq .mostCriticalStatus 1 }}
<div class="notification info">
<h1>Performance Issues on some services</h1>
</div>
{{ else if eq .mostCriticalStatus 2 }}
<div class="notification warning">
<h1>Partial Outage on some services</h1>
</div>
{{ else if eq .mostCriticalStatus 3 }}
<div class="notification critical">
<h1>Major Outage on some services</h1>
</div>
{{ end }}

</div>

Expand Down Expand Up @@ -70,35 +85,43 @@

<div class="services">

<div class="group">
<div class="line">
<h2>LEGO</h2>
</div>
<div class="line">
<p>API <span class="fa fa-check-circle indicator success"></span></p>
</div>
<div class="line">
<p>WEBAPP</p>
</div>
</div>
{{ $success := "fa-check-circle success" }}
{{ $info := "fa-info-circle info" }}
{{ $warning := "fa-exclamation-circle warning" }}
{{ $critical := "fa-times-circle critical" }}

{{ range $group, $services := .services }}

<div class="group">
<div class="line">
<h2>{{ $group }}</h2>
</div>

{{ range $service := $services }}

<div class="line">
<p>
{{ $service.Name }} - {{ $service.Status }}
<span class="
fa indicator
{{ if eq $service.Status "Operational" }}
{{ $success }}
{{ else if eq $service.Status "Performance Issues" }}
{{ $info }}
{{ else if eq $service.Status "Partial Outage" }}
{{ $warning }}
{{ else if eq $service.Status "Major Outage" }}
{{ $critical }}
{{ end }}
"></span>
</p>
</div>

{{ end }}

<div class="group">
<div class="line">
<h2>Other</h2>
</div>
<div class="line">
<p>FOTO <span class="fa fa-check-circle indicator success"></span></p>
</div>
<div class="line">
<p>PR <span class="fa fa-info-circle indicator info"></span></p>
</div>
<div class="line">
<p>Sentry <span class="fa fa-exclamation-circle indicator warning"></span></p>
</div>
<div class="line">
<p>Drone <span class="fa fa-times-circle indicator critical"></span></p>
</div>
</div>

{{ end }}

</div>

Expand Down

0 comments on commit 5161289

Please sign in to comment.