Skip to content

Commit

Permalink
refactor(chalice): refactored schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
tahayk committed Jul 17, 2024
1 parent 8c233f2 commit c0ff226
Show file tree
Hide file tree
Showing 50 changed files with 1,312 additions and 1,325 deletions.
2 changes: 1 addition & 1 deletion api/chalicelib/core/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,5 @@ def get_predefined_values():
"unit": "count" if v.endswith(".count") else "ms",
"predefined": True,
"metricId": None,
"seriesId": None} for v in values if v != schemas.AlertColumn.custom]
"seriesId": None} for v in values if v != schemas.AlertColumn.CUSTOM]
return values
46 changes: 23 additions & 23 deletions api/chalicelib/core/alerts_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,60 @@
logging.basicConfig(level=config("LOGLEVEL", default=logging.INFO))

LeftToDb = {
schemas.AlertColumn.performance__dom_content_loaded__average: {
schemas.AlertColumn.PERFORMANCE__DOM_CONTENT_LOADED__AVERAGE: {
"table": "events.pages INNER JOIN public.sessions USING(session_id)",
"formula": "COALESCE(AVG(NULLIF(dom_content_loaded_time ,0)),0)"},
schemas.AlertColumn.performance__first_meaningful_paint__average: {
schemas.AlertColumn.PERFORMANCE__FIRST_MEANINGFUL_PAINT__AVERAGE: {
"table": "events.pages INNER JOIN public.sessions USING(session_id)",
"formula": "COALESCE(AVG(NULLIF(first_contentful_paint_time,0)),0)"},
schemas.AlertColumn.performance__page_load_time__average: {
schemas.AlertColumn.PERFORMANCE__PAGE_LOAD_TIME__AVERAGE: {
"table": "events.pages INNER JOIN public.sessions USING(session_id)", "formula": "AVG(NULLIF(load_time ,0))"},
schemas.AlertColumn.performance__dom_build_time__average: {
schemas.AlertColumn.PERFORMANCE__DOM_BUILD_TIME__AVERAGE: {
"table": "events.pages INNER JOIN public.sessions USING(session_id)",
"formula": "AVG(NULLIF(dom_building_time,0))"},
schemas.AlertColumn.performance__speed_index__average: {
schemas.AlertColumn.PERFORMANCE__SPEED_INDEX__AVERAGE: {
"table": "events.pages INNER JOIN public.sessions USING(session_id)", "formula": "AVG(NULLIF(speed_index,0))"},
schemas.AlertColumn.performance__page_response_time__average: {
schemas.AlertColumn.PERFORMANCE__PAGE_RESPONSE_TIME__AVERAGE: {
"table": "events.pages INNER JOIN public.sessions USING(session_id)",
"formula": "AVG(NULLIF(response_time,0))"},
schemas.AlertColumn.performance__ttfb__average: {
schemas.AlertColumn.PERFORMANCE__TTFB__AVERAGE: {
"table": "events.pages INNER JOIN public.sessions USING(session_id)",
"formula": "AVG(NULLIF(first_paint_time,0))"},
schemas.AlertColumn.performance__time_to_render__average: {
schemas.AlertColumn.PERFORMANCE__TIME_TO_RENDER__AVERAGE: {
"table": "events.pages INNER JOIN public.sessions USING(session_id)",
"formula": "AVG(NULLIF(visually_complete,0))"},
schemas.AlertColumn.performance__image_load_time__average: {
schemas.AlertColumn.PERFORMANCE__IMAGE_LOAD_TIME__AVERAGE: {
"table": "events.resources INNER JOIN public.sessions USING(session_id)",
"formula": "AVG(NULLIF(resources.duration,0))", "condition": "type='img'"},
schemas.AlertColumn.performance__request_load_time__average: {
schemas.AlertColumn.PERFORMANCE__REQUEST_LOAD_TIME__AVERAGE: {
"table": "events.resources INNER JOIN public.sessions USING(session_id)",
"formula": "AVG(NULLIF(resources.duration,0))", "condition": "type='fetch'"},
schemas.AlertColumn.resources__load_time__average: {
schemas.AlertColumn.RESOURCES__LOAD_TIME__AVERAGE: {
"table": "events.resources INNER JOIN public.sessions USING(session_id)",
"formula": "AVG(NULLIF(resources.duration,0))"},
schemas.AlertColumn.resources__missing__count: {
schemas.AlertColumn.RESOURCES__MISSING__COUNT: {
"table": "events.resources INNER JOIN public.sessions USING(session_id)",
"formula": "COUNT(DISTINCT url_hostpath)", "condition": "success= FALSE AND type='img'"},
schemas.AlertColumn.errors__4xx_5xx__count: {
schemas.AlertColumn.ERRORS__4XX_5XX__COUNT: {
"table": "events.resources INNER JOIN public.sessions USING(session_id)", "formula": "COUNT(session_id)",
"condition": "status/100!=2"},
schemas.AlertColumn.errors__4xx__count: {
schemas.AlertColumn.ERRORS__4XX__COUNT: {
"table": "events.resources INNER JOIN public.sessions USING(session_id)",
"formula": "COUNT(session_id)", "condition": "status/100=4"},
schemas.AlertColumn.errors__5xx__count: {
schemas.AlertColumn.ERRORS__5XX__COUNT: {
"table": "events.resources INNER JOIN public.sessions USING(session_id)",
"formula": "COUNT(session_id)", "condition": "status/100=5"},
schemas.AlertColumn.errors__javascript__impacted_sessions__count: {
schemas.AlertColumn.ERRORS__JAVASCRIPT__IMPACTED_SESSIONS__COUNT: {
"table": "events.resources INNER JOIN public.sessions USING(session_id)",
"formula": "COUNT(DISTINCT session_id)", "condition": "success= FALSE AND type='script'"},
schemas.AlertColumn.performance__crashes__count: {
schemas.AlertColumn.PERFORMANCE__CRASHES__COUNT: {
"table": "public.sessions",
"formula": "COUNT(DISTINCT session_id)",
"condition": "errors_count > 0 AND duration>0"},
schemas.AlertColumn.errors__javascript__count: {
schemas.AlertColumn.ERRORS__JAVASCRIPT__COUNT: {
"table": "events.errors INNER JOIN public.errors AS m_errors USING (error_id)",
"formula": "COUNT(DISTINCT session_id)", "condition": "source='js_exception'", "joinSessions": False},
schemas.AlertColumn.errors__backend__count: {
schemas.AlertColumn.ERRORS__BACKEND__COUNT: {
"table": "events.errors INNER JOIN public.errors AS m_errors USING (error_id)",
"formula": "COUNT(DISTINCT session_id)", "condition": "source!='js_exception'", "joinSessions": False},
}
Expand All @@ -87,7 +87,7 @@ def can_check(a) -> bool:
now = TimeUTC.now()

repetitionBase = a["options"]["currentPeriod"] \
if a["detectionMethod"] == schemas.AlertDetectionMethod.change \
if a["detectionMethod"] == schemas.AlertDetectionMethod.CHANGE \
and a["options"]["currentPeriod"] > a["options"]["previousPeriod"] \
else a["options"]["previousPeriod"]

Expand All @@ -110,7 +110,7 @@ def Build(a):
main_table = ""
if a["seriesId"] is not None:
a["filter"]["sort"] = "session_id"
a["filter"]["order"] = schemas.SortOrderType.desc
a["filter"]["order"] = schemas.SortOrderType.DESC
a["filter"]["startDate"] = 0
a["filter"]["endDate"] = TimeUTC.now()
try:
Expand All @@ -136,15 +136,15 @@ def Build(a):
is_ss = main_table == "public.sessions"
q = f"""SELECT coalesce(value,0) AS value, coalesce(value,0) {a["query"]["operator"]} {a["query"]["right"]} AS valid"""

if a["detectionMethod"] == schemas.AlertDetectionMethod.threshold:
if a["detectionMethod"] == schemas.AlertDetectionMethod.THRESHOLD:
if a["seriesId"] is not None:
q += f""" FROM ({subQ}) AS stat"""
else:
q += f""" FROM ({subQ} {"AND timestamp >= %(startDate)s AND timestamp <= %(now)s" if not is_ss else ""}
{"AND start_ts >= %(startDate)s AND start_ts <= %(now)s" if j_s else ""}) AS stat"""
params = {**params, **full_args, "startDate": TimeUTC.now() - a["options"]["currentPeriod"] * 60 * 1000}
else:
if a["change"] == schemas.AlertDetectionType.change:
if a["change"] == schemas.AlertDetectionType.CHANGE:
if a["seriesId"] is not None:
sub2 = subQ.replace("%(startDate)s", "%(timestamp_sub2)s").replace("%(endDate)s", "%(startDate)s")
sub1 = f"SELECT (({subQ})-({sub2})) AS value"
Expand Down
28 changes: 14 additions & 14 deletions api/chalicelib/core/assist.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_live_sessions_ws(project_id, body: schemas.LiveSessionsSearchPayloadSche
"sort": {"key": body.sort, "order": body.order}
}
for f in body.filters:
if f.type == schemas.LiveFilterType.metadata:
if f.type == schemas.LiveFilterType.METADATA:
data["filter"][f.source] = {"values": f.value, "operator": f.operator}

else:
Expand Down Expand Up @@ -281,23 +281,23 @@ def session_exists(project_id, session_id):

def __change_keys(key):
return {
"PAGETITLE": schemas.LiveFilterType.page_title.value,
"PAGETITLE": schemas.LiveFilterType.PAGE_TITLE.value,
"ACTIVE": "active",
"LIVE": "live",
"SESSIONID": schemas.LiveFilterType.session_id.value,
"METADATA": schemas.LiveFilterType.metadata.value,
"USERID": schemas.LiveFilterType.user_id.value,
"USERUUID": schemas.LiveFilterType.user_UUID.value,
"SESSIONID": schemas.LiveFilterType.SESSION_ID.value,
"METADATA": schemas.LiveFilterType.METADATA.value,
"USERID": schemas.LiveFilterType.USER_ID.value,
"USERUUID": schemas.LiveFilterType.USER_UUID.value,
"PROJECTKEY": "projectKey",
"REVID": schemas.LiveFilterType.rev_id.value,
"REVID": schemas.LiveFilterType.REV_ID.value,
"TIMESTAMP": "timestamp",
"TRACKERVERSION": schemas.LiveFilterType.tracker_version.value,
"TRACKERVERSION": schemas.LiveFilterType.TRACKER_VERSION.value,
"ISSNIPPET": "isSnippet",
"USEROS": schemas.LiveFilterType.user_os.value,
"USERBROWSER": schemas.LiveFilterType.user_browser.value,
"USERBROWSERVERSION": schemas.LiveFilterType.user_browser_version.value,
"USERDEVICE": schemas.LiveFilterType.user_device.value,
"USERDEVICETYPE": schemas.LiveFilterType.user_device_type.value,
"USERCOUNTRY": schemas.LiveFilterType.user_country.value,
"USEROS": schemas.LiveFilterType.USER_OS.value,
"USERBROWSER": schemas.LiveFilterType.USER_BROWSER.value,
"USERBROWSERVERSION": schemas.LiveFilterType.USER_BROWSER_VERSION.value,
"USERDEVICE": schemas.LiveFilterType.USER_DEVICE.value,
"USERDEVICETYPE": schemas.LiveFilterType.USER_DEVICE_TYPE.value,
"USERCOUNTRY": schemas.LiveFilterType.USER_COUNTRY.value,
"PROJECTID": "projectId"
}.get(key.upper(), key)
30 changes: 15 additions & 15 deletions api/chalicelib/core/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@


def __get_autocomplete_table(value, project_id):
autocomplete_events = [schemas.FilterType.rev_id,
schemas.EventType.click,
schemas.FilterType.user_device,
schemas.FilterType.user_id,
schemas.FilterType.user_browser,
schemas.FilterType.user_os,
schemas.EventType.custom,
schemas.FilterType.user_country,
schemas.FilterType.user_city,
schemas.FilterType.user_state,
schemas.EventType.location,
schemas.EventType.input]
autocomplete_events = [schemas.FilterType.REV_ID,
schemas.EventType.CLICK,
schemas.FilterType.USER_DEVICE,
schemas.FilterType.USER_ID,
schemas.FilterType.USER_BROWSER,
schemas.FilterType.USER_OS,
schemas.EventType.CUSTOM,
schemas.FilterType.USER_COUNTRY,
schemas.FilterType.USER_CITY,
schemas.FilterType.USER_STATE,
schemas.EventType.LOCATION,
schemas.EventType.INPUT]
autocomplete_events.sort()
sub_queries = []
c_list = []
for e in autocomplete_events:
if e == schemas.FilterType.user_country:
if e == schemas.FilterType.USER_COUNTRY:
c_list = countries.get_country_code_autocomplete(value)
if len(c_list) > 0:
sub_queries.append(f"""(SELECT DISTINCT ON(value) '{e.value}' AS _type, value
Expand Down Expand Up @@ -72,7 +72,7 @@ def __get_autocomplete_table(value, project_id):


def __generic_query(typename, value_length=None):
if typename == schemas.FilterType.user_country:
if typename == schemas.FilterType.USER_COUNTRY:
return f"""SELECT DISTINCT value, type
FROM {TABLE}
WHERE
Expand Down Expand Up @@ -127,7 +127,7 @@ def f(project_id, text):
params = {"project_id": project_id, "value": helper.string_to_sql_like(text),
"svalue": helper.string_to_sql_like("^" + text)}

if typename == schemas.FilterType.user_country:
if typename == schemas.FilterType.USER_COUNTRY:
params["value"] = tuple(countries.get_country_code_autocomplete(text))
if len(params["value"]) == 0:
return []
Expand Down
8 changes: 4 additions & 4 deletions api/chalicelib/core/collaboration_msteams.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class MSTeams(BaseCollaboration):
@classmethod
def add(cls, tenant_id, data: schemas.AddCollaborationSchema):
if webhook.exists_by_name(tenant_id=tenant_id, name=data.name, exclude_id=None,
webhook_type=schemas.WebhookType.msteams):
webhook_type=schemas.WebhookType.MSTEAMS):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"name already exists.")
if cls.say_hello(data.url):
return webhook.add(tenant_id=tenant_id,
endpoint=data.url.unicode_string(),
webhook_type=schemas.WebhookType.msteams,
webhook_type=schemas.WebhookType.MSTEAMS,
name=data.name)
return None

Expand Down Expand Up @@ -157,9 +157,9 @@ def share_error(cls, tenant_id, project_id, error_id, user, comment, project_nam
def get_integration(cls, tenant_id, integration_id=None):
if integration_id is not None:
return webhook.get_webhook(tenant_id=tenant_id, webhook_id=integration_id,
webhook_type=schemas.WebhookType.msteams)
webhook_type=schemas.WebhookType.MSTEAMS)

integrations = webhook.get_by_type(tenant_id=tenant_id, webhook_type=schemas.WebhookType.msteams)
integrations = webhook.get_by_type(tenant_id=tenant_id, webhook_type=schemas.WebhookType.MSTEAMS)
if integrations is None or len(integrations) == 0:
return None
return integrations[0]
8 changes: 4 additions & 4 deletions api/chalicelib/core/collaboration_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class Slack(BaseCollaboration):
@classmethod
def add(cls, tenant_id, data: schemas.AddCollaborationSchema):
if webhook.exists_by_name(tenant_id=tenant_id, name=data.name, exclude_id=None,
webhook_type=schemas.WebhookType.slack):
webhook_type=schemas.WebhookType.SLACK):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"name already exists.")
if cls.say_hello(data.url):
return webhook.add(tenant_id=tenant_id,
endpoint=data.url.unicode_string(),
webhook_type=schemas.WebhookType.slack,
webhook_type=schemas.WebhookType.SLACK,
name=data.name)
return None

Expand Down Expand Up @@ -118,9 +118,9 @@ def share_error(cls, tenant_id, project_id, error_id, user, comment, project_nam
def get_integration(cls, tenant_id, integration_id=None):
if integration_id is not None:
return webhook.get_webhook(tenant_id=tenant_id, webhook_id=integration_id,
webhook_type=schemas.WebhookType.slack)
webhook_type=schemas.WebhookType.SLACK)

integrations = webhook.get_by_type(tenant_id=tenant_id, webhook_type=schemas.WebhookType.slack)
integrations = webhook.get_by_type(tenant_id=tenant_id, webhook_type=schemas.WebhookType.SLACK)
if integrations is None or len(integrations) == 0:
return None
return integrations[0]
Loading

0 comments on commit c0ff226

Please sign in to comment.