-
Notifications
You must be signed in to change notification settings - Fork 1
/
public.controller.ts
137 lines (126 loc) · 4.17 KB
/
public.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Router, Request, Response, NextFunction } from 'express';
import geotsModel from '../model/geots.model';
import Controller from '../interface/controller.interface';
import { PUBLIC_PATH, QUERY_DATA_PATH, QUERY_PATH } from '../util/common';
import HttpException from '../exception/HttpException';
import IDataQueryParams from '../interface/dataqueryparams.interface';
import IPolygon from '../interface/polygon.interface';
import logger from '../util/logger';
class PublicController implements Controller {
public path = `/${PUBLIC_PATH}`;
public router = Router();
private geotsModel = geotsModel;
constructor() {
this.initializeRoutes();
}
private initializeRoutes() {
this.router.get(`${this.path}/${QUERY_PATH}`, this.getAvgTempForPolygonByDateRange);
this.router.get(`${this.path}/${QUERY_DATA_PATH}`, this.getDataForPolygonByDateRange);
}
private getAvgTempForPolygonByDateRange = async (req: Request, res: Response, next: NextFunction) => {
const { polygon, startdate, enddate, providerId } = req.query as unknown as IDataQueryParams;
if (!polygon || !startdate || !enddate || !providerId) {
return next(new HttpException(400, 'Invalid query parameters'));
}
let poly: IPolygon;
let start: Date;
let end: Date;
try {
poly = JSON.parse(polygon);
start = new Date(startdate);
end = new Date(enddate);
} catch (error) {
logger.log({ level: 'error', message: `getAvgTempForPolygonByDateRange: ${error}` });
return next(new HttpException(400, error.reason));
}
await this.geotsModel.aggregate([
{
// apply polygon to location
$match:
{
$and:
[
{
'metadata.location': {
$geoWithin: {
$geometry: poly
}
}
},
{ "metadata.provider": providerId },
{ ts: { $gte: start, $lt: end } }
]
}
},
// flatten measurements to one array
{
"$unwind": "$measurements"
},
// get averages for each measurement
{
$group: {
_id: "$measurements.type",
"average": { "$avg": { "$sum": "$measurements.value" } },
}
},
// get temperature only
{ $match: { _id: "T" } }
]).then((result) => {
if (result.length === 0) {
return next(new HttpException(404, 'No measurements found'));
}
console.log(parseFloat(result[0].average).toFixed(2));
return res.status(200).json(result[0].average);
}).catch((error: Error) => {
return next(new HttpException(400, error.message));
});
}
private getDataForPolygonByDateRange = async (req: Request, res: Response, next: NextFunction) => {
const { polygon, startdate, enddate, providerId } = req.query as unknown as IDataQueryParams;
if (!polygon || !startdate || !enddate || !providerId) {
return next(new HttpException(400, 'Invalid query parameters'));
}
let poly: IPolygon;
let start: Date;
let end: Date;
try {
poly = JSON.parse(polygon);
start = new Date(startdate);
end = new Date(enddate);
} catch (error) {
logger.log({ level: 'error', message: `getDataForPolygonByDateRange: ${error}` });
return next(new HttpException(400, error.reason));
}
await this.geotsModel.aggregate([
{
// apply polygon to location
$match:
{
$and:
[
{
'metadata.location': {
$geoWithin: {
$geometry: poly
}
}
},
{ "metadata.provider": providerId },
{ ts: { $gte: start, $lt: end } }
]
}
},
// set limit
{ $limit: 100 },
{ $unset: [ "_id", "__v", "hash" ] },
]).then((result) => {
if (result.length === 0) {
return next(new HttpException(404, 'No data found'));
}
return res.status(200).json(result);
}).catch((error: Error) => {
return next(new HttpException(400, error.message));
});
}
}
export default PublicController;