Skip to content

Commit

Permalink
feat: publc status pages
Browse files Browse the repository at this point in the history
  • Loading branch information
chamanbravo committed Feb 28, 2024
1 parent 3360b67 commit af1a644
Show file tree
Hide file tree
Showing 11 changed files with 461 additions and 65 deletions.
1 change: 0 additions & 1 deletion controllers/monitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func CreateMonitor(c *fiber.Ctx) error {
})
}

fmt.Println(newMonitor.StatusPages)
err = queries.StatusPageMonitor(monitor.ID, newMonitor.StatusPages)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
Expand Down
65 changes: 62 additions & 3 deletions controllers/statuspage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func DeleteStatusPage(c *fiber.Ctx) error {
})
}

// @Tags Notifications
// @Tags StatusPages
// @Accept json
// @Produce json
// @Param id path string true "Status Page ID"
Expand Down Expand Up @@ -143,10 +143,10 @@ func UpdateStatusPage(c *fiber.Ctx) error {

}

// @Tags Notifications
// @Tags StatusPages
// @Accept json
// @Produce json
// @Param id path string true "Status Page ID"
// @Param id path string true "Status Page Id"
// @Success 200 {object} serializers.StatusPageInfo
// @Success 400 {object} serializers.ErrorResponse
// @Router /api/status-pages/info/{id} [get]
Expand Down Expand Up @@ -177,3 +177,62 @@ func StatusPageInfo(c *fiber.Ctx) error {
"statusPage": statusPage,
})
}

// @Tags Notifications
// @Accept json
// @Produce json
// @Param slug path string true "Status Page Slug"
// @Success 200 {object} serializers.StatusPageInfo
// @Success 400 {object} serializers.ErrorResponse
// @Router /api/status-pages/summary/{slug} [get]
func StatusSummary(c *fiber.Ctx) error {
slug := c.Params("slug")
if slug == "" {
return c.Status(400).JSON(fiber.Map{
"message": "ID parameter is missing",
})
}

statusPageInfo, err := queries.FindStatusPageBySlug(slug)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

if statusPageInfo == nil {
return c.Status(400).JSON(fiber.Map{
"message": "Status page not found",
"statusPageInfo": nil,
})
}

monitors, err := queries.RetrieveStatusPageMonitors(slug)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

var monitorsList []fiber.Map
for _, v := range monitors {
heartbeat, err := queries.RetrieveHeartbeats(v.ID, 45)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}
monitorItem := fiber.Map{
"id": v.ID,
"name": v.Name,
"heartbeat": heartbeat,
}
monitorsList = append(monitorsList, monitorItem)
}

return c.JSON(fiber.Map{
"message": "Status pages list",
"statusPageInfo": statusPageInfo,
"monitors": monitorsList,
})
}
55 changes: 53 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,12 @@ 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"
}
}
]
Expand Down Expand Up @@ -947,6 +947,45 @@ const docTemplate = `
}
}
},
"/api/status-pages/summary/{slug}": {
"get": {
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/StatusPageInfo"
}
}
}
},
"400": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
}
},
"parameters": [
{
"name": "slug",
"in": "path",
"description": "Status Page Slug",
"required": true,
"schema": {
"type": "string",
"format": "string",
"description": "Status Page Slug"
}
}
]
}
},
"/api/status-pages/update/{id}": {
"put": {
"responses": {
Expand Down Expand Up @@ -1753,6 +1792,18 @@ const docTemplate = `
}
}
},
"serializers.StatusPageInfo": {
"type": "object",
"properties": {
"statusPage": {
"type": "object",
"$ref": "#/components/schemas/StatusPage"
},
"message": {
"type": "string"
}
}
},
"serializers.SuccessResponse": {
"type": "object",
"properties": {
Expand Down
55 changes: 53 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -904,12 +904,12 @@
{
"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"
}
}
]
Expand Down Expand Up @@ -941,6 +941,45 @@
}
}
},
"/api/status-pages/summary/{slug}": {
"get": {
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/StatusPageInfo"
}
}
}
},
"400": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
}
},
"parameters": [
{
"name": "slug",
"in": "path",
"description": "Status Page Slug",
"required": true,
"schema": {
"type": "string",
"format": "string",
"description": "Status Page Slug"
}
}
]
}
},
"/api/status-pages/update/{id}": {
"put": {
"responses": {
Expand Down Expand Up @@ -1747,6 +1786,18 @@
}
}
},
"serializers.StatusPageInfo": {
"type": "object",
"properties": {
"statusPage": {
"type": "object",
"$ref": "#/components/schemas/StatusPage"
},
"message": {
"type": "string"
}
}
},
"serializers.SuccessResponse": {
"type": "object",
"properties": {
Expand Down
68 changes: 67 additions & 1 deletion queries/StatusPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func FindStatusPageByMonitorId(id int) ([]models.StatusPage, error) {
stmt, err := database.DB.Prepare(`
SELECT
n.id,
n.name AS notification_name,
n.name,
n.slug
FROM
status_pages_monitors nm
Expand Down Expand Up @@ -223,3 +223,69 @@ func FindStatusPageByMonitorId(id int) ([]models.StatusPage, error) {

return statusPages, nil
}

func FindStatusPageBySlug(slug string) (*models.StatusPage, error) {
stmt, err := database.DB.Prepare("SELECT * FROM status_pages WHERE slug = $1")
if err != nil {
log.Println("Error when trying to prepare statement")
log.Println(err)
return nil, err
}
defer stmt.Close()

statusPage := new(models.StatusPage)
err = stmt.QueryRow(slug).Scan(&statusPage.ID, &statusPage.Name, &statusPage.Slug)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
log.Println("Error when trying to find monitor")
log.Println(err)
return nil, err
}

return statusPage, nil
}

func RetrieveStatusPageMonitors(slug string) ([]*models.Monitor, error) {
stmt, err := database.DB.Prepare(`
SELECT DISTINCT
spm.monitor_id,
m.name
FROM
status_pages_monitors spm
JOIN
monitors m ON spm.monitor_id = m.id
LEFT JOIN
heartbeats hb ON spm.monitor_id = hb.monitor_id
WHERE
spm.status_pages_id = (SELECT id FROM status_pages WHERE slug = $1);
`)
if err != nil {
log.Println("Error when trying to prepare statement")
log.Println(err)
return nil, err
}
defer stmt.Close()

rows, err := stmt.Query(slug)
if err != nil {
log.Println("Error when trying to query the database")
log.Println(err)
return nil, err
}

var monitors []*models.Monitor
for rows.Next() {
monitor := new(models.Monitor)
err = rows.Scan(&monitor.ID, &monitor.Name)
if err != nil {
log.Println("Error when trying to scan the row")
log.Println(err)
return nil, err
}
monitors = append(monitors, monitor)
}

return monitors, nil
}
1 change: 1 addition & 0 deletions routes/status_pages_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ func StatusPagesRoutes(app *fiber.App) {
route.Delete("/delete/:id", controllers.DeleteStatusPage)
route.Put("/update/:id", controllers.UpdateStatusPage)
route.Get("/info/:id", controllers.StatusPageInfo)
route.Get("/summary/:slug", controllers.StatusSummary)
}
Loading

0 comments on commit af1a644

Please sign in to comment.