Skip to content

Commit

Permalink
fix(chalice): fixed predefined metrics
Browse files Browse the repository at this point in the history
refactor(chalice): refactored schemas
refactor(chalice): refactored routers
refactor(chalice): refactored unprocessed sessions
  • Loading branch information
tahayk committed Aug 13, 2024
1 parent 32a8cd8 commit 69966e4
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 146 deletions.
3 changes: 2 additions & 1 deletion api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from psycopg import AsyncConnection
from psycopg.rows import dict_row
from starlette.responses import StreamingResponse

from chalicelib.utils import helper
Expand All @@ -20,7 +21,7 @@
loglevel = config("LOGLEVEL", default=logging.WARNING)
print(f">Loglevel set to: {loglevel}")
logging.basicConfig(level=loglevel)
from psycopg.rows import dict_row



class ORPYAsyncConnection(AsyncConnection):
Expand Down
5 changes: 2 additions & 3 deletions api/auth/auth_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ async def __call__(self, request: Request) -> Optional[schemas.CurrentContext]:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid authentication scheme.")
jwt_payload = authorizers.jwt_authorizer(scheme=credentials.scheme, token=credentials.credentials)
auth_exists = jwt_payload is not None \
and users.auth_exists(user_id=jwt_payload.get("userId", -1),
jwt_iat=jwt_payload.get("iat", 100))
auth_exists = jwt_payload is not None and users.auth_exists(user_id=jwt_payload.get("userId", -1),
jwt_iat=jwt_payload.get("iat", 100))
if jwt_payload is None \
or jwt_payload.get("iat") is None or jwt_payload.get("aud") is None \
or not auth_exists:
Expand Down
5 changes: 1 addition & 4 deletions api/chalicelib/core/custom_metrics_predefined.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import logging
from typing import Union

import logging
from typing import Union

import schemas
from chalicelib.core import metrics

