📈 Help network bad areas access analytics API. (#111)

* 📈 帮助网络不好的地区访问统计 API
This commit is contained in:
Xtao_dada 2021-07-19 11:27:02 +08:00 committed by GitHub
parent c17224d882
commit 6b502a1478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 22 deletions

View File

@ -75,4 +75,4 @@ update_delete: "True"
ipv6: "False" ipv6: "False"
# Analytics # Analytics
allow_analytics: "True" allow_analytic: "True"

View File

@ -5,7 +5,6 @@ from concurrent.futures import CancelledError
# Analytics # Analytics
import sentry_sdk import sentry_sdk
from sentry_sdk.integrations.redis import RedisIntegration from sentry_sdk.integrations.redis import RedisIntegration
from mixpanel import Mixpanel
python36 = True python36 = True
try: try:
@ -90,16 +89,17 @@ def lang(text: str) -> str:
return result return result
analytics = None
try: try:
allow_analytics = strtobool(config['allow_analytics']) allow_analytics = strtobool(config['allow_analytic'])
except KeyError: except KeyError:
allow_analytics = True allow_analytics = True
except ValueError: except ValueError:
allow_analytics = True allow_analytics = True
if allow_analytics: if allow_analytics:
mp = Mixpanel("7be1833326f803740214fe276f5a5a3d") import analytics
else: analytics.write_key = 'EI5EyxFl8huwAvv932Au7XoRSdZ63wC4'
mp = None analytics = analytics
if strtobool(config['debug']): if strtobool(config['debug']):
logs.setLevel(DEBUG) logs.setLevel(DEBUG)
else: else:
@ -213,11 +213,16 @@ async def save_id():
if me.username is not None: if me.username is not None:
sentry_sdk.set_user({"id": user_id, "name": me.first_name, "username": me.username, "ip_address": "{{auto}}"}) sentry_sdk.set_user({"id": user_id, "name": me.first_name, "username": me.username, "ip_address": "{{auto}}"})
if allow_analytics: if allow_analytics:
mp.people_set(str(user_id), {'$first_name': me.first_name, "username": me.username}) analytics.identify(user_id, {
'name': me.first_name,
'username': me.username
})
else: else:
sentry_sdk.set_user({"id": user_id, "name": me.first_name, "ip_address": "{{auto}}"}) sentry_sdk.set_user({"id": user_id, "name": me.first_name, "ip_address": "{{auto}}"})
if allow_analytics: if allow_analytics:
mp.people_set(str(user_id), {'$first_name': me.first_name}) analytics.identify(user_id, {
'name': me.first_name
})
logs.info(f"{lang('save_id')} {me.first_name}({user_id})") logs.info(f"{lang('save_id')} {me.first_name}({user_id})")

View File

@ -8,11 +8,11 @@ from distutils2.util import strtobool
from traceback import format_exc from traceback import format_exc
from time import gmtime, strftime, time from time import gmtime, strftime, time
from telethon.events import StopPropagation from telethon.events import StopPropagation
from pagermaid import bot, config, help_messages, logs, mp, user_id from pagermaid import bot, config, help_messages, logs, user_id, analytics
from pagermaid.utils import attach_report, lang, alias_command from pagermaid.utils import attach_report, lang, alias_command
try: try:
allow_analytics = strtobool(config['allow_analytics']) allow_analytics = strtobool(config['allow_analytic'])
except KeyError: except KeyError:
allow_analytics = True allow_analytics = True
except ValueError: except ValueError:
@ -57,7 +57,7 @@ def listener(**args):
async def handler(context): async def handler(context):
try: try:
analytics = True analytic = True
try: try:
parameter = context.pattern_match.group(1).split(' ') parameter = context.pattern_match.group(1).split(' ')
if parameter == ['']: if parameter == ['']:
@ -65,23 +65,23 @@ def listener(**args):
context.parameter = parameter context.parameter = parameter
context.arguments = context.pattern_match.group(1) context.arguments = context.pattern_match.group(1)
except BaseException: except BaseException:
analytics = False analytic = False
context.parameter = None context.parameter = None
context.arguments = None context.arguments = None
await function(context) await function(context)
if analytics and allow_analytics: if analytic and allow_analytics:
try: try:
upload_command = context.text.split()[0].replace('-', '') upload_command = context.text.split()[0].replace('-', '')
upload_command = alias_command(upload_command) upload_command = alias_command(upload_command)
if context.sender_id: if context.sender_id:
if context.sender_id > 0: if context.sender_id > 0:
mp.track(str(context.sender_id), f'Function {upload_command}', analytics.track(context.sender_id, f'Function {upload_command}',
{'command': upload_command}) {'command': upload_command})
else: else:
mp.track(str(user_id), f'Function {upload_command}', analytics.track(user_id, f'Function {upload_command}',
{'command': upload_command}) {'command': upload_command})
else: else:
mp.track(str(user_id), f'Function {upload_command}', analytics.track(user_id, f'Function {upload_command}',
{'command': upload_command}) {'command': upload_command})
except Exception as e: except Exception as e:
logs.info(f"Analytics Error ~ {e}") logs.info(f"Analytics Error ~ {e}")
@ -110,8 +110,10 @@ def listener(**args):
await attach_report(report, f"exception.{time()}.pagermaid", None, await attach_report(report, f"exception.{time()}.pagermaid", None,
"Error report generated.") "Error report generated.")
try: try:
sentry_sdk.set_context("Target", {"ChatID": str(context.chat_id), "UserID": str(context.sender_id), "Msg": context.text}) sentry_sdk.set_context("Target",
sentry_sdk.set_tag('com', re.findall("\w+",str.lower(context.text.split()[0]))[0]) {"ChatID": str(context.chat_id), "UserID": str(context.sender_id),
"Msg": context.text})
sentry_sdk.set_tag('com', re.findall("\w+", str.lower(context.text.split()[0]))[0])
sentry_sdk.capture_exception(e) sentry_sdk.capture_exception(e)
except: except:
logs.info( logs.info(

View File

@ -36,5 +36,5 @@ python-socks[asyncio]>=1.2.4
certifi>=2021.5.30 certifi>=2021.5.30
magic_google>=0.2.9 magic_google>=0.2.9
sentry-sdk>=1.3.0 sentry-sdk>=1.3.0
mixpanel>=4.9.0 analytics-python>=1.4.0
beautifulsoup4>=4.9.3 beautifulsoup4>=4.9.3