feat(chalice): changed recordings response
This commit is contained in:
parent
93737f4ebb
commit
753d1cac11
3 changed files with 21 additions and 14 deletions
|
|
@ -19,7 +19,7 @@ def random_string(length=36):
|
|||
return "".join(random.choices(string.hexdigits, k=length))
|
||||
|
||||
|
||||
def list_to_camel_case(items, flatten=False):
|
||||
def list_to_camel_case(items: list[dict], flatten: bool = False) -> list[dict]:
|
||||
for i in range(len(items)):
|
||||
if flatten:
|
||||
items[i] = flatten_nested_dicts(items[i])
|
||||
|
|
|
|||
|
|
@ -38,18 +38,19 @@ def save_record(project_id, data: schemas_ee.AssistRecordSavePayloadSchema, cont
|
|||
return result
|
||||
|
||||
|
||||
def search_records(project_id, data: schemas_ee.AssistRecordSearchPayloadSchema, context: schemas_ee.CurrentContext):
|
||||
def search_records(project_id: int, data: schemas_ee.AssistRecordSearchPayloadSchema,
|
||||
context: schemas_ee.CurrentContext):
|
||||
conditions = ["projects.tenant_id=%(tenant_id)s",
|
||||
"projects.deleted_at ISNULL",
|
||||
"projects.project_id=%(project_id)s",
|
||||
"assist_records.deleted_at ISNULL"]
|
||||
if data.startDate:
|
||||
if data.startTimestamp:
|
||||
conditions.append("assist_records.created_at>=%(startDate)s")
|
||||
if data.endDate:
|
||||
if data.endTimestamp:
|
||||
conditions.append("assist_records.created_at<=%(endDate)s")
|
||||
|
||||
params = {"tenant_id": context.tenant_id, "project_id": project_id,
|
||||
"startDate": data.startDate, "endDate": data.endDate,
|
||||
"startDate": data.startTimestamp, "endDate": data.endTimestamp,
|
||||
"p_start": (data.page - 1) * data.limit, "p_limit": data.limit,
|
||||
**data.dict()}
|
||||
if data.user_id is not None:
|
||||
|
|
@ -59,17 +60,26 @@ def search_records(project_id, data: schemas_ee.AssistRecordSearchPayloadSchema,
|
|||
params["query"] = helper.values_for_operator(value=data.query,
|
||||
op=schemas.SearchEventOperator._contains)
|
||||
with pg_client.PostgresClient() as cur:
|
||||
query = cur.mogrify(f"""SELECT record_id, user_id, session_id, assist_records.created_at,
|
||||
assist_records.name, duration, users.name AS created_by
|
||||
query = cur.mogrify(f"""SELECT COUNT(assist_records.record_id) OVER () AS count,
|
||||
record_id, user_id, session_id, assist_records.created_at,
|
||||
assist_records.name, duration, users.name AS created_by
|
||||
FROM assist_records
|
||||
INNER JOIN projects USING (project_id)
|
||||
LEFT JOIN users USING (user_id)
|
||||
INNER JOIN projects USING (project_id)
|
||||
LEFT JOIN users USING (user_id)
|
||||
WHERE {" AND ".join(conditions)}
|
||||
ORDER BY assist_records.created_at {data.order}
|
||||
LIMIT %(p_limit)s OFFSET %(p_start)s;""",
|
||||
params)
|
||||
cur.execute(query)
|
||||
results = helper.list_to_camel_case(cur.fetchall())
|
||||
rows = helper.list_to_camel_case(cur.fetchall())
|
||||
if len(rows) == 0:
|
||||
return {"count": 0, "records": []}
|
||||
|
||||
results = {"count": rows[0]["count"]}
|
||||
for r in rows:
|
||||
r.pop("count")
|
||||
results["records"] = results
|
||||
|
||||
return results
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -135,10 +135,7 @@ class AssistRecordSavePayloadSchema(AssistRecordPayloadSchema):
|
|||
key: str = Field(...)
|
||||
|
||||
|
||||
class AssistRecordSearchPayloadSchema(schemas._PaginatedSchema):
|
||||
limit: int = Field(default=200, gt=0)
|
||||
startDate: Optional[int] = Field(default=None)
|
||||
endDate: Optional[int] = Field(default=None)
|
||||
class AssistRecordSearchPayloadSchema(schemas._PaginatedSchema, schemas._TimedSchema):
|
||||
user_id: Optional[int] = Field(default=None)
|
||||
query: Optional[str] = Field(default=None)
|
||||
order: Literal["asc", "desc"] = Field(default="desc")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue