aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/register/css.py
blob: 65a2c43dab400b9bfee79a9d740f1663bde9603b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import itertools
from typing import NamedTuple, List

from ctrack.register.models import EventType, EventBase


class TagAttributes(NamedTuple):
    inline_style: str
    id_str: str


class Swimlane:
    def __init__(self, org_name: str, events: List[EventBase]):
        self.events = events
        self.attrs_added = []
        self.attrs_ordered = []
        self.org_name = org_name
        self._process_args()

    def _sort_func(self, type_name):
        for e in self.attrs_ordered:
            if list(type_name.keys())[0] == e[1]:
                return e[0]

    def tag_attrs(self, event) -> TagAttributes:
        if event.type_descriptor == EventType.CAF_INITIAL_CAF_RECEIVED.name:
            try:
                self.attrs_added.pop(
                    self.attrs_added.index(EventType.CAF_INITIAL_CAF_RECEIVED.name)
                )
            except ValueError:
                pass
            return TagAttributes(
                inline_style=' style="background-color: green; color: white;"',
                id_str="caf-initial-received-event",
            )
        if event.type_descriptor == EventType.CAF_INITIAL_REVIEW_COMPLETE.name:
            try:
                self.attrs_added.pop(
                    self.attrs_added.index(EventType.CAF_INITIAL_REVIEW_COMPLETE.name)
                )
            except ValueError:
                pass
            return TagAttributes(
                inline_style=' style="background-color: green; color: white;"',
                id_str="caf-initial-review-complete-event",
            )

    def table_row_builder(self):
        org = self.org_name
        if len(self.events) == 0:
            tmpl = "<td{0}>{1}</td>"
            processing_these_attrs = [x.type_descriptor for x in self.__dict__.values() if isinstance(x, EventBase)]
            empties = [{e: tmpl.format("", e)} for e in self.attrs_added if
                       e[:3] == "CAF" and e not in processing_these_attrs]
            empties = sorted(empties, key=self._sort_func)
            empties = [list(x.values())[0] for x in empties]
            empties = "\n".join(empties)
            return "".join(
                ["<tr>\n", f"<td>{org}</td>\n", empties, "\n", "</tr>"]
            )
        tmpl = "<td{0}>{1}</td>"
        processing_these_attrs = [x.type_descriptor for x in self.__dict__.values() if isinstance(x, EventBase)]
        empties = [{e: tmpl.format("", e)} for e in self.attrs_added if
                   e[:3] == "CAF" and e not in processing_these_attrs]
        _tds = [
            {
                e.type_descriptor: tmpl.format(
                    self.tag_attrs(e).inline_style, e.type_descriptor
                )
            }
            for e in self.events
        ]
        _tds = list(itertools.chain(_tds, empties))
        _tds = sorted(_tds, key=self._sort_func)
        _tds = [list(x.values())[0] for x in _tds]
        tds = "\n".join(_tds)
        return "".join(
            ["<tr>\n", f"<td>{org}</td>\n", tds, "\n", "</tr>"]
        )

    def _process_args(self):
        for v in EventType:
            setattr(self, v.name, None)
            self.attrs_added.append(v.name)
            self.attrs_ordered = list(enumerate(self.attrs_added))
        for e in self.events:
            setattr(self, str(e.type_descriptor), e)

    @property
    def tr(self):
        return self.table_row_builder()