Commit 584b13cc0826cea01030885a8919743e0403f5d5

Authored by Antoine Goutenoir
1 parent 0ff25083
Exists in master

Order the results, and unify the output schema.

Now we probably can freeze and start building on it.

/spend 6h
Showing 1 changed file with 31 additions and 12 deletions   Show diff stats
flaskr/controllers/main_controller.py
... ... @@ -230,9 +230,11 @@ def compute(): # process the queue of estimation requests
230 230 _results = {}
231 231 footprints = {}
232 232  
  233 + destinations_by_city_key = {}
  234 +
233 235 cities_sum = {}
234 236 for model in emission_models:
235   - cities = {}
  237 + cities_dict = {}
236 238 for _destination in _destinations:
237 239 footprint = model.compute_travel_footprint(
238 240 origin_latitude=_origin.latitude,
... ... @@ -243,13 +245,22 @@ def compute(): # process the queue of estimation requests
243 245  
244 246 city_key = get_city_key(_destination)
245 247  
246   - if city_key not in cities:
247   - cities[city_key] = 0.0
248   - cities[city_key] += footprint
  248 + destinations_by_city_key[city_key] = _destination
  249 +
  250 + if city_key not in cities_dict:
  251 + cities_dict[city_key] = {
  252 + 'city': city_key,
  253 + 'address': _destination.address,
  254 + 'footprint': 0.0,
  255 + }
  256 + cities_dict[city_key]['footprint'] += footprint
249 257 if city_key not in cities_sum:
250 258 cities_sum[city_key] = 0.0
251 259 cities_sum[city_key] += footprint
252 260  
  261 + cities = [cities_dict[k] for k in cities_dict.keys()]
  262 + cities = sorted(cities, key=lambda c: c['footprint'])
  263 +
253 264 footprints[model.slug] = {
254 265 'cities': cities,
255 266 }
... ... @@ -258,12 +269,19 @@ def compute(): # process the queue of estimation requests
258 269  
259 270 total = 0.0
260 271  
261   - cities_mean = {}
  272 + cities_mean_dict = {}
262 273 for city in cities_sum.keys():
263 274 city_mean = 1.0 * cities_sum[city] / len(emission_models)
264   - cities_mean[city] = city_mean
  275 + cities_mean_dict[city] = {
  276 + 'address': destinations_by_city_key[city].address,
  277 + 'city': city,
  278 + 'footprint': city_mean,
  279 + }
265 280 total += city_mean
266 281  
  282 + cities_mean = [cities_mean_dict[k] for k in cities_mean_dict.keys()]
  283 + cities_mean = sorted(cities_mean, key=lambda c: c['footprint'])
  284 +
267 285 _results['mean_footprint'] = {
268 286 'cities': cities_mean
269 287 }
... ... @@ -300,22 +318,23 @@ def compute(): # process the queue of estimation requests
300 318 # Run Scenario A for each Destination, and expose optimum Destination.
301 319 #
302 320 else:
303   - results = {
304   - 'cities': [],
305   - }
  321 + result_cities = []
306 322 for destination in destinations:
307 323 city_key = get_city_key(destination)
308 324  
309 325 city_results = compute_one_to_many(
310   - _origin=destinations[0],
  326 + _origin=destination,
311 327 _destinations=origins,
312 328 use_train_below=0,
313 329 )
314 330 city_results['city'] = city_key
315 331 city_results['address'] = destination.address
316   - results['cities'].append(city_results)
  332 + result_cities.append(city_results)
317 333  
318   - # Todo: sort cities, and perhaps extract optimum
  334 + result_cities = sorted(result_cities, key=lambda c: int(c['total']))
  335 + results = {
  336 + 'cities': result_cities,
  337 + }
319 338  
320 339 # WRITE RESULTS INTO THE DATABASE #########################################
321 340  
... ...