Merge commit 'b9d7ded29b64c9d3b66ae97e966e019fe0537b62'

This commit is contained in:
jasquat 2022-10-27 10:50:50 -04:00
commit f2aa518830
10 changed files with 108 additions and 49 deletions

View File

@ -157,18 +157,26 @@ def configure_sentry(app: flask.app.Flask) -> None:
return None return None
return event return event
sentry_sample_rate = app.config.get("SENTRY_SAMPLE_RATE") sentry_errors_sample_rate = app.config.get("SENTRY_ERRORS_SAMPLE_RATE")
if sentry_sample_rate is None: if sentry_errors_sample_rate is None:
return raise Exception("SENTRY_ERRORS_SAMPLE_RATE is not set somehow")
sentry_traces_sample_rate = app.config.get("SENTRY_TRACES_SAMPLE_RATE")
if sentry_traces_sample_rate is None:
raise Exception("SENTRY_TRACES_SAMPLE_RATE is not set somehow")
sentry_sdk.init( sentry_sdk.init(
dsn=app.config.get("SENTRY_DSN"), dsn=app.config.get("SENTRY_DSN"),
integrations=[ integrations=[
FlaskIntegration(), FlaskIntegration(),
], ],
environment=app.config["ENV_IDENTIFIER"], environment=app.config["ENV_IDENTIFIER"],
# Set traces_sample_rate to 1.0 to capture 100% # sample_rate is the errors sample rate. we usually set it to 1 (100%)
# so we get all errors in sentry.
sample_rate=float(sentry_errors_sample_rate),
# Set traces_sample_rate to capture a certain percentage
# of transactions for performance monitoring. # of transactions for performance monitoring.
# We recommend adjusting this value in production. # We recommend adjusting this value to less than 1(00%) in production.
traces_sample_rate=float(sentry_sample_rate), traces_sample_rate=float(sentry_traces_sample_rate),
before_send=before_send, before_send=before_send,
) )

View File

@ -54,10 +54,17 @@ def setup_config(app: Flask) -> None:
else: else:
app.config.from_pyfile(f"{app.instance_path}/config.py", silent=True) app.config.from_pyfile(f"{app.instance_path}/config.py", silent=True)
env_config_module = "spiffworkflow_backend.config." + app.config["ENV_IDENTIFIER"] env_config_prefix = "spiffworkflow_backend.config."
env_config_module = env_config_prefix + app.config["ENV_IDENTIFIER"]
try: try:
app.config.from_object(env_config_module) app.config.from_object(env_config_module)
except ImportStringError as exception: except ImportStringError as exception:
if (
os.environ.get("TERRAFORM_DEPLOYED_ENVIRONMENT") == "true"
and os.environ.get("SPIFFWORKFLOW_BACKEND_ENV") is not None
):
app.config.from_object("{env_config_prefix}terraform_deployed_environment")
else:
raise ModuleNotFoundError( raise ModuleNotFoundError(
f"Cannot find config module: {env_config_module}" f"Cannot find config module: {env_config_module}"
) from exception ) from exception

View File

