feat(api): search live sessions
This commit is contained in:
parent
971dbd40a4
commit
a2ec909ace
3 changed files with 52 additions and 12 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import requests
|
||||
from decouple import config
|
||||
|
||||
import schemas
|
||||
from chalicelib.core import projects
|
||||
|
||||
SESSION_PROJECTION_COLS = """s.project_id,
|
||||
|
|
@ -19,14 +20,29 @@ SESSION_PROJECTION_COLS = """s.project_id,
|
|||
"""
|
||||
|
||||
|
||||
def get_live_sessions_ws(project_id, user_id=None):
|
||||
def get_live_sessions_ws_user_id(project_id, user_id):
|
||||
data = {
|
||||
"filter": {"userId": user_id}
|
||||
}
|
||||
return __get_live_sessions_ws(project_id=project_id, data=data)
|
||||
|
||||
|
||||
def get_live_sessions_ws(project_id, body: schemas.LiveSessionsSearchPayloadSchema):
|
||||
data = {
|
||||
"filter": {},
|
||||
"pagination": {"limit": body.limit, "page": body.page},
|
||||
"sort": {"key": body.sort, "order": body.order}
|
||||
}
|
||||
for f in body.filters:
|
||||
data["filter"][f.type] = f.value
|
||||
return __get_live_sessions_ws(project_id=project_id, data=data)
|
||||
|
||||
|
||||
def __get_live_sessions_ws(project_id, data):
|
||||
project_key = projects.get_project_key(project_id)
|
||||
params = {}
|
||||
if user_id and len(user_id) > 0:
|
||||
params["userId"] = user_id
|
||||
try:
|
||||
connected_peers = requests.get(config("assist") % config("S3_KEY") + f"/{project_key}", params,
|
||||
timeout=config("assistTimeout", cast=int, default=5))
|
||||
connected_peers = requests.post(config("assist") % config("S3_KEY") + f"/{project_key}", json=data,
|
||||
timeout=config("assistTimeout", cast=int, default=5))
|
||||
if connected_peers.status_code != 200:
|
||||
print("!! issue with the peer-server")
|
||||
print(connected_peers.text)
|
||||
|
|
@ -53,7 +69,7 @@ def get_live_sessions_ws(project_id, user_id=None):
|
|||
|
||||
|
||||
def get_live_session_by_id(project_id, session_id):
|
||||
all_live = get_live_sessions_ws(project_id)
|
||||
all_live = __get_live_sessions_ws(project_id, data={"filter": {"sessionId": session_id}})
|
||||
for l in all_live:
|
||||
if str(l.get("sessionID")) == str(session_id):
|
||||
return l
|
||||
|
|
@ -64,8 +80,9 @@ def is_live(project_id, session_id, project_key=None):
|
|||
if project_key is None:
|
||||
project_key = projects.get_project_key(project_id)
|
||||
try:
|
||||
connected_peers = requests.get(config("assistList") % config("S3_KEY") + f"/{project_key}",
|
||||
timeout=config("assistTimeout", cast=int, default=5))
|
||||
connected_peers = requests.post(config("assistList") % config("S3_KEY") + f"/{project_key}",
|
||||
json={"filter": {"sessionId": session_id}},
|
||||
timeout=config("assistTimeout", cast=int, default=5))
|
||||
if connected_peers.status_code != 200:
|
||||
print("!! issue with the peer-server")
|
||||
print(connected_peers.text)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Union
|
||||
from typing import Union, Optional
|
||||
|
||||
from decouple import config
|
||||
from fastapi import Depends, Body, BackgroundTasks, HTTPException
|
||||
|
|
@ -773,7 +773,7 @@ def get_funnel_sessions_on_the_fly(projectId: int, funnelId: int, data: schemas.
|
|||
|
||||
@app.get('/{projectId}/funnels/issues/{issueId}/sessions', tags=["funnels"])
|
||||
def get_funnel_issue_sessions(projectId: int, issueId: str, startDate: int = None, endDate: int = None,
|
||||
context: schemas.CurrentContext = Depends(OR_context)):
|
||||
context: schemas.CurrentContext = Depends(OR_context)):
|
||||
issue = issues.get(project_id=projectId, issue_id=issueId)
|
||||
if issue is None:
|
||||
return {"errors": ["issue not found"]}
|
||||
|
|
@ -859,7 +859,14 @@ def all_issue_types(context: schemas.CurrentContext = Depends(OR_context)):
|
|||
|
||||
@app.get('/{projectId}/assist/sessions', tags=["assist"])
|
||||
def sessions_live(projectId: int, userId: str = None, context: schemas.CurrentContext = Depends(OR_context)):
|
||||
data = assist.get_live_sessions_ws(projectId, user_id=userId)
|
||||
data = assist.get_live_sessions_ws_user_id(projectId, user_id=userId)
|
||||
return {'data': data}
|
||||
|
||||
|
||||
@app.post('/{projectId}/assist/sessions', tags=["assist"])
|
||||
def sessions_live(projectId: int, data: schemas.LiveSessionsSearchPayloadSchema = Body(...),
|
||||
context: schemas.CurrentContext = Depends(OR_context)):
|
||||
data = assist.get_live_sessions_ws(projectId, body=data)
|
||||
return {'data': data}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1008,3 +1008,19 @@ class CustomMetricAndTemplate(BaseModel):
|
|||
|
||||
class Config:
|
||||
alias_generator = attribute_to_camel_case
|
||||
|
||||
|
||||
class LiveSessionsSearchPayloadSchema(_PaginatedSchema):
|
||||
filters: List[SessionSearchFilterSchema] = Field([])
|
||||
sort: str = Field(default="startTs")
|
||||
order: SortOrderType = Field(default=SortOrderType.desc)
|
||||
group_by_user: bool = Field(default=False)
|
||||
|
||||
@root_validator(pre=True)
|
||||
def transform_order(cls, values):
|
||||
if values.get("order") is not None:
|
||||
values["order"] = values["order"].upper()
|
||||
return values
|
||||
|
||||
class Config:
|
||||
alias_generator = attribute_to_camel_case
|
||||
Loading…
Add table
Reference in a new issue