Skip to content

Commit d328a63

Browse files
Robin BuschmannRobin Buschmann
authored andcommitted
raw query for location cluster added to improve performance
1 parent a7761ca commit d328a63

File tree

2 files changed

+70
-18
lines changed

2 files changed

+70
-18
lines changed

services/ChargingLocationService.ts

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ export class ChargingLocationService {
6060
chargingFacilityIds?: number[],
6161
plugIds?: number[]): Promise<ChargingLocation[]|LocationCluster[]> {
6262

63-
63+
const epsilon = this.geoService.getEpsilonByZoom(zoom);
6464
let evseWhere: any;
65+
let evseChargingFacilityJoin: string = '';
66+
let evsePlugJoin: string = '';
67+
let evseWhereStr: string = '';
68+
const replacements: any = {longitude1, longitude2, latitude1, latitude2, epsilon};
69+
6570
const evseInclude: IIncludeOptions[] = [
6671
{
6772
model: db.model(Status),
@@ -71,10 +76,14 @@ export class ChargingLocationService {
7176
// required: true
7277
}
7378
];
79+
7480
if (isOpen24Hours !== void 0) {
7581
evseWhere = {};
7682
evseWhere.isOpen24Hours = isOpen24Hours;
83+
evseWhereStr += `AND e.isOpen24Hours = :isOpen24Hours`;
84+
replacements.isOpen24Hours = isOpen24Hours;
7785
}
86+
7887
const chargingLocationInclude = [{
7988
model: db.model(EVSE),
8089
attributes: ['id'],
@@ -83,6 +92,7 @@ export class ChargingLocationService {
8392
include: evseInclude,
8493
where: evseWhere
8594
}];
95+
8696
const where = <any>{
8797
longitude: {
8898
$gte: longitude1,
@@ -101,7 +111,12 @@ export class ChargingLocationService {
101111
as: 'chargingFacilities',
102112
through: {attributes: []}, // removes EVSEChargingFacility property from status,
103113
where: {id: {$in: chargingFacilityIds}}
104-
})
114+
});
115+
evseChargingFacilityJoin = `
116+
INNER JOIN EVSEChargingFacility ecf ON ecf.evseId = e.id
117+
AND ecf.chargingFacilityId IN (:chargingFacilityIds)
118+
`;
119+
replacements.chargingFacilityIds = chargingFacilityIds;
105120
}
106121

107122
if (plugIds) {
@@ -110,7 +125,12 @@ export class ChargingLocationService {
110125
as: 'plugs',
111126
through: {attributes: []}, // removes EVSEPlug property from status
112127
where: {id: {$in: plugIds}}
113-
})
128+
});
129+
evsePlugJoin = `
130+
INNER JOIN EVSEPlug ep ON ep.evseId = e.id
131+
AND ep.plugId IN (:plugIds)
132+
`;
133+
replacements.plugIds = plugIds;
114134
}
115135

116136
if (zoom >= 12) {
@@ -123,22 +143,50 @@ export class ChargingLocationService {
123143
;
124144
}
125145

126-
where.epsilon = this.geoService.getEpsilonByZoom(zoom);
146+
if (zoom > 9) {
147+
148+
where.epsilon = epsilon;
149+
150+
return db.model(LocationCluster)
151+
.findAll<LocationCluster>({
152+
attributes: ['latitude', 'longitude'],
153+
include: [
154+
{
155+
model: db.model(ChargingLocation),
156+
attributes: ['id'],
157+
as: 'chargingLocations',
158+
include: chargingLocationInclude
159+
}
160+
],
161+
where
162+
})
163+
;
164+
}
127165

128-
return db.model(LocationCluster)
129-
.findAll<LocationCluster>({
130-
attributes: ['latitude', 'longitude'],
131-
include: [
132-
{
133-
model: db.model(ChargingLocation),
134-
attributes: ['id'],
135-
as: 'chargingLocations',
136-
include: chargingLocationInclude
137-
}
138-
],
139-
where
140-
})
141-
;
166+
return db.sequelize
167+
.query(`
168+
SELECT
169+
lc.id,
170+
lc.latitude,
171+
lc.longitude,
172+
COUNT(cl.id) AS groupCount
173+
FROM LocationCluster lc
174+
INNER JOIN LocationClusterChargingLocation lccl ON lccl.locationClusterId = lc.id
175+
INNER JOIN ChargingLocation cl ON cl.id = lccl.chargingLocationId
176+
INNER JOIN EVSE e ON e.chargingLocationId = cl.id ${evseWhereStr}
177+
${evseChargingFacilityJoin}
178+
${evsePlugJoin}
179+
WHERE
180+
lc.longitude >= :longitude1
181+
AND lc.longitude <= :longitude2
182+
AND lc.latitude >= :latitude1
183+
AND lc.latitude <= :latitude2
184+
AND lc.epsilon = :epsilon
185+
GROUP BY lc.id
186+
HAVING groupCount > 1
187+
`, {replacements})
188+
.then(res => res && res.length ? res[0] : [])
189+
;
142190
}
143191

144192
}

services/UserService.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ export class UserService {
137137
return Promise.resolve()
138138
.then(() => {
139139

140+
if(!autoGeneratedUser) {
141+
throw new UserNotFoundError();
142+
}
143+
140144
// check if user is an auto generated user
141145
if (!autoGeneratedUser.isAutoGenerated) {
142146

0 commit comments

Comments
 (0)