2024-02-18 01:26:13 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-03-02 03:15:51 +00:00
|
|
|
"errors"
|
2024-02-18 19:10:27 +00:00
|
|
|
"fmt"
|
2024-03-01 22:25:33 +00:00
|
|
|
"strings"
|
2024-02-18 01:26:13 +00:00
|
|
|
"time"
|
2024-03-01 22:25:33 +00:00
|
|
|
|
|
|
|
"github.com/kschamplin/gotelem/skylab"
|
2024-02-18 01:26:13 +00:00
|
|
|
)
|
|
|
|
|
2024-02-24 22:48:19 +00:00
|
|
|
// Modifier augments SQL strings.
|
|
|
|
type Modifier interface {
|
2024-03-01 22:25:33 +00:00
|
|
|
ModifyStatement(*strings.Builder) error
|
2024-02-24 22:48:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 22:25:33 +00:00
|
|
|
// LimitOffsetModifier is a modifier to support pagniation.
|
2024-02-24 22:48:19 +00:00
|
|
|
type LimitOffsetModifier struct {
|
2024-03-01 22:25:33 +00:00
|
|
|
Limit int
|
2024-02-24 22:48:19 +00:00
|
|
|
Offset int
|
|
|
|
}
|
|
|
|
|
2024-03-01 22:25:33 +00:00
|
|
|
func (l LimitOffsetModifier) ModifyStatement(sb *strings.Builder) error {
|
|
|
|
clause := fmt.Sprintf(" LIMIT %d OFFSET %d", l.Limit, l.Offset)
|
|
|
|
sb.WriteString(clause)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-24 22:48:19 +00:00
|
|
|
// BusEventFilter is a filter for bus events.
|
|
|
|
type BusEventFilter struct {
|
2024-03-01 22:25:33 +00:00
|
|
|
Names []string
|
2024-02-24 22:48:19 +00:00
|
|
|
TimerangeStart time.Time
|
2024-03-01 22:25:33 +00:00
|
|
|
TimerangeEnd time.Time
|
2024-02-24 22:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now we can optionally add a limit.
|
|
|
|
|
2024-03-01 22:25:33 +00:00
|
|
|
func (tdb *TelemDb) GetPackets(ctx context.Context, filter BusEventFilter, options ...Modifier) ([]skylab.BusEvent, error) {
|
|
|
|
// construct a simple
|
|
|
|
var whereFrags = make([]string, 0)
|
|
|
|
|
|
|
|
// if we're filtering by names, add a where clause for it.
|
|
|
|
if len(filter.Names) > 0 {
|
|
|
|
names := strings.Join(filter.Names, ", ")
|
|
|
|
qString := fmt.Sprintf("name IN (%s)", names)
|
|
|
|
whereFrags = append(whereFrags, qString)
|
|
|
|
}
|
|
|
|
// TODO: identify if we need a special case for both time ranges
|
|
|
|
// using BETWEEN since apparenlty that can be better?
|
|
|
|
|
|
|
|
// next, check if we have a start/end time, add constraints
|
|
|
|
if !filter.TimerangeEnd.IsZero() {
|
|
|
|
qString := fmt.Sprintf("ts <= %d", filter.TimerangeEnd.UnixMilli())
|
|
|
|
whereFrags = append(whereFrags, qString)
|
|
|
|
}
|
|
|
|
if !filter.TimerangeStart.IsZero() {
|
|
|
|
// we have an end range
|
|
|
|
qString := fmt.Sprintf("ts >= %d", filter.TimerangeStart.UnixMilli())
|
|
|
|
whereFrags = append(whereFrags, qString)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb := strings.Builder{}
|
|
|
|
sb.WriteString("SELECT * from \"bus_events\"")
|
|
|
|
// construct the full statement.
|
|
|
|
if len(whereFrags) > 0 {
|
|
|
|
// use the where clauses.
|
|
|
|
sb.WriteString(" WHERE ")
|
|
|
|
sb.WriteString(strings.Join(whereFrags, " AND "))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Augment our data further if there's i.e a limit modifier.
|
|
|
|
// TODO: factor this out maybe?
|
|
|
|
for _, m := range options {
|
|
|
|
m.ModifyStatement(&sb)
|
|
|
|
}
|
|
|
|
rows, err := tdb.db.QueryxContext(ctx, sb.String())
|
2024-03-02 03:15:51 +00:00
|
|
|
if err != nil {
|
2024-03-01 22:25:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var events = make([]skylab.BusEvent, 0, 10)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var ev skylab.RawJsonEvent
|
|
|
|
err := rows.Scan(&ev.Timestamp, &ev.Name, (*[]byte)(&ev.Data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-02 03:15:51 +00:00
|
|
|
BusEv := skylab.BusEvent{
|
2024-03-01 22:25:33 +00:00
|
|
|
Timestamp: time.UnixMilli(int64(ev.Timestamp)),
|
2024-03-02 03:15:51 +00:00
|
|
|
Name: ev.Name,
|
2024-03-01 22:25:33 +00:00
|
|
|
}
|
|
|
|
BusEv.Data, err = skylab.FromJson(ev.Name, ev.Data)
|
2024-03-01 22:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return events, nil
|
|
|
|
}
|
2024-03-01 22:25:33 +00:00
|
|
|
events = append(events, BusEv)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
|
|
|
|
return events, err
|
|
|
|
}
|
|
|
|
|
2024-03-02 01:12:35 +00:00
|
|
|
// We now need a different use-case: we would like to extract a value from
|
|
|
|
// a specific packet.
|
|
|
|
|
2024-02-18 01:26:13 +00:00
|
|
|
// 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.
|
2024-03-02 03:15:51 +00:00
|
|
|
func (tdb *TelemDb) GetValues(ctx context.Context, bef BusEventFilter,
|
|
|
|
field string, opts ...Modifier) ([]Datum, error) {
|
2024-02-18 01:26:13 +00:00
|
|
|
// this fragment uses json_extract from sqlite to get a single
|
|
|
|
// nested value.
|
2024-03-02 03:15:51 +00:00
|
|
|
sb := strings.Builder{}
|
|
|
|
sb.WriteString(`SELECT ts as timestamp, json_extract(data, '$.' || ?) as val FROM bus_events WHERE `)
|
|
|
|
if len(bef.Names) != 1 {
|
|
|
|
return nil, errors.New("invalid number of names")
|
|
|
|
}
|
|
|
|
|
|
|
|
qStrings := []string{"name is ?"}
|
|
|
|
// add timestamp limit.
|
|
|
|
if !bef.TimerangeStart.IsZero() {
|
|
|
|
qString := fmt.Sprintf("ts >= %d", bef.TimerangeStart.UnixMilli())
|
|
|
|
qStrings = append(qStrings, qString)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bef.TimerangeEnd.IsZero() {
|
|
|
|
qString := fmt.Sprintf("ts <= %d", bef.TimerangeEnd.UnixMilli())
|
|
|
|
qStrings = append(qStrings, qString)
|
|
|
|
}
|
|
|
|
// join qstrings with AND
|
|
|
|
sb.WriteString(strings.Join(qStrings, " AND "))
|
|
|
|
|
|
|
|
for _, m := range opts {
|
|
|
|
if m == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.ModifyStatement(&sb)
|
|
|
|
}
|
|
|
|
rows, err := tdb.db.QueryxContext(ctx, sb.String(), field, bef.Names[0])
|
2024-02-18 01:26:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-18 19:10:27 +00:00
|
|
|
defer rows.Close()
|
2024-02-18 01:26:13 +00:00
|
|
|
data := make([]Datum, 0, 10)
|
|
|
|
for rows.Next() {
|
2024-02-19 04:41:22 +00:00
|
|
|
var d Datum = Datum{}
|
2024-03-02 03:15:51 +00:00
|
|
|
var ts int64
|
|
|
|
err = rows.Scan(&ts, &d.Value)
|
|
|
|
d.Timestamp = time.UnixMilli(ts)
|
|
|
|
|
2024-02-18 01:26:13 +00:00
|
|
|
if err != nil {
|
2024-02-18 19:40:41 +00:00
|
|
|
fmt.Print(err)
|
2024-02-18 01:26:13 +00:00
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
data = append(data, d)
|
|
|
|
}
|
2024-02-19 04:41:22 +00:00
|
|
|
fmt.Print(rows.Err())
|
2024-02-18 01:26:13 +00:00
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|