ok

Mini Shell

Direktori : /opt/cloudlinux/venv/lib/python3.11/site-packages/ssa/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/ssa/__pycache__/db.cpython-311.pyc

�

��f���6�ddlZddlZddlZddlmZmZddlmZmZmZm	Z	m
Z
mZmZm
Z
mZddlmZddlmZddlmZddlmZddlmZd	Zed
zZdZe��ZGd�d
e��Zd�Zd�Zd�Z d�Z!d�Z"dd�Z#d�Z$d�Z%d�Z&ej'defd���Z(dS)�N)�datetime�	timedelta)	�Column�Boolean�DateTime�Integer�String�
create_engine�event�func�text)�declarative_base)�	Inspector)�Session)�close_all_sessions)�
DatabaseErrorz/var/lve/ssa.dbz.old�c���eZdZdZdZeed���Zeedd���Z	eedd���Z
eed���Zeed���Zee
d���Zee
d���Zeed���Zeed���Zee
d���Zeed���ej���	��Zeed���ej��ej���
��ZdS)�
RequestResultay
    Describes processed request stored in database file.

    E.g.
    {
        "timestamp": "1650008727",
        "url": "http://mydomain.com/index.php",
        "duration": 162077,
        "hitting_limits": false,
        "throttled_time": 0,
        "io_throttled_time": 0,
        "wordpress": true
    }

    Note: created_at, updated_at is saved in local TZ format
    �
