65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
package platform
|
|
|
|
import "strings"
|
|
|
|
func buildRawFrameSQL(database string, query RawFrameQuery) SQLQuery {
|
|
table := qualifyTDengine(database, "raw_frames")
|
|
where := []string{"1 = 1"}
|
|
args := []any{}
|
|
if query.Protocol != "" {
|
|
where = append(where, "protocol = ?")
|
|
args = append(args, query.Protocol)
|
|
}
|
|
if query.VIN != "" {
|
|
where = append(where, "vin = ?")
|
|
args = append(args, query.VIN)
|
|
}
|
|
if query.DateFrom != "" {
|
|
where = append(where, "ts >= ?")
|
|
args = append(args, query.DateFrom)
|
|
}
|
|
if query.DateTo != "" {
|
|
where = append(where, "ts <= ?")
|
|
args = append(args, query.DateTo)
|
|
}
|
|
limit := query.Limit
|
|
if limit <= 0 {
|
|
limit = 100
|
|
}
|
|
args = append(args, limit, query.Offset)
|
|
return SQLQuery{
|
|
Text: `SELECT ts, frame_id, event_time, received_at, raw_size_bytes, parsed_fields, parse_status, ` +
|
|
`parse_error, source_endpoint, protocol, vehicle_key, vin, phone FROM ` + table +
|
|
` WHERE ` + strings.Join(where, " AND ") + ` ORDER BY ts DESC LIMIT ? OFFSET ?`,
|
|
Args: args,
|
|
}
|
|
}
|
|
|
|
func buildHistoryLocationSQL(database string, query map[string]string) SQLQuery {
|
|
table := qualifyTDengine(database, "vehicle_locations")
|
|
where := []string{"1 = 1"}
|
|
args := []any{}
|
|
if protocol := strings.TrimSpace(query["protocol"]); protocol != "" {
|
|
where = append(where, "protocol = ?")
|
|
args = append(args, protocol)
|
|
}
|
|
if vin := strings.TrimSpace(query["vin"]); vin != "" {
|
|
where = append(where, "vin = ?")
|
|
args = append(args, vin)
|
|
}
|
|
limit, offset := parseLimitOffset(query["limit"], query["offset"])
|
|
args = append(args, limit, offset)
|
|
return SQLQuery{
|
|
Text: `SELECT ts, vin, protocol, longitude, latitude, speed_kmh, total_mileage_km, event_time, received_at FROM ` +
|
|
table + ` WHERE ` + strings.Join(where, " AND ") + ` ORDER BY ts DESC LIMIT ? OFFSET ?`,
|
|
Args: args,
|
|
}
|
|
}
|
|
|
|
func qualifyTDengine(database, table string) string {
|
|
if strings.TrimSpace(database) == "" {
|
|
return table
|
|
}
|
|
return database + "." + table
|
|
}
|