Skip to content

Commit

Permalink
Update access logic
Browse files Browse the repository at this point in the history
  • Loading branch information
maxachis committed Jan 9, 2025
1 parent 213077e commit 72065e5
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from resources.LoginWithGithub import namespace_login_with_github
from resources.Map import namespace_map
from resources.Match import namespace_match
from resources.Metrics import namespace_metrics
from resources.Notifications import namespace_notifications
from resources.OAuth import namespace_oauth
from resources.Permissions import namespace_permissions
Expand Down Expand Up @@ -70,6 +71,7 @@
namespace_bulk,
namespace_match,
namespace_locations,
namespace_metrics,
]

MY_PREFIX = "/api"
Expand Down
45 changes: 45 additions & 0 deletions database_client/database_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,3 +1486,48 @@ def get_location_by_id(self, location_id: int):
where_mappings={"id": location_id},
alias_mappings={"id": "location_id"},
)

def get_metrics(self):

Check warning on line 1490 in database_client/database_client.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] database_client/database_client.py#L1490 <102>

Missing docstring in public method
Raw output
./database_client/database_client.py:1490:1: D102 Missing docstring in public method
result = self.execute_raw_sql(
"""
SELECT
COUNT(*),
'source_count' "Count Type"
FROM
DATA_SOURCES
UNION
SELECT
COUNT(DISTINCT (AGENCY_ID)),
'agency_count' "Count Type"
FROM
LINK_AGENCIES_DATA_SOURCES
UNION
SELECT
COUNT(DISTINCT L.ID),
'state_count' "Count Type"
FROM
LINK_AGENCIES_DATA_SOURCES LINK
INNER JOIN AGENCIES A ON A.ID = LINK.AGENCY_ID
JOIN DEPENDENT_LOCATIONS DL ON A.LOCATION_ID = DL.DEPENDENT_LOCATION_ID
JOIN LOCATIONS L ON L.ID = A.LOCATION_ID
OR L.ID = DL.PARENT_LOCATION_ID
WHERE
L.TYPE = 'State'
UNION
SELECT
COUNT(DISTINCT L.ID),
'county_count' "Count Type"
FROM
LINK_AGENCIES_DATA_SOURCES LINK
INNER JOIN AGENCIES A ON A.ID = LINK.AGENCY_ID
JOIN DEPENDENT_LOCATIONS DL ON A.LOCATION_ID = DL.DEPENDENT_LOCATION_ID
JOIN LOCATIONS L ON L.ID = A.LOCATION_ID
OR L.ID = DL.PARENT_LOCATION_ID
WHERE
L.TYPE = 'County'
"""

Check warning on line 1528 in database_client/database_client.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] database_client/database_client.py#L1528 <191>

indentation contains tabs
Raw output
./database_client/database_client.py:1528:1: W191 indentation contains tabs

Check failure on line 1528 in database_client/database_client.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] database_client/database_client.py#L1528 <101>

indentation contains mixed spaces and tabs
Raw output
./database_client/database_client.py:1528:1: E101 indentation contains mixed spaces and tabs
)
d = {}
for row in result:
d[row["Count Type"]] = row["count"]
return d
2 changes: 1 addition & 1 deletion middleware/SimpleJWT.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Union

import jwt
from jwt import ExpiredSignatureError, InvalidSignatureError
from jwt import InvalidSignatureError

from middleware.flask_response_manager import FlaskResponseManager
from middleware.util import get_env_variable
Expand Down
5 changes: 5 additions & 0 deletions middleware/primary_resource_logic/metrics_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from database_client.database_client import DatabaseClient

Check warning on line 1 in middleware/primary_resource_logic/metrics_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/metrics_logic.py#L1 <100>

Missing docstring in public module
Raw output
./middleware/primary_resource_logic/metrics_logic.py:1:1: D100 Missing docstring in public module


def get_metrics(db_client: DatabaseClient):

Check warning on line 4 in middleware/primary_resource_logic/metrics_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/metrics_logic.py#L4 <103>

Missing docstring in public function
Raw output
./middleware/primary_resource_logic/metrics_logic.py:4:1: D103 Missing docstring in public function
return db_client.get_metrics()
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from marshmallow import Schema, fields

Check warning on line 1 in middleware/schema_and_dto_logic/primary_resource_schemas/metrics_schemas.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/primary_resource_schemas/metrics_schemas.py#L1 <100>

Missing docstring in public module
Raw output
./middleware/schema_and_dto_logic/primary_resource_schemas/metrics_schemas.py:1:1: D100 Missing docstring in public module

from middleware.schema_and_dto_logic.util import get_json_metadata


class MetricsGetResponseSchema(Schema):

Check warning on line 6 in middleware/schema_and_dto_logic/primary_resource_schemas/metrics_schemas.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/primary_resource_schemas/metrics_schemas.py#L6 <101>

Missing docstring in public class
Raw output
./middleware/schema_and_dto_logic/primary_resource_schemas/metrics_schemas.py:6:1: D101 Missing docstring in public class
source_count = fields.Int(metadata=get_json_metadata("The number of data sources"))
agency_count = fields.Int(metadata=get_json_metadata("The number of agencies"))
county_count = fields.Int(metadata=get_json_metadata("The number of counties"))
state_count = fields.Int(metadata=get_json_metadata("The number of states"))
29 changes: 29 additions & 0 deletions resources/Metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from flask import Response

Check warning on line 1 in resources/Metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] resources/Metrics.py#L1 <100>

Missing docstring in public module
Raw output
./resources/Metrics.py:1:1: D100 Missing docstring in public module

