Improving client viewing log
This commit is contained in:
97
app/methods/log_client_viewing_methods.py
Normal file
97
app/methods/log_client_viewing_methods.py
Normal file
@@ -0,0 +1,97 @@
|
||||
from __future__ import annotations
|
||||
import datetime
|
||||
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
||||
|
||||
from app.db_sql import redis_lookup_id_random, sql_insert, sql_insert_or_update, sql_select, sql_update
|
||||
from app.lib_general import log, logging
|
||||
|
||||
|
||||
from app.models.common_field_schema import default_num_bytes
|
||||
from app.models.log_client_viewing_models import Log_Client_Viewing_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Log Client Viewing Methods ### load_log_client_viewing_obj() ###
|
||||
def load_log_client_viewing_obj(
|
||||
log_client_viewing_id: int|str,
|
||||
limit: int = 10000,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
model_as_dict: bool = False,
|
||||
) -> Log_Client_Viewing_Base|bool:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if log_client_viewing_id := redis_lookup_id_random(record_id_random=log_client_viewing_id, table_name='log_client_viewing'): pass
|
||||
else: return False
|
||||
|
||||
if log_client_viewing_rec := sql_select(table_name='v_log_client_viewing', record_id=log_client_viewing_id): pass
|
||||
else: return False
|
||||
|
||||
try:
|
||||
log_client_viewing_obj = Log_Client_Viewing_Base(**log_client_viewing_rec)
|
||||
log.debug(log_client_viewing_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
|
||||
if model_as_dict:
|
||||
return log_client_viewing_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
||||
else:
|
||||
return log_client_viewing_obj
|
||||
# ### END ### API Log Client Viewing Methods ### load_log_client_viewing_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Log Client Viewing Methods ### get_log_client_viewing_rec_list() ###
|
||||
def get_log_client_viewing_rec_list(
|
||||
account_id: str,
|
||||
from_datetime: datetime.datetime = None,
|
||||
to_datetime: datetime.datetime = None,
|
||||
limit: int = 1000,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
else: return False
|
||||
data = {}
|
||||
data['account_id'] = account_id
|
||||
sql_account_id = f'`tbl`.account_id = :account_id'
|
||||
|
||||
# if enabled in ['enabled', 'disabled', 'all']:
|
||||
# if enabled == 'enabled':
|
||||
# data['enable'] = True
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# elif enabled == 'disabled':
|
||||
# data['enable'] = False
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# elif enabled == 'all':
|
||||
# sql_enabled = ''
|
||||
sql_enabled = ''
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
sql_limit = f'LIMIT :limit'
|
||||
else:
|
||||
sql_limit = ''
|
||||
|
||||
sql = f"""
|
||||
SELECT `tbl`.id AS 'log_client_viewing_id', `tbl`.id_random AS 'log_client_viewing_id_random'
|
||||
FROM `log_client_viewing` AS `tbl`
|
||||
WHERE
|
||||
{sql_account_id}
|
||||
{sql_enabled}
|
||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
|
||||
if log_client_viewing_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
log_client_viewing_rec_li = log_client_viewing_rec_li_result
|
||||
else:
|
||||
log_client_viewing_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(log_client_viewing_rec_li_result)
|
||||
|
||||
return log_client_viewing_rec_li
|
||||
# ### END ### API Log Client Viewing Methods ### get_log_client_viewing_rec_list() ###
|
||||
Reference in New Issue
Block a user