def post_move_metric(metric_uuid: MetricId, target_subject_uuid: SubjectId, database: Database): """Move the metric to another subject.""" data_model, reports = latest_datamodel(database), latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, target_subject_uuid) delta_description = ( f"{{user}} moved the metric '{source.metric_name}' from subject '{source.subject_name}' in report " f"'{source.report_name}' to subject '{target.subject_name}' in report '{target.report_name}'." ) target.subject["metrics"][metric_uuid] = source.metric if target.report_uuid == source.report_uuid: # Metric is moved within the same report del target.report["subjects"][ source.subject_uuid]["metrics"][metric_uuid] target_uuids = [ target.report_uuid, source.subject_uuid, target_subject_uuid, metric_uuid ] reports_to_insert = [(target.report, target_uuids)] else: # Metric is moved from one report to another, update both del source.subject["metrics"][metric_uuid] source_uuids = [source.report_uuid, source.subject_uuid, metric_uuid] target_uuids = [target.report_uuid, target_subject_uuid, metric_uuid] reports_to_insert = [(target.report, target_uuids), (source.report, source_uuids)] return insert_new_report(database, delta_description, *reports_to_insert)
def post_move_source(source_uuid: SourceId, target_metric_uuid: MetricId, database: Database): """Move the source to another metric.""" data_model = latest_datamodel(database) reports = latest_reports(database) source = SourceData(data_model, reports, source_uuid) target = MetricData(data_model, reports, target_metric_uuid) delta_description = ( f"{{user}} moved the source '{source.source_name}' from metric '{source.metric_name}' of subject " f"'{source.subject_name}' in report '{source.report_name}' to metric '{target.metric_name}' of subject " f"'{target.subject_name}' in report '{target.report_name}'." ) target.metric["sources"][source_uuid] = source.source target_uuids: List[Union[Optional[ReportId], Optional[SubjectId], Optional[MetricId], Optional[SourceId]]] = [ target.report_uuid ] reports_to_insert = [(target.report, target_uuids)] if target.report_uuid == source.report_uuid: # Source is moved within the same report del target.report["subjects"][source.subject_uuid]["metrics"][source.metric_uuid]["sources"][source_uuid] if target.subject_uuid != source.subject_uuid: # Source is moved from one subject to another subject, include both subject uuids in the delta target_uuids.append(source.subject_uuid) target_uuids.extend([target.subject_uuid, source.metric_uuid]) else: # Source is moved from one report to another, update both del source.metric["sources"][source_uuid] source_uuids = [source.report_uuid, source.subject_uuid, source.metric_uuid, source_uuid] reports_to_insert.append((source.report, source_uuids)) target_uuids.append(target.subject_uuid) target_uuids.extend([target_metric_uuid, source_uuid]) return insert_new_report(database, delta_description, *reports_to_insert)
def post_move_metric(metric_uuid: MetricId, target_subject_uuid: SubjectId, database: Database): """Move the metric to another subject.""" data_model = latest_datamodel(database) reports = latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, target_subject_uuid) user = sessions.user(database) delta_description = f"{user['user']} moved the metric '{source.metric_name}' from subject " \ f"'{source.subject_name}' in report '{source.report_name}' to subject " \ f"'{target.subject_name}' in report '{target.report_name}'." target.subject["metrics"][metric_uuid] = source.metric reports_to_insert = [target.report] if target.report_uuid == source.report_uuid: # Metric is moved within the same report del target.report["subjects"][ source.subject_uuid]["metrics"][metric_uuid] target_uuids = [ target.report_uuid, source.subject_uuid, target_subject_uuid, metric_uuid ] else: # Metric is moved from one report to another, update both reports_to_insert.append(source.report) del source.subject["metrics"][metric_uuid] source.report["delta"] = dict( uuids=[source.report_uuid, source.subject_uuid, metric_uuid], email=user["email"], description=delta_description) target_uuids = [target.report_uuid, target_subject_uuid, metric_uuid] target.report["delta"] = dict(uuids=target_uuids, email=user["email"], description=delta_description) return insert_new_report(database, *reports_to_insert)
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)
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)
def post_source_copy(source_uuid: SourceId, metric_uuid: MetricId, database: Database): """Add a copy of the source to the metric (new in v3).""" data_model = latest_datamodel(database) reports = latest_reports(database) source = SourceData(data_model, reports, source_uuid) target = MetricData(data_model, reports, metric_uuid) target.metric["sources"][(source_copy_uuid := uuid())] = copy_source(source.source, source.datamodel) user = sessions.user(database) target.report["delta"] = dict( uuids=[ target.report_uuid, target.subject_uuid, target.metric_uuid, source_copy_uuid ], email=user["email"], description= f"{user['user']} copied the source '{source.source_name}' of metric " f"'{source.metric_name}' of subject '{source.subject_name}' from report '{source.report_name}' to " f"metric '{target.metric_name}' of subject '{target.subject_name}' in report " f"'{target.report_name}'.") result = insert_new_report(database, target.report) result["new_source_uuid"] = source_copy_uuid return result
def delete_metric(metric_uuid: MetricId, database: Database): """Delete a metric.""" data = MetricData(latest_datamodel(database), latest_reports(database), metric_uuid) description = ( f"{{user}} deleted metric '{data.metric_name}' from subject '{data.subject_name}' in report " f"'{data.report_name}'." ) uuids = [data.report_uuid, data.subject_uuid, metric_uuid] del data.subject["metrics"][metric_uuid] return insert_new_report(database, description, (data.report, uuids))
def post_metric_copy(metric_uuid: MetricId, subject_uuid: SubjectId, database: Database): """Add a copy of the metric to the subject (new in v3).""" data_model, reports = latest_datamodel(database), latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, subject_uuid) target.subject["metrics"][(metric_copy_uuid := uuid())] = copy_metric(source.metric, source.datamodel) description = ( f"{{user}} copied the metric '{source.metric_name}' of subject '{source.subject_name}' from report " f"'{source.report_name}' to subject '{target.subject_name}' in report '{target.report_name}'." ) uuids = [target.report_uuid, target.subject_uuid, metric_copy_uuid] result = insert_new_report(database, description, (target.report, uuids)) result["new_metric_uuid"] = metric_copy_uuid return result
def delete_metric(metric_uuid: MetricId, database: Database): """Delete a metric.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = MetricData(data_model, reports, metric_uuid) user = sessions.user(database) data.report["delta"] = dict( uuids=[data.report_uuid, data.subject_uuid, metric_uuid], email=user["email"], description= f"{user['user']} deleted metric '{data.metric_name}' from subject " f"'{data.subject_name}' in report '{data.report_name}'.") del data.subject["metrics"][metric_uuid] return insert_new_report(database, data.report)
def post_source_copy(source_uuid: SourceId, metric_uuid: MetricId, database: Database): """Add a copy of the source to the metric (new in v3).""" data_model = latest_datamodel(database) reports = latest_reports(database) source = SourceData(data_model, reports, source_uuid) target = MetricData(data_model, reports, metric_uuid) target.metric["sources"][(source_copy_uuid := uuid())] = copy_source(source.source, source.datamodel) delta_description = ( f"{{user}} copied the source '{source.source_name}' of metric '{source.metric_name}' of subject " f"'{source.subject_name}' from report '{source.report_name}' to metric '{target.metric_name}' of subject " f"'{target.subject_name}' in report '{target.report_name}'." ) uuids = [target.report_uuid, target.subject_uuid, target.metric_uuid, source_copy_uuid] result = insert_new_report(database, delta_description, (target.report, uuids)) result["new_source_uuid"] = source_copy_uuid return result
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
def post_metric_copy(metric_uuid: MetricId, subject_uuid: SubjectId, database: Database): """Add a copy of the metric to the subject (new in v3).""" data_model = latest_datamodel(database) reports = latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, subject_uuid) target.subject["metrics"][(metric_copy_uuid := uuid())] = copy_metric(source.metric, source.datamodel) user = sessions.user(database) target.report["delta"] = dict( uuids=[target.report_uuid, target.subject_uuid, metric_copy_uuid], email=user["email"], description= f"{user['user']} copied the metric '{source.metric_name}' of subject " f"'{source.subject_name}' from report '{source.report_name}' to subject '{target.subject_name}' " f"in report '{target.report_name}'.") result = insert_new_report(database, target.report) result["new_metric_uuid"] = metric_copy_uuid return result
def post_move_source(source_uuid: SourceId, target_metric_uuid: MetricId, database: Database): """Move the source to another metric.""" data_model = latest_datamodel(database) reports = latest_reports(database) source = SourceData(data_model, reports, source_uuid) target = MetricData(data_model, reports, target_metric_uuid) user = sessions.user(database) delta_description = f"{user['user']} moved the source '{source.source_name}' from metric " \ f"'{source.metric_name}' of subject '{source.subject_name}' in report '{source.report_name}' " \ f"to metric '{target.metric_name}' of subject '{target.subject_name}' in report " \ f"'{target.report_name}'." target.metric["sources"][source_uuid] = source.source target_uuids: List[Union[Optional[ReportId], Optional[SubjectId], Optional[MetricId], Optional[SourceId]]] = \ [target.report_uuid] reports_to_insert = [target.report] if target.report_uuid == source.report_uuid: # Source is moved within the same report del target.report["subjects"][source.subject_uuid]["metrics"][ source.metric_uuid]["sources"][source_uuid] if target.subject_uuid != source.subject_uuid: # Source is moved from one subject to another subject, include both subject uuids in the delta target_uuids.append(source.subject_uuid) target_uuids.extend([target.subject_uuid, source.metric_uuid]) else: # Source is moved from one report to another, update both reports_to_insert.append(source.report) del source.metric["sources"][source_uuid] source.report["delta"] = dict(uuids=[ source.report_uuid, source.subject_uuid, source.metric_uuid, source_uuid ], email=user["email"], description=delta_description) target_uuids.append(target.subject_uuid) target.report["delta"] = dict(uuids=target_uuids + [target_metric_uuid, source_uuid], email=user["email"], description=delta_description) return insert_new_report(database, *reports_to_insert)