Skip to content

Commit

Permalink
Merge pull request #50 from mgxd/fix/query-fmt
Browse files Browse the repository at this point in the history
FIX: Add safeguards to query formulation, exception handling
  • Loading branch information
mgxd authored Nov 17, 2023
2 parents e4bcd1d + e8e756d commit 880920a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
2 changes: 1 addition & 1 deletion migas/error/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def inspect_error(error_funcs: dict | None = None) -> dict:
etb = sys.last_traceback

if err and etype:
evalue = err.args[0]
evalue = err.args[0] if err.args else str(err)
ename = etype.__name__

if isinstance(error_funcs, dict) and ename in error_funcs:
Expand Down
113 changes: 62 additions & 51 deletions migas/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
from __future__ import annotations

import dataclasses
import enum
import json
import typing as ty
import warnings

from migas.config import Config, logger, telemetry_enabled
from migas.request import request

FREE = '"{}"' # FREE text fields
FIXED = '{}' # FIXED text fields
class QueryParamType(enum.Enum):
LITERAL = enum.auto()
TEXT = enum.auto()

ERROR = '[migas-py] An error occurred.'


Expand Down Expand Up @@ -48,23 +52,23 @@ class AddBreadcrumb(Operation):
operation_type = "mutation"
operation_name = "add_breadcrumb"
query_args = {
"project": FREE,
"project_version": FREE,
"language": FREE,
"language_version": FREE,
"project": QueryParamType.TEXT,
"project_version": QueryParamType.TEXT,
"language": QueryParamType.TEXT,
"language_version": QueryParamType.TEXT,
"ctx": {
"session_id": FREE,
"user_id": FREE,
"user_type": FIXED,
"platform": FREE,
"container": FIXED,
"is_ci": FIXED,
"session_id": QueryParamType.TEXT,
"user_id": QueryParamType.TEXT,
"user_type": QueryParamType.LITERAL,
"platform": QueryParamType.TEXT,
"container": QueryParamType.LITERAL,
"is_ci": QueryParamType.LITERAL,
},
"proc": {
"status": FIXED,
"status_desc": FREE,
"error_type": FREE,
"error_desc": FREE,
"status": QueryParamType.LITERAL,
"status_desc": QueryParamType.TEXT,
"error_type": QueryParamType.TEXT,
"error_desc": QueryParamType.TEXT,
},
}
fingerprint = True
Expand Down Expand Up @@ -114,21 +118,21 @@ class AddProject(Operation):
operation_name = "add_project"
query_args = {
"p": {
"project": FREE,
"project_version": FREE,
"language": FREE,
"language_version": FREE,
"is_ci": FIXED,
"status": FIXED,
"status_desc": FREE,
"error_type": FREE,
"error_desc": FREE,
"user_id": FREE,
"session_id": FREE,
"container": FIXED,
"user_type": FIXED,
"platform": FREE,
"arguments": FREE,
"project": QueryParamType.TEXT,
"project_version": QueryParamType.TEXT,
"language": QueryParamType.TEXT,
"language_version": QueryParamType.TEXT,
"is_ci": QueryParamType.LITERAL,
"status": QueryParamType.LITERAL,
"status_desc": QueryParamType.TEXT,
"error_type": QueryParamType.TEXT,
"error_desc": QueryParamType.TEXT,
"user_id": QueryParamType.TEXT,
"session_id": QueryParamType.TEXT,
"container": QueryParamType.LITERAL,
"user_type": QueryParamType.LITERAL,
"platform": QueryParamType.TEXT,
"arguments": QueryParamType.TEXT,
},
}
fingerprint = True
Expand Down Expand Up @@ -174,20 +178,20 @@ class CheckProject(Operation):
operation_type = "query"
operation_name = "check_project"
query_args = {
"project": FREE,
"project_version": FREE,
"language": FREE,
"language_version": FREE,
"is_ci": FIXED,
"status": FIXED,
"status_desc": FREE,
"error_type": FREE,
"error_desc": FREE,
"user_id": FREE,
"session_id": FREE,
"container": FIXED,
"platform": FREE,
"arguments": FREE,
"project": QueryParamType.TEXT,
"project_version": QueryParamType.TEXT,
"language": QueryParamType.TEXT,
"language_version": QueryParamType.TEXT,
"is_ci": QueryParamType.LITERAL,
"status": QueryParamType.LITERAL,
"status_desc": QueryParamType.TEXT,
"error_type": QueryParamType.TEXT,
"error_desc": QueryParamType.TEXT,
"user_id": QueryParamType.TEXT,
"session_id": QueryParamType.TEXT,
"container": QueryParamType.LITERAL,
"platform": QueryParamType.TEXT,
"arguments": QueryParamType.TEXT,
}
selections = ('success', 'flagged', 'latest', 'message')

Expand Down Expand Up @@ -216,10 +220,10 @@ class GetUsage(Operation):
operation_type = 'query'
operation_name = 'get_usage'
query_args = {
"project": FREE,
"start": FREE,
"end": FREE,
"unique": FIXED,
"project": QueryParamType.TEXT,
"start": QueryParamType.TEXT,
"end": QueryParamType.TEXT,
"unique": QueryParamType.LITERAL,
}


Expand Down Expand Up @@ -273,8 +277,15 @@ def _parse_format_params(params: dict, query_args: dict) -> str:
val = params[qarg]
if isinstance(val, bool):
val = str(val).lower()
val = qval.format(val)
query_inputs.append(f'{qarg}:{val}')

if qval.name == 'TEXT':
fval = json.dumps(val)
elif qval.name == 'LITERAL':
fval = val
else:
logger.error('Do not know how to handle type %s', qval.name)
fval = ''
query_inputs.append(f'{qarg}:{fval}')

elif isinstance(qval, dict):
vals = _parse_format_params(params, qval)
Expand Down

0 comments on commit 880920a

Please sign in to comment.