-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from GSA-TTS/reports
Data Submission Reports Feature
- Loading branch information
Showing
21 changed files
with
388 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[flake8] | ||
max-line-length = 88 | ||
exclude = venv,.git,__pycache__,docs/source/conf.py,old,build,dist,node_modules | ||
ignore = F403, F401 | ||
ignore = F403, F401, W503 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Add report column | ||
Revision ID: 851709d3a162 | ||
Revises: c5596492c87b | ||
Create Date: 2024-02-06 13:22:25.266733 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.types import JSON | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "851709d3a162" | ||
down_revision: Union[str, None] = "c5596492c87b" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade(): | ||
op.add_column( | ||
"data_submissions", | ||
sa.Column("report", JSON, nullable=True), | ||
) | ||
|
||
|
||
def add_column(): | ||
op.drop_column("data_submissions", "report") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,73 @@ | ||
from dataclasses import dataclass | ||
from dataclasses import dataclass, asdict, field, is_dataclass | ||
from typing import List | ||
import numpy as np | ||
|
||
|
||
@dataclass | ||
class DownloadResult: | ||
temp_dir: str | ||
extracted_dir: str | ||
|
||
|
||
@dataclass | ||
class DataSubmissionReportOverview: | ||
feature_count: int = 0 | ||
features_flagged: int = 0 | ||
etl_update_required: bool = False | ||
data_update_required: bool = False | ||
|
||
|
||
@dataclass | ||
class DataSubmissionReportFeature: | ||
provided_feature_name: str | ||
nad_feature_name: str | ||
populated_count: int | ||
null_count: int | ||
|
||
|
||
@dataclass | ||
class DataSubmissionReport: | ||
overview: DataSubmissionReportOverview | ||
features: List[DataSubmissionReportFeature] = field(default_factory=list) | ||
|
||
|
||
def report_to_dict(data_submission_report: DataSubmissionReport) -> dict: | ||
""" | ||
Converts a DataSubmissionReport instance into a dictionary because all data types | ||
within the dictionary must be JSON-serializable. | ||
""" | ||
return convert(asdict(data_submission_report)) | ||
|
||
|
||
def report_from_dict(data: dict) -> DataSubmissionReport: | ||
""" | ||
Creates a DataSubmissionReport instance from a dictionary, reconstructing the | ||
overview and features properties from their respective dictionary representations. | ||
""" | ||
|
||
overview_data = data.get("overview", {}) | ||
features_data = data.get("features", []) | ||
|
||
overview = DataSubmissionReportOverview(**overview_data) | ||
features = [ | ||
DataSubmissionReportFeature(**feature_data) for feature_data in features_data | ||
] | ||
|
||
return DataSubmissionReport(overview=overview, features=features) | ||
|
||
|
||
def convert(item): | ||
""" | ||
Recursively converts items within a data structure (including dictionaries, lists, | ||
and dataclass instances) such that all numeric types are JSON-serializable. | ||
""" | ||
if isinstance(item, dict): | ||
return {k: convert(v) for k, v in item.items()} | ||
elif isinstance(item, list): | ||
return [convert(i) for i in item] | ||
elif isinstance(item, (np.int64, np.int32, np.float64, np.float32)): | ||
return int(item) if isinstance(item, (np.int64, np.int32)) else float(item) | ||
elif is_dataclass(item): | ||
return convert(asdict(item)) | ||
else: | ||
return item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,26 @@ | ||
from typing import List | ||
from geopandas import GeoDataFrame | ||
from nad_ch.application.dtos import DataSubmissionReportFeature | ||
|
||
|
||
def get_feature_count(gdf: GeoDataFrame) -> int: | ||
return len(gdf) | ||
|
||
|
||
def get_feature_details(gdf: GeoDataFrame) -> List[DataSubmissionReportFeature]: | ||
report_features = [] | ||
|
||
for column in gdf.columns: | ||
populated_count = gdf[column].notna().sum() | ||
null_count = gdf[column].isna().sum() | ||
|
||
report_feature = DataSubmissionReportFeature( | ||
provided_feature_name=column, | ||
nad_feature_name=column, | ||
populated_count=populated_count, | ||
null_count=null_count, | ||
) | ||
|
||
report_features.append(report_feature) | ||
|
||
return report_features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.