mirror of
https://github.com/hanzoai/status.git
synced 2026-10-30 18:21:48 +00:00
zip's registration is a verb method on the group, so a route that is not a typed operation says which method it answers with. The security package's own two-method router goes with it: it existed so both the app and a sub-router could satisfy one name, and restating a routing surface is what let the two disagree — the methods it named no longer exist. The app spells itself as a group, which registers at exactly the paths it did. The toolchain and the pin move together, because below zip v1.36.50 the JSON encoder was selected on a build tag: a repo whose go directive rises first writes a wire that is neither the old release's nor the new one's until the pin catches up. metrics_test.go still does not compile, before and after: it compares a metric registry against a gatherer from a different metrics library, so the two Gather signatures disagree. That is its own change. Co-authored-by: Hanzo Dev <[email protected]>
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/zap-proto/zip"
|
|
"hanzo.ai/status/storage/store"
|
|
"hanzo.ai/status/storage/store/common"
|
|
)
|
|
|
|
func UptimeRaw(c *zip.Ctx) error {
|
|
duration := c.Param("duration")
|
|
var from time.Time
|
|
switch duration {
|
|
case "30d":
|
|
from = time.Now().Add(-30 * 24 * time.Hour)
|
|
case "7d":
|
|
from = time.Now().Add(-7 * 24 * time.Hour)
|
|
case "24h":
|
|
from = time.Now().Add(-24 * time.Hour)
|
|
case "1h":
|
|
from = time.Now().Add(-2 * time.Hour) // Because uptime metrics are stored by hour, we have to cheat a little
|
|
default:
|
|
return c.String(400, "Durations supported: 30d, 7d, 24h, 1h")
|
|
}
|
|
key, err := url.QueryUnescape(c.Param("key"))
|
|
if err != nil {
|
|
return c.String(400, "invalid key encoding")
|
|
}
|
|
uptime, err := store.Get().GetUptimeByKey(key, from, time.Now())
|
|
if err != nil {
|
|
if errors.Is(err, common.ErrEndpointNotFound) {
|
|
return c.String(404, err.Error())
|
|
} else if errors.Is(err, common.ErrInvalidTimeRange) {
|
|
return c.String(400, err.Error())
|
|
}
|
|
return c.String(500, err.Error())
|
|
}
|
|
|
|
c.SetHeader("Content-Type", "text/plain")
|
|
c.SetHeader("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
c.SetHeader("Expires", "0")
|
|
return c.Bytes(200, []byte(fmt.Sprintf("%f", uptime)))
|
|
}
|
|
|
|
func ResponseTimeRaw(c *zip.Ctx) error {
|
|
duration := c.Param("duration")
|
|
var from time.Time
|
|
switch duration {
|
|
case "30d":
|
|
from = time.Now().Add(-30 * 24 * time.Hour)
|
|
case "7d":
|
|
from = time.Now().Add(-7 * 24 * time.Hour)
|
|
case "24h":
|
|
from = time.Now().Add(-24 * time.Hour)
|
|
case "1h":
|
|
from = time.Now().Add(-2 * time.Hour) // Because uptime metrics are stored by hour, we have to cheat a little
|
|
default:
|
|
return c.String(400, "Durations supported: 30d, 7d, 24h, 1h")
|
|
}
|
|
key, err := url.QueryUnescape(c.Param("key"))
|
|
if err != nil {
|
|
return c.String(400, "invalid key encoding")
|
|
}
|
|
responseTime, err := store.Get().GetAverageResponseTimeByKey(key, from, time.Now())
|
|
if err != nil {
|
|
if errors.Is(err, common.ErrEndpointNotFound) {
|
|
return c.String(404, err.Error())
|
|
} else if errors.Is(err, common.ErrInvalidTimeRange) {
|
|
return c.String(400, err.Error())
|
|
}
|
|
return c.String(500, err.Error())
|
|
}
|
|
|
|
c.SetHeader("Content-Type", "text/plain")
|
|
c.SetHeader("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
c.SetHeader("Expires", "0")
|
|
return c.Bytes(200, []byte(fmt.Sprintf("%d", responseTime)))
|
|
}
|