wip: debugging why parameters don't substitute right

This commit is contained in:
saji 2024-02-18 13:10:27 -06:00
parent d456e64656
commit 8af1b725d1
3 changed files with 52 additions and 10 deletions

View file

@ -69,7 +69,6 @@ type svcDeps struct {
var serveThings = []service{
&xBeeService{},
&canLoggerService{},
&rpcService{},
&dbWriterService{},
&httpService{},
}
@ -326,8 +325,21 @@ func (h *httpService) Start(cCtx *cli.Context, deps svcDeps) (err error) {
r := gotelem.TelemRouter(logger, broker, db)
//
/// TODO: use custom port if specified
http.ListenAndServe(":8080", r)
server := &http.Server{
Addr: ":8080",
Handler: r,
}
go func() {
<- cCtx.Done()
logger.Info("shutting down server")
server.Shutdown(cCtx.Context)
}()
if err := server.ListenAndServe(); err != http.ErrServerClosed {
logger.ErrorCtx(cCtx.Context, "Error listening", "err", err)
}
return
}

33
http.go
View file

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
@ -82,10 +83,40 @@ func apiV1(broker *Broker, db *db.TelemDb) chi.Router {
})
// this is to get a single field
r.Get("/{name:[a-z_]+}/{field:[a-z_]}", func(w http.ResponseWriter, r *http.Request) {
r.Get("/{name:[a-z_]+}/{field:[a-z_]+}", func(w http.ResponseWriter, r *http.Request) {
var err error
// we need a start and end time. If none is provided,
// we use unix epoch as start, and now + 1 day as end.
start := time.Unix(0,0)
startString := r.URL.Query().Get("start")
if startString != "" {
start, err = time.Parse(time.RFC3339, startString)
if err != nil {
}
}
end := time.Now().Add(1*time.Hour)
endParam := r.URL.Query().Get("start")
if endParam != "" {
end, err = time.Parse(time.RFC3339, endParam)
if err != nil {
}
}
name := chi.URLParam(r, "name")
field := chi.URLParam(r, "field")
// TODO: add limit/pagination ?
res, err := db.GetValues(r.Context(), name, field, start, end)
if err != nil {
// 500 server error:
fmt.Print(err)
}
b, err := json.Marshal(res)
fmt.Print(b)
w.Write(b)
})
})

View file

@ -2,7 +2,7 @@ package db
import (
"context"
"strings"
"fmt"
"time"
)
@ -23,17 +23,16 @@ func (tdb *TelemDb) GetValues(ctx context.Context, packetName, field string , st
const SqlFrag = `
SELECT
datetime(ts /1000.0, 'unixepoch', 'subsec') as timestamp,
json_extract(data, ?) as val,
FROM bus_events WHERE name IS ? AND timestamp BETWEEN ? AND ?
json_extract(data, ?) as val
FROM bus_events WHERE name IS ?;
`
fieldJson := "$." + field
rows, err := tdb.db.QueryxContext(ctx, fieldJson, packetName, start, end)
defer rows.Close()
rows, err := tdb.db.QueryxContext(ctx, "'$.current'", packetName)
if err != nil {
fmt.Print(err)
return nil, err
}
defer rows.Close()
data := make([]Datum, 0, 10)
for rows.Next() {
var d Datum