Skip to content

Commit

Permalink
fix: use lowercase readings properties names everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Bladesheng committed Oct 20, 2024
1 parent a880956 commit 57d9bf2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
40 changes: 20 additions & 20 deletions src/controllers/readings.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ export class ReadingsController {
throw new AppError(400, 'Bad request', errors.array());
}

const temperature_BMP = parseFloat(req.body.temperature_BMP);
const temperature_DHT = parseFloat(req.body.temperature_DHT);
const pressure_BMP = parseFloat(req.body.pressure_BMP);
const humidity_DHT = parseFloat(req.body.humidity_DHT);
const temperature_bmp = parseFloat(req.body.temperature_bmp);
const temperature_dht = parseFloat(req.body.temperature_dht);
const pressure_bmp = parseFloat(req.body.pressure_bmp);
const humidity_dht = parseFloat(req.body.humidity_dht);

const createdAt = req.body.createdAt ? new Date(req.body.createdAt) : undefined;
const created_at = req.body.created_at ? new Date(req.body.created_at) : undefined;

const reading = await ReadingsService.createReading(
temperature_BMP,
temperature_DHT,
pressure_BMP,
humidity_DHT,
createdAt
temperature_bmp,
temperature_dht,
pressure_bmp,
humidity_dht,
created_at
);

