wip: debugging why parameters don't substitute right
This commit is contained in:
parent
d456e64656
commit
8af1b725d1
|
@ -69,7 +69,6 @@ type svcDeps struct {
|
||||||
var serveThings = []service{
|
var serveThings = []service{
|
||||||
&xBeeService{},
|
&xBeeService{},
|
||||||
&canLoggerService{},
|
&canLoggerService{},
|
||||||
&rpcService{},
|
|
||||||
&dbWriterService{},
|
&dbWriterService{},
|
||||||
&httpService{},
|
&httpService{},
|
||||||
}
|
}
|
||||||
|
@ -326,8 +325,21 @@ func (h *httpService) Start(cCtx *cli.Context, deps svcDeps) (err error) {
|
||||||
|
|
||||||
r := gotelem.TelemRouter(logger, broker, db)
|
r := gotelem.TelemRouter(logger, broker, db)
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
/// TODO: use custom port if specified
|
/// 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
33
http.go
33
http.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"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
|
// 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 need a start and end time. If none is provided,
|
||||||
// we use unix epoch as start, and now + 1 day as end.
|
// 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)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,7 +2,7 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,17 +23,16 @@ func (tdb *TelemDb) GetValues(ctx context.Context, packetName, field string , st
|
||||||
const SqlFrag = `
|
const SqlFrag = `
|
||||||
SELECT
|
SELECT
|
||||||
datetime(ts /1000.0, 'unixepoch', 'subsec') as timestamp,
|
datetime(ts /1000.0, 'unixepoch', 'subsec') as timestamp,
|
||||||
json_extract(data, ?) as val,
|
json_extract(data, ?) as val
|
||||||
FROM bus_events WHERE name IS ? AND timestamp BETWEEN ? AND ?
|
FROM bus_events WHERE name IS ?;
|
||||||
`
|
`
|
||||||
|
|
||||||
fieldJson := "$." + field
|
rows, err := tdb.db.QueryxContext(ctx, "'$.current'", packetName)
|
||||||
|
|
||||||
rows, err := tdb.db.QueryxContext(ctx, fieldJson, packetName, start, end)
|
|
||||||
defer rows.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Print(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
data := make([]Datum, 0, 10)
|
data := make([]Datum, 0, 10)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var d Datum
|
var d Datum
|
||||||
|
|
Loading…
Reference in a new issue