feat(chalice): save clickMap session data
This commit is contained in:
parent
7e34bec76f
commit
6a24d04ff2
7 changed files with 58 additions and 67 deletions
|
|
@ -27,7 +27,7 @@ COALESCE((SELECT TRUE
|
|||
AND fs.user_id = %(userId)s LIMIT 1), FALSE) AS viewed """
|
||||
|
||||
|
||||
def search_short_session(data: schemas.FlatClickMapSessionsSearch, project_id, user_id):
|
||||
def search_short_session(data: schemas.FlatClickMapSessionsSearch, project_id, user_id, include_mobs:bool=True):
|
||||
no_platform = True
|
||||
for f in data.filters:
|
||||
if f.type == schemas.FilterType.platform:
|
||||
|
|
@ -68,8 +68,9 @@ def search_short_session(data: schemas.FlatClickMapSessionsSearch, project_id, u
|
|||
|
||||
session = cur.fetchone()
|
||||
if session:
|
||||
session['domURL'] = sessions_mobs.get_urls(session_id=session["session_id"], project_id=project_id)
|
||||
session['mobsUrl'] = sessions_mobs.get_urls_depercated(session_id=session["session_id"])
|
||||
if include_mobs:
|
||||
session['domURL'] = sessions_mobs.get_urls(session_id=session["session_id"], project_id=project_id)
|
||||
session['mobsUrl'] = sessions_mobs.get_urls_depercated(session_id=session["session_id"])
|
||||
session['events'] = events.get_by_session_id(project_id=project_id, session_id=session["session_id"],
|
||||
event_type=schemas.EventType.location)
|
||||
|
||||
|
|
|
|||
|
|
@ -105,13 +105,14 @@ def __is_click_map(data: schemas.CreateCardSchema):
|
|||
return data.metric_type == schemas.MetricType.click_map
|
||||
|
||||
|
||||
def __get_click_map_chart(project_id, user_id, data: schemas.CreateCardSchema):
|
||||
def __get_click_map_chart(project_id, user_id, data: schemas.CreateCardSchema, include_mobs: bool = True):
|
||||
if len(data.series) == 0:
|
||||
return None
|
||||
data.series[0].filter.startDate = data.startTimestamp
|
||||
data.series[0].filter.endDate = data.endTimestamp
|
||||
return click_maps.search_short_session(project_id=project_id, user_id=user_id,
|
||||
data=schemas.FlatClickMapSessionsSearch(**data.series[0].filter.dict()))
|
||||
data=schemas.FlatClickMapSessionsSearch(**data.series[0].filter.dict()),
|
||||
include_mobs=include_mobs)
|
||||
|
||||
|
||||
def merged_live(project_id, data: schemas.CreateCardSchema, user_id=None):
|
||||
|
|
@ -153,7 +154,8 @@ def __merge_metric_with_data(metric: schemas.CreateCardSchema,
|
|||
return metric
|
||||
|
||||
|
||||
def make_chart(project_id, user_id, metric_id, data: schemas.CardChartSchema, metric: schemas.CreateCardSchema = None):
|
||||
def make_chart(project_id, user_id, metric_id, data: schemas.CardChartSchema,
|
||||
metric: schemas.CreateCardSchema = None):
|
||||
if metric is None:
|
||||
metric = get_card(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
||||
if metric is None:
|
||||
|
|
@ -164,7 +166,7 @@ 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):
|
||||
raw_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, include_data=True)
|
||||
if raw_metric is None:
|
||||
return None
|
||||
metric: schemas.CreateCardSchema = schemas.CreateCardSchema(**raw_metric)
|
||||
|
|
@ -172,11 +174,18 @@ def get_sessions(project_id, user_id, metric_id, data: schemas.CardSessionsSchem
|
|||
if metric is None:
|
||||
return None
|
||||
results = []
|
||||
is_click_map = False
|
||||
if __is_click_map(metric) and raw_metric.get("data") is not None:
|
||||
is_click_map = True
|
||||
for s in metric.series:
|
||||
s.filter.startDate = data.startTimestamp
|
||||
s.filter.endDate = data.endTimestamp
|
||||
s.filter.limit = data.limit
|
||||
s.filter.page = data.page
|
||||
if is_click_map:
|
||||
results.append(
|
||||
{"seriesId": s.series_id, "seriesName": s.name, "total": 1, "sessions": [raw_metric["data"]]})
|
||||
break
|
||||
results.append({"seriesId": s.series_id, "seriesName": s.name,
|
||||
**sessions.search_sessions(data=s.filter, project_id=project_id, user_id=user_id)})
|
||||
|
||||
|
|
@ -234,7 +243,11 @@ def try_sessions(project_id, user_id, data: schemas.CardSessionsSchema):
|
|||
|
||||
def create(project_id, user_id, data: schemas.CreateCardSchema, dashboard=False):
|
||||
with pg_client.PostgresClient() as cur:
|
||||
_data = {}
|
||||
session_data = None
|
||||
if __is_click_map(data):
|
||||
session_data = json.dumps(__get_click_map_chart(project_id=project_id, user_id=user_id,
|
||||
data=data, include_mobs=False))
|
||||
_data = {"session_data": session_data}
|
||||
for i, s in enumerate(data.series):
|
||||
for k in s.dict().keys():
|
||||
_data[f"{k}_{i}"] = s.__getattribute__(k)
|
||||
|
|
@ -245,10 +258,10 @@ def create(project_id, user_id, data: schemas.CreateCardSchema, dashboard=False)
|
|||
params["default_config"] = json.dumps(data.default_config.dict())
|
||||
query = """INSERT INTO metrics (project_id, user_id, name, is_public,
|
||||
view_type, metric_type, metric_of, metric_value,
|
||||
metric_format, default_config, thumbnail)
|
||||
metric_format, default_config, thumbnail, data)
|
||||
VALUES (%(project_id)s, %(user_id)s, %(name)s, %(is_public)s,
|
||||
%(view_type)s, %(metric_type)s, %(metric_of)s, %(metric_value)s,
|
||||
%(metric_format)s, %(default_config)s, %(thumbnail)s)
|
||||
%(metric_format)s, %(default_config)s, %(thumbnail)s, %(session_data)s)
|
||||
RETURNING metric_id"""
|
||||
if len(data.series) > 0:
|
||||
query = f"""WITH m AS ({query})
|
||||
|
|
@ -440,10 +453,13 @@ def delete(project_id, metric_id, user_id):
|
|||
return {"state": "success"}
|
||||
|
||||
|
||||
def get_card(metric_id, project_id, user_id, flatten=True):
|
||||
def get_card(metric_id, project_id, user_id, flatten: bool = True, include_data: bool = False):
|
||||
with pg_client.PostgresClient() as cur:
|
||||
query = cur.mogrify(
|
||||
"""SELECT *, default_config AS config
|
||||
f"""SELECT metric_id, project_id, user_id, name, is_public, created_at, deleted_at, edited_at, metric_type,
|
||||
view_type, metric_of, metric_value, metric_format, is_pinned, predefined_key, default_config,
|
||||
thumbnail, default_config AS config,
|
||||
series, dashboards, owner_email {',data' if include_data else ''}
|
||||
FROM metrics
|
||||
LEFT JOIN LATERAL (SELECT COALESCE(jsonb_agg(metric_series.* ORDER BY index),'[]'::jsonb) AS series
|
||||
FROM metric_series
|
||||
|
|
@ -494,7 +510,10 @@ def get_with_template(metric_id, project_id, user_id, include_dashboard=True):
|
|||
AND ((user_id = %(user_id)s OR is_public))) AS connected_dashboards
|
||||
) AS connected_dashboards ON (TRUE)"""
|
||||
query = cur.mogrify(
|
||||
f"""SELECT *, default_config AS config
|
||||
f"""SELECT metric_id, project_id, user_id, name, is_public, created_at, deleted_at, edited_at, metric_type,
|
||||
view_type, metric_of, metric_value, metric_format, is_pinned, predefined_key, default_config,
|
||||
thumbnail, default_config AS config,
|
||||
series
|
||||
FROM metrics
|
||||
LEFT JOIN LATERAL (SELECT COALESCE(jsonb_agg(metric_series.* ORDER BY index),'[]'::jsonb) AS series
|
||||
FROM metric_series
|
||||
|
|
@ -651,27 +670,3 @@ PREDEFINED = {schemas.MetricOfWebVitals.count_sessions: metrics.get_processed_se
|
|||
def get_predefined_metric(key: Union[schemas.MetricOfWebVitals, schemas.MetricOfErrors, \
|
||||
schemas.MetricOfPerformance, schemas.MetricOfResources], project_id: int, data: dict):
|
||||
return PREDEFINED.get(key, lambda *args: None)(project_id=project_id, **data)
|
||||
|
||||
# def add_thumbnail(metric_id, user_id, project_id):
|
||||
# key = generate_file_key(project_id=project_id, key=f"{metric_id}.png")
|
||||
# params = {"metric_id": metric_id, "user_id": user_id, "project_id": project_id, "key": key}
|
||||
# with pg_client.PostgresClient() as cur:
|
||||
# query = cur.mogrify(f"""\
|
||||
# UPDATE metrics
|
||||
# SET thumbnail_url = %(key)s
|
||||
# WHERE metric_id = %(metric_id)s
|
||||
# AND project_id = %(project_id)s
|
||||
# AND (user_id = %(user_id)s OR is_public)
|
||||
# RETURNING metric_id;""", params)
|
||||
# cur.execute(query)
|
||||
# row = cur.fetchone()
|
||||
# if row is None:
|
||||
# return {"errors": ["Card not found"]}
|
||||
# return {"data": s3.get_presigned_url_for_upload(bucket=config('THUMBNAILS_BUCKET'), expires_in=180, key=key,
|
||||
# # content-length-range is in bytes
|
||||
# conditions=["content-length-range", 1, 1 * 1024 * 1024],
|
||||
# content_type="image/png")}
|
||||
#
|
||||
#
|
||||
# def generate_file_key(project_id, key):
|
||||
# return f"{project_id}/cards/{key}"
|
||||
|
|
|
|||
|
|
@ -195,7 +195,7 @@ def make_chart(project_id, user_id, metric_id, data: schemas.CardChartSchema,
|
|||
|
||||
|
||||
def get_sessions(project_id, user_id, metric_id, data: schemas.CardSessionsSchema):
|
||||
raw_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, include_data=True)
|
||||
if raw_metric is None:
|
||||
return None
|
||||
metric: schemas_ee.CreateCardSchema = schemas_ee.CreateCardSchema(**raw_metric)
|
||||
|
|
@ -203,11 +203,18 @@ def get_sessions(project_id, user_id, metric_id, data: schemas.CardSessionsSchem
|
|||
if metric is None:
|
||||
return None
|
||||
results = []
|
||||
is_click_map = False
|
||||
if __is_click_map(metric) and raw_metric.get("data") is not None:
|
||||
is_click_map = True
|
||||
for s in metric.series:
|
||||
s.filter.startDate = data.startTimestamp
|
||||
s.filter.endDate = data.endTimestamp
|
||||
s.filter.limit = data.limit
|
||||
s.filter.page = data.page
|
||||
if is_click_map:
|
||||
results.append(
|
||||
{"seriesId": s.series_id, "seriesName": s.name, "total": 1, "sessions": [raw_metric["data"]]})
|
||||
break
|
||||
results.append({"seriesId": s.series_id, "seriesName": s.name,
|
||||
**sessions.search_sessions(data=s.filter, project_id=project_id, user_id=user_id)})
|
||||
|
||||
|
|
@ -471,10 +478,13 @@ def delete(project_id, metric_id, user_id):
|
|||
return {"state": "success"}
|
||||
|
||||
|
||||
def get_card(metric_id, project_id, user_id, flatten=True):
|
||||
def get_card(metric_id, project_id, user_id, flatten=True, include_data: bool = False):
|
||||
with pg_client.PostgresClient() as cur:
|
||||
query = cur.mogrify(
|
||||
"""SELECT *, default_config AS config
|
||||
f"""SELECT metric_id, project_id, user_id, name, is_public, created_at, deleted_at, edited_at, metric_type,
|
||||
view_type, metric_of, metric_value, metric_format, is_pinned, predefined_key, default_config,
|
||||
thumbnail, default_config AS config,
|
||||
series, dashboards, owner_email, {',data' if include_data else ''}
|
||||
FROM metrics
|
||||
LEFT JOIN LATERAL (SELECT COALESCE(jsonb_agg(metric_series.* ORDER BY index),'[]'::jsonb) AS series
|
||||
FROM metric_series
|
||||
|
|
@ -525,7 +535,10 @@ def get_with_template(metric_id, project_id, user_id, include_dashboard=True):
|
|||
AND ((user_id = %(user_id)s OR is_public))) AS connected_dashboards
|
||||
) AS connected_dashboards ON (TRUE)"""
|
||||
query = cur.mogrify(
|
||||
f"""SELECT *, default_config AS config
|
||||
f"""SELECT metric_id, project_id, user_id, name, is_public, created_at, deleted_at, edited_at, metric_type,
|
||||
view_type, metric_of, metric_value, metric_format, is_pinned, predefined_key, default_config,
|
||||
thumbnail, default_config AS config,
|
||||
series
|
||||
FROM metrics
|
||||
LEFT JOIN LATERAL (SELECT COALESCE(jsonb_agg(metric_series.* ORDER BY index),'[]'::jsonb) AS series
|
||||
FROM metric_series
|
||||
|
|
@ -682,27 +695,3 @@ PREDEFINED = {schemas.MetricOfWebVitals.count_sessions: metrics.get_processed_se
|
|||
def get_predefined_metric(key: Union[schemas.MetricOfWebVitals, schemas.MetricOfErrors, \
|
||||
schemas.MetricOfPerformance, schemas.MetricOfResources], project_id: int, data: dict):
|
||||
return PREDEFINED.get(key, lambda *args: None)(project_id=project_id, **data)
|
||||
|
||||
# def add_thumbnail(metric_id, user_id, project_id):
|
||||
# key = generate_file_key(project_id=project_id, key=f"{metric_id}.png")
|
||||
# params = {"metric_id": metric_id, "user_id": user_id, "project_id": project_id, "key": key}
|
||||
# with pg_client.PostgresClient() as cur:
|
||||
# query = cur.mogrify(f"""\
|
||||
# UPDATE metrics
|
||||
# SET thumbnail_url = %(key)s
|
||||
# WHERE metric_id = %(metric_id)s
|
||||
# AND project_id = %(project_id)s
|
||||
# AND (user_id = %(user_id)s OR is_public)
|
||||
# RETURNING metric_id;""", params)
|
||||
# cur.execute(query)
|
||||
# row = cur.fetchone()
|
||||
# if row is None:
|
||||
# return {"errors": ["Card not found"]}
|
||||
# return {"data": s3.get_presigned_url_for_upload(bucket=config('THUMBNAILS_BUCKET'), expires_in=180, key=key,
|
||||
# # content-length-range is in bytes
|
||||
# conditions=["content-length-range", 1, 1 * 1024 * 1024],
|
||||
# content_type="image/png")}
|
||||
#
|
||||
#
|
||||
# def generate_file_key(project_id, key):
|
||||
# return f"{project_id}/cards/{key}"
|
||||
|
|
|
|||
|
|
@ -330,6 +330,8 @@ DROP FUNCTION get_new_event_filter_key;
|
|||
DROP FUNCTION get_new_event_key;
|
||||
|
||||
DROP TABLE IF EXISTS public.funnels;
|
||||
ALTER TABLE IF EXISTS public.metrics
|
||||
ADD COLUMN IF NOT EXISTS data jsonb NULL;
|
||||
COMMIT;
|
||||
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS clicks_selector_idx ON events.clicks (selector);
|
||||
|
|
|
|||
|
|
@ -751,7 +751,8 @@ $$
|
|||
"col": 2,
|
||||
"row": 2,
|
||||
"position": 0
|
||||
}'::jsonb
|
||||
}'::jsonb,
|
||||
data jsonb NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS metrics_user_id_is_public_idx ON public.metrics (user_id, is_public);
|
||||
CREATE TABLE IF NOT EXISTS metric_series
|
||||
|
|
|
|||
|
|
@ -301,6 +301,8 @@ $$
|
|||
LANGUAGE plpgsql;
|
||||
|
||||
DROP TABLE IF EXISTS public.funnels;
|
||||
ALTER TABLE IF EXISTS public.metrics
|
||||
ADD COLUMN IF NOT EXISTS data jsonb NULL;
|
||||
COMMIT;
|
||||
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS clicks_selector_idx ON events.clicks (selector);
|
||||
|
|
|
|||
|
|
@ -859,7 +859,8 @@ $$
|
|||
"col": 2,
|
||||
"row": 2,
|
||||
"position": 0
|
||||
}'::jsonb
|
||||
}'::jsonb,
|
||||
data jsonb NULL
|
||||
);
|
||||
|
||||
CREATE INDEX metrics_user_id_is_public_idx ON public.metrics (user_id, is_public);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue