Skip to content

Commit

Permalink
wip: querying data from influx
Browse files Browse the repository at this point in the history
  • Loading branch information
raska-vilem committed Jan 26, 2025
1 parent 675e65d commit c42c8da
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
14 changes: 14 additions & 0 deletions GAPP/apps/gapp-server/src/controllers/cars.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { B_CarStatus, Q_Callsign } from '../schemas';
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';

export const carsController: FastifyPluginAsyncTypebox = async (fastify) => {
fastify.get(
'/status',
{
schema: {
summary: 'Get cars positions',
description: 'Get latest location for all registered cars.',
},
},
async (req, rep) => {
req.server.carsService.getCarsStatus(['cesilko-test']);
rep.status(200).send();
}
);

fastify.post(
'/status',
{
Expand Down
27 changes: 26 additions & 1 deletion GAPP/apps/gapp-server/src/services/cars.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { InfluxDB, Point, WriteApi } from '@influxdata/influxdb-client';
import { InfluxDB, Point, QueryApi, WriteApi } from '@influxdata/influxdb-client';
import { InfluxDbServiceBase } from '../utils/influxdb-service-base';
import { Organization } from '@influxdata/influxdb-client-apis';
import { CarStatus } from '../schemas';

export class CarsService extends InfluxDbServiceBase {
private writeAPi: WriteApi;
private queryAPi: QueryApi;

constructor(private client: InfluxDB, private org: Organization) {
super(client, org.id);
Expand All @@ -13,6 +14,7 @@ export class CarsService extends InfluxDbServiceBase {
public async init() {
await this.ensureBucket('cars');
this.writeAPi = this.client.getWriteApi(this.org.id, 'cars', 'ms');
this.queryAPi = this.client.getQueryApi(this.org.id);
}

public async deinit() {
Expand All @@ -29,4 +31,27 @@ export class CarsService extends InfluxDbServiceBase {

this.writeAPi.writePoint(point);
}

public getCarsStatus(callsigns: string[]) {
console.log(callsigns);
const query = `from(bucket: "cars")
|> range(start: -48h)
|> filter(fn: (r) => r._measurement == "car_status")
|> filter(fn: (r) => r.callsign == "cesilko-test")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> last()`;

this.queryAPi.queryRows(query, {
next(row, tableMeta) {
const obj = tableMeta.toObject(row);
console.log(obj);
},
error(error) {
console.error(error);
},
complete() {
console.log('Query completed');
},
});
}
}
4 changes: 3 additions & 1 deletion GAPP/apps/gapp-server/src/services/telemetry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export class TelemetryService extends InfluxDbServiceBase {
.tag('callsign', telemetry.payload_callsign)
.floatField('latitude', telemetry.lat)
.floatField('longitude', telemetry.lon)
.floatField('altitude', telemetry.alt);
.floatField('altitude', telemetry.alt)
.floatField('heading', telemetry.heading)
.floatField('vel_h', telemetry.vel_h);

this.writeAPi.writePoint(point);
}
Expand Down
1 change: 1 addition & 0 deletions GAPP/apps/gapp-server/src/utils/ttn-packet-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const ttnPacketDto = (ttnPayload: Static<typeof B_SondeTtnTelemetry>): Te
lon: ttnPayload.uplink_message.decoded_payload.lon,
alt: ttnPayload.uplink_message.decoded_payload.alt_m,
heading: ttnPayload.uplink_message.decoded_payload.course,
vel_h: ttnPayload.uplink_message.decoded_payload.speed_mps,
modulation: 'LoRa',
uploader_callsign: 'GAPP-Server',
};
Expand Down

0 comments on commit c42c8da

Please sign in to comment.