Commit 3d8865daf23f43aa72d867ad39d7340babe4397a
1 parent
dc6abfd1
Exists in
master
Improve warnings and backoffice usability.
Showing
2 changed files
with
65 additions
and
25 deletions
Show diff stats
flaskr/controllers/main_controller.py
... | ... | @@ -251,9 +251,15 @@ def compute(): # process the queue of estimation requests |
251 | 251 | db.session.commit() |
252 | 252 | |
253 | 253 | def _handle_warning(_estimation, _warning_message): |
254 | - _estimation.warnings = _warning_message | |
254 | + if not _estimation.warnings: | |
255 | + _estimation.warnings = _warning_message | |
256 | + else: | |
257 | + _estimation.warnings += u"\n" + _warning_message | |
258 | + # _estimation.warnings = u"%s\n%s" % \ | |
259 | + # (_estimation.warnings, _warning_message) | |
255 | 260 | db.session.commit() |
256 | 261 | |
262 | + estimation = None | |
257 | 263 | try: |
258 | 264 | response = "" |
259 | 265 | |
... | ... | @@ -296,37 +302,48 @@ def compute(): # process the queue of estimation requests |
296 | 302 | origins = [] |
297 | 303 | |
298 | 304 | if origins_addresses_count > maximum_addresses_to_compute: |
299 | - errmsg = u"Too many origins. (%d > %d) Please contact us for support of that many origins." % (origins_addresses_count, maximum_addresses_to_compute) | |
305 | + errmsg = u"Too many origins. (%d > %d) \n" \ | |
306 | + u"Please contact us " \ | |
307 | + u"for support of more origins." % \ | |
308 | + (origins_addresses_count, maximum_addresses_to_compute) | |
300 | 309 | _handle_failure(estimation, errmsg) |
301 | 310 | return _respond(errmsg) |
302 | 311 | |
303 | 312 | for i in range(origins_addresses_count): |
304 | 313 | |
305 | 314 | origin_address = origins_addresses[i].strip() |
315 | + | |
316 | + if not origin_address: | |
317 | + continue | |
318 | + | |
306 | 319 | if origin_address in failed_addresses: |
307 | 320 | continue |
308 | 321 | |
309 | 322 | try: |
310 | 323 | origin = geocoder.geocode(origin_address.encode('utf-8')) |
311 | 324 | except geopy.exc.GeopyError as e: |
312 | - response += u"Failed to geocode origin `%s`.\n%s\n" % ( | |
313 | - origin_address, e, | |
325 | + warning = u"Ignoring origin `%s` " \ | |
326 | + u"since we failed to geocode it.\n%s\n" % ( | |
327 | + origin_address, e, | |
314 | 328 | ) |
315 | - _handle_warning(estimation, response) | |
329 | + response += warning | |
330 | + _handle_warning(estimation, warning) | |
316 | 331 | failed_addresses.append(origin_address) |
317 | 332 | continue |
318 | 333 | |
319 | 334 | if origin is None: |
320 | - response += u"Failed to geocode origin `%s`.\n" % ( | |
321 | - origin_address, | |
335 | + warning = u"Ignoring origin `%s` " \ | |
336 | + u"since we failed to geocode it.\n" % ( | |
337 | + origin_address, | |
322 | 338 | ) |
323 | - _handle_warning(estimation, response) | |
339 | + response += warning | |
340 | + _handle_warning(estimation, warning) | |
324 | 341 | failed_addresses.append(origin_address) |
325 | 342 | continue |
326 | 343 | |
327 | 344 | origins.append(origin) |
328 | 345 | |
329 | - response += u"Origin: %s == %s (%f, %f)\n" % ( | |
346 | + response += u"Origin `%s` geocoded to `%s` (%f, %f).\n" % ( | |
330 | 347 | origin_address, origin.address, |
331 | 348 | origin.latitude, origin.longitude, |
332 | 349 | ) |
... | ... | @@ -338,31 +355,45 @@ def compute(): # process the queue of estimation requests |
338 | 355 | destinations = [] |
339 | 356 | |
340 | 357 | if destinations_addresses_count > maximum_addresses_to_compute: |
341 | - errmsg = u"Too many destinations. (%d > %d) Please contact us for support of that many destinations." % (destinations_addresses_count, maximum_addresses_to_compute) | |
358 | + errmsg = u"Too many destinations. (%d > %d) \n" \ | |
359 | + u"Please contact us " \ | |
360 | + u"for support of that many destinations." \ | |
361 | + % ( | |
362 | + destinations_addresses_count, | |
363 | + maximum_addresses_to_compute, | |
364 | + ) | |
342 | 365 | _handle_failure(estimation, errmsg) |
343 | 366 | return _respond(errmsg) |
344 | 367 | |
345 | 368 | for i in range(destinations_addresses_count): |
346 | 369 | |
347 | 370 | destination_address = destinations_addresses[i].strip() |
371 | + | |
372 | + if not destination_address: | |
373 | + continue | |
374 | + | |
348 | 375 | if destination_address in failed_addresses: |
349 | 376 | continue |
350 | 377 | |
351 | 378 | try: |
352 | 379 | destination = geocoder.geocode(destination_address.encode('utf-8')) |
353 | 380 | except geopy.exc.GeopyError as e: |
354 | - response += u"Failed to geocode destination `%s`.\n%s\n" % ( | |
355 | - destination_address, e, | |
381 | + warning = u"Ignoring destination `%s` " \ | |
382 | + u"since we failed to geocode it.\n%s\n" % ( | |
383 | + destination_address, e, | |
356 | 384 | ) |
357 | - _handle_warning(estimation, response) | |
385 | + response += warning | |
386 | + _handle_warning(estimation, warning) | |
358 | 387 | failed_addresses.append(destination_address) |
359 | 388 | continue |
360 | 389 | |
361 | 390 | if destination is None: |
362 | - response += u"Failed to geocode destination `%s`.\n" % ( | |
363 | - destination_address, | |
391 | + warning = u"Ignoring destination `%s` " \ | |
392 | + u"since we failed to geocode it.\n" % ( | |
393 | + destination_address, | |
364 | 394 | ) |
365 | - _handle_warning(estimation, response) | |
395 | + response += warning | |
396 | + _handle_warning(estimation, warning) | |
366 | 397 | failed_addresses.append(destination_address) |
367 | 398 | continue |
368 | 399 | |
... | ... | @@ -370,7 +401,7 @@ def compute(): # process the queue of estimation requests |
370 | 401 | |
371 | 402 | destinations.append(destination) |
372 | 403 | |
373 | - response += u"Destination: %s == %s (%f, %f)\n" % ( | |
404 | + response += u"Destination `%s` geocoded to `%s` (%f, %f).\n" % ( | |
374 | 405 | destination_address, destination.address, |
375 | 406 | destination.latitude, destination.longitude, |
376 | 407 | ) |
... | ... | @@ -380,11 +411,11 @@ def compute(): # process the queue of estimation requests |
380 | 411 | # GTFO IF NO ORIGINS OR NO DESTINATIONS ################################### |
381 | 412 | |
382 | 413 | if 0 == len(origins): |
383 | - response += u"Failed to geocode all the origin(s).\n" | |
414 | + response += u"Failed to geocode ALL the origin(s).\n" | |
384 | 415 | _handle_failure(estimation, response) |
385 | 416 | return _respond(response) |
386 | 417 | if 0 == len(destinations): |
387 | - response += u"Failed to geocode all the destination(s).\n" | |
418 | + response += u"Failed to geocode ALL the destination(s).\n" | |
388 | 419 | _handle_failure(estimation, response) |
389 | 420 | return _respond(response) |
390 | 421 | |
... | ... | @@ -576,6 +607,7 @@ def compute(): # process the queue of estimation requests |
576 | 607 | |
577 | 608 | estimation.status = StatusEnum.success |
578 | 609 | # estimation.output_yaml = u"%s" % yaml_dump(results) |
610 | + estimation.informations = response | |
579 | 611 | estimation.set_output_dict(results) |
580 | 612 | db.session.commit() |
581 | 613 | ... | ... |
flaskr/models.py
... | ... | @@ -81,6 +81,18 @@ class Estimation(db.Model): |
81 | 81 | return self.destination_addresses.strip().count("\n") + 1 |
82 | 82 | |
83 | 83 | @property |
84 | + def errors_tail(self): | |
85 | + return self.get_tail(self.errors) | |
86 | + | |
87 | + @property | |
88 | + def warnings_tail(self): | |
89 | + return self.get_tail(self.warnings) | |
90 | + | |
91 | + def get_tail(self, of_string, of_length=140): | |
92 | + if not of_string: | |
93 | + return "" | |
94 | + return u"...%s" % of_string[-(min(of_length, len(of_string))):] | |
95 | + | |
84 | 96 | def has_failed(self): |
85 | 97 | return self.status == StatusEnum.failure |
86 | 98 | |
... | ... | @@ -120,19 +132,15 @@ class Estimation(db.Model): |
120 | 132 | self._output_dict = yaml_load(self.output_yaml) |
121 | 133 | return self._output_dict |
122 | 134 | |
123 | - @property | |
124 | 135 | def is_one_to_one(self): |
125 | 136 | return self.scenario == ScenarioEnum.one_to_one |
126 | 137 | |
127 | - @property | |
128 | 138 | def is_one_to_many(self): |
129 | 139 | return self.scenario == ScenarioEnum.one_to_many |
130 | 140 | |
131 | - @property | |
132 | 141 | def is_many_to_one(self): |
133 | 142 | return self.scenario == ScenarioEnum.many_to_one |
134 | 143 | |
135 | - @property | |
136 | 144 | def is_many_to_many(self): |
137 | 145 | return self.scenario == ScenarioEnum.many_to_many |
138 | 146 | |
... | ... | @@ -157,8 +165,8 @@ class EstimationView(ModelView): |
157 | 165 | 'scenario', |
158 | 166 | 'origins_count', |
159 | 167 | 'destinations_count', |
160 | - 'warnings', | |
161 | - 'errors', | |
168 | + 'warnings_tail', | |
169 | + 'errors_tail', | |
162 | 170 | ) |
163 | 171 | |
164 | 172 | # Enable search functionality - it will search for terms in | ... | ... |