🐛 Donot report data when reloaded (#71)

确保sentry和mixpanel不会在初始配置关闭时上报数据
This commit is contained in:
Akkia 2024-01-03 19:44:17 +08:00 committed by GitHub
parent 454481ecc9
commit e9fbdb09f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 17 deletions

View File

@ -26,8 +26,8 @@ async def reload_all():
loaded_plugins = list(pagermaid.modules.plugin_list) loaded_plugins = list(pagermaid.modules.plugin_list)
loaded_plugins.extend(iter(pagermaid.modules.module_list)) loaded_plugins.extend(iter(pagermaid.modules.module_list))
# init # init
importlib.reload(pagermaid.modules)
importlib.reload(pagermaid.config) importlib.reload(pagermaid.config)
importlib.reload(pagermaid.modules)
help_messages.clear() help_messages.clear()
all_permissions.clear() all_permissions.clear()
for functions in hook_functions.values(): for functions in hook_functions.values():

View File

@ -4,7 +4,8 @@ import json
import time import time
import uuid import uuid
from pagermaid import Config, logs from pagermaid.config import Config
from pagermaid import logs
from pagermaid.enums import Client, Message from pagermaid.enums import Client, Message
from pagermaid.services import client as request from pagermaid.services import client as request
from pagermaid.hook import Hook from pagermaid.hook import Hook
@ -25,6 +26,7 @@ class Mixpanel:
self._serializer = DatetimeSerializer self._serializer = DatetimeSerializer
self._request = request self._request = request
self.api_host = "api.mixpanel.com" self.api_host = "api.mixpanel.com"
self.is_people_set = False
@staticmethod @staticmethod
def _now(): def _now():
@ -57,7 +59,9 @@ class Mixpanel:
await self._request.post(request_url, data=params, timeout=10.0) await self._request.post(request_url, data=params, timeout=10.0)
logs.debug(f"Mixpanel request took {self._now() - start} seconds") logs.debug(f"Mixpanel request took {self._now() - start} seconds")
async def people_set(self, distinct_id: str, properties: dict): async def people_set(self, distinct_id: str, properties: dict, force_update: bool = False):
if self.is_people_set and (not force_update):
return
message = { message = {
"$distinct_id": distinct_id, "$distinct_id": distinct_id,
"$set": properties, "$set": properties,
@ -65,9 +69,11 @@ class Mixpanel:
record = {"$token": self._token, "$time": self._now()} record = {"$token": self._token, "$time": self._now()}
# sourcery skip: dict-assign-update-to-union # sourcery skip: dict-assign-update-to-union
record.update(message) record.update(message)
return await self.api_call( res = await self.api_call(
"people", self.json_dumps(record, cls=self._serializer) "people", self.json_dumps(record, cls=self._serializer)
) )
self.is_people_set = True
return res
async def track(self, distinct_id: str, event_name: str, properties: dict): async def track(self, distinct_id: str, event_name: str, properties: dict):
all_properties = { all_properties = {
@ -93,20 +99,31 @@ class Mixpanel:
mp = Mixpanel(Config.MIXPANEL_API) mp = Mixpanel(Config.MIXPANEL_API)
@Hook.on_startup() async def set_people(bot: Client, force_update: bool = False):
async def mixpanel_init_id(bot: Client): if not Config.ALLOW_ANALYTIC:
return
if mp.is_people_set and (not force_update):
return
if not bot.me: if not bot.me:
bot.me = await bot.get_me() bot.me = await bot.get_me()
data = {"$first_name": bot.me.first_name} data = {"$first_name": bot.me.first_name}
if bot.me.username: if bot.me.username:
data["username"] = bot.me.username data["username"] = bot.me.username
bot.loop.create_task(mp.people_set(str(bot.me.id), data)) bot.loop.create_task(mp.people_set(str(bot.me.id), data, force_update=force_update))
@Hook.on_startup()
async def mixpanel_init_id(bot: Client):
if not Config.ALLOW_ANALYTIC:
return
await set_people(bot)
@Hook.command_postprocessor() @Hook.command_postprocessor()
async def mixpanel_report(bot: Client, message: Message, command): async def mixpanel_report(bot: Client, message: Message, command):
if not Config.ALLOW_ANALYTIC: if not Config.ALLOW_ANALYTIC:
return return
await set_people(bot)
if not bot.me: if not bot.me:
bot.me = await bot.get_me() bot.me = await bot.get_me()
sender_id = message.from_user.id if message.from_user else "" sender_id = message.from_user.id if message.from_user else ""

View File

@ -34,16 +34,22 @@ sentry_sdk_git_hash = (
.stdout.decode() .stdout.decode()
.strip() .strip()
) )
sentry_sdk.init(
Config.SENTRY_API, # fixme: Not enough for dynamic disable sentry,
traces_sample_rate=1.0, # web server will still report if pgm start with Config.ERROR_REPORT = True
release=sentry_sdk_git_hash, if Config.ERROR_REPORT:
before_send=sentry_before_send, sentry_sdk.init(
environment="production", Config.SENTRY_API,
integrations=[ traces_sample_rate=1.0,
HttpxIntegration(), release=sentry_sdk_git_hash,
], before_send=sentry_before_send,
) environment="production",
integrations=[
HttpxIntegration(),
],
)
else:
sentry_sdk.init()
@Hook.on_startup() @Hook.on_startup()