add v1.user.log.query message

This commit is contained in:
Emmanuel Garette 2021-05-12 18:36:59 +02:00
parent 09cd0a4e4c
commit 88c2c168ac
4 changed files with 28 additions and 47 deletions

View File

@ -5,7 +5,7 @@ CREATE TABLE RisottoLog(
URI VARCHAR(255), URI VARCHAR(255),
URIS VARCHAR(255), URIS VARCHAR(255),
UserLogin VARCHAR(100) NOT NULL, UserLogin VARCHAR(100) NOT NULL,
Level VARCHAR(10) NOT NULL, Status INTEGER NOT NULL,
Kwargs JSON, Kwargs JSON,
Returns JSON, Returns JSON,
StartDate timestamp DEFAULT current_timestamp, StartDate timestamp DEFAULT current_timestamp,

View File

@ -42,14 +42,21 @@ class CallDispatcher:
for ret in returns: for ret in returns:
async with await Config(response, display_name=lambda self, dyn_name, suffix: self.impl_getname()) as config: async with await Config(response, display_name=lambda self, dyn_name, suffix: self.impl_getname()) as config:
await config.property.read_write() await config.property.read_write()
key = None
try: try:
for key, value in ret.items(): for key, value in ret.items():
await config.option(key).value.set(value) await config.option(key).value.set(value)
except AttributeError: except AttributeError as err:
err = _(f'function {module_name}.{function_name} return the unknown parameter "{key}" for the uri "{risotto_context.version}.{risotto_context.message}"') if key is not None:
err = _(f'function {module_name}.{function_name} return the unknown parameter "{key}" for the uri "{risotto_context.version}.{risotto_context.message}"')
else:
err = _(f'function {module_name}.{function_name} return unconsistency data "{err}" for the uri "{risotto_context.version}.{risotto_context.message}"')
raise CallError(err) raise CallError(err)
except ValueError as err: except ValueError as err:
err = _(f'function {module_name}.{function_name} return the invalid parameter "{key}" for the uri "{risotto_context.version}.{risotto_context.message}": {err}') if key is not None:
err = _(f'function {module_name}.{function_name} return the invalid parameter "{key}" for the uri "{risotto_context.version}.{risotto_context.message}": {err}')
else:
err = _(f'function {module_name}.{function_name} return unconsistency error for the uri "{risotto_context.version}.{risotto_context.message}": {err}')
raise CallError(err) raise CallError(err)
await config.property.read_only() await config.property.read_only()
mandatories = await config.value.mandatory() mandatories = await config.value.mandatory()

View File

@ -11,6 +11,9 @@ from .config import get_config
database_lock = Lock() database_lock = Lock()
LEVELS = ['Error', 'Info', 'Success', 'Started', 'Failure']
class Logger: class Logger:
""" An object to manager log """ An object to manager log
""" """
@ -39,9 +42,9 @@ class Logger:
) -> None: ) -> None:
uri = self._get_last_uri(risotto_context) uri = self._get_last_uri(risotto_context)
uris = " ".join(risotto_context.paths) uris = " ".join(risotto_context.paths)
insert = 'INSERT INTO RisottoLog(Msg, URI, URIS, UserLogin, Level' insert = 'INSERT INTO RisottoLog(Msg, URI, URIS, UserLogin, Status'
values = 'VALUES($1,$2,$3,$4,$5' values = 'VALUES($1,$2,$3,$4,$5'
args = [msg, uri, uris, risotto_context.username, level] args = [msg, uri, uris, risotto_context.username, LEVELS.index(level)]
if kwargs: if kwargs:
insert += ', Kwargs' insert += ', Kwargs'
values += ',$6' values += ',$6'
@ -67,40 +70,6 @@ class Logger:
except UndefinedTableError as err: except UndefinedTableError as err:
raise Exception(_(f'cannot access to database ({err}), was the database really created?')) raise Exception(_(f'cannot access to database ({err}), was the database really created?'))
async def query(self,
risotto_context: Context,
context_id: int,
uri: Optional[str],
) -> list:
sql = '''SELECT Msg as msg, URI as uri_name, URIS as uris, UserLogin as user_login, Level as level, Kwargs as kwargs, Returns as returns, StartDate as start_date, StopDate as stop_date
FROM RisottoLog
WHERE UserLogin = $1 AND (LogId = $2 OR ContextId = $2)
'''
args = [sql, risotto_context.username, context_id]
if uri is not None:
sql += ' AND URI = $3'
args.append(uri)
ret = []
async with database_lock:
connection = await self.get_connection(risotto_context)
for row in await connection.fetch(*args):
d = {}
for key, value in row.items():
if key in ['kwargs', 'returns']:
if isinstance(value, dict):
pass
elif not value:
value = {}
else:
value = loads(value)
if key == 'uris':
value = value.split(' ')
elif key in ['start_date', 'stop_date']:
value = str(value)
d[key] = value
ret.append(d)
return ret
def _get_last_uri(self, def _get_last_uri(self,
risotto_context: Context, risotto_context: Context,
) -> str: ) -> str:
@ -171,7 +140,7 @@ class Logger:
print(_(f'{risotto_context.username}: START{context}:{paths_msg}: {msg}')) print(_(f'{risotto_context.username}: START{context}:{paths_msg}: {msg}'))
await self.insert(msg, await self.insert(msg,
risotto_context, risotto_context,
'Start', 'Started',
arguments, arguments,
start=True, start=True,
) )
@ -185,11 +154,11 @@ class Logger:
print(_(f'{risotto_context.username}: SUCCESS({risotto_context.context_id}):{paths_msg}')) print(_(f'{risotto_context.username}: SUCCESS({risotto_context.context_id}):{paths_msg}'))
sql = """UPDATE RisottoLog sql = """UPDATE RisottoLog
SET StopDate = $2, SET StopDate = $2,
Level = 'SUCCESS' Status = $3
""" """
args = [datetime.now()] args = [datetime.now(), LEVELS.index('Success')]
if returns: if returns:
sql += """, Returns = $3 sql += """, Returns = $4
""" """
args.append(dumps(returns)) args.append(dumps(returns))
sql += """WHERE LogId = $1 sql += """WHERE LogId = $1
@ -214,7 +183,7 @@ class Logger:
print(_(f'{risotto_context.username}: FAILED({risotto_context.context_id}):{paths_msg}: {err}')) print(_(f'{risotto_context.username}: FAILED({risotto_context.context_id}):{paths_msg}: {err}'))
sql = """UPDATE RisottoLog sql = """UPDATE RisottoLog
SET StopDate = $2, SET StopDate = $2,
Level = 'FAILED', Status = $4,
Msg = $3 Msg = $3
WHERE LogId = $1 WHERE LogId = $1
""" """
@ -224,6 +193,7 @@ class Logger:
risotto_context.start_id, risotto_context.start_id,
datetime.now(), datetime.now(),
err, err,
LEVELS.index('Failure'),
) )
async def info(self, async def info(self,

View File

@ -313,6 +313,7 @@ class CustomParam:
'string': 'String', 'string': 'String',
'number': 'Number', 'number': 'Number',
'object': 'Dict', 'object': 'Dict',
'any': 'Any',
'array': 'Array', 'array': 'Array',
'file': 'File', 'file': 'File',
'float': 'Float'} 'float': 'Float'}
@ -448,6 +449,7 @@ def _get_option(name,
'reverse_condition': ParamValue(True)}), 'reverse_condition': ParamValue(True)}),
calc_value_property_help)) calc_value_property_help))
props.append('notunique')
description = arg.description.strip().rstrip() description = arg.description.strip().rstrip()
kwargs = {'name': name, kwargs = {'name': name,
'doc': _get_description(description, name), 'doc': _get_description(description, name),
@ -523,6 +525,7 @@ def _parse_responses(message_def,
'Number': IntOption, 'Number': IntOption,
'Boolean': BoolOption, 'Boolean': BoolOption,
'Dict': DictOption, 'Dict': DictOption,
'Any': AnyOption,
'Float': FloatOption, 'Float': FloatOption,
# FIXME # FIXME
'File': StrOption}.get(type_) 'File': StrOption}.get(type_)
@ -530,8 +533,9 @@ def _parse_responses(message_def,
raise Exception(f'unknown param type {obj.type} in responses of message {message_def.message}') raise Exception(f'unknown param type {obj.type} in responses of message {message_def.message}')
if hasattr(obj, 'default'): if hasattr(obj, 'default'):
kwargs['default'] = obj.default kwargs['default'] = obj.default
kwargs['properties'] = ('notunique',)
else: else:
kwargs['properties'] = ('mandatory',) kwargs['properties'] = ('mandatory', 'notunique')
options.append(option(**kwargs)) options.append(option(**kwargs))
od = OptionDescription(uri, od = OptionDescription(uri,
message_def.response.description, message_def.response.description,
@ -600,7 +604,7 @@ def get_messages(current_module_names,
select_option = ChoiceOption('message', select_option = ChoiceOption('message',
'Nom du message.', 'Nom du message.',
tuple(messages), tuple(messages),
properties=frozenset(['mandatory', 'positional'])) properties=frozenset(['mandatory', 'positional', 'notunique']))
for uri in messages: for uri in messages:
message_def = get_message(uri, message_def = get_message(uri,
current_module_names, current_module_names,