monitor_metric.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: yq
  4. @time: 2024/11/1
  5. @desc: 监控报告
  6. """
  7. import threading
  8. from typing import Dict
  9. from commom import GeneralException
  10. from entitys import MonitorConfigEntity, MetricFucResultEntity
  11. from enums import ResultCodesEnum
  12. from .report_generate import ReportWord, ReportExcel
  13. class MonitorMetric():
  14. def __init__(self, monitor_config_path: str):
  15. self._monitor_config = MonitorConfigEntity.from_config(monitor_config_path)
  16. self.lock = threading.Lock()
  17. self._metric_value_dict: Dict[str, MetricFucResultEntity] = {}
  18. @property
  19. def metric_value_dict(self):
  20. return self._metric_value_dict
  21. def _update_metric_value_dict(self, key, value):
  22. with self.lock:
  23. self._metric_value_dict[key] = value
  24. # TODO 多线程计算指标
  25. def calculate_metric(self, *args, **kwargs):
  26. metric_dict = self._monitor_config.metric_dict
  27. for metric_code, metric_clazz in metric_dict.items():
  28. metric_value = metric_clazz.calculate(*args, **kwargs)
  29. self._update_metric_value_dict(metric_code, metric_value)
  30. def generate_report(self):
  31. if ".docx" in self._monitor_config.template_path:
  32. ReportWord.generate_report(self._metric_value_dict, self._monitor_config.template_path)
  33. elif ".xlsx" in self._monitor_config.template_path:
  34. ReportExcel.generate_report(self._metric_value_dict, self._monitor_config.template_path)
  35. else:
  36. raise GeneralException(ResultCodesEnum.NOT_FOUND,
  37. message=f"模板【{self._monitor_config.template_path}】不能处理,请使用【.docx】或【.xlsx】模板")
  38. if __name__ == "__main__":
  39. pass