Commit 1d48272e83607f7c2f8c93637171f4ffad5e59f3

Authored by Antoine Goutenoir
1 parent fa3d2b43
Exists in master

Compute the mean footprint and fix a small bug.

Also, going to sleep.

/spend 5h
Showing 1 changed file with 25 additions and 7 deletions   Show diff stats
flaskr/controllers/main_controller.py
... ... @@ -64,7 +64,7 @@ def compute(): # process the queue of estimation requests
64 64 try:
65 65 estimation = Estimation.query \
66 66 .filter_by(status=StatusEnum.pending) \
67   - .order_by(Estimation.id.desc()) \
  67 + .order_by(Estimation.id.asc()) \
68 68 .first()
69 69 except sqlalchemy.orm.exc.NoResultFound:
70 70 return _respond("No estimation in the queue.")
... ... @@ -121,14 +121,14 @@ def compute(): # process the queue of estimation requests
121 121 try:
122 122 destination = geocoder.geocode(destination_address)
123 123 except geopy.exc.GeopyError as e:
124   - response += u"Failed to geolocalize destination `%s`.\n%s" % (
  124 + response += u"Failed to geocode destination `%s`.\n%s" % (
125 125 destination_address, e,
126 126 )
127 127 _handle_failure(estimation, response)
128 128 return _respond(response)
129 129  
130 130 if destination is None:
131   - response += u"Failed to geolocalize destination `%s`." % (
  131 + response += u"Failed to geocode destination `%s`." % (
132 132 destination_address,
133 133 )
134 134 _handle_failure(estimation, response)
... ... @@ -177,11 +177,11 @@ def compute(): # process the queue of estimation requests
177 177 # In this scenario, we compute the sum of each of the travels' footprint,
178 178 # for each of the Emission Models, and present a mean of all Models.
179 179 #
180   -
181 180 if 1 == len(origins):
182 181  
183 182 footprints = {}
184 183  
  184 + cities_sum = {}
185 185 for model in emission_models:
186 186 cities = {}
187 187 for destination in destinations:
... ... @@ -189,23 +189,41 @@ def compute(): # process the queue of estimation requests
189 189 origin.latitude, origin.longitude,
190 190 destination.latitude, destination.longitude,
191 191 )
192   - cities[destination.address] = footprint
  192 + cities[destination.address] += footprint
  193 + if destination.address not in cities:
  194 + cities[destination.address] = 0.0
  195 + if destination.address not in cities_sum:
  196 + cities_sum[destination.address] = 0.0
  197 + cities_sum[destination.address] += footprint
  198 +
193 199 footprints[model.config.name] = {
194 200 'cities': cities,
195 201 }
196 202  
197   - response += repr(footprints)
198   -
199 203 results['footprints'] = footprints
200 204  
  205 + cities_mean = {}
  206 + for city in cities_sum.keys():
  207 + cities_mean[city] = 1.0 * cities_sum[city] / len(emission_models)
  208 +
  209 + results['mean_footprint'] = {
  210 + 'cities': cities_mean
  211 + }
  212 +
201 213 # SCENARIO B : At Least One Origin, One Destination #######################
202 214 #
203 215 # Same as A for now.
204 216 #
  217 + elif 1 == len(destinations):
  218 + pass
205 219  
206 220 # SCENARIO C : At Least One Origin, At Least One Destination ##############
207 221 #
208 222 # Run Scenario A for each Destination, and expose optimum Destination.
209 223 #
  224 + else:
  225 + pass
  226 +
  227 + response += repr(results) + "\n"
210 228  
211 229 return _respond(response)
... ...