refactor: 优化谷歌统计

This commit is contained in:
涵曦 2024-09-15 23:10:00 +08:00
parent 30926c6b79
commit 7ccbd6ce79
2 changed files with 20 additions and 12 deletions

View File

@ -1,3 +1,4 @@
import asyncio
from datetime import datetime
from ga4mp import GtagMP
@ -26,27 +27,34 @@ class Analytics:
self.gtag = gtag
self.log.info("analytics init ok")
def send_startup_event(self):
async def run_with_timeout(self, func, *args, **kwargs):
try:
self._send_startup_event()
return await asyncio.wait_for(func(*args, **kwargs), 3)
except asyncio.TimeoutError as e:
self.log.warning(f"analytics run_with_timeout failed {e}")
return None
async def send_startup_event(self):
try:
await self.run_with_timeout(self._send_startup_event)
except Exception as e:
self.log.warning(f"analytics send_startup_event failed {e}")
self.init()
def _send_startup_event(self):
async def _send_startup_event(self):
event = self.gtag.create_new_event(name="startup")
event.set_event_param(name="version", value=__version__)
event_list = [event]
self.gtag.send(events=event_list)
def send_daily_event(self):
async def send_daily_event(self):
try:
self._send_daily_event()
await self.run_with_timeout(self._send_daily_event)
except Exception as e:
self.log.warning(f"analytics send_daily_event failed {e}")
self.init()
def _send_daily_event(self):
async def _send_daily_event(self):
current_date = datetime.now().strftime("%Y-%m-%d")
if self.current_date == current_date:
return
@ -58,14 +66,14 @@ class Analytics:
self.gtag.send(events=event_list)
self.current_date = current_date
def send_play_event(self, name, sec):
async def send_play_event(self, name, sec):
try:
self._send_play_event(name, sec)
await self.run_with_timeout(self._send_play_event, name, sec)
except Exception as e:
self.log.warning(f"analytics send_play_event failed {e}")
self.init()
def _send_play_event(self, name, sec):
async def _send_play_event(self, name, sec):
event = self.gtag.create_new_event(name="play")
event.set_event_param(name="version", value=__version__)
event.set_event_param(name="music", value=name)

View File

@ -87,7 +87,6 @@ class XiaoMusic:
# 启动统计
self.analytics = Analytics(self.log)
self.analytics.send_startup_event()
debug_config = deepcopy_data_no_sensitive_info(self.config)
self.log.info(f"Startup OK. {debug_config}")
@ -178,7 +177,7 @@ class XiaoMusic:
# sleep to avoid too many request
# self.log.debug(f"Sleep {d}, timestamp: {self.last_timestamp}")
await asyncio.sleep(1 - d)
self.analytics.send_daily_event()
await self.analytics.send_daily_event()
async def init_all_data(self, session):
await self.login_miboy(session)
@ -522,6 +521,7 @@ class XiaoMusic:
self.log.exception(f"Execption {e}")
async def run_forever(self):
await self.analytics.send_startup_event()
async with ClientSession() as session:
self.session = session
await self.init_all_data(session)
@ -1073,7 +1073,7 @@ class XiaoMusicDevice:
return
self.log.info(f"{name}】已经开始播放了")
self.xiaomusic.analytics.send_play_event(name, sec)
await self.xiaomusic.analytics.send_play_event(name, sec)
# 设置下一首歌曲的播放定时器
if sec <= 1: