-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.ts
154 lines (139 loc) · 3.76 KB
/
services.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { Injectable } from '@nestjs/common';
import { Client, ApiResponse } from '@elastic/elasticsearch';
import { Feature, FeatureCollection } from 'geojson';
import { elasticConfig, indices } from './config';
const esClient = new Client(elasticConfig);
/*
Traverse the ElasticSearch aggregation result to extract the geometry and properties
*/
const getAggFlight = function(buckets): FeatureCollection {
const features: Feature[] = buckets.map(b => {
const hits = b.hits.hits;
const docCount = hits.total.value;
const source = hits.hits[0];
const timePosition = source.sort[0];
const props = source._source;
const [lat, lon] = source.fields.location[0].split(',').map(Number.parseFloat);
const geometry = {
type: 'Point',
coordinates: [lon, lat],
};
const properties = {
docCount,
timePosition,
...props,
};
return {
type: 'Feature',
geometry,
properties,
};
});
return {
type: 'FeatureCollection',
features,
};
};
@Injectable()
export class PositionsService {
async getLastPosition(): Promise<Feature> {
const { indexPattern } = indices.flightTracking;
const results: ApiResponse = await esClient.search({
index: indexPattern,
body:{
"size": 1,
"query": {
"match_all": {}
}
, "sort": [
{
"timePosition": {
"order": "desc"
}
}
]
}
});
if (results.statusCode == 200) {
const result = results.body.hits.hits[0];
const {lat, lon} = result._source.location;
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [lon, lat]
},
properties: result._source
}
} else {
throw new Error('Something happened retrieving the count');
}
};
async getTotalCount(): Promise<number> {
const { indexPattern } = indices.flightTracking;
const results: ApiResponse = await esClient.count({
index: indexPattern,
});
if (results.statusCode == 200) {
return results.body.count;
} else {
throw new Error('Something happened retrieving the count');
}
}
async getAllLastPositions(): Promise<FeatureCollection> {
const { indexPattern, body } = indices.flightTracking;
const results: ApiResponse = await esClient.search({
index: indexPattern,
body,
});
if (results.statusCode == 200) {
return getAggFlight(results.body.aggregations.top_hits.buckets);
} else {
throw new Error('ErrorRetrieving last positions');
}
}
}
@Injectable()
export class AirportsService {
/*
Get all airports as a GeoJSON Feature Collection
*/
async getAirports(): Promise<FeatureCollection> {
const { indexPattern, fields, geoField } = indices.airports;
const results: ApiResponse = await esClient.search({
index: indexPattern,
size: 10000,
_source: fields,
body: {
// eslint-disable-next-line @typescript-eslint/camelcase
query: { match_all: {} },
},
});
if (results.statusCode == 200) {
const features: Feature[] = results.body?.hits.hits.map(hit => {
const source = hit._source;
const properties = Object.keys(source)
.filter(key => key !== geoField)
.reduce((obj, key) => {
obj[key] = source[key];
return obj;
}, {});
const geometry = {
type: 'Point',
coordinates: source[geoField]
}
return {
type: 'Feature',
properties,
geometry,
};
});
return {
type: 'FeatureCollection',
features,
};
} else {
throw new Error('Error getting the airports');
}
}
}