def handle(self, *args, **options):
        if len(args)==1 and "-h"==args[0]:
            print "Usage: test_locations"
            sys.exit(0)



        tstart = time.time()
        run = DimRun.objects.get(id = 1509)
        number = len(run.dimexecution_set.all())
        print "%s executions to go" % number
        adapter = EMOD_Adapter("admin")
        i = 0
        for execution in run.dimexecution_set.all():
            adapter.fetch_data(execution.id, channel_name="Daily EIR")
            i += 1
            print "%s / %s (%s)" % (i, number, time.time() - tstart)

        tend = time.time()
        print "timing: fetch_data from run 1509 took %f sec" % (tend-tstart)
Пример #2
0
def viewtastic_fetch_data(request):
    """
    For a given execution dictionary, the fetch_data function returns a chart json for insertion into
    a highcharts object.
    This function also returns the channel id for asynchronous data flow and identification reasons.

    :param request: needed for providing a validated username and providing the execution dictionary via the
    packaged GET data.
    :type request: ajax request object
    :return: a dictionary containing the channel id and the associated chart for the selected channel
    """
    temp_chart_json = {}
    temp_chart_json.clear()
    adapter = EMOD_Adapter(user_name=request.user.username)  # get_appropriate_model_adapter(request)

    dict_list = []
    for key in request.POST:
        dict_list.append(json.loads(key))

    # if there weren't any sweeps done, then the execution id needs to be passed to the function.
    the_params = None
    try:
        the_params = dict_list[0]['parameters']
        # Turn the dict of lists into a list of dicts, ordered by the mod order from when the fetch_keys
        # was called
        the_param_list = []
        for sweep_object in the_params:
            the_params[sweep_object]['name'] = sweep_object
            the_param_list.append(the_params[sweep_object])

        final_param_list = []
        for param in sorted(the_param_list, key=lambda sweep: sweep['mod_order']):
            final_param_list.append({param['name']: param['values']})

    except:
        the_execid = dict_list[0]['execution']

    try:
        chart_type = dict_list[0]['chart_type']
    except:
        chart_type = 'time_series'

    try:
        the_aggregation = dict_list[0]['aggregation']
    except:
        the_aggregation = 'daily'

    try:
        the_run = int(dict_list[0]['run_id'])
        the_channel = int(dict_list[0]['channel_id'])
    except:
        # Requesting Simulation data
        sim_id = int(dict_list[0]['run_id'][1:])  # Remove first 's' character
        the_channel = dict_list[0]['channel_id']
        start_date = datetime.datetime(1995, 1, 1)
        insetchartfile = SimulationOutputFile.objects.get(simulation=sim_id, name="InsetChart.json")
        data = json.loads(insetchartfile.get_contents())["Channels"][the_channel]["Data"]
        data = as_chart(data, start_date)

    else:
        # if the parameters aren't available then try getting the data via the execution id
        if the_params is not None:
            if the_aggregation == 'yearly_sum':
                data = adapter.fetch_data(group_by=True,
                                          exec_dict=final_param_list,
                                          run_id=the_run,
                                          channel_id=the_channel,
                                          as_chart=True, as_highstock=True)
            else:
                data = adapter.fetch_data(exec_dict=final_param_list,
                                          run_id=the_run,
                                          channel_id=the_channel,
                                          as_chart=True, as_highstock=True)
        else:
            if the_aggregation == 'yearly_sum':
                data = adapter.fetch_data(group_by=True,
                                          execution_id=the_execid,
                                          run_id=the_run,
                                          channel_id=the_channel,
                                          as_chart=True, as_highstock=True)
            else:
                data = adapter.fetch_data(execution_id=the_execid,
                                          run_id=the_run,
                                          channel_id=the_channel,
                                          as_chart=True, as_highstock=True)

    temp_chart_json = json.loads(data)

    temp_chart_json['chart']['height'] = 335 + (15 * len(temp_chart_json['series']))
    temp_chart_json['chart']['width'] = 500

    temp_chart_json['rangeSelector'] = {'enabled': False}
    real_return_json = dict(channel_id=the_channel,
                            chart_JSON=temp_chart_json,
                            chart_type=chart_type,
                            aggregation=the_aggregation)

    return HttpResponse(content=json.dumps(real_return_json), content_type="application/json")