C-Dash: Prepare layout for telemetry in trending
[csit.git] / csit.infra.dash / app / cdash / utils / trigger.py
1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """A module implementing the processing of a trigger.
15 """
16
17 from json import loads, JSONDecodeError
18
19
20 class Trigger:
21     """
22     """
23     def __init__(self, trigger) -> None:
24         """
25         """
26         self._id = trigger[0]["prop_id"].split(".")
27         self._param = self._id[1]
28         try:
29             self._id = loads(self._id[0])
30         except (JSONDecodeError, TypeError):
31             # It is a string
32             self._id = {"type": self._id[0], "index": None}
33         self._val = trigger[0]["value"]
34
35     def __str__(self) -> str:
36         return (
37             f"\nTrigger:\n"
38             f"  ID:        {self._id}\n"
39             f"  Type:      {self._id['type']}\n"
40             f"  Index:     {self._id['index']}\n"
41             f"  Parameter: {self._param}\n"
42             f"  Value:     {self._val}\n"
43         )
44
45     @property
46     def id(self) -> dict:
47         return self._id
48
49     @property
50     def type(self) -> str:
51         return self._id["type"]
52
53     @property
54     def idx(self) -> any:
55         return self._id["index"]
56
57     @property
58     def parameter(self) -> str:
59         return self._param
60
61     @property
62     def value(self) -> any:
63         return self._val