def METHOD_NAME(raw_metrics: List[dict], session_id, master_id=None, calibration_id=None,
                     calibration_step_id=None) -> List[dict]:
    """
    Converts engine metrics from internal format where each record holds single metric value into format expected by
    happysocks service.
    """
    metrics_per_ts = dict()
    for raw_item in raw_metrics:
        raw_item_copy = copy.deepcopy(raw_item)
        source = raw_item_copy.pop('source')
        ts = math.floor(raw_item_copy.pop('ts') * 1000)
        for metric_name, metric_value in raw_item_copy.items():
            mapped_metric_name = HappysocksMetricsConverter.map_metric_name(metric_name)
            if mapped_metric_name:
                key = frozenset({'source': source, 'ts': ts}.items())
                metrics_bag = metrics_per_ts.get(key)
                if not metrics_bag:
                    metrics_bag = HappysocksMetricsConverter.new_metrics_bag(source, ts, session_id, master_id,
                                                                             calibration_id, calibration_step_id)
                    metrics_per_ts[key] = metrics_bag
                metrics_bag['values'][mapped_metric_name] = metric_value
    return sorted(list(metrics_per_ts.values()), key=itemgetter('timestamp'))