Beispiel #1
0
def import_json_report(database: Database, imported_report):
    """ Store the report given as json in the database. """
    report_to_store = dict(title=imported_report.get("title",
                                                     "Example report"),
                           report_uuid=imported_report["report_uuid"],
                           subjects={})
    for imported_subject in imported_report.get("subjects", []):
        subject_to_store = default_subject_attributes(database,
                                                      imported_subject["type"])
        subject_to_store["metrics"] = {}  # Remove default metrics
        subject_to_store["name"] = imported_subject["name"]
        report_to_store["subjects"][uuid()] = subject_to_store
        for imported_metric in imported_subject.get("metrics", []):
            metric_to_store = default_metric_attributes(
                database, imported_metric["type"])
            metric_to_store.update(imported_metric)
            metric_to_store["sources"] = {
            }  # Sources in the example report json are lists, we transform them to dicts
            subject_to_store["metrics"][uuid()] = metric_to_store
            for imported_source in imported_metric.get("sources", []):
                source_to_store = metric_to_store["sources"][
                    uuid()] = imported_source
                source_parameters = default_source_parameters(
                    database, imported_metric["type"], imported_source["type"])
                for key, value in source_parameters.items():
                    if key not in source_to_store["parameters"]:
                        source_to_store["parameters"][key] = value
    return insert_new_report(database, "{{user}} imported a new report",
                             (report_to_store, report_to_store["report_uuid"]))
Beispiel #2
0
def post_source_attribute(source_uuid: SourceId, source_attribute: str,
                          database: Database):
    """Set a source attribute."""
    data_model = latest_datamodel(database)
    reports = latest_reports(database)
    data = SourceData(data_model, reports, source_uuid)
    value = dict(bottle.request.json)[source_attribute]
    old_value: Any
    if source_attribute == "position":
        old_value, value = move_item(data, value, "source")
    else:
        old_value = data.source.get(source_attribute) or ""
        data.source[source_attribute] = value
    if old_value == value:
        return dict(ok=True)  # Nothing to do
    delta_description = (
        f"{{user}} changed the {source_attribute} of source '{data.source_name}' of metric '{data.metric_name}' of "
        f"subject '{data.subject_name}' in report '{data.report_name}' from '{old_value}' to '{value}'."
    )
    uuids = [
        data.report_uuid, data.subject_uuid, data.metric_uuid, source_uuid
    ]
    if source_attribute == "type":
        data.source["parameters"] = default_source_parameters(
            database, data.metric["type"], value)
    return insert_new_report(database, delta_description, (data.report, uuids))
Beispiel #3
0
 def test_default_source_parameters(self):
     """Test that the default source parameters can be retrieved from the data model."""
     self.database.datamodels.find_one.return_value = dict(
         _id=123,
         sources=dict(
             source_type=dict(parameters=dict(
                 other_parameter=dict(metrics=[]),
                 parameter=dict(default_value="name", metrics=["metric_type"])))))
     self.assertEqual(dict(parameter="name"), default_source_parameters(self.database, "metric_type", "source_type"))
Beispiel #4
0
def post_source_new(metric_uuid: MetricId, database: Database):
    """Add a new source."""
    data_model = latest_datamodel(database)
    reports = latest_reports(database)
    data = MetricData(data_model, reports, metric_uuid)
    metric_type = data.metric["type"]
    source_type = data_model["metrics"][metric_type]["default_source"]
    parameters = default_source_parameters(database, metric_type, source_type)
    data.metric["sources"][(source_uuid := uuid())] = dict(type=source_type, parameters=parameters)
    delta_description = (
        f"{{user}} added a new source to metric '{data.metric_name}' of subject "
        f"'{data.subject_name}' in report '{data.report_name}'."
    )
    uuids = [data.report_uuid, data.subject_uuid, metric_uuid, source_uuid]
    result = insert_new_report(database, delta_description, (data.report, uuids))
    result["new_source_uuid"] = source_uuid
    return result