1. added the ability to filter on level and user.

2. added a total to the response, so you can see what the total count of users is.
3. adding user_uid to the task_log table.
This commit is contained in:
Dan 2022-01-12 08:58:50 -05:00
parent 7176452fe3
commit d346589e07
4 changed files with 31 additions and 8 deletions

View File

@ -2078,6 +2078,12 @@ components:
code:
example: "email_sent"
type: string
level:
example: "warning"
type: string
user:
example: "email_sent"
type: string
page:
type: integer
example: 0
@ -2094,6 +2100,12 @@ components:
type: array
items:
$ref: "#/components/schemas/TaskLog"
has_next:
type: boolean
example: true
has_previous:
type: boolean
example: false
TaskLog:
properties:
level:

View File

@ -43,14 +43,19 @@ class TaskLogModelSchema(ma.Schema):
class TaskLogQuery:
"""Encapsulates the paginated queries and results when retrieving and filtering task logs over the
API"""
def __init__(self, code=None, page=1, per_page=10, sort_column=None, sort_reverse=False, items=None):
self.code = code # Filter down to just this code.
def __init__(self, code="", level="", user="", page=1, per_page=10,
sort_column=None, sort_reverse=False, items=None,
pages=0, total=0, has_next=False, has_prev=False):
self.code = code # Filter on code.
self.level = level # Filter on level.
self.user = user # Filter on user.
self.page = page
self.per_page = per_page
self.sort_column = sort_column
self.sort_reverse = sort_reverse
self.items = items
self.pages = 0
self.total = total
self.pages = pages
self.has_next = False
self.has_prev = False
@ -62,11 +67,12 @@ class TaskLogQuery:
self.pages = paginator.pages
self.has_next = paginator.has_next
self.has_prev = paginator.has_prev
self.total = paginator.total
class TaskLogQuerySchema(ma.Schema):
class Meta:
model = TaskLogModel
fields = ["code", "page", "per_page", "sort_column", "sort_reverse", "items", "pages",
"has_next", "has_previous"]
fields = ["code", "level", "user",
"page", "per_page", "sort_column", "sort_reverse", "items", "pages", "total",
"has_next", "has_prev"]
items = marshmallow.fields.List(marshmallow.fields.Nested(TaskLogModelSchema))

View File

@ -68,8 +68,12 @@ class TaskLoggingService(object):
if task_log_query.sort_column is None:
task_log_query.sort_column = "timestamp"
task_log_query.sort_reverse = True
if task_log_query.code is not None:
sql_query = sql_query.filter(TaskLogModel.code == task_log_query.code)
if task_log_query.code:
sql_query = sql_query.filter(TaskLogModel.code.like(task_log_query.code + "%"))
if task_log_query.level:
sql_query = sql_query.filter(TaskLogModel.level.like(task_log_query.level + "%"))
if task_log_query.user:
sql_query = sql_query.filter(TaskLogModel.user_uid.like(task_log_query.user + "%"))
if task_log_query.sort_reverse:
sort_column = desc(task_log_query.sort_column)
else:

View File

@ -134,6 +134,7 @@ class TestTaskLogging(BaseTest):
self.assertEqual(40, len(results.items), "There should be 40 logs total")
logs = TaskLoggingService.get_logs_for_study(study.id, TaskLogQuery(per_page=5))
self.assertEqual(40, logs.total)
self.assertEqual(5, len(logs.items), "I can limit results to 5")
self.assertEqual(1, logs.page)
self.assertEqual(8, logs.pages)