29 lines
871 B
Python
29 lines
871 B
Python
from __future__ import annotations
|
|
from collections import Counter
|
|
from dataclasses import dataclass
|
|
from typing import List
|
|
from .models import WorkingWithRelation
|
|
|
|
|
|
@dataclass
|
|
class DepartmentLink:
|
|
source_department: str
|
|
destination_department: str
|
|
weight: int
|
|
|
|
|
|
def aggregate_department_links(relations: List[WorkingWithRelation]) -> List[DepartmentLink]:
|
|
counter: Counter[tuple[str, str]] = Counter()
|
|
for rel in relations:
|
|
src = (rel.source.department or "").strip()
|
|
dst = (rel.destination.department or "").strip()
|
|
if not src or not dst:
|
|
continue
|
|
counter[(src, dst)] += 1
|
|
|
|
links: List[DepartmentLink] = [
|
|
DepartmentLink(source_department=s, destination_department=d, weight=w)
|
|
for (s, d), w in counter.items()
|
|
]
|
|
return sorted(links, key=lambda l: l.weight, reverse=True)
|