You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.1 KiB
Go

2 years ago
package routes
import (
"fmt"
"time"
"github.com/gofiber/fiber/v2"
)
2 years ago
func (r *Service) Api(api fiber.Router) {
2 years ago
api.Get("/status", func(c *fiber.Ctx) error {
return c.JSON("ok")
})
// GetNodeStatus
api.Get("/node/:hostname", func(c *fiber.Ctx) error {
hostname := c.Params("hostname")
if hostname == "" {
return fmt.Errorf(`must provide hostname`)
}
node, err := r.Database.GetNode(hostname)
if err != nil {
return err
}
return c.JSON(node)
})
// GetJobStatus
api.Get("/job/:id", func(c *fiber.Ctx) error {
id := c.Params("id")
if id == "" {
return fmt.Errorf(`must provide id`)
}
job, err := r.Database.GetJob(id)
if err != nil {
return err
}
return c.JSON(job)
})
// AllNodes
api.Get("/nodes", func(c *fiber.Ctx) error {
nodes, err := r.Database.AllNodes()
if err != nil {
return err
}
return c.JSON(nodes)
})
// AllJobs
api.Get("/jobs", func(c *fiber.Ctx) error {
jobs, err := r.Database.AllJobs()
if err != nil {
return err
}
return c.JSON(jobs)
})
// QueryVoltageSamples
api.Get("/stats/voltage", QueryTimeRangeMiddleware(func(c *fiber.Ctx, from, to time.Time) error {
samples, err := r.Database.QueryVoltageSamples(from, to)
if err != nil {
return err
}
return c.JSON(samples)
}))
// QueryMemorySamples
api.Get("/stats/memory", QueryTimeRangeMiddleware(func(c *fiber.Ctx, from, to time.Time) error {
samples, err := r.Database.QueryMemorySamples(from, to)
if err != nil {
return err
}
return c.JSON(samples)
}))
// QueryStorageSamples
api.Get("/stats/storage", QueryTimeRangeMiddleware(func(c *fiber.Ctx, from, to time.Time) error {
samples, err := r.Database.QueryStorageSamples(from, to)
if err != nil {
return err
}
return c.JSON(samples)
}))
// QueryCPUSamples
api.Get("/stats/cpu", QueryTimeRangeMiddleware(func(c *fiber.Ctx, from, to time.Time) error {
samples, err := r.Database.QueryCPUSamples(from, to)
if err != nil {
return err
}
return c.JSON(samples)
}))
// QueryNetworkUploadSamples
api.Get("/stats/network-upload", QueryTimeRangeMiddleware(func(c *fiber.Ctx, from, to time.Time) error {
samples, err := r.Database.QueryNetworkUploadSamples(from, to)
if err != nil {
return err
}
return c.JSON(samples)
}))
// QueryNetworkDownloadSamples
api.Get("/stats/network-download", QueryTimeRangeMiddleware(func(c *fiber.Ctx, from, to time.Time) error {
samples, err := r.Database.QueryNetworkDownloadSamples(from, to)
if err != nil {
return err
}
return c.JSON(samples)
}))
}
func QueryTimeRangeMiddleware(queryHandler func(c *fiber.Ctx, from, to time.Time) error) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
// by default just show 1 day of data
from, err := parseTimeParam(c.Query("from"), time.Now().Add(-24*time.Hour))
if err != nil {
return err
}
to, err := parseTimeParam(c.Query("to"), time.Now())
if err != nil {
return err
}
return queryHandler(c, from, to)
}
}
func parseTimeParam(s string, defaultValue time.Time) (time.Time, error) {
if s == "" {
return defaultValue, nil
}
return time.Parse(time.RFC3339, s)
2 years ago
}