from o11ydiagnosis.subtasks.metricsconverter import metric from datetime import datetime, timezone import sqlite3 increment_data_path_template = "metrics/list/hourly/{}.sqlite" class Config: def __init__(self, task, subtask_info): self.task = task self.subtask_info = subtask_info self.metrics_rules = [] for m in metric.metrics_list: self.metrics_rules.append(metric.Rule(m["name"], m["expr"])) def is_timing(self, start_ts): return start_ts % self.subtask_info["interval"] == 0 def ready(self, start_ts): end_ts = start_ts start_ts = end_ts - self.subtask_info["interval"] data_file_name = increment_data_path_template.format(toHour(start_ts)) self.task.pull([data_file_name]) if self.task.exist_cache(data_file_name): return True else: self.task.create_cache(data_file_name) full_path = self.task.get_cache_full_path(data_file_name) conn = sqlite3.connect(full_path) for m in self.metrics_rules: c = conn.cursor() create_sql = metric.create_table_sql_template.format(m.name) c.execute(create_sql) conn.commit() conn.close() return True def start(self, start_ts): end_ts = start_ts start_ts = end_ts - self.subtask_info["interval"] step = 15 data_file_name = increment_data_path_template.format(toHour(start_ts)) metrics_data = metric.getMetrics(self.task.o11y_api, self.task.provider, self.task.region, self.task.o11y_cluster_id, self.metrics_rules, start_ts, end_ts, step) full_path = self.task.get_cache_full_path(data_file_name) metric.write_metrics(metrics_data, full_path, start_ts, step) return True def close(self, start_ts): end_ts = start_ts start_ts = end_ts - self.subtask_info["interval"] data_file_name = increment_data_path_template.format(toHour(start_ts)) self.task.push([data_file_name]) return True def toHour(ts): timedate = datetime.utcfromtimestamp(ts).replace(tzinfo=timezone.utc) timestr = timedate.strftime('%Y%m%d%H') return timestr