Changeset View
Changeset View
Standalone View
Standalone View
swh/counters/history.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import json | import json | ||||
import logging | import logging | ||||
import os | |||||
import time | import time | ||||
from typing import Dict, List, Optional | from typing import Dict, List, Optional | ||||
import requests | import requests | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | ): | ||||
static_data = {} | static_data = {} | ||||
# for live content, we retrieve existing data and merges with the new one | # for live content, we retrieve existing data and merges with the new one | ||||
live_data = {} | live_data = {} | ||||
for object in objects: | for object in objects: | ||||
prometheus_data = self._get_timestamp_history(object=object) | prometheus_data = self._get_timestamp_history(object=object) | ||||
live_data[object] = static_data.get(object, []) + prometheus_data | live_data[object] = static_data.get(object, []) + prometheus_data | ||||
with open(f"{self.cache_base_directory}/{cache_file}", "w") as f: | target_file = f"{self.cache_base_directory}/{cache_file}" | ||||
tmp_file = f"{target_file}.tmp" | |||||
with open(tmp_file, "w") as f: | |||||
f.write(json.dumps(live_data)) | f.write(json.dumps(live_data)) | ||||
os.rename(tmp_file, target_file) |