From 7c05bbacb63940cec7e2864602e3d36827b60ef4 Mon Sep 17 00:00:00 2001 From: Kirill Zhukov Date: Thu, 1 Jun 2023 11:57:27 +0200 Subject: [PATCH] Release 0.3.5 (2023-06-01) build_analytics: ALBS-1103 start using persistent HTTP connections --- build_analytics/build_analytics/api_client.py | 35 +++++++++++++++++-- .../build_analytics/extractor/start.py | 3 ++ releases.txt | 6 +++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/build_analytics/build_analytics/api_client.py b/build_analytics/build_analytics/api_client.py index 512e5f9..bd7a5ce 100644 --- a/build_analytics/build_analytics/api_client.py +++ b/build_analytics/build_analytics/api_client.py @@ -27,6 +27,8 @@ class APIclient(): self.api_root = api_root self.jwt = jwt self.timeout = timeout + # will be set at first call of __send_request + self.session: Optional[requests.Session] = None def get_builds(self, page_num: int = 1) -> List[Build]: ep = '/api/v1/builds' @@ -34,8 +36,7 @@ class APIclient(): params = {'pageNumber': page_num} headers = {'accept': 'appilication/json'} - response = requests.get( - url, params=params, headers=headers, timeout=self.timeout) + response = self.__send_request(url, 'get', params, headers) response.raise_for_status() result = [] @@ -54,7 +55,7 @@ class APIclient(): ep = f'/api/v1/builds/{build_id}' url = urljoin(self.api_root, ep) headers = {'accept': 'application/json'} - response = requests.get(url, headers=headers, timeout=self.timeout) + response = self.__send_request(url, 'get', headers=headers) if response.status_code == 404: return None @@ -228,3 +229,31 @@ class APIclient(): start_ts = stat.start_ts return start_ts + + def __send_request(self, + url: str, + method: str, + params: Optional[Dict[str, Any]] = None, + headers: Optional[Dict[str, Any]] = None, + ) -> requests.Response: + """ + Simple wrapper around requests.get/posts.. methods + so we can use same session between API calls + """ + if not self.session: + self.session = requests.Session() + + m = getattr(self.session, method, None) + if not m: + raise ValueError(f"method {method} is not supported") + + # pylint: disable=not-callable + return m(url, params=params, headers=headers, timeout=self.timeout) + + def close_session(self): + if self.session: + self.session.close() + self.session = None + + def __del__(self): + self.close_session() diff --git a/build_analytics/build_analytics/extractor/start.py b/build_analytics/build_analytics/extractor/start.py index c32aaca..7128632 100644 --- a/build_analytics/build_analytics/extractor/start.py +++ b/build_analytics/build_analytics/extractor/start.py @@ -99,7 +99,10 @@ def start(yml_path: str): else: logging.info('test tasks were updated') + # freeing up resources extractor.db.close_conn() + extractor.api.close_session() + logging.info("Extraction was finished") logging.info("Sleeping for %d seconds", config.scrape_interval) time.sleep(config.scrape_interval) diff --git a/releases.txt b/releases.txt index 59531e8..fb16bbd 100644 --- a/releases.txt +++ b/releases.txt @@ -33,4 +33,8 @@ build-analytics 0.3.4 (2023-05-12) build_analytics - - Bigfix ALBS-1111 \ No newline at end of file + - Bigfix ALBS-1111 + +0.3.5 (2023-06-01) +build_analytics: + ALBS-1103 start using persistent HTTP connections \ No newline at end of file