from middleware.access_logic import AccessInfoPrimary, GET_AUTH_INFO
from middleware.decorators import endpoint_info
from middleware.primary_resource_logic.metrics_logic import get_metrics
from resources.PsycopgResource import PsycopgResource
from resources.endpoint_schema_config import SchemaConfigs
from resources.resource_helpers import ResponseInfo
from utilities.namespace import AppNamespaces, create_namespace

namespace_metrics = create_namespace(AppNamespaces.METRICS)


@namespace_metrics.route("")
class Metrics(PsycopgResource):

Check warning on line 15 in resources/Metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] resources/Metrics.py#L15 <101>

Missing docstring in public class
Raw output
./resources/Metrics.py:15:1: D101 Missing docstring in public class

@endpoint_info(
namespace=namespace_metrics,
auth_info=GET_AUTH_INFO,
schema_config=SchemaConfigs.METRICS_GET,
description="Returns the metrics for the application.",
response_info=ResponseInfo(
success_message="Returns the metrics for the application."
),
)
def get(self, access_info: AccessInfoPrimary) -> Response:

Check warning on line 26 in resources/Metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] resources/Metrics.py#L26 <102>

Missing docstring in public method
Raw output
./resources/Metrics.py:26:1: D102 Missing docstring in public method

Check warning on line 26 in resources/Metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] resources/Metrics.py#L26 <100>

Unused argument 'access_info'
Raw output
./resources/Metrics.py:26:19: U100 Unused argument 'access_info'
return self.run_endpoint(
wrapper_function=get_metrics,
)
10 changes: 10 additions & 0 deletions resources/endpoint_schema_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
AgencyMatchSchema,
MatchAgencyResponseSchema,
)
from middleware.schema_and_dto_logic.primary_resource_schemas.metrics_schemas import (
MetricsGetResponseSchema,
)
from middleware.schema_and_dto_logic.primary_resource_schemas.reset_token_schemas import (
ResetPasswordSchema,
)
Expand Down Expand Up @@ -548,3 +551,10 @@ class SchemaConfigs(Enum):
primary_output_schema=GetManyDataRequestsResponseSchema(),
)
# endregion

# region Metrics
METRICS_GET = EndpointSchemaConfig(
primary_output_schema=MetricsGetResponseSchema(),
)

# endregion
10 changes: 10 additions & 0 deletions tests/helper_scripts/helper_classes/RequestValidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,4 +610,14 @@ def get_location_related_data_requests(
expected_schema=SchemaConfigs.LOCATIONS_RELATED_DATA_REQUESTS_GET.value.primary_output_schema,
)

def get_metrics(

Check warning on line 613 in tests/helper_scripts/helper_classes/RequestValidator.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/helper_scripts/helper_classes/RequestValidator.py#L613 <102>

Missing docstring in public method
Raw output
./tests/helper_scripts/helper_classes/RequestValidator.py:613:1: D102 Missing docstring in public method
self,
headers: dict,
):
return self.get(
endpoint=f"/api/metrics",

Check warning on line 618 in tests/helper_scripts/helper_classes/RequestValidator.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/helper_scripts/helper_classes/RequestValidator.py#L618 <541>

f-string is missing placeholders
Raw output
./tests/helper_scripts/helper_classes/RequestValidator.py:618:22: F541 f-string is missing placeholders
headers=headers,
expected_schema=SchemaConfigs.METRICS_GET.value.primary_output_schema,
)

# endregion
17 changes: 17 additions & 0 deletions tests/integration/test_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from tests.helper_scripts.helper_classes.TestDataCreatorFlask import (

Check warning on line 1 in tests/integration/test_metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/integration/test_metrics.py#L1 <100>

Missing docstring in public module
Raw output
./tests/integration/test_metrics.py:1:1: D100 Missing docstring in public module
TestDataCreatorFlask,
)
from conftest import test_data_creator_flask, monkeysession

Check warning on line 4 in tests/integration/test_metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/integration/test_metrics.py#L4 <401>

'conftest.test_data_creator_flask' imported but unused
Raw output
./tests/integration/test_metrics.py:4:1: F401 'conftest.test_data_creator_flask' imported but unused

Check warning on line 4 in tests/integration/test_metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/integration/test_metrics.py#L4 <401>

'conftest.monkeysession' imported but unused
Raw output
./tests/integration/test_metrics.py:4:1: F401 'conftest.monkeysession' imported but unused


def test_metrics(test_data_creator_flask: TestDataCreatorFlask):

Check warning on line 7 in tests/integration/test_metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/integration/test_metrics.py#L7 <103>

Missing docstring in public function
Raw output
./tests/integration/test_metrics.py:7:1: D103 Missing docstring in public function

Check warning on line 7 in tests/integration/test_metrics.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/integration/test_metrics.py#L7 <811>

redefinition of unused 'test_data_creator_flask' from line 4
Raw output
./tests/integration/test_metrics.py:7:18: F811 redefinition of unused 'test_data_creator_flask' from line 4

tdc = test_data_creator_flask
metrics = tdc.request_validator.get_metrics(
headers=tdc.get_admin_tus().jwt_authorization_header
)

assert metrics["source_count"] > 0
assert metrics["agency_count"] > 0
assert metrics["county_count"] > 0
assert metrics["state_count"] > 0
1 change: 1 addition & 0 deletions utilities/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AppNamespaces(Enum):
BULK = NamespaceAttributes(path="bulk", description="Bulk Namespace")
MATCH = NamespaceAttributes(path="match", description="Match Namespace")
LOCATIONS = NamespaceAttributes(path="locations", description="Locations Namespace")
METRICS = NamespaceAttributes(path="metrics", description="Metrics Namespace")


def create_namespace(
Expand Down

0 comments on commit 72065e5

Please sign in to comment.