if (req.headers['short'] === 'true') {
Expand Down Expand Up @@ -239,19 +239,19 @@ export class ReadingsController {
}

const id = parseInt(req.params.id);
const temperature_BMP = parseFloat(req.body.temperature_BMP);
const temperature_DHT = parseFloat(req.body.temperature_DHT);
const pressure_BMP = parseFloat(req.body.pressure_BMP);
const humidity_DHT = parseFloat(req.body.humidity_DHT);
const createdAt = new Date(req.body.createdAt);
const temperature_bmp = parseFloat(req.body.temperature_bmp);
const temperature_dht = parseFloat(req.body.temperature_dht);
const pressure_bmp = parseFloat(req.body.pressure_bmp);
const humidity_dht = parseFloat(req.body.humidity_dht);
const created_at = new Date(req.body.created_at);

const reading = await ReadingsService.upsertReading(
id,
temperature_BMP,
temperature_DHT,
pressure_BMP,
humidity_DHT,
createdAt
temperature_bmp,
temperature_dht,
pressure_bmp,
humidity_dht,
created_at
);

res.json(reading);
Expand Down
44 changes: 21 additions & 23 deletions src/services/readings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,41 @@ export class ReadingsService {
const readings = await ReadingsService.getTimeRange(startTime, endTime);

// lttb requires arrays of x and y value
const temperature_BMP_Full = readings.map<[number, number]>((reading) => {
const temperature_bmp_Full = readings.map<[number, number]>((reading) => {
return [reading.created_at.getTime(), reading.temperature_bmp];
});
const temperature_DHT_Full = readings.map<[number, number]>((reading) => {
const temperature_dht_Full = readings.map<[number, number]>((reading) => {
return [reading.created_at.getTime(), reading.temperature_dht];
});
const humidity_DHT_Full = readings.map<[number, number]>((reading) => {
const humidity_dht_Full = readings.map<[number, number]>((reading) => {
return [reading.created_at.getTime(), reading.humidity_dht];
});
const pressure_BMP_Full = readings.map<[number, number]>((reading) => {
const pressure_bmp_Full = readings.map<[number, number]>((reading) => {
return [reading.created_at.getTime(), reading.pressure_bmp];
});

// Usually, there is ~8000 readings per month.
// With that many points, the browser starts to lag when rendering the chart.
// 1000 readings seems to be a nice compromise between browser lag and data resolution.
const DONWSAMPLED_COUNT = 1000;
const temperature_BMP_Trimmed = lttb(temperature_BMP_Full, DONWSAMPLED_COUNT);
const temperature_DHT_Trimmed = lttb(temperature_DHT_Full, DONWSAMPLED_COUNT);
const humidity_DHT_Trimmed = lttb(humidity_DHT_Full, DONWSAMPLED_COUNT);
const pressure_BMP_Trimmed = lttb(pressure_BMP_Full, DONWSAMPLED_COUNT);
const temperature_bmp_Trimmed = lttb(temperature_bmp_Full, DONWSAMPLED_COUNT);
const temperature_dht_Trimmed = lttb(temperature_dht_Full, DONWSAMPLED_COUNT);
const humidity_dht_Trimmed = lttb(humidity_dht_Full, DONWSAMPLED_COUNT);
const pressure_bmp_Trimmed = lttb(pressure_bmp_Full, DONWSAMPLED_COUNT);

// You can downsample only 1 X and Y value at a time.
// Lttb will select points from different dates (X) from each dataset.
// If you save the results into the same time point (X), some values will then be slightly shifted.
// The alternative is to use separate X axis for each dataset. But then the chart tooltip won't work properly...
const trimmedReadings = temperature_BMP_Trimmed.map((_, i) => {
const trimmedReadings = temperature_bmp_Trimmed.map((_, i) => {
return {
id: i,
createdAt: temperature_BMP_Trimmed[i][0],
created_at: temperature_bmp_Trimmed[i][0],

temperature_BMP: temperature_BMP_Trimmed[i][1],
temperature_DHT: temperature_DHT_Trimmed[i][1],
humidity_DHT: humidity_DHT_Trimmed[i][1],
pressure_BMP: pressure_BMP_Trimmed[i][1],
temperature_bmp: temperature_bmp_Trimmed[i][1],
temperature_dht: temperature_dht_Trimmed[i][1],
humidity_dht: humidity_dht_Trimmed[i][1],
pressure_bmp: pressure_bmp_Trimmed[i][1],
};
});

Expand Down Expand Up @@ -108,25 +108,23 @@ export class ReadingsService {
* @param temperature_dht
* @param pressure_bmp
* @param humidity_dht
* @param createdAt optional, if not included, current time will be used
* @param created_at optional, if not included, current time will be used
*/
public static async createReading(
temperature_bmp: number,
temperature_dht: number,
pressure_bmp: number,
humidity_dht: number,

createdAt: Date | undefined = undefined
created_at: Date | undefined = undefined
) {
const reading = await prisma.readings.create({
data: {
temperature_bmp,
temperature_dht,
pressure_bmp,
humidity_dht,

// Undefined means Prisma will use default value (current time).
created_at: createdAt,
created_at,
},
});

Expand All @@ -141,20 +139,20 @@ export class ReadingsService {
* @param temperature_dht
* @param pressure_bmp
* @param humidity_dht
* @param createdAt
* @param created_at
*/
public static async upsertReading(
id: number,
temperature_bmp: number,
temperature_dht: number,
pressure_bmp: number,
humidity_dht: number,
createdAt: Date
created_at: Date
) {
const reading = await prisma.readings.upsert({
where: { id: id },
update: {
created_at: createdAt,
created_at,

temperature_bmp,
temperature_dht,
Expand All @@ -163,7 +161,7 @@ export class ReadingsService {
},
create: {
id,
created_at: createdAt,
created_at,

temperature_bmp,
temperature_dht,
Expand Down
12 changes: 6 additions & 6 deletions src/validations/readings.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import type { Schema } from 'express-validator';

export class ReadingsValidation {
public static reading: Schema = {
temperature_BMP: {
temperature_bmp: {
in: ['body'],
trim: true,
isNumeric: true,
escape: true,
},
temperature_DHT: {
temperature_dht: {
in: ['body'],
trim: true,
isNumeric: true,
escape: true,
},
pressure_BMP: {
pressure_bmp: {
in: ['body'],
trim: true,
isNumeric: true,
escape: true,
},
humidity_DHT: {
humidity_dht: {
in: ['body'],
trim: true,
isNumeric: true,
Expand Down Expand Up @@ -59,7 +59,7 @@ export class ReadingsValidation {
};

public static readingDate: Schema = {
createdAt: {
created_at: {
in: ['body'],
optional: true,
trim: true,
Expand All @@ -71,7 +71,7 @@ export class ReadingsValidation {
};

public static readingDateRequired: Schema = {
createdAt: {
created_at: {
in: ['body'],
optional: false,
trim: true,
Expand Down

0 comments on commit 57d9bf2

Please sign in to comment.