Ejemplo n.º 1
0
def compute_dataset(formula,
                    datasets,
                    title,
                    unit_id,
                    time_resolution,
                    indicator_id,
                    class_id,
                    creator_path,
                    metric_id=None):
    """
    Compute and store a dataset. Returns the id of the newly created dataset.
    """

    result = compute_formula(formula, datasets)
    data = DatasetData(data_frame=result,
                       unit=unit_id,
                       resolution=time_resolution)

    dataset = Dataset(
        title=title,
        description="Computed formula '%s' with %s" % (formula, ", ".join([
            "'%s' as %s" % (title, variable)
            for variable, dataset in datasets.items()
        ])),
        keywords=", ".join(
            set(
                itertools.chain(
                    [dataset.keywords for dataset in datasets.values()]))),
        version=0,
        # ressource related info
        resource_url="not available",
        resource_issued=datetime.datetime.now(),
        # metrics identifier
        is_applied=True,
        metric_id=metric_id,
        # contained data
        time_resolution=time_resolution,
        time_start=data.get_time_start(),
        time_end=data.get_time_end(),
        data=data,
        # references to other services
        # TODO add useful values here
        language_id=0,
        creator_path=creator_path,
        unit_id=unit_id,
        indicator_id=indicator_id,
        class_id=class_id)

    return dataset_api.store(dataset)