wip: add openmct-type historical api

This commit is contained in:
saji 2024-02-17 19:26:13 -06:00
parent 245a654164
commit 3f9df5c1eb
5 changed files with 59 additions and 5 deletions

View file

@ -81,11 +81,11 @@ func apiV1(broker *Broker, db *db.TelemDb) chi.Router {
}) })
// this is to get packets by a name. // this is to get a single field
r.Get("/{name:[a-z_]+}", func(w http.ResponseWriter, r *http.Request) { r.Get("/{name:[a-z_]+}/{field:[a-z_]}", func(w http.ResponseWriter, r *http.Request) {
// support field getting (matching too?)
// support limit
// we need a start and end time. If none is provided,
// we use unix epoch as start, and now + 1 day as end.
}) })
}) })

49
internal/db/getters.go Normal file
View file

@ -0,0 +1,49 @@
package db
import (
"context"
"strings"
"time"
)
// Datum is a single measurement - it is more granular than a packet.
// the classic example is bms_measurement.current
type Datum struct {
Timestamp time.Time `db:"timestamp"`
Value any `db:"val"`
}
// GetValues queries the database for values in a given time range.
// A value is a specific data point. For example, bms_measurement.current
// would be a value.
func (tdb *TelemDb) GetValues(ctx context.Context, packetName, field string , start time.Time,
end time.Time) ([]Datum, error) {
// this fragment uses json_extract from sqlite to get a single
// nested value.
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 ?
`
fieldJson := "$." + field
rows, err := tdb.db.QueryxContext(ctx, fieldJson, packetName, start, end)
defer rows.Close()
if err != nil {
return nil, err
}
data := make([]Datum, 0, 10)
for rows.Next() {
var d Datum
err = rows.StructScan(&d)
if err != nil {
return data, err
}
data = append(data, d)
}
return data, nil
}

View file

@ -1,6 +1,6 @@
CREATE TABLE "import_log" ( CREATE TABLE "import_log" (
"filename" TEXT NOT NULL, "filename" TEXT NOT NULL,
"date" INTEGER NOT NULL, "date" TIMESTAMP NOT NULL,
"count" INTEGER NOT NULL, "count" INTEGER NOT NULL,
"start_time" INTEGER NOT NULL, "start_time" INTEGER NOT NULL,
"end_time" INTEGER NOT NULL "end_time" INTEGER NOT NULL

View file

@ -128,6 +128,11 @@ func parseTelemLogLine(line string) (b skylab.BusEvent, err error) {
} }
ts := time.Unix(unixSeconds, unixMillis*1e6) ts := time.Unix(unixSeconds, unixMillis*1e6)
// VALIDATION STEP: sometimes the data gets really whack.
// We check that the time is between 2017 and 2032.
// Realistically we will not be using this software then.
// TODO: add this
id, err := strconv.ParseUint(a[3], 16, 16) id, err := strconv.ParseUint(a[3], 16, 16)
if err != nil { if err != nil {
err = NewFormatError("failed to parse id", err) err = NewFormatError("failed to parse id", err)