Coretex
metric_factory.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from typing import Type, Optional, List
19 
20 from .metric import Metric, MetricType
21 from .predefined_metrics import *
22 
23 
24 def getClassForMetric(name: str) -> Optional[Type[Metric]]:
25  if name == "disk_read":
26  return MetricDiskRead
27 
28  if name == "disk_write":
29  return MetricDiskWrite
30 
31  if name == "cpu_usage":
32  return MetricCPUUsage
33 
34  if name == "ram_usage":
35  return MetricRAMUsage
36 
37  if name == "swap_usage":
38  return MetricSwapUsage
39 
40  if name == "gpu_usage":
41  return MetricGPUUsage
42 
43  if name == "gpu_memory_usage":
44  return MetricGPUMemoryUsage
45 
46  if name == "gpu_temperature":
47  return MetricGPUTemperature
48 
49  if name == "upload_speed":
50  return MetricUploadSpeed
51 
52  if name == "download_speed":
53  return MetricDownloadSpeed
54 
55  return None
56 
57 
58 def createMetric(
59  name: str,
60  xLabel: str,
61  xType: MetricType,
62  yLabel: str,
63  yType: MetricType,
64  xRange: Optional[List[float]] = None,
65  yRange: Optional[List[float]] = None
66 ) -> Metric:
67 
68  metric = getClassForMetric(name)
69  if metric is None:
70  raise ValueError(f"[Coretex] Failed to create {name} metric")
71 
72  return metric.create(name, xLabel, xType, yLabel, yType, xRange, yRange)