Pc׷ZddlZddlZddlZddlZddlmZmZmZdZGdde Z dS)N)DictIterableUnionzAleksandr ShyshatskycxeZdZdZdZdZdZdZdZdZ dZ d Z e d Z d Zd Zd ZdZdZdZdS)NotificationsHelperza Helper for our StatsNotifier plugin, contains logic related to notification periods z/var/lve/statsnotifier_last.tsz%/var/lve/statsnotifier_timestamps.binci|_i|_d|_tjt |_|dS)N)_users_notification_info_resellers_notification_info_admin_notify_timelogging getLogger__name___log_load_from_persistent_storageselfs /builddir/build/BUILDROOT/lve-stats-4.3.2-1.el9.x86_64/opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/lib/notifications_helper.py__init__zNotificationsHelper.__init__sF(*%,.)"$%h//  **,,,,,c4||_tj|jsdS t |jdd5}tj|}dddn #1swxYwYn#t$r| YdStj $r$|j d|jYdSttf$r|j dYdSwxYw d|dD}d |d D}n8#t"t$t&f$r|j dYdSwxYw||_||_dS) z Load information about periods from persistent storage. Admin timestamp contains in separate file, in order to make it backwards-compatible with old logic Nrutf-8encodingu#Corrupted JSON in %s — discarding(Cannot load data from persistent storagec4i|]\}}t||Sint.0kvs r zENotificationsHelper._load_from_persistent_storage..?s$AAA41aSVVQAAArusersc4i|]\}}t||Srrr!s rr%zENotificationsHelper._load_from_persistent_storage..@s$IIItq!QIIIr resellers)_read_ts_from_filer ospathexistsRESELLERS_NOTIFICATIONS_STORAGEopenjsonloadUnicodeDecodeError_migrate_from_pickleJSONDecodeErrorr exceptionIOErrorOSErroritemsKeyError TypeError ValueErrorr r )rfdatar&r(s rrz1NotificationsHelper._load_from_persistent_storage#s#'"9"9";";w~~dBCC  F d:C'RRR $VWy|| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $!     % % ' ' ' FF#    I  54    FF!    I   J K K K FF  AA4=+>+>+@+@AAAEIItK/@/F/F/H/HIIIII)Z0    I   J K K K FF ).%,5)))sUBA9- B9A==BA=BD&.D*DD AE1FFc`ddl} t|jd5}||\|_|_dddn #1swxYwY||jddS#t$r|j dYdSwxYw)z4One-time migration of legacy pickle storage to JSON.rNrbz;Migrated notification timestamps from pickle to JSON formatr) pickler.r-r0r r save_to_persistent_storagerinfo Exceptionr4)rr?r;s rr2z(NotificationsHelper._migrate_from_pickleGs   Ld:DAA bQSYS^S^_`SaSaP-t/P b b b b b b b b b b b b b b b  + + - - - INNM      L L L I   J K K K K K K Ls3B#A  B ABA1B$B-,B-c||j tj|jtjtjztjzd}tj|dd5}tj |j |j d|ddddS#1swxYwYdS#tttf$r|jdYdSwxYw)z Save information about periods on disk. Admin timestamp contains in separate file, plain text. Resellers info is serialized as JSON and saved into other file. iwrr)r&r(Nz+Unable to save resellers timestamps to file)_save_ts_to_filer r*r.r-O_WRONLYO_CREATO_TRUNCfdopenr/dumpr r r5r6r9rwarning)rfdr;s rr@z.NotificationsHelper.save_to_persistent_storageUsR d5666 M=rz1BJ>GGB2sW555 O D$A(,(IKKLMOOO O O O O O O O O O O O O O O O O O O), M M M I  K L L L L L L Ms6AB/2#B" B/"B&&B/)B&*B//1C$#C$c t|jdd5}|t|ddddS#1swxYwYdS#t$r|jdYdSwxYw)NrDrrz&Unable to save admin timestamp to file)r.STATSNOTIFIER_LAST_TSwritestrr5rrK)rtsr;s rrEz$NotificationsHelper._save_ts_to_filegs Hd0#HHH !AB    ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! H H H I  F G G G G G G Hs3A#A  A A  AA A$A>=A>ct t|jdd5}t|}|cdddS#1swxYwYdS#t $rYdSt $r9}|jd|jt|Yd}~dSd}~wwxYw)Nrrrr zUnable to read %s (%s)) r.rNfloatreadlinerstripr5r:rrKrP)rr;rQes rr)z&NotificationsHelper._read_ts_from_fileos d0#HHH A1::<<..0011                     22    I  68RTWXYTZTZ [ [ [22222 s@A(5A A(AA("A#A(( B75 B7>.B22B7c(tjS)zr Get current timestamp. In future, we may do some things here, like "round(time / 60**2)" )timerrr_get_current_timestampz*NotificationsHelper._get_current_timestamp{sy{{rc|}|D](}|jd||||j|<)dS)zY Mark resellers as notified. S aves current timestamp in memory. z(Reseller marked as notified at %s for %sN)rYrdebugr r resellers_idrQ reseller_ids rmark_resellers_notifiedz+NotificationsHelper.mark_resellers_notifieds[  ( ( * *' @ @K IOOFK X X X=?D -k : : @ @rc|}|D](}|jd||||j|<)dS)zT Mark users as notified. Saves current timestamp in memory. z%Users marked as notified at %s for %sN)rYrr[r r\s rmark_users_notifiedz'NotificationsHelper.mark_users_notifiedsW  ( ( * *' < rus  (((((((((( # jWjWjWjWjW&jWjWjWjWjWr