@ -47,7 +47,12 @@ SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME = environ.get(
# Sentry Configuration # Sentry Configuration
SENTRY_DSN = environ.get("SENTRY_DSN", default="") SENTRY_DSN = environ.get("SENTRY_DSN", default="")
SENTRY_SAMPLE_RATE = environ.get("SENTRY_SAMPLE_RATE", default="1.0") SENTRY_ERRORS_SAMPLE_RATE = environ.get(
"SENTRY_ERRORS_SAMPLE_RATE", default="1"
) # send all errors
SENTRY_TRACES_SAMPLE_RATE = environ.get(
"SENTRY_TRACES_SAMPLE_RATE", default="0.01"
) # send 1% of traces
SPIFFWORKFLOW_BACKEND_LOG_LEVEL = environ.get( SPIFFWORKFLOW_BACKEND_LOG_LEVEL = environ.get(
"SPIFFWORKFLOW_BACKEND_LOG_LEVEL", default="info" "SPIFFWORKFLOW_BACKEND_LOG_LEVEL", default="info"

View File

@ -1,11 +1,12 @@
"""Staging.""" """Demo environment."""
from os import environ from os import environ
GIT_COMMIT_ON_SAVE = True GIT_COMMIT_ON_SAVE = True
GIT_COMMIT_USERNAME = "demo" GIT_COMMIT_USERNAME = "demo"
GIT_COMMIT_EMAIL = "demo@example.com" GIT_COMMIT_EMAIL = "demo@example.com"
SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME = environ.get( SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME = environ.get(
"SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME", default="demo.yml" "SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME",
default="terraform_deployed_environment.yml",
) )
RUN_BACKGROUND_SCHEDULER = ( RUN_BACKGROUND_SCHEDULER = (

View File

@ -23,6 +23,21 @@ groups:
Finance Team: Finance Team:
users: [finance_user1] users: [finance_user1]
Project Lead:
users:
[
jakub,
alex,
dan,
mike,
jason,
jarrad,
elizabeth,
jon,
natalia,
manuchehr,
]
permissions: permissions:
admin: admin:
groups: [admin] groups: [admin]
@ -50,7 +65,7 @@ permissions:
uri: /v1.0/process-models/finance/* uri: /v1.0/process-models/finance/*
read-all: read-all:
groups: [finance, admin] groups: [finance, admin, "Project Lead"]
users: [] users: []
allowed_permissions: [read] allowed_permissions: [read]
uri: /* uri: /*

View File

@ -0,0 +1,16 @@
"""Terraform-deployed environment."""
from os import environ
# default.py already ensured that this key existed as was not None
environment_identifier_for_this_config_file_only = environ["SPIFFWORKFLOW_BACKEND_ENV"]
GIT_COMMIT_ON_SAVE = True
GIT_COMMIT_USERNAME = environment_identifier_for_this_config_file_only
GIT_COMMIT_EMAIL = f"{environment_identifier_for_this_config_file_only}@example.com"
SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME = environ.get(
"SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME", default="terraform_deployed_environment.yml"
)
RUN_BACKGROUND_SCHEDULER = (
environ.get("RUN_BACKGROUND_SCHEDULER", default="false") == "true"
)

View File

@ -1,21 +1,22 @@
from flask_bpmn.api.api_error import ApiError """Get_localtime."""
from spiffworkflow_backend.scripts.script import Script
from datetime import datetime from datetime import datetime
import pytz
from typing import Any from typing import Any
from typing import Optional from typing import Optional
import pytz
from flask_bpmn.api.api_error import ApiError
from SpiffWorkflow.task import Task as SpiffTask # type: ignore from SpiffWorkflow.task import Task as SpiffTask # type: ignore
from spiffworkflow_backend.scripts.script import Script
class GetLocaltime(Script): class GetLocaltime(Script):
"""GetLocaltime."""
def get_description(self) -> str: def get_description(self) -> str:
"""Get_description."""
return """Converts a Datetime object into a Datetime object for a specific timezone. return """Converts a Datetime object into a Datetime object for a specific timezone.
Defaults to US/Eastern""" Defaults to US/Eastern."""
def run( def run(
self, self,
@ -24,20 +25,23 @@ class GetLocaltime(Script):
*args: Any, *args: Any,
**kwargs: Any **kwargs: Any
) -> datetime: ) -> datetime:
if len(args) > 0 or 'datetime' in kwargs: """Run."""
if 'datetime' in kwargs: if len(args) > 0 or "datetime" in kwargs:
date_time = kwargs['datetime'] if "datetime" in kwargs:
date_time = kwargs["datetime"]
else: else:
date_time = args[0] date_time = args[0]
if 'timezone' in kwargs: if "timezone" in kwargs:
timezone = kwargs['timezone'] timezone = kwargs["timezone"]
elif len(args) > 1: elif len(args) > 1:
timezone = args[1] timezone = args[1]
else: else:
timezone = 'US/Eastern' timezone = "US/Eastern"
localtime: datetime = date_time.astimezone(pytz.timezone(timezone)) localtime: datetime = date_time.astimezone(pytz.timezone(timezone))
return localtime return localtime
else: else:
raise ApiError(error_code='missing_datetime', raise ApiError(
message='You must include a datetime to convert.') error_code="missing_datetime",
message="You must include a datetime to convert.",
)

View File

@ -2,19 +2,12 @@
"title": "Get Timezone", "title": "Get Timezone",
"description": "Form to select a timezone.", "description": "Form to select a timezone.",
"type": "object", "type": "object",
"required": [ "required": ["timezone"],
"timezone"
],
"properties": { "properties": {
"timezone": { "timezone": {
"type": "string", "type": "string",
"title": "Timezone", "title": "Timezone",
"enum": [ "enum": ["US/Eastern", "US/Pacific", "Europe/Berlin", "Australia/ACT"]
"US/Eastern",
"US/Pacific",
"Europe/Berlin",
"Australia/ACT"
]
} }
} }
} }

View File

@ -1,24 +1,34 @@
"""Test_get_localtime."""
import datetime import datetime
import pytz
import pytz
from flask.app import Flask from flask.app import Flask
from flask.testing import FlaskClient from flask.testing import FlaskClient
from spiffworkflow_backend.scripts.get_localtime import GetLocaltime
from spiffworkflow_backend.services.process_instance_processor import ProcessInstanceProcessor
from spiffworkflow_backend.services.process_instance_service import ProcessInstanceService
from tests.spiffworkflow_backend.helpers.base_test import BaseTest from tests.spiffworkflow_backend.helpers.base_test import BaseTest
from tests.spiffworkflow_backend.helpers.test_data import load_test_spec from tests.spiffworkflow_backend.helpers.test_data import load_test_spec
from spiffworkflow_backend.scripts.get_localtime import GetLocaltime
from spiffworkflow_backend.services.process_instance_processor import (
ProcessInstanceProcessor,
)
from spiffworkflow_backend.services.process_instance_service import (
ProcessInstanceService,
)
class TestGetLocaltime(BaseTest): class TestGetLocaltime(BaseTest):
"""TestProcessAPi.""" """TestProcessAPi."""
def test_get_localtime_script_directly(self) -> None: def test_get_localtime_script_directly(self) -> None:
"""Test_get_localtime_script_directly."""
current_time = datetime.datetime.now() current_time = datetime.datetime.now()
timezone = "US/Pacific" timezone = "US/Pacific"
result = GetLocaltime().run(task=None, environment_identifier='testing', datetime=current_time, timezone=timezone) result = GetLocaltime().run(
task=None,
environment_identifier="testing",
datetime=current_time,
timezone=timezone,
)
assert result == current_time.astimezone(pytz.timezone(timezone)) assert result == current_time.astimezone(pytz.timezone(timezone))
def test_get_localtime_script_through_bpmn( def test_get_localtime_script_through_bpmn(
@ -54,8 +64,8 @@ class TestGetLocaltime(BaseTest):
assert spiff_task assert spiff_task
data = spiff_task.data data = spiff_task.data
some_time = data['some_time'] some_time = data["some_time"]
localtime = data['localtime'] localtime = data["localtime"]
timezone = data['timezone'] timezone = data["timezone"]
assert localtime == some_time.astimezone(pytz.timezone(timezone)) assert localtime == some_time.astimezone(pytz.timezone(timezone))