scrape_resultT)�primary_keyF)�index�nullable)r)�timezone)�server_default)�onupdaterN)�__name__�
__module__�__qualname__�__doc__�
__tablename__rr�idr	�domain�path�	timestamp�durationr�is_slow_request�hitting_limits�throttled_time�io_throttled_time�	wordpressrr�now�
created_at�
updated_at���=/opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/db.pyrr%sM�������� $�M�	���T�	*�	*�	*�B�
�V�F�$��
7�
7�
7�F��6�&��u�5�5�5�D���w��/�/�/�I��v�g��.�.�.�H��f�W�u�5�5�5�O��V�G�e�4�4�4�N��V�G�e�4�4�4�N���w��7�7�7����w��/�/�/�I�����$�/�/�/����
�
�K�K�K�J�����$�/�/�/�(�$�(�*�*�U]�UY�U]�U_�U_�`�`�`�J�J�Jr0rc�B�tj��tt���z
}t	|��5}|�t���tj|k���	��ddd��dS#1swxYwYdS)zD
    Removes outdated records from database, saving disk space.
    )�daysN)
r�todayr�RETENTION_TIME_DAYS�
session_scope�queryr�filterr-�delete)�engine�
n_days_ago�sessions   r1�cleanup_old_datar=Js�����!�!�I�3F�$G�$G�$G�G�J�	�v�	�	��'��
�
�m�$�$�
�V�M�,�z�9�
:�
:�
�V�X�X�X���������������������s�AB�B�Bc�f�t|��s!tj�|��dSdS�N)�
is_db_present�Base�metadata�
create_all)r:s r1�create_db_if_not_existrDUs8���� � �)��
� � ��(�(�(�(�(�)�)r0c���tj�t��sdSt	j|��}d�|���D��}t|��dkS)NFc��g|]}|��Sr/r/)�.0�tables  r1�
<listcomp>z!is_db_present.<locals>.<listcomp>^s��
G�
G�
G��e�
G�
G�
Gr0r)�osr$�isfile�SSA_DBr�from_engine�get_table_names�len)r:�database_inspection�tabless   r1r@r@Zs^��
�7�>�>�&�!�!���u�#�/��7�7��
G�
G�!4�!D�!D�!F�!F�
G�
G�
G�F��v�;�;��?�r0c�0�|�d��dS)NzPRAGMA journal_mode = WAL)�execute)�	dbapi_con�
con_records  r1�setup_wal_moderVbs��
���1�2�2�2�2�2r0c���dt���|r��d���fd�}td|d���}tj|dt��t|��|S)Nzfile:z?mode=roc�0��tj�d���S)NT)�uri)�sqlite3�connect)�connection_strings�r1�<lambda>z!_setup_database.<locals>.<lambda>js���g�o�&7�T�B�B�B�r0�sqlite:////F)�creator�echor[)rLr
r�listenrVrD)�readonlyr_r:r\s   @r1�_setup_databasercfsz���(��(�(���;�0�:�:�:��B�B�B�B�G�
��w�U����F�
�L���N�3�3�3��6�"�"�"��Mr0Fc� �t|��Sr?)rc)rbs r1�setup_databaseress���8�$�$�$r0c���tj�t��r-t	��tjtt��t��}|���tdt����}	t|��5}t|��5}|�t���
��t||��ddd��n#1swxYwYddd��n#1swxYwYn#t$rYnwxYwttdztdzfD]5}tj�|��rtj|���6dS)z�
    Restore database by establish connections to old and new databases,
    merge data to new one if possible and delete old one.
    r^Nz-walz-shm)rJr$�existsrLr�replace�
OLD_SSA_DBre�disposer
r6r7r�first�!merge_unharmed_data_from_databaser�remove)r:�
new_engine�
old_engine�session_old�session_newr$s      r1�restore_databaserrws���

�w�~�~�f���'�����
�
�6�:�&�&�&��!�!�J�
�N�N�����9�Z�9�9�:�:�J�
�
�:�
&�
&�	H�+�}�Z�7P�7P�	H�T_����m�,�,�2�2�4�4�4�-�k�;�G�G�G�	H�	H�	H�	H�	H�	H�	H�	H�	H�	H�	H����	H�	H�	H�	H�	H�	H�	H�	H�	H�	H�	H�	H�	H�	H�	H����	H�	H�	H�	H����
�
�
���
�����V�f�_�f�v�o�>����
�7�>�>�$���	��I�d�O�O�O���sZ�D�D�+=C4�(D�4C8	�8D�;C8	�<D�?D�D�D�D�D�
D$�#D$c��d}d}	|�t���|���|��}	|���}|sdS|D]}|�|���nv#t$rit|��D]V}	|�||z�����}|sn&|�|���G#t$rY�SwxYwYnwxYw|�	��||z
}��)z_
    Scrape all unharmed records from malformed database and merge them into new database.
    r�
TN)
r7r�offset�limit�all�merger�rangerk�commit)	rprqru�
batch_sizer7�records_to_save�record�pos_in_batch�record_to_saves	         r1rlrl�sN���F��J���!�!�-�0�0�7�7��?�?�E�E�j�Q�Q��	�#�i�i�k�k�O�"�
���)�
*�
*���!�!�&�)�)�)�)�
*���	�	�	� %�j� 1� 1�
�
���%*�\�\�&�<�2G�%H�%H�%N�%N�%P�%P�N�)�����%�%�n�5�5�5�5��$�����D�������	����	�������*���%sH�A:�A:�:C-�,C�C-�C�C-�
C'�$C-�&C'�'C-�,C-c���tj�t��r=tj�t��rtjt��	t
|��5}|�td����}|�	��}|dddkcddd��S#1swxYwYdS#t$rYdSwxYw)z�
    Try integrity check of database file to see if it is malformed.
    If database unable to execute it, will also count as malformed.
    zPRAGMA integrity_checkr�okNT)rJr$rgrirLrmr6rSr
�fetchallr)r:�db�result�errorss    r1�is_malformed_databaser��s��

�w�~�~�j�!�!��b�g�n�n�V�&<�&<��
�	�*�����
�6�
"�
"�	(�b��Z�Z��%=� >� >�?�?�F��_�_�&�&�F��!�9�Q�<�4�'�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(����	(�	(�	(�	(�	(�	(�������t�t����s7�#C�2AC�:C�C�C�C�C�
C"�!C"�returnc#��K�t|���}	|V�|���n#|����xYw	|���dS#|���wxYw)zF
    Provide a transactional scope around a series of operations.
    )�bindN)rrz�rollback�close)r:r<s  r1r6r6�s�����
�6�"�"�"�G���
�
�
����������������
����	�
	�
�
��������
�
�������s�-�A�A�A�A4)F))rJ�
contextlibrZrr�
sqlalchemyrrrrr	r
rrr
�sqlalchemy.ext.declarativer�sqlalchemy.engine.reflectionr�sqlalchemy.ormr�sqlalchemy.orm.sessionr�sqlalchemy.excrrLrir5rArr=rDr@rVrcrerrrlr��contextmanagerr6r/r0r1�<module>r�s��
�	�	�	���������(�(�(�(�(�(�(�(�����������������������8�7�7�7�7�7�2�2�2�2�2�2�"�"�"�"�"�"�5�5�5�5�5�5�(�(�(�(�(�(�	��
�f�_�
��������"a�"a�"a�"a�"a�D�"a�"a�"a�J���)�)�)�
���3�3�3�
�
�
�%�%�%�%����8���6
�
�
� ���W��������r0

Zerion Mini Shell 1.0