Skip to content

Commit

Permalink
[Community] init metrics migration
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Dec 19, 2023
1 parent 8258f59 commit 1961bc6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
15 changes: 7 additions & 8 deletions octobot/community/community_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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:
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions octobot/community/community_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
3 changes: 3 additions & 0 deletions octobot/community/identifiers_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions octobot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions tests/unit_tests/community/test_community_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

0 comments on commit 1961bc6

Please sign in to comment.