feat(chalice): get card's data variations
feat(chalice): get the shortest session with minimum duration
This commit is contained in:
parent
930033dda4
commit
8928ca0cbc
3 changed files with 36 additions and 22 deletions
|
|
@ -1,6 +1,9 @@
|
||||||
import json
|
import json
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
|
from fastapi import HTTPException
|
||||||
|
from starlette import status
|
||||||
|
|
||||||
import schemas
|
import schemas
|
||||||
from chalicelib.core import sessions, funnels, errors, issues, metrics, click_maps
|
from chalicelib.core import sessions, funnels, errors, issues, metrics, click_maps
|
||||||
from chalicelib.utils import helper, pg_client
|
from chalicelib.utils import helper, pg_client
|
||||||
|
|
@ -104,7 +107,7 @@ def __is_click_map(data: schemas.CreateCardSchema):
|
||||||
|
|
||||||
def __get_click_map_chat(project_id, user_id, data: schemas.CreateCardSchema):
|
def __get_click_map_chat(project_id, user_id, data: schemas.CreateCardSchema):
|
||||||
if len(data.series) == 0:
|
if len(data.series) == 0:
|
||||||
return None
|
return {}
|
||||||
data.series[0].filter.startDate = data.startTimestamp
|
data.series[0].filter.startDate = data.startTimestamp
|
||||||
data.series[0].filter.endDate = data.endTimestamp
|
data.series[0].filter.endDate = data.endTimestamp
|
||||||
return click_maps.search_short_session(project_id=project_id, user_id=user_id,
|
return click_maps.search_short_session(project_id=project_id, user_id=user_id,
|
||||||
|
|
@ -134,12 +137,12 @@ def merged_live(project_id, data: schemas.CreateCardSchema, user_id=None):
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def __merge_metric_with_data(metric, data: Union[schemas.CardChartSchema,
|
def __merge_metric_with_data(metric: schemas.CreateCardSchema,
|
||||||
schemas.CardSessionsSchema]) \
|
data: schemas.CardChartSchema) -> schemas.CreateCardSchema:
|
||||||
-> Union[schemas.CreateCardSchema, None]:
|
|
||||||
if data.series is not None and len(data.series) > 0:
|
if data.series is not None and len(data.series) > 0:
|
||||||
metric["series"] = data.series
|
metric.series = data.series
|
||||||
metric: schemas.CreateCardSchema = schemas.CreateCardSchema(**{**data.dict(), **metric})
|
metric: schemas.CreateCardSchema = schemas.CreateCardSchema(
|
||||||
|
**{**data.dict(by_alias=True), **metric.dict(by_alias=True)})
|
||||||
if len(data.filters) > 0 or len(data.events) > 0:
|
if len(data.filters) > 0 or len(data.events) > 0:
|
||||||
for s in metric.series:
|
for s in metric.series:
|
||||||
if len(data.filters) > 0:
|
if len(data.filters) > 0:
|
||||||
|
|
@ -149,7 +152,7 @@ schemas.CardSessionsSchema]) \
|
||||||
return metric
|
return metric
|
||||||
|
|
||||||
|
|
||||||
def make_chart(project_id, user_id, metric_id, data: schemas.CardChartSchema, metric=None):
|
def make_chart(project_id, user_id, metric_id, data: schemas.CardChartSchema, metric: schemas.CreateCardSchema = None):
|
||||||
if metric is None:
|
if metric is None:
|
||||||
metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
||||||
if metric is None:
|
if metric is None:
|
||||||
|
|
@ -160,9 +163,10 @@ def make_chart(project_id, user_id, metric_id, data: schemas.CardChartSchema, me
|
||||||
|
|
||||||
|
|
||||||
def get_sessions(project_id, user_id, metric_id, data: schemas.CardSessionsSchema):
|
def get_sessions(project_id, user_id, metric_id, data: schemas.CardSessionsSchema):
|
||||||
metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
raw_metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
||||||
if metric is None:
|
if raw_metric is None:
|
||||||
return None
|
return None
|
||||||
|
metric: schemas.CreateCardSchema = schemas.CreateCardSchema(**raw_metric)
|
||||||
metric: schemas.CreateCardSchema = __merge_metric_with_data(metric=metric, data=data)
|
metric: schemas.CreateCardSchema = __merge_metric_with_data(metric=metric, data=data)
|
||||||
if metric is None:
|
if metric is None:
|
||||||
return None
|
return None
|
||||||
|
|
@ -179,9 +183,10 @@ def get_sessions(project_id, user_id, metric_id, data: schemas.CardSessionsSchem
|
||||||
|
|
||||||
|
|
||||||
def get_funnel_issues(project_id, user_id, metric_id, data: schemas.CardSessionsSchema):
|
def get_funnel_issues(project_id, user_id, metric_id, data: schemas.CardSessionsSchema):
|
||||||
metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
raw_metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
||||||
if metric is None:
|
if raw_metric is None:
|
||||||
return None
|
return None
|
||||||
|
metric: schemas.CreateCardSchema = schemas.CreateCardSchema(**raw_metric)
|
||||||
metric: schemas.CreateCardSchema = __merge_metric_with_data(metric=metric, data=data)
|
metric: schemas.CreateCardSchema = __merge_metric_with_data(metric=metric, data=data)
|
||||||
if metric is None:
|
if metric is None:
|
||||||
return None
|
return None
|
||||||
|
|
@ -195,9 +200,10 @@ def get_funnel_issues(project_id, user_id, metric_id, data: schemas.CardSessions
|
||||||
|
|
||||||
|
|
||||||
def get_errors_list(project_id, user_id, metric_id, data: schemas.CardSessionsSchema):
|
def get_errors_list(project_id, user_id, metric_id, data: schemas.CardSessionsSchema):
|
||||||
metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
raw_metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
||||||
if metric is None:
|
if raw_metric is None:
|
||||||
return None
|
return None
|
||||||
|
metric: schemas.CreateCardSchema = schemas.CreateCardSchema(**raw_metric)
|
||||||
metric: schemas.CreateCardSchema = __merge_metric_with_data(metric=metric, data=data)
|
metric: schemas.CreateCardSchema = __merge_metric_with_data(metric=metric, data=data)
|
||||||
if metric is None:
|
if metric is None:
|
||||||
return None
|
return None
|
||||||
|
|
@ -584,12 +590,12 @@ def make_chart_from_card(project_id, user_id, metric_id, data: schemas.CardChart
|
||||||
raw_metric = get_with_template(metric_id=metric_id, project_id=project_id, user_id=user_id,
|
raw_metric = get_with_template(metric_id=metric_id, project_id=project_id, user_id=user_id,
|
||||||
include_dashboard=False)
|
include_dashboard=False)
|
||||||
if raw_metric is None:
|
if raw_metric is None:
|
||||||
return None
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="card not found")
|
||||||
metric: schemas.CreateCardSchema = schemas.CreateCardSchema(**raw_metric)
|
metric: schemas.CreateCardSchema = schemas.CreateCardSchema(**raw_metric)
|
||||||
if metric.is_template:
|
if metric.is_template:
|
||||||
return get_predefined_metric(key=metric.metric_of, project_id=project_id, data=data.dict())
|
return get_predefined_metric(key=metric.metric_of, project_id=project_id, data=data.dict())
|
||||||
else:
|
else:
|
||||||
return make_chart(project_id=project_id, user_id=user_id, metric_id=metric_id, data=data, metric=raw_metric)
|
return make_chart(project_id=project_id, user_id=user_id, metric_id=metric_id, data=data, metric=metric)
|
||||||
|
|
||||||
|
|
||||||
PREDEFINED = {schemas.MetricOfWebVitals.count_sessions: metrics.get_processed_sessions,
|
PREDEFINED = {schemas.MetricOfWebVitals.count_sessions: metrics.get_processed_sessions,
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ def try_card_sessions(projectId: int, data: schemas.CardSessionsSchema = Body(..
|
||||||
return {"data": data}
|
return {"data": data}
|
||||||
|
|
||||||
|
|
||||||
@app.post('/{projectId}/card/try/issues', tags=["cards"])
|
@app.post('/{projectId}/cards/try/issues', tags=["cards"])
|
||||||
@app.post('/{projectId}/metrics/try/issues', tags=["dashboard"])
|
@app.post('/{projectId}/metrics/try/issues', tags=["dashboard"])
|
||||||
@app.post('/{projectId}/custom_metrics/try/issues', tags=["customMetrics"])
|
@app.post('/{projectId}/custom_metrics/try/issues', tags=["customMetrics"])
|
||||||
def try_card_funnel_issues(projectId: int, data: schemas.CardSessionsSchema = Body(...),
|
def try_card_funnel_issues(projectId: int, data: schemas.CardSessionsSchema = Body(...),
|
||||||
|
|
@ -186,9 +186,12 @@ def get_card_sessions(projectId: int, metric_id: int,
|
||||||
@app.post('/{projectId}/cards/{metric_id}/issues', tags=["cards"])
|
@app.post('/{projectId}/cards/{metric_id}/issues', tags=["cards"])
|
||||||
@app.post('/{projectId}/metrics/{metric_id}/issues', tags=["dashboard"])
|
@app.post('/{projectId}/metrics/{metric_id}/issues', tags=["dashboard"])
|
||||||
@app.post('/{projectId}/custom_metrics/{metric_id}/issues', tags=["customMetrics"])
|
@app.post('/{projectId}/custom_metrics/{metric_id}/issues', tags=["customMetrics"])
|
||||||
def get_card_funnel_issues(projectId: int, metric_id: int,
|
def get_card_funnel_issues(projectId: int, metric_id: Union[int, str],
|
||||||
data: schemas.CardSessionsSchema = Body(...),
|
data: schemas.CardSessionsSchema = Body(...),
|
||||||
context: schemas.CurrentContext = Depends(OR_context)):
|
context: schemas.CurrentContext = Depends(OR_context)):
|
||||||
|
if not isinstance(metric_id, int):
|
||||||
|
return {"errors": [f"invalid card_id: {metric_id}"]}
|
||||||
|
|
||||||
data = custom_metrics.get_funnel_issues(project_id=projectId, user_id=context.user_id, metric_id=metric_id,
|
data = custom_metrics.get_funnel_issues(project_id=projectId, user_id=context.user_id, metric_id=metric_id,
|
||||||
data=data)
|
data=data)
|
||||||
if data is None:
|
if data is None:
|
||||||
|
|
@ -227,8 +230,6 @@ def get_card_chart(projectId: int, metric_id: int, data: schemas.CardChartSchema
|
||||||
context: schemas.CurrentContext = Depends(OR_context)):
|
context: schemas.CurrentContext = Depends(OR_context)):
|
||||||
data = custom_metrics.make_chart_from_card(project_id=projectId, user_id=context.user_id, metric_id=metric_id,
|
data = custom_metrics.make_chart_from_card(project_id=projectId, user_id=context.user_id, metric_id=metric_id,
|
||||||
data=data)
|
data=data)
|
||||||
if data is None:
|
|
||||||
return {"errors": ["custom metric not found"]}
|
|
||||||
return {"data": data}
|
return {"data": data}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -912,9 +912,6 @@ class CardSessionsSchema(FlatSessionsSearch, _PaginatedSchema):
|
||||||
class CardChartSchema(CardSessionsSchema):
|
class CardChartSchema(CardSessionsSchema):
|
||||||
density: int = Field(7)
|
density: int = Field(7)
|
||||||
|
|
||||||
class Config:
|
|
||||||
alias_generator = attribute_to_camel_case
|
|
||||||
|
|
||||||
|
|
||||||
class CardConfigSchema(BaseModel):
|
class CardConfigSchema(BaseModel):
|
||||||
col: Optional[int] = Field(...)
|
col: Optional[int] = Field(...)
|
||||||
|
|
@ -1200,6 +1197,16 @@ class FlatClickMapSessionsSearch(SessionsSearchPayloadSchema):
|
||||||
events: Optional[List[_ClickMapSearchEventRaw]] = Field([])
|
events: Optional[List[_ClickMapSearchEventRaw]] = Field([])
|
||||||
filters: List[Union[SessionSearchFilterSchema, _ClickMapSearchEventRaw]] = Field([])
|
filters: List[Union[SessionSearchFilterSchema, _ClickMapSearchEventRaw]] = Field([])
|
||||||
|
|
||||||
|
@root_validator(pre=True)
|
||||||
|
def transform(cls, values):
|
||||||
|
for f in values.get("filters"):
|
||||||
|
if f.get("type") == FilterType.duration:
|
||||||
|
return values
|
||||||
|
values["filters"] = values.get("filters", [])
|
||||||
|
values["filters"].append({"value": [5000], "type": FilterType.duration,
|
||||||
|
"operator": SearchEventOperator._is, "filters": []})
|
||||||
|
return values
|
||||||
|
|
||||||
@root_validator()
|
@root_validator()
|
||||||
def flat_to_original(cls, values):
|
def flat_to_original(cls, values):
|
||||||
n_filters = []
|
n_filters = []
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue