Skip to content

Commit

Permalink
Merge pull request #5 from JeanPoffo/feature/issue-1-2
Browse files Browse the repository at this point in the history
Improved Create Data Raw and Index Dashboard
  • Loading branch information
JeanPoffo committed Jun 22, 2022
2 parents 70252fa + a48d581 commit b489bba
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 27 deletions.
10 changes: 2 additions & 8 deletions src/controllers/ControllerDashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import { Request, Response } from 'express';
import ServiceIndexDashboard from '../service/ServiceIndexDashboard';

class ControllerDashboard {
public async index(request: Request, response: Response): Promise<Response> {
const {
startDate,
} = request.params;

public async index(_request: Request, response: Response): Promise<Response> {
const serviceIndexDashboard = new ServiceIndexDashboard();

const dataDashboard = serviceIndexDashboard.execute({
startDate: new Date(startDate),
});
const dataDashboard = await serviceIndexDashboard.execute();

return response.json(dataDashboard);
}
Expand Down
40 changes: 37 additions & 3 deletions src/service/ServiceCreateDataRaw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { aqmDataSouce } from '../config/database';
import Station from '../models/Station';
import DataRaw from '../models/DataRaw';
import Data from '../models/Data';
import ServiceConvertParticulateMaterialTwoFive from './converters/ServiceConvertParticulateMaterialTwoFive';
import ServiceConvertCarbonMonoxide from './converters/ServiceConvertCarbonMonoxide';
import ServiceConvertOzone from './converters/ServiceConvertOzone';

interface Request {
stationId: string
Expand All @@ -25,9 +29,13 @@ interface Request {
}

class ServiceCreateDataRaw {
public async execute({ stationId, ...data }: Request): Promise<DataRaw> {
public async execute({ stationId, ...restDataRaw }: Request): Promise<DataRaw> {
const stationRepository = aqmDataSouce.getRepository(Station);
const dataRawRepository = aqmDataSouce.getRepository(DataRaw);
const dataRepository = aqmDataSouce.getRepository(Data);
const serviceConvertParticulateMaterialTwoFive = new ServiceConvertParticulateMaterialTwoFive();
const serviceConvertCarbonMonoxide = new ServiceConvertCarbonMonoxide();
const serviceConvertOzone = new ServiceConvertOzone();

const station = await stationRepository.findOne({ where: { id: stationId } });

Expand All @@ -37,10 +45,36 @@ class ServiceCreateDataRaw {

const dataRaw = dataRawRepository.create({
station,
...data,
...restDataRaw,
});

return dataRawRepository.save(dataRaw);
const {
id,
particulateMaterialTwoFive,
carbonMonoxide,
ozone,
...restData
} = await dataRawRepository.save(dataRaw);

const data = dataRepository.create({
dataRaw: {
id,
},
particulateMaterialTwoFive: serviceConvertParticulateMaterialTwoFive.execute(
particulateMaterialTwoFive,
),
carbonMonoxide: serviceConvertCarbonMonoxide.execute(
carbonMonoxide,
),
ozone: serviceConvertOzone.execute(
ozone,
),
...restData,
});

await dataRepository.save(data);

return dataRaw;
}
}

Expand Down
72 changes: 56 additions & 16 deletions src/service/ServiceIndexDashboard.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { MoreThan } from 'typeorm';
import { sumAndDivide } from '../utils/numeric';
import { aqmDataSouce } from '../config/database';
import Station from '../models/Station';
import Data from '../models/Data';

interface Request {
startDate: Date
/** Only Registered Data */
interface DataResponse {
id: string,

dateRegister: Date,

particulateMaterialTwoFive: number,

carbonMonoxide: number,

ozone: number,

temperature: number,

humidity: number,
}

interface Response {
station: Station,
data: Data[],
data: DataResponse[],
conama: {
particulateMaterialTwoFive: number,
carbonMonoxide: number,
Expand All @@ -18,33 +32,59 @@ interface Response {
}

class ServiceIndexDashboard {
public async execute({ startDate }: Request): Promise<Response[]> {
public async execute(): Promise<Response[]> {
const lastTwentyFourHours = new Date();
lastTwentyFourHours.setHours(lastTwentyFourHours.getHours() - 24);

const lastEightHours = new Date();
lastEightHours.setHours(lastEightHours.getHours() - 8);

const stationRepository = aqmDataSouce.getRepository(Station);
const dataRepository = aqmDataSouce.getRepository(Data);

const stations = await stationRepository.find({ where: { isActive: true } });

const promiseReponses = stations.map(async (station) => {
const [data, records] = await dataRepository.findAndCount({
const dataTwentyFourHours = await dataRepository.find({
relations: ['dataRaw'],
where: {
dataRaw: {
station: { id: station.id },
createdAt: MoreThan(startDate),
createdAt: MoreThan(lastTwentyFourHours),
},
},
});

const particulateMaterialTwoFive = data
.map((d) => d.particulateMaterialTwoFive)
.reduce((a, b) => a + b, 0) / records;
const dataEightHours = dataTwentyFourHours.filter(
(actualData) => actualData.dataRaw.dateRegister >= lastEightHours,
);
const countTwentyFourHours = dataTwentyFourHours.length;
const countEightHours = dataEightHours.length;

const particulateMaterialTwoFive = sumAndDivide(
dataTwentyFourHours.flatMap((data) => data.particulateMaterialTwoFive),
countTwentyFourHours,
);

const carbonMonoxide = sumAndDivide(
dataEightHours.flatMap((data) => data.carbonMonoxide),
countEightHours,
);

const carbonMonoxide = data
.map((d) => d.carbonMonoxide)
.reduce((a, b) => a + b, 0) / records;
const ozone = sumAndDivide(
dataEightHours.flatMap((data) => data.ozone),
countEightHours,
);

const ozone = data
.map((d) => d.ozone)
.reduce((a, b) => a + b, 0) / records;
const data = dataTwentyFourHours.flatMap((dataActual) => ({
id: dataActual.id,
dateRegister: dataActual.dataRaw.dateRegister,
particulateMaterialTwoFive: dataActual.particulateMaterialTwoFive,
carbonMonoxide: dataActual.carbonMonoxide,
ozone: dataActual.ozone,
temperature: dataActual.temperature,
humidity: dataActual.humidity,
}));

return {
station,
Expand All @@ -57,7 +97,7 @@ class ServiceIndexDashboard {
};
});

const response = Promise.all(promiseReponses);
const response = await Promise.all(promiseReponses);

return response;
}
Expand Down
13 changes: 13 additions & 0 deletions src/service/converters/ServiceConvertCarbonMonoxide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ServiceConvertCarbonMonoxide {
readonly BASE_VALUE_CONVERSION = 0.0409;

readonly MOLECULAR_WEIGHT = 28.01;

public execute(carbonMonoxide: number): number {
return this.BASE_VALUE_CONVERSION
* carbonMonoxide
* this.MOLECULAR_WEIGHT;
}
}

export default ServiceConvertCarbonMonoxide;
13 changes: 13 additions & 0 deletions src/service/converters/ServiceConvertOzone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ServiceConvertOzone {
readonly BASE_VALUE_CONVERSION = 0.0409;

readonly MOLECULAR_WEIGHT = 48.00;

public execute(ozone: number): number {
return this.BASE_VALUE_CONVERSION
* ozone
* this.MOLECULAR_WEIGHT;
}
}

export default ServiceConvertOzone;
13 changes: 13 additions & 0 deletions src/service/converters/ServiceConvertParticulateMaterialTwoFive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ServiceConvertParticulateMaterialTwoFive {
readonly BASE_VALUE_CONVERSION = 0.0409;

readonly MOLECULAR_WEIGHT = 1;

public execute(particulateMaterialTwoFive: number): number {
return this.BASE_VALUE_CONVERSION
* particulateMaterialTwoFive
* this.MOLECULAR_WEIGHT;
}
}

export default ServiceConvertParticulateMaterialTwoFive;
8 changes: 8 additions & 0 deletions src/utils/numeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const sumAndDivide = (numbers: number[], divider: number): number => {
const sum = numbers.reduce((total, actual) => Number(total) + Number(actual), 0);
return (sum / divider) ?? 0;
};

export {
sumAndDivide,
};

0 comments on commit b489bba

Please sign in to comment.