Expand All @@ -30,7 +27,7 @@ def get_metric(key: Union[schemas.MetricOfWebVitals, schemas.MetricOfErrors, \
schemas.MetricOfWebVitals.COUNT_REQUESTS: metrics.get_top_metrics_count_requests,
schemas.MetricOfWebVitals.AVG_TIME_TO_RENDER: metrics.get_time_to_render,
schemas.MetricOfWebVitals.AVG_USED_JS_HEAP_SIZE: metrics.get_memory_consumption,
schemas.MetricOfWebVitals.avg_cpu: metrics.get_avg_cpu,
schemas.MetricOfWebVitals.AVG_CPU: metrics.get_avg_cpu,
schemas.MetricOfWebVitals.AVG_FPS: metrics.get_avg_fps,
schemas.MetricOfErrors.IMPACTED_SESSIONS_BY_JS_ERRORS: metrics.get_impacted_sessions_by_js_errors,
schemas.MetricOfErrors.DOMAINS_ERRORS_4XX: metrics.get_domains_errors_4xx,
Expand Down
18 changes: 18 additions & 0 deletions api/chalicelib/core/unprocessed_sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import logging

from chalicelib.core import sessions, assist

logger = logging.getLogger(__name__)


def check_exists(project_id, session_id, not_found_response) -> (int | None, dict | None):
if session_id is None or not session_id.isnumeric():
return session_id, not_found_response
else:
session_id = int(session_id)
if not sessions.session_exists(project_id=project_id, session_id=session_id):
logger.warning(f"{project_id}/{session_id} not found in DB.")
if not assist.session_exists(project_id=project_id, session_id=session_id):
logger.warning(f"{project_id}/{session_id} not found in Assist.")
return session_id, not_found_response
return session_id, None
2 changes: 1 addition & 1 deletion api/chalicelib/utils/TimeUTC.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __now(delta_days=0, delta_minutes=0, delta_seconds=0):
.astimezone(UTC_ZI)

@staticmethod
def now(delta_days=0, delta_minutes=0, delta_seconds=0):
def now(delta_days: int = 0, delta_minutes: int = 0, delta_seconds: int = 0) -> int:
return int(TimeUTC.__now(delta_days=delta_days, delta_minutes=delta_minutes,
delta_seconds=delta_seconds).timestamp() * 1000)

Expand Down
18 changes: 8 additions & 10 deletions api/routers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
from fastapi import Depends, Body, BackgroundTasks

import schemas
from chalicelib.core import log_tool_rollbar, sourcemaps, events, sessions_assignments, projects, \
alerts, issues, integrations_manager, metadata, \
log_tool_elasticsearch, log_tool_datadog, \
log_tool_stackdriver, reset_password, log_tool_cloudwatch, log_tool_sentry, log_tool_sumologic, log_tools, sessions, \
log_tool_newrelic, announcements, log_tool_bugsnag, weekly_report, integration_jira_cloud, integration_github, \
assist, mobile, tenants, boarding, notifications, webhook, users, \
custom_metrics, saved_search, integrations_global, tags, autocomplete
from chalicelib.core import log_tool_rollbar, sourcemaps, events, sessions_assignments, projects, alerts, issues, \
integrations_manager, metadata, log_tool_elasticsearch, log_tool_datadog, log_tool_stackdriver, reset_password, \
log_tool_cloudwatch, log_tool_sentry, log_tool_sumologic, log_tools, sessions, log_tool_newrelic, announcements, \
log_tool_bugsnag, weekly_report, integration_jira_cloud, integration_github, assist, mobile, tenants, boarding, \
notifications, webhook, users, custom_metrics, saved_search, integrations_global, tags, autocomplete
from chalicelib.core.collaboration_msteams import MSTeams
from chalicelib.core.collaboration_slack import Slack
from or_dependencies import OR_context, OR_role
Expand Down Expand Up @@ -556,7 +554,7 @@ def get_all_alerts(projectId: int, context: schemas.CurrentContext = Depends(OR_

@app.get('/{projectId}/alerts/triggers', tags=["alerts", "customMetrics"])
def get_alerts_triggers(projectId: int, context: schemas.CurrentContext = Depends(OR_context)):
return {"data": alerts.get_predefined_values() \
return {"data": alerts.get_predefined_values()
+ custom_metrics.get_series_for_alert(project_id=projectId, user_id=context.user_id)}


Expand Down Expand Up @@ -839,8 +837,8 @@ def edit_msteams_integration(webhookId: int, data: schemas.EditCollaborationSche
if old["endpoint"] != data.url.unicode_string():
if not MSTeams.say_hello(data.url.unicode_string()):
return {
"errors": [
"We couldn't send you a test message on your Microsoft Teams channel. Please verify your webhook url."]
"errors": ["We couldn't send you a test message on your Microsoft Teams channel. "
"Please verify your webhook url."]
}
return {"data": webhook.update(tenant_id=context.tenant_id, webhook_id=webhookId,
changes={"name": data.name, "endpoint": data.url.unicode_string()})}
Expand Down
37 changes: 14 additions & 23 deletions api/routers/core_dynamic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Optional, Union

from decouple import config
Expand All @@ -6,19 +7,21 @@
from starlette.responses import RedirectResponse, FileResponse, JSONResponse, Response

import schemas
from chalicelib.core import scope
from chalicelib.core import sessions, errors, errors_viewed, errors_favorite, sessions_assignments, heatmaps, \
sessions_favorite, assist, sessions_notes, sessions_replay, signup, feature_flags
from chalicelib.core import sessions_viewed
from chalicelib.core import tenants, users, projects, license
from chalicelib.core import unprocessed_sessions
from chalicelib.core import webhook
from chalicelib.core import scope
from chalicelib.core.collaboration_slack import Slack
from chalicelib.utils import captcha, smtp
from chalicelib.utils import helper
from chalicelib.utils.TimeUTC import TimeUTC
from or_dependencies import OR_context, OR_role
from routers.base import get_routers

logger = logging.getLogger(__name__)
public_app, app, app_apikey = get_routers()

COOKIE_PATH = "/api/refresh"
Expand Down Expand Up @@ -249,7 +252,7 @@ def session_ids_search(projectId: int, data: schemas.SessionsSearchPayloadSchema


@app.get('/{projectId}/sessions/{sessionId}/first-mob', tags=["sessions", "replay"])
def get_first_mob_file(projectId: int, sessionId: Union[int, str], background_tasks: BackgroundTasks,
def get_first_mob_file(projectId: int, sessionId: Union[int, str],
context: schemas.CurrentContext = Depends(OR_context)):
if not sessionId.isnumeric():
return {"errors": ["session not found"]}
Expand Down Expand Up @@ -368,16 +371,10 @@ def get_live_session(projectId: int, sessionId: str, background_tasks: Backgroun
def get_live_session_replay_file(projectId: int, sessionId: Union[int, str],
context: schemas.CurrentContext = Depends(OR_context)):
not_found = {"errors": ["Replay file not found"]}
if not sessionId.isnumeric():
return not_found
else:
sessionId = int(sessionId)
if not sessions.session_exists(project_id=projectId, session_id=sessionId):
print(f"{projectId}/{sessionId} not found in DB.")
if not assist.session_exists(project_id=projectId, session_id=sessionId):
print(f"{projectId}/{sessionId} not found in Assist.")
return not_found

sessionId, err = unprocessed_sessions.check_exists(project_id=projectId, session_id=sessionId,
not_found_response=not_found)
if err is not None:
return err
path = assist.get_raw_mob_by_id(project_id=projectId, session_id=sessionId)
if path is None:
return not_found
Expand All @@ -389,19 +386,13 @@ def get_live_session_replay_file(projectId: int, sessionId: Union[int, str],
def get_live_session_devtools_file(projectId: int, sessionId: Union[int, str],
context: schemas.CurrentContext = Depends(OR_context)):
not_found = {"errors": ["Devtools file not found"]}
if not sessionId.isnumeric():
return not_found
else:
sessionId = int(sessionId)
if not sessions.session_exists(project_id=projectId, session_id=sessionId):
print(f"{projectId}/{sessionId} not found in DB.")
if not assist.session_exists(project_id=projectId, session_id=sessionId):
print(f"{projectId}/{sessionId} not found in Assist.")
return not_found

sessionId, err = unprocessed_sessions.check_exists(project_id=projectId, session_id=sessionId,
not_found_response=not_found)
if err is not None:
return err
path = assist.get_raw_devtools_by_id(project_id=projectId, session_id=sessionId)
if path is None:
return {"errors": ["Devtools file not found"]}
return not_found

return FileResponse(path=path, media_type="application/octet-stream")

Expand Down
2 changes: 1 addition & 1 deletion api/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .schemas import *
from . import overrides as _overrides
from . import overrides as _overrides
2 changes: 1 addition & 1 deletion api/schemas/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def has_value(cls, value) -> bool:


class ORUnion:
def __new__(self, union_types: Union[AnyType], discriminator: str) -> T:
def __new__(cls, union_types: Union[AnyType], discriminator: str) -> T:
return lambda **args: TypeAdapter(Annotated[union_types, Field(discriminator=discriminator)]) \
.validate_python(args)
Loading

0 comments on commit 69966e4

Please sign in to comment.