From 1961bc656ec42e3bfcc7d08c8edd44ae4a4bf47d Mon Sep 17 00:00:00 2001 From: Guillaume De Saint Martin Date: Tue, 19 Dec 2023 20:19:35 +0100 Subject: [PATCH] [Community] init metrics migration --- octobot/community/community_analysis.py | 15 +++++++-------- octobot/community/community_manager.py | 4 ++-- octobot/community/identifiers_provider.py | 3 +++ octobot/constants.py | 2 ++ tests/unit_tests/community/test_community_data.py | 2 ++ 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/octobot/community/community_analysis.py b/octobot/community/community_analysis.py index 199ff2c93..ba16d0cbc 100644 --- a/octobot/community/community_analysis.py +++ b/octobot/community/community_analysis.py @@ -20,15 +20,12 @@ import octobot_commons.logging as logging import octobot_commons.constants as commons_constants import octobot.constants as constants +import octobot.community.identifiers_provider as identifiers_provider async def get_current_octobots_stats(): - bot_metrics_url = f"{commons_constants.METRICS_URL}metrics/community/count/" - return await _get_stats({ - "daily": f"{bot_metrics_url}0/0/-1", - "monthly": f"{bot_metrics_url}0/-1/0", - "all": f"{bot_metrics_url}0/0/0" - }) + bot_metrics_url = f"{identifiers_provider.IdentifiersProvider.COMMUNITY_API_URL}/stats" + return await _get_stats({"total_bots": bot_metrics_url}) async def _ensure_closed_sockets(): @@ -50,8 +47,8 @@ async def get_stats(url, stats_key): logger.error(f"Error when getting community status : error code={resp.status}") else: json_resp = await resp.json() - if "total" in json_resp: - bots_stats[stats_key] = json_resp["total"] + if any("total" in key for key in json_resp): + bots_stats[stats_key] = json_resp[stats_key] else: bots_stats[stats_key] = _format_top_elements(json_resp) except Exception as e: @@ -67,6 +64,8 @@ async def get_stats(url, stats_key): async def get_community_metrics(): + # todo migrate with new metrics + return {} bot_count_metrics_url = f"{commons_constants.METRICS_URL}metrics/community/count/" bot_top_metrics_url = f"{commons_constants.METRICS_URL}metrics/community/top/" one_month_time = int(time.time() - 30 * commons_constants.DAYS_TO_SECONDS) diff --git a/octobot/community/community_manager.py b/octobot/community/community_manager.py index c842acdb9..d75f8ad0a 100644 --- a/octobot/community/community_manager.py +++ b/octobot/community/community_manager.py @@ -72,13 +72,13 @@ async def start_community_task(self): # first ensure this session is not just a configuration test: register after a timer await asyncio.sleep(common_constants.TIMER_BEFORE_METRICS_REGISTRATION_SECONDS) self._init_community_config() - await self.register_session() + # await self.register_session() # waiting for metrics migration await self._update_authenticated_bot() while self.keep_running: # send a keepalive at periodic intervals await asyncio.sleep(common_constants.TIMER_BETWEEN_METRICS_UPTIME_UPDATE) try: - await self._update_session() + # await self._update_session() # waiting for metrics migration await self._update_authenticated_bot() except Exception as e: self.logger.debug(f"Exception when handling community data : {e}") diff --git a/octobot/community/identifiers_provider.py b/octobot/community/identifiers_provider.py index 3b406b92c..03c7101ea 100644 --- a/octobot/community/identifiers_provider.py +++ b/octobot/community/identifiers_provider.py @@ -22,6 +22,7 @@ class IdentifiersProvider: ENABLED_ENVIRONMENT: str = None COMMUNITY_LANDING_URL: str = None + COMMUNITY_API_URL: str = None COMMUNITY_URL: str = None FRONTEND_PASSWORD_RECOVER_URL: str = None BACKEND_URL: str = None @@ -31,6 +32,7 @@ class IdentifiersProvider: def use_production(): IdentifiersProvider.COMMUNITY_URL = constants.OCTOBOT_COMMUNITY_URL IdentifiersProvider.COMMUNITY_LANDING_URL = constants.OCTOBOT_COMMUNITY_LANDING_URL + IdentifiersProvider.COMMUNITY_API_URL = constants.OCTOBOT_COMMUNITY_API_URL IdentifiersProvider.FRONTEND_PASSWORD_RECOVER_URL = constants.OCTOBOT_COMMUNITY_RECOVER_PASSWORD_URL IdentifiersProvider.BACKEND_URL = constants.COMMUNITY_BACKEND_URL IdentifiersProvider.BACKEND_KEY = constants.COMMUNITY_BACKEND_KEY @@ -40,6 +42,7 @@ def use_production(): def use_staging(): IdentifiersProvider.COMMUNITY_URL = constants.STAGING_OCTOBOT_COMMUNITY_URL IdentifiersProvider.COMMUNITY_LANDING_URL = constants.STAGING_OCTOBOT_COMMUNITY_LANDING_URL + IdentifiersProvider.COMMUNITY_API_URL = constants.STAGING_OCTOBOT_COMMUNITY_API_URL IdentifiersProvider.FRONTEND_PASSWORD_RECOVER_URL = constants.STAGING_COMMUNITY_RECOVER_PASSWORD_URL IdentifiersProvider.BACKEND_URL = constants.STAGING_COMMUNITY_BACKEND_URL IdentifiersProvider.BACKEND_KEY = constants.STAGING_COMMUNITY_BACKEND_KEY diff --git a/octobot/constants.py b/octobot/constants.py index 52baf943a..a4c41aa3d 100644 --- a/octobot/constants.py +++ b/octobot/constants.py @@ -62,6 +62,7 @@ # production env SHOULD ONLY BE USED THROUGH CommunityIdentifiersProvider OCTOBOT_COMMUNITY_LANDING_URL = os.getenv("COMMUNITY_SERVER_URL", "https://octobot.cloud") +OCTOBOT_COMMUNITY_API_URL = os.getenv("COMMUNITY_SERVER_URL", f"{OCTOBOT_COMMUNITY_LANDING_URL}/api/community") OCTOBOT_COMMUNITY_URL = os.getenv("COMMUNITY_SERVER_URL", "https://app.octobot.cloud") OCTOBOT_COMMUNITY_RECOVER_PASSWORD_URL = OCTOBOT_COMMUNITY_URL # default env @@ -70,6 +71,7 @@ # staging env SHOULD ONLY BE USED THROUGH CommunityIdentifiersProvider STAGING_OCTOBOT_COMMUNITY_LANDING_URL = os.getenv("COMMUNITY_SERVER_URL", "https://beta.octobot.cloud") +STAGING_OCTOBOT_COMMUNITY_API_URL = os.getenv("COMMUNITY_SERVER_URL", f"{STAGING_OCTOBOT_COMMUNITY_LANDING_URL}/api/community") STAGING_OCTOBOT_COMMUNITY_URL = os.getenv("COMMUNITY_SERVER_URL", "https://app-beta.octobot.cloud/") STAGING_COMMUNITY_RECOVER_PASSWORD_URL = STAGING_OCTOBOT_COMMUNITY_URL STAGING_COMMUNITY_BACKEND_URL = os.getenv("COMMUNITY_BACKEND_URL", "https://wmfkgvgzokyzhvxowbyg.supabase.co") diff --git a/tests/unit_tests/community/test_community_data.py b/tests/unit_tests/community/test_community_data.py index fb6ffb7e7..15b9f081c 100644 --- a/tests/unit_tests/community/test_community_data.py +++ b/tests/unit_tests/community/test_community_data.py @@ -21,6 +21,8 @@ @pytest.mark.asyncio async def test_get_community_metrics(): metrics = await community_analysis.get_community_metrics() + assert metrics == {} + return # todo migrate with new metrics assert len(metrics) == 9 # ensure metrics are not empty assert all(content for content in metrics.values())