Esempio n. 1
0
def post_metric_new(subject_uuid: SubjectId, database: Database):
    """Add a new metric."""
    data = SubjectData(latest_datamodel(database), latest_reports(database), subject_uuid)
    data.subject["metrics"][(metric_uuid := uuid())] = default_metric_attributes(database)
    description = f"{{user}} added a new metric to subject '{data.subject_name}' in report '{data.report_name}'."
    uuids = [data.report_uuid, data.subject_uuid, metric_uuid]
    result = insert_new_report(database, description, (data.report, uuids))
    result["new_metric_uuid"] = metric_uuid
    return result
Esempio n. 2
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"]))
Esempio n. 3
0
def post_metric_attribute(metric_uuid: MetricId, metric_attribute: str,
                          database: Database):
    """Set the metric attribute."""
    new_value = dict(bottle.request.json)[metric_attribute]
    data = MetricData(latest_datamodel(database), latest_reports(database),
                      metric_uuid)
    if metric_attribute == "comment" and new_value:
        new_value = sanitize_html(new_value)
    old_value: Any
    if metric_attribute == "position":
        old_value, new_value = move_item(data, new_value, "metric")
    else:
        old_value = data.metric.get(metric_attribute) or ""
    if old_value == new_value:
        return dict(ok=True)  # Nothing to do
    data.metric[metric_attribute] = new_value
    if metric_attribute == "type":
        data.metric.update(default_metric_attributes(database, new_value))
    description = (
        f"{{user}} changed the {metric_attribute} of metric '{data.metric_name}' of subject "
        f"'{data.subject_name}' in report '{data.report_name}' from '{old_value}' to '{new_value}'."
    )
    uuids = [data.report_uuid, data.subject_uuid, metric_uuid]
    insert_new_report(database, description, (data.report, uuids))
    if metric_attribute in ATTRIBUTES_IMPACTING_STATUS and (
            latest := latest_measurement(database, metric_uuid)):
        return insert_new_measurement(database, data.datamodel, data.metric,
                                      latest.copy(), latest)
Esempio n. 4
0
def post_metric_attribute(metric_uuid: MetricId, metric_attribute: str,
                          database: Database):
    """Set the metric attribute."""
    value = dict(bottle.request.json)[metric_attribute]
    data_model = latest_datamodel(database)
    reports = latest_reports(database)
    data = MetricData(data_model, reports, metric_uuid)
    if metric_attribute == "comment" and value:
        value = sanitize_html(value)
    old_value: Any
    if metric_attribute == "position":
        old_value, value = move_item(data, value, "metric")
    else:
        old_value = data.metric.get(metric_attribute) or ""
    if old_value == value:
        return dict(ok=True)  # Nothing to do
    data.metric[metric_attribute] = value
    if metric_attribute == "type":
        data.metric.update(default_metric_attributes(database, value))
    user = sessions.user(database)
    data.report["delta"] = dict(
        uuids=[data.report_uuid, data.subject_uuid, metric_uuid],
        email=user["email"],
        description=
        f"{user['user']} changed the {metric_attribute} of metric '{data.metric_name}' "
        f"of subject '{data.subject_name}' in report '{data.report_name}' from '{old_value}' to '{value}'."
    )
    insert_new_report(database, data.report)
    attributes_impacting_status = ("accept_debt", "debt_target",
                                   "debt_end_date", "direction", "near_target",
                                   "target")
    if metric_attribute in attributes_impacting_status and (
            latest := latest_measurement(database, metric_uuid)):
        return insert_new_measurement(database, data.datamodel, data.metric,
                                      latest)
Esempio n. 5
0
def post_metric_new(subject_uuid: SubjectId, database: Database):
    """Add a new metric."""
    data_model = latest_datamodel(database)
    reports = latest_reports(database)
    data = SubjectData(data_model, reports, subject_uuid)
    data.subject["metrics"][(metric_uuid :=
                             uuid())] = default_metric_attributes(database)
    user = sessions.user(database)
    data.report["delta"] = dict(
        uuids=[data.report_uuid, data.subject_uuid, metric_uuid],
        email=user["email"],
        description=
        f"{user['user']} added a new metric to subject '{data.subject_name}' in report "
        f"'{data.report_name}'.")
    result = insert_new_report(database, data.report)
    result["new_metric_uuid"] = metric_uuid
    return result