* feat(api): dynamic-api 1/2
* feat(api): dynamic-api 2/2
feat(api): core-api 1/2
* feat(api): changed schemas
feat(api): aipkey authorizer
feat(api): jwt authorizer payload
feat(api): core-api 2/3
* feat(api): apikey authorizer
feat(api): shared context
feat(api): response editor
feat(api): middleware
feat(api): custom router
feat(api): fix auth double call
* feat(api): dashboard
feat(api): insights
feat(api): public api v1
* feat(api): allow full CORS
* feat(api): use decouple-config instead of env
feat(api): fixed conflict slack endpoint
feat(api): fixed favorite errors param
* feat(api): migration fixes
* feat(api): changes
* feat(api): crons
* feat(api): changes and fixes
* feat(api): added new endpoints
feat(api): applied new changes
feat(api): Docker image
* feat(api): EE 1/4
* feat(api): EE core_dynamic
* feat(api): global routers generator
* feat(api): project authorizer
feat(api): docker image
feat(api): crons
* feat(api): EE trace activity
* feat(api): changed ORRouter
* feat(api): EE trace activity parameters&payload
* feat(api): EE trace activity action name & path_format
* feat(db): user trace
* feat(api): EE trace activity ignore routes and hide attribute
feat(api): fix funnel payload schema
* feat(api): mobile support
* feat(api): changed build script
* feat(api): changed mobile sign endpoint
feat(api): changed requirements.txt
* feat(api): changed dockerfile
* feat(api): changed mobile-env-var
* feat(api): removed insights
* feat(api): changed EE Dockerfile
* feat(api): cast session_id to str for signing
* feat(api): fixed error_id type
* feat(api): fixed /errors priority conflict
* feat(api): fixed /errors/{errorId} default params
* feat(api): fixed change password after invitation
* feat(api): use background task for emails instead of low-timeout-api
feat(api): EE fixed missing required params
* feat(api): funnel-insights payload change
* feat(api): funnel-insights payload change
* feat(api): changed edit user payload schema
* feat(api): changed metrics payload schema
* feat(api): changed metrics payload schema
* feat(api): changed edit user default values
feat(api): fixed change error status route
* feat(api): changed edit user
* feat(api): stop user from changing his own role
* feat(api): changed add slack
* feat(api): changed get funnel
* feat(api): changed get funnel on the fly payload
feat(api): changed update payload
* feat(api): changed get funnel on the fly payload
* feat(api): changed update funnel payload
* feat(api): changed get funnel-sessions/issues on the fly payload
* feat(api): fixed funnel missing rangeValue
* feat(api): fixes
* feat(api): iceServers configuration
* feat(api): fix issueId casting
* feat(api): changed issues-sessions endpoint payload-schema
* feat(api): EE changed traces-ignored-routes
* feat(api): EE include core sessions.py
* feat(api): EE check licence on every request if expired
* feat(api): move general stats to dynamic
* feat(api): code cleanup
feat(api): removed sentry
* feat(api): changed traces-ignore-routes
* feat(api): changed dependencies
* feat(api): changed jwt-auth-response code
* feat(api): changed traces-ignore-routes
* feat(api): changed traces-ignore-routes
* feat(api): removed PyTZ
feat(api): migrated time-helper to zoneinfo
* feat(api): EE added missing dependency
feat(api): changed base docker image
* feat(api): merge after roles
* feat(api): EE roles fastapi
* feat(db): handel HTTPExceptions
* feat(db): changed payload schema
* feat(db): changed payload schema
* feat(api): included insights
* feat(api): removed unused helper
* feat(api): merge from dev to fatsapi
* feat(api): merge fixes
feat(api): SAML migration
* feat(api): changed GET /signup response
feat(api): changed EE Dockerfile
* feat(api): changed edition detection
* feat(api): include ee endpoints
* feat(api): add/edit member changes
* feat(api): saml changed redirect
* feat(api): track session's replay
feat(api): track error's details
* feat(api): ignore tracking for read roles
* feat(api): define global queue
feat(api): define global scheduler
feat(api): traces use queue
feat(api): traces batch insert
feat(DB): changed traces schema
* feat(api): fix signup captcha
* feat(api): fix signup captcha
* feat(api): optional roleId
feat(api): set roleId to member if None
* feat(api): fixed edit role
* feat(api): return role details when creating a new member
* feat(api): trace: use BackgroundTasks instead of BackgroundTask to not override previous tasks
* feat(api): trace: use BackgroundTask if no other background task is defined
* feat(api): optimised delete metadata
* feat(api): Notification optional message
* feat(api): fix background-task reference
* feat(api): fix trace-background-task
* feat(api): fixed g-captcha for reset password
* feat(api): fix edit self-user
* feat(api): fixed create github-issue
* feat(api): set misfire_grace_time for crons
* feat(api): removed chalice
feat(api): freeze dependencies
* feat(api): refactored blueprints
* feat(api): /metadata/session_search allow projectId=None
* feat(api): public API, changed userId type
* feat(api): fix upload sourcemaps
* feat(api): user-trace support ApiKey endpoints
* feat(api): fixed user-trace foreign key type
* feat(api): fixed trace schema
* feat(api): trace save auth-method
* feat(api): trace fixed auth-method
* feat(api): trace changed schema
334 lines
9.5 KiB
Python
334 lines
9.5 KiB
Python
import random
|
|
import re
|
|
import string
|
|
|
|
import math
|
|
import requests
|
|
|
|
local_prefix = 'local-'
|
|
from decouple import config
|
|
|
|
|
|
def get_version_number():
|
|
return config("version")
|
|
|
|
|
|
def get_stage_name():
|
|
stage = config("stage")
|
|
return stage[len(local_prefix):] if stage.startswith(local_prefix) else stage
|
|
|
|
|
|
def is_production():
|
|
return get_stage_name() == "production"
|
|
|
|
|
|
def is_staging():
|
|
return get_stage_name() == "staging"
|
|
|
|
|
|
def is_onprem():
|
|
return not is_production() and not is_staging()
|
|
|
|
|
|
def is_local():
|
|
return config("stage").startswith(local_prefix)
|
|
|
|
|
|
def generate_salt():
|
|
return "".join(random.choices(string.hexdigits, k=36))
|
|
|
|
|
|
def unique_ordered_list(array):
|
|
uniq = []
|
|
[uniq.append(x) for x in array if x not in uniq]
|
|
return uniq
|
|
|
|
|
|
def unique_unordered_list(array):
|
|
return list(set(array))
|
|
|
|
|
|
def list_to_camel_case(items, flatten=False):
|
|
for i in range(len(items)):
|
|
if flatten:
|
|
items[i] = flatten_nested_dicts(items[i])
|
|
items[i] = dict_to_camel_case(items[i])
|
|
|
|
return items
|
|
|
|
|
|
def dict_to_camel_case(variable, delimiter='_', ignore_keys=[]):
|
|
if variable is None:
|
|
return None
|
|
if isinstance(variable, str):
|
|
return variable
|
|
elif isinstance(variable, dict):
|
|
aux = {}
|
|
for key in variable.keys():
|
|
if key in ignore_keys:
|
|
aux[key] = variable[key]
|
|
elif isinstance(variable[key], dict):
|
|
aux[key_to_camel_case(key, delimiter)] = dict_to_camel_case(variable[key])
|
|
elif isinstance(variable[key], list):
|
|
aux[key_to_camel_case(key, delimiter)] = list_to_camel_case(variable[key])
|
|
else:
|
|
aux[key_to_camel_case(key, delimiter)] = variable[key]
|
|
return aux
|
|
else:
|
|
return variable
|
|
|
|
|
|
def dict_to_CAPITAL_keys(variable):
|
|
if variable is None:
|
|
return None
|
|
if isinstance(variable, str):
|
|
return variable.upper()
|
|
elif isinstance(variable, dict):
|
|
aux = {}
|
|
for key in variable.keys():
|
|
if isinstance(variable[key], dict):
|
|
aux[key.upper()] = dict_to_CAPITAL_keys(variable[key])
|
|
else:
|
|
aux[key.upper()] = variable[key]
|
|
return aux
|
|
else:
|
|
return variable
|
|
|
|
|
|
def variable_to_snake_case(variable, delimiter='_', split_number=False):
|
|
if isinstance(variable, str):
|
|
return key_to_snake_case(variable, delimiter, split_number)
|
|
elif isinstance(variable, dict):
|
|
aux = {}
|
|
for key in variable.keys():
|
|
if isinstance(variable[key], dict):
|
|
aux[key_to_snake_case(key, delimiter, split_number)] = variable_to_snake_case(variable[key], delimiter,
|
|
split_number)
|
|
else:
|
|
aux[key_to_snake_case(key, delimiter, split_number)] = variable[key]
|
|
return aux
|
|
else:
|
|
return variable
|
|
|
|
|
|
def key_to_camel_case(snake_str, delimiter='_'):
|
|
if snake_str.startswith(delimiter):
|
|
snake_str = snake_str[1:]
|
|
components = snake_str.split(delimiter)
|
|
return components[0] + ''.join(x.title() for x in components[1:])
|
|
|
|
|
|
def key_to_snake_case(name, delimiter='_', split_number=False):
|
|
s1 = re.sub('(.)([A-Z][a-z]+)', fr'\1{delimiter}\2', name)
|
|
return re.sub('([a-z])([A-Z0-9])' if split_number else '([a-z0-9])([A-Z])', fr'\1{delimiter}\2', s1).lower()
|
|
|
|
|
|
TRACK_TIME = True
|
|
|
|
|
|
def __sbool_to_bool(value):
|
|
if value is None or not isinstance(value, str):
|
|
return False
|
|
return value.lower() in ["true", "yes", "1"]
|
|
|
|
|
|
def allow_captcha():
|
|
return config("captcha_server", default=None) is not None and config("captcha_key", default=None) is not None \
|
|
and len(config("captcha_server")) > 0 and len(config("captcha_key")) > 0
|
|
|
|
|
|
def allow_sentry():
|
|
return config("sentryURL", default=None) is not None and len(config("sentryURL")) > 0
|
|
|
|
|
|
def async_post(endpoint, data):
|
|
data["auth"] = config("async_Token")
|
|
try:
|
|
requests.post(endpoint, timeout=1, json=data)
|
|
except requests.exceptions.ReadTimeout:
|
|
pass
|
|
|
|
|
|
def string_to_sql_like(value):
|
|
value = re.sub(' +', ' ', value)
|
|
value = value.replace("*", "%")
|
|
if value.startswith("^"):
|
|
value = value[1:]
|
|
elif not value.startswith("%"):
|
|
value = '%' + value
|
|
|
|
if value.endswith("$"):
|
|
value = value[:-1]
|
|
elif not value.endswith("%"):
|
|
value = value + '%'
|
|
# value = value.replace(" ", "%")
|
|
return value
|
|
|
|
|
|
def string_to_sql_like_with_op(value, op):
|
|
if isinstance(value, list) and len(value) > 0:
|
|
_value = value[0]
|
|
else:
|
|
_value = value
|
|
if _value is None:
|
|
return _value
|
|
if op.lower() != 'ilike':
|
|
return _value.replace("%", "%%")
|
|
_value = _value.replace("*", "%")
|
|
if _value.startswith("^"):
|
|
_value = _value[1:]
|
|
elif not _value.startswith("%"):
|
|
_value = '%' + _value
|
|
|
|
if _value.endswith("$"):
|
|
_value = _value[:-1]
|
|
elif not _value.endswith("%"):
|
|
_value = _value + '%'
|
|
return _value.replace("%", "%%")
|
|
|
|
|
|
def is_valid_email(email):
|
|
return re.match(r"[^@]+@[^@]+\.[^@]+", email) is not None
|
|
|
|
|
|
def is_valid_http_url(url):
|
|
regex = re.compile(
|
|
r'^(?:http|ftp)s?://' # http:// or https://
|
|
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
|
|
r'localhost|' # localhost...
|
|
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
|
|
r'(?::\d+)?' # optional port
|
|
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
|
|
|
return re.match(regex, url) is not None
|
|
|
|
|
|
def is_valid_url(url):
|
|
regex = re.compile(
|
|
# r'^(?:http|ftp)s?://' # http:// or https://
|
|
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
|
|
r'localhost|' # localhost...
|
|
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
|
|
r'(?::\d+)?' # optional port
|
|
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
|
|
|
return re.match(regex, url) is not None
|
|
|
|
|
|
def is_alphabet_space(word):
|
|
r = re.compile("^[a-zA-Z ]*$")
|
|
return r.match(word) is not None
|
|
|
|
|
|
def is_alphabet_latin_space(word):
|
|
r = re.compile("^[a-zA-Z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\s ]*$")
|
|
return r.match(word) is not None
|
|
|
|
|
|
def is_alphabet_space_dash(word):
|
|
r = re.compile("^[a-zA-Z -]*$")
|
|
return r.match(word) is not None
|
|
|
|
|
|
def is_alphanumeric_space(word):
|
|
r = re.compile("^[a-zA-Z0-9._\- ]*$")
|
|
return r.match(word) is not None
|
|
|
|
|
|
def merge_lists_by_key(l1, l2, key):
|
|
merged = {}
|
|
for item in l1 + l2:
|
|
if item[key] in merged:
|
|
merged[item[key]].update(item)
|
|
else:
|
|
merged[item[key]] = item
|
|
return [val for (_, val) in merged.items()]
|
|
|
|
|
|
def flatten_nested_dicts(obj):
|
|
if obj is None:
|
|
return None
|
|
result = {}
|
|
for key in obj.keys():
|
|
if isinstance(obj[key], dict):
|
|
result = {**result, **flatten_nested_dicts(obj[key])}
|
|
else:
|
|
result[key] = obj[key]
|
|
return result
|
|
|
|
|
|
def delete_keys_from_dict(d, to_delete):
|
|
if isinstance(to_delete, str):
|
|
to_delete = [to_delete]
|
|
if isinstance(d, dict):
|
|
for single_to_delete in set(to_delete):
|
|
if single_to_delete in d:
|
|
del d[single_to_delete]
|
|
for k, v in d.items():
|
|
delete_keys_from_dict(v, to_delete)
|
|
elif isinstance(d, list):
|
|
for i in d:
|
|
delete_keys_from_dict(i, to_delete)
|
|
return d
|
|
|
|
|
|
def explode_widget(data, key=None):
|
|
result = []
|
|
for k in data.keys():
|
|
if k.endswith("Progress") or k == "chart":
|
|
continue
|
|
result.append({"key": key_to_snake_case(k) if key is None else key, "data": {"value": data[k]}})
|
|
if k + "Progress" in data:
|
|
result[-1]["data"]["progress"] = data[k + "Progress"]
|
|
if "chart" in data:
|
|
result[-1]["data"]["chart"] = []
|
|
for c in data["chart"]:
|
|
result[-1]["data"]["chart"].append({"timestamp": c["timestamp"], "value": c[k]})
|
|
return result
|
|
|
|
|
|
TEMP_PATH = "./" if is_local() else "/tmp/"
|
|
|
|
|
|
def get_issue_title(issue_type):
|
|
return {'click_rage': "Click Rage",
|
|
'dead_click': "Dead Click",
|
|
'excessive_scrolling': "Excessive Scrolling",
|
|
'bad_request': "Bad Request",
|
|
'missing_resource': "Missing Image",
|
|
'memory': "High Memory Usage",
|
|
'cpu': "High CPU",
|
|
'slow_resource': "Slow Resource",
|
|
'slow_page_load': "Slow Page",
|
|
'crash': "Crash",
|
|
'ml_cpu': "High CPU",
|
|
'ml_memory': "High Memory Usage",
|
|
'ml_dead_click': "Dead Click",
|
|
'ml_click_rage': "Click Rage",
|
|
'ml_mouse_thrashing': "Mouse Thrashing",
|
|
'ml_excessive_scrolling': "Excessive Scrolling",
|
|
'ml_slow_resources': "Slow Resource",
|
|
'custom': "Custom Event",
|
|
'js_exception': "Error",
|
|
'custom_event_error': "Custom Error",
|
|
'js_error': "Error"}.get(issue_type, issue_type)
|
|
|
|
|
|
def __progress(old_val, new_val):
|
|
return ((old_val - new_val) / new_val) * 100 if new_val > 0 else 0 if old_val == 0 else 100
|
|
|
|
|
|
def __decimal_limit(value, limit):
|
|
factor = pow(10, limit)
|
|
value = math.floor(value * factor)
|
|
if value % factor == 0:
|
|
return value // factor
|
|
return value / factor
|
|
|
|
|
|
def has_smtp():
|
|
return config("EMAIL_HOST") is not None and len(config("EMAIL_HOST")) > 0
|
|
|
|
|
|
def get_edition():
|
|
return "ee" if "ee" in config("ENTERPRISE_BUILD", default="").lower() else "foss"
|