remove QueryModifiers, replace with explicit
Some checks failed
Go / build (1.21) (push) Failing after 1m4s
Go / build (1.22) (push) Failing after 1m3s

This commit is contained in:
saji 2024-03-06 17:19:16 -06:00
parent daf4fe97dc
commit 456f84b5c7
2 changed files with 9 additions and 24 deletions

21
db.go
View file

@ -130,11 +130,6 @@ func (tdb *TelemDb) AddEvents(events ...skylab.BusEvent) (int64, error) {
return tdb.AddEventsCtx(context.Background(), events...) return tdb.AddEventsCtx(context.Background(), events...)
} }
// QueryModifier augments SQL strings.
type QueryModifier interface {
ModifyStatement(*strings.Builder) error
}
// LimitOffsetModifier is a modifier to support pagniation. // LimitOffsetModifier is a modifier to support pagniation.
type LimitOffsetModifier struct { type LimitOffsetModifier struct {
Limit int Limit int
@ -157,7 +152,7 @@ type BusEventFilter struct {
// now we can optionally add a limit. // now we can optionally add a limit.
func (tdb *TelemDb) GetPackets(ctx context.Context, filter BusEventFilter, options ...QueryModifier) ([]skylab.BusEvent, error) { func (tdb *TelemDb) GetPackets(ctx context.Context, filter BusEventFilter, lim *LimitOffsetModifier) ([]skylab.BusEvent, error) {
// construct a simple // construct a simple
var whereFrags = make([]string, 0) var whereFrags = make([]string, 0)
@ -204,8 +199,8 @@ func (tdb *TelemDb) GetPackets(ctx context.Context, filter BusEventFilter, optio
// Augment our data further if there's i.e a limit modifier. // Augment our data further if there's i.e a limit modifier.
// TODO: factor this out maybe? // TODO: factor this out maybe?
for _, m := range options { if lim != nil {
m.ModifyStatement(&sb) lim.ModifyStatement(&sb)
} }
rows, err := tdb.db.QueryxContext(ctx, sb.String()) rows, err := tdb.db.QueryxContext(ctx, sb.String())
if err != nil { if err != nil {
@ -252,7 +247,7 @@ type Datum struct {
// A value is a specific data point. For example, bms_measurement.current // A value is a specific data point. For example, bms_measurement.current
// would be a value. // would be a value.
func (tdb *TelemDb) GetValues(ctx context.Context, filter BusEventFilter, func (tdb *TelemDb) GetValues(ctx context.Context, filter BusEventFilter,
field string, opts ...QueryModifier) ([]Datum, error) { field string, lim *LimitOffsetModifier) ([]Datum, error) {
// this fragment uses json_extract from sqlite to get a single // this fragment uses json_extract from sqlite to get a single
// nested value. // nested value.
sb := strings.Builder{} sb := strings.Builder{}
@ -285,12 +280,10 @@ func (tdb *TelemDb) GetValues(ctx context.Context, filter BusEventFilter,
sb.WriteString(" ORDER BY ts DESC") sb.WriteString(" ORDER BY ts DESC")
for _, m := range opts { if lim != nil {
if m == nil { lim.ModifyStatement(&sb)
continue
}
m.ModifyStatement(&sb)
} }
rows, err := tdb.db.QueryxContext(ctx, sb.String(), field, filter.Names[0]) rows, err := tdb.db.QueryxContext(ctx, sb.String(), field, filter.Names[0])
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -234,11 +234,7 @@ func apiV1GetPackets(tdb *TelemDb) http.HandlerFunc {
// TODO: is the following check needed? // TODO: is the following check needed?
var res []skylab.BusEvent var res []skylab.BusEvent
if lim != nil {
res, err = tdb.GetPackets(r.Context(), *bef, lim) res, err = tdb.GetPackets(r.Context(), *bef, lim)
} else {
res, err = tdb.GetPackets(r.Context(), *bef)
}
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
@ -281,11 +277,7 @@ func apiV1GetValues(db *TelemDb) http.HandlerFunc {
var res []Datum var res []Datum
// make the call, skip the limit modifier if it's nil. // make the call, skip the limit modifier if it's nil.
if lim == nil {
res, err = db.GetValues(r.Context(), *bef, field)
} else {
res, err = db.GetValues(r.Context(), *bef, field, lim) res, err = db.GetValues(r.Context(), *bef, field, lim)
}
if err != nil { if err != nil {
// 500 server error: // 500 server error:
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)