chore: bootstrap api-go scaffold
This commit is contained in:
1 parent
aa26549bd1
commit
907164f972
18 files changed
+1919
No files matched your search
@@ -0,0 +1,154 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net/http"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"pulsecast/api/internal/model"
|
||||
)
|
||||
|
||||
func (s *Server) handleHealthz(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]bool{"ok": true})
|
||||
}
|
||||
|
||||
func (s *Server) handleReadyz(w http.ResponseWriter, _ *http.Request) {
|
||||
ready := s.store != nil && s.store.Ready()
|
||||
dataOK := s.store != nil && s.store.Len() > 0 && !s.store.Context().UpdatedAt.IsZero()
|
||||
modelOK := s.store != nil && s.store.Meta().ModelVersion != ""
|
||||
status := http.StatusOK
|
||||
if !ready {
|
||||
status = http.StatusServiceUnavailable
|
||||
}
|
||||
writeJSON(w, status, map[string]bool{"ok": ready, "data": dataOK, "model": modelOK})
|
||||
}
|
||||
|
||||
func (s *Server) handleMeta(w http.ResponseWriter, _ *http.Request) {
|
||||
s.setVersions(w)
|
||||
writeJSON(w, http.StatusOK, s.store.Meta())
|
||||
}
|
||||
|
||||
func (s *Server) handleSensors(w http.ResponseWriter, _ *http.Request) {
|
||||
s.setVersions(w)
|
||||
writeJSON(w, http.StatusOK, s.store.Sensors())
|
||||
}
|
||||
|
||||
func (s *Server) handleSeries(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
sensor := query.Get("sensor")
|
||||
if sensor == "" {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "sensor is required")
|
||||
return
|
||||
}
|
||||
if _, ok := s.store.Sensor(sensor); !ok {
|
||||
writeError(w, http.StatusNotFound, "not_found", "unknown sensor")
|
||||
return
|
||||
}
|
||||
step := query.Get("step")
|
||||
if step == "" {
|
||||
step = "1h"
|
||||
}
|
||||
if step != "15m" && step != "1h" {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "step must be 15m or 1h")
|
||||
return
|
||||
}
|
||||
|
||||
raw, _ := s.store.Series(sensor)
|
||||
if len(raw) == 0 {
|
||||
s.setVersions(w)
|
||||
writeJSON(w, http.StatusOK, map[string]any{"sensor": sensor, "step": step, "points": []model.Point{}})
|
||||
return
|
||||
}
|
||||
|
||||
to := time.Time{}
|
||||
if rawTime := query.Get("to"); rawTime != "" {
|
||||
parsed, err := time.Parse(time.RFC3339, rawTime)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "to must be RFC 3339")
|
||||
return
|
||||
}
|
||||
to = parsed.UTC()
|
||||
} else if len(raw) > 0 {
|
||||
to = raw[len(raw)-1].T.UTC()
|
||||
}
|
||||
|
||||
from := time.Time{}
|
||||
if rawTime := query.Get("from"); rawTime != "" {
|
||||
parsed, err := time.Parse(time.RFC3339, rawTime)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "from must be RFC 3339")
|
||||
return
|
||||
}
|
||||
from = parsed.UTC()
|
||||
} else {
|
||||
from = to.AddDate(0, 0, -7)
|
||||
}
|
||||
if to.IsZero() || !from.Before(to) {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "from must be before to and the series must not be empty")
|
||||
return
|
||||
}
|
||||
|
||||
points := filterSeries(raw, from, to)
|
||||
if step == "1h" {
|
||||
points = hourlyMean(points)
|
||||
}
|
||||
|
||||
s.setVersions(w)
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"sensor": sensor,
|
||||
"step": step,
|
||||
"points": points,
|
||||
})
|
||||
}
|
||||
|
||||
func filterSeries(points []model.Point, from, to time.Time) []model.Point {
|
||||
out := make([]model.Point, 0, len(points))
|
||||
for _, point := range points {
|
||||
if point.T.Before(from) || point.T.After(to) {
|
||||
continue
|
||||
}
|
||||
out = append(out, point)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// hourlyMean aggregates 15-minute observations into clock hours (UTC buckets)
|
||||
// using the mean of the density index, not the sum.
|
||||
func hourlyMean(points []model.Point) []model.Point {
|
||||
type bucket struct {
|
||||
sum float64
|
||||
count int
|
||||
}
|
||||
buckets := make(map[time.Time]bucket)
|
||||
for _, point := range points {
|
||||
key := point.T.UTC().Truncate(time.Hour)
|
||||
b := buckets[key]
|
||||
b.sum += point.V
|
||||
b.count++
|
||||
buckets[key] = b
|
||||
}
|
||||
keys := make([]time.Time, 0, len(buckets))
|
||||
for key := range buckets {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Slice(keys, func(i, j int) bool { return keys[i].Before(keys[j]) })
|
||||
|
||||
out := make([]model.Point, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
b := buckets[key]
|
||||
out = append(out, model.Point{T: key, V: math.Round(b.sum/float64(b.count)*10) / 10})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Server) handleContext(w http.ResponseWriter, _ *http.Request) {
|
||||
ctx := s.store.Context()
|
||||
s.setVersions(w)
|
||||
writeJSON(w, http.StatusOK, model.ContextResponse{
|
||||
Weather: ctx.Weather,
|
||||
Holidays: ctx.Holidays,
|
||||
UpdatedAt: ctx.UpdatedAt,
|
||||
Cached: false,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user