def calc_results(request, calc_id): """ Get a summarized list of calculation results for a given ``calc_id``. Result is a JSON array of objects containing the following attributes: * id * name * type (hazard_curve, hazard_map, etc.) * url (the exact url where the full result can be accessed) """ user = utils.get_user_data(request) # If the specified calculation doesn't exist OR is not yet complete, # throw back a 404. try: oqjob = oqe_models.OqJob.objects.get(id=calc_id) if not user['is_super'] and oqjob.user_name != user['name']: return HttpResponseNotFound() if not oqjob.status == 'complete': return HttpResponseNotFound() except ObjectDoesNotExist: return HttpResponseNotFound() base_url = _get_base_url(request) # NB: export_output has as keys the list (output_type, extension) # so this returns an ordered map output_type -> extensions such as # OrderedDict([('agg_loss_curve', ['xml', 'csv']), ...]) output_types = groupby(export_output, lambda oe: oe[0], lambda oes: [e for o, e in oes]) results = oq_engine.get_outputs(calc_id) if not results: return HttpResponseNotFound() response_data = [] for result in results: try: # output from the old calculators rtype = result.output_type outtypes = output_types[rtype] except KeyError: try: # output from the datastore rtype = result.ds_key outtypes = output_types[rtype] except KeyError: continue # non-exportable outputs should not be shown url = urlparse.urljoin(base_url, 'v1/calc/result/%d' % result.id) datum = dict( id=result.id, name=result.display_name, type=rtype, outtypes=outtypes, url=url) response_data.append(datum) return HttpResponse(content=json.dumps(response_data))
def calc_results(request, calc_id): """ Get a summarized list of calculation results for a given ``calc_id``. Result is a JSON array of objects containing the following attributes: * id * name * type (hazard_curve, hazard_map, etc.) * url (the exact url where the full result can be accessed) """ user_name = utils.getusername(request) # If the specified calculation doesn't exist OR is not yet complete, # throw back a 404. try: oqjob = oqe_models.OqJob.objects.get(id=calc_id, user_name=user_name) if not oqjob.status == 'complete': return HttpResponseNotFound() except ObjectDoesNotExist: return HttpResponseNotFound() base_url = _get_base_url(request) # NB: export_output has as keys the list (output_type, extension) # so this returns an ordered map output_type -> extensions such as # OrderedDict([('agg_loss_curve', ['xml', 'csv']), ...]) output_types = groupby(export_output, lambda oe: oe[0], lambda oes: [e for o, e in oes]) results = oq_engine.get_outputs(calc_id) if not results: return HttpResponseNotFound() response_data = [] for result in results: url = urlparse.urljoin(base_url, 'v1/calc/result/%d' % result.id) datum = dict( id=result.id, name=result.display_name, type=result.output_type, outtypes=output_types[result.output_type], url=url, ) response_data.append(datum) return HttpResponse(content=json.dumps(response_data))
def calc_results(request, job_type, calc_id): """ Get a summarized list of calculation results for a given ``calc_id``. Result is a JSON array of objects containing the following attributes: * id * name * type (hazard_curve, hazard_map, etc.) * url (the exact url where the full result can be accessed) """ # If the specified calculation doesn't exist OR is not yet complete, # throw back a 404. try: if job_type == 'risk': oqjob = oqe_models.RiskCalculation.objects.get(id=calc_id).oqjob else: oqjob = oqe_models.OqJob.objects.get(id=calc_id) if not oqjob.status == 'complete': return HttpResponseNotFound() except ObjectDoesNotExist: return HttpResponseNotFound() base_url = _get_base_url(request) results = oq_engine.get_outputs(job_type, calc_id) if not results: return HttpResponseNotFound() response_data = [] for result in results: url = urlparse.urljoin(base_url, 'v1/calc/%s/result/%d' % (job_type, result.id)) datum = dict( id=result.id, name=result.display_name, type=result.output_type, url=url, ) response_data.append(datum) return HttpResponse(content=json.dumps(response_data))