@@ -30,45 +30,51 @@ class Event:
3030 def __init__ (self , analytics , event_key : str ):
3131 self .analytics = analytics
3232 self .event_key = event_key
33+ self .last_action = ""
3334
3435 self .ensure ()
3536
36- def ensure (self ):
37- if self .analytics .debug :
38- print (f"[DISCORDANALYTICS] Ensuring event { self .event_key } exists" )
37+ async def ensure (self ):
3938 if not isinstance (self .event_key , str ) or len (self .event_key ) < 1 or len (self .event_key ) > 50 :
4039 raise ValueError (ErrorCodes .INVALID_EVENTS_COUNT )
41-
40+
41+ if self .event_key not in self .analytics .stats ["custom_events" ]:
42+ if self .analytics .debug :
43+ print (f"[DISCORDANALYTICS] Fetching value for event { self .event_key } " )
44+
4245 url = ApiEndpoints .EVENT_URL .replace (":id" , str (self .analytics .client .user .id )).replace (":event_key" , self .event_key )
4346
44- self .analytics .api_call_with_retries ("GET" , url , self .analytics .headers )
47+ res = await self .analytics .api_call_with_retries ("GET" , url , self .analytics .headers )
4548
46- if self . event_key not in self .analytics . stats [ "custom_events" ] :
47- self .analytics .stats ["custom_events" ][self .event_key ] = 0
49+ if res is not None and self .last_action != 'set' :
50+ self .analytics .stats ["custom_events" ][self .event_key ] = ( self . analytics . stats [ "custom_events" ]. get ( self . event_key , 0 ) + ( await res . json ()). get ( "value" , 0 ))
4851
4952 if self .analytics .debug :
50- print (f"[DISCORDANALYTICS] Event { self .event_key } ensured " )
53+ print (f"[DISCORDANALYTICS] Value fetched for event { self .event_key } " )
5154
5255 def increment (self , count : int = 1 ):
5356 if self .analytics .debug :
5457 print (f"[DISCORDANALYTICS] Incrementing event { self .event_key } by { count } " )
5558 if not isinstance (count , int ) or count < 0 :
5659 raise ValueError (ErrorCodes .INVALID_VALUE_TYPE )
57- self .analytics .stats ["custom_events" ][self .event_key ] += count
60+ self .analytics .stats ["custom_events" ][self .event_key ] = self .analytics .stats ["custom_events" ].get (self .event_key , 0 ) + count
61+ self .last_action = "increment"
5862
5963 def decrement (self , count : int = 1 ):
6064 if self .analytics .debug :
6165 print (f"[DISCORDANALYTICS] Decrementing event { self .event_key } by { count } " )
6266 if not isinstance (count , int ) or count < 0 or self .get () - count < 0 :
6367 raise ValueError (ErrorCodes .INVALID_VALUE_TYPE )
64- self .analytics .stats ["custom_events" ][self .event_key ] -= count
68+ self .analytics .stats ["custom_events" ][self .event_key ] = self .analytics .stats ["custom_events" ].get (self .event_key , 0 ) - count
69+ self .last_action = "decrement"
6570
6671 def set (self , value : int ):
6772 if self .analytics .debug :
6873 print (f"[DISCORDANALYTICS] Setting event { self .event_key } to { value } " )
6974 if not isinstance (value , int ) or value < 0 :
7075 raise ValueError (ErrorCodes .INVALID_VALUE_TYPE )
7176 self .analytics .stats ["custom_events" ][self .event_key ] = value
77+ self .last_action = "set"
7278
7379 def get (self ):
7480 if self .analytics .debug :
@@ -180,9 +186,9 @@ async def init(self):
180186
181187 if self .debug :
182188 if "--dev" in sys .argv :
183- print ("[DISCORDANALYTICS] DevMode is enabled. Stats will be sent every 30s." )
189+ print ("[DISCORDANALYTICS] Fast mode is enabled. Stats will be sent every 30s." )
184190 else :
185- print ("[DISCORDANALYTICS] DevMode is disabled. Stats will be sent every 5 minutes." )
191+ print ("[DISCORDANALYTICS] Fast mode is disabled. Stats will be sent every 5 minutes." )
186192
187193 if not self .chunk_guilds :
188194 await self .load_members_for_all_guilds ()
@@ -252,10 +258,10 @@ async def send_stats(self):
252258 "other" : 0 ,
253259 "private_message" : 0
254260 },
255- "custom_events" : {} ,
261+ "custom_events" : self . stats [ "custom_events" ] ,
256262 }
257263
258- await asyncio .sleep (30 if "--dev " in sys .argv else 300 )
264+ await asyncio .sleep (30 if "--fast " in sys .argv else 300 )
259265
260266 def calculate_guild_members_repartition (self ):
261267 thresholds = {
0 commit comments