Skip to content

Commit

Permalink
feat: status page
Browse files Browse the repository at this point in the history
  • Loading branch information
chamanbravo committed Aug 8, 2024
1 parent 17b4987 commit 12d5e77
Show file tree
Hide file tree
Showing 14 changed files with 673 additions and 136 deletions.
44 changes: 38 additions & 6 deletions controllers/statuspage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package controllers

import (
"strconv"
"time"

"github.com/chamanbravo/upstat/dto"
"github.com/chamanbravo/upstat/models"
"github.com/chamanbravo/upstat/queries"
"github.com/chamanbravo/upstat/utils"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -182,9 +184,9 @@ func StatusPageInfo(c *fiber.Ctx) error {
// @Accept json
// @Produce json
// @Param slug path string true "Status Page Slug"
// @Success 200 {object} dto.StatusPageInfo
// @Success 200 {object} dto.StatusPageSummary
// @Success 400 {object} dto.ErrorResponse
// @Router /api/status-pages/summary/{slug} [get]
// @Router /api/status-pages/{slug}/summary [get]
func StatusSummary(c *fiber.Ctx) error {
slug := c.Params("slug")
if slug == "" {
Expand Down Expand Up @@ -215,17 +217,47 @@ func StatusSummary(c *fiber.Ctx) error {
}

var monitorsList []fiber.Map
startTime := time.Now().Add(time.Duration(-45) * time.Hour * 24)
heartbeatMap := make(map[string]dto.HeartbeatSummary)
for _, v := range monitors {
heartbeat, err := queries.RetrieveHeartbeats(v.ID, 45)
heartbeat, err := queries.RetrieveHeartbeatsByTime(v.ID, startTime)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}
for _, v := range heartbeat {
dateKey := v.Timestamp.Format("2006-01-02")
value := heartbeatMap[dateKey]

value.Total++
value.Timestamp = dateKey
if v.Status == "green" {
value.Up++
} else {
value.Down++
}

heartbeatMap[dateKey] = value
}

allHeartbeats := []dto.HeartbeatSummary{}
for _, v := range heartbeatMap {
allHeartbeats = append(allHeartbeats, v)
}

recentHeartbeats := []models.Heartbeat{}
for _, hb := range heartbeat {
if hb.Timestamp.After(time.Now().Add(-12 * time.Hour)) {
recentHeartbeats = append(recentHeartbeats, *hb)
}
}

monitorItem := fiber.Map{
"id": v.ID,
"name": v.Name,
"heartbeat": heartbeat,
"id": v.ID,
"name": v.Name,
"recent": recentHeartbeats,
"all": allHeartbeats,
}
monitorsList = append(monitorsList, monitorItem)
}
Expand Down
178 changes: 136 additions & 42 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ const docTemplate = `
}
}
},
"/api/status-pages/summary/{slug}": {
"/api/status-pages/{id}": {
"get": {
"responses": {
"200": {
Expand All @@ -881,28 +881,26 @@ const docTemplate = `
},
"parameters": [
{
"name": "slug",
"name": "id",
"in": "path",
"description": "Status Page Slug",
"description": "Status Page Id",
"required": true,
"schema": {
"type": "string",
"format": "string",
"description": "Status Page Slug"
"description": "Status Page Id"
}
}
]
}
},
"/api/status-pages/{id}": {
"get": {
},
"patch": {
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/StatusPageInfo"
"$ref": "#/components/schemas/SuccessResponse"
}
}
}
Expand All @@ -922,17 +920,27 @@ const docTemplate = `
{
"name": "id",
"in": "path",
"description": "Status Page Id",
"description": "Status Page ID",
"required": true,
"schema": {
"type": "string",
"format": "string",
"description": "Status Page Id"
"description": "Status Page ID"
}
}
]
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateStatusPageIn"
}
}
},
"required": true
}
},
"patch": {
"delete": {
"responses": {
"200": {
"description": "",
Expand Down Expand Up @@ -967,26 +975,18 @@ const docTemplate = `
"description": "Status Page ID"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateStatusPageIn"
}
}
},
"required": true
}
},
"delete": {
]
}
},
"/api/status-pages/{slug}/summary": {
"get": {
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SuccessResponse"
"$ref": "#/components/schemas/StatusPageSummary"
}
}
}
Expand All @@ -1004,14 +1004,14 @@ const docTemplate = `
},
"parameters": [
{
"name": "id",
"name": "slug",
"in": "path",
"description": "Status Page ID",
"description": "Status Page Slug",
"required": true,
"schema": {
"type": "string",
"format": "string",
"description": "Status Page ID"
"description": "Status Page Slug"
}
}
]
Expand Down Expand Up @@ -1220,6 +1220,23 @@ const docTemplate = `
}
}
},
"HeartbeatSummary": {
"type": "object",
"properties": {
"timestamp": {
"type": "string"
},
"total": {
"type": "integer"
},
"up": {
"type": "integer"
},
"down": {
"type": "integer"
}
}
},
"HeartbeatsOut": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1560,6 +1577,95 @@ const docTemplate = `
}
}
},
"StatusPageMonitorSummary": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"recent": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Heartbeat"
}
},
"all": {
"type": "array",
"items": {
"type": "object",
"properties": {
"timestamp": {
"type": "string"
},
"total": {
"type": "integer"
},
"up": {
"type": "integer"
},
"down": {
"type": "integer"
}
}
}
}
}
},
"StatusPageSummary": {
"type": "object",
"properties": {
"statusPageInfo": {
"type": "object",
"$ref": "#/components/schemas/StatusPage"
},
"monitors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"recent": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Heartbeat"
}
},
"all": {
"type": "array",
"items": {
"type": "object",
"properties": {
"timestamp": {
"type": "string"
},
"total": {
"type": "integer"
},
"up": {
"type": "integer"
},
"down": {
"type": "integer"
}
}
}
}
}
}
},
"message": {
"type": "string"
}
}
},
"SuccessResponse": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1760,18 +1866,6 @@ const docTemplate = `
}
}
},
"dto.StatusPageInfo": {
"type": "object",
"properties": {
"statusPage": {
"type": "object",
"$ref": "#/components/schemas/StatusPage"
},
"message": {
"type": "string"
}
}
},
"dto.SuccessResponse": {
"type": "object",
"properties": {
Expand Down
Loading

0 comments on commit 12d5e77

Please sign in to comment.