-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update fix/analytics data export cleanup #4300
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,10 +12,7 @@ | |||||||||||||
SiteModel, | ||||||||||||||
ExceedanceModel, | ||||||||||||||
) | ||||||||||||||
from api.utils.data_formatters import ( | ||||||||||||||
filter_non_private_sites, | ||||||||||||||
filter_non_private_devices, | ||||||||||||||
) | ||||||||||||||
from api.utils.data_formatters import filter_non_private_sites_devices | ||||||||||||||
|
||||||||||||||
# Middlewares | ||||||||||||||
from api.utils.http import AirQoRequests | ||||||||||||||
|
@@ -45,12 +42,12 @@ class ChartDataResource(Resource): | |||||||||||||
"chartType|required:str", | ||||||||||||||
) | ||||||||||||||
def post(self): | ||||||||||||||
tenant = request.args.get("tenant", "airqo") | ||||||||||||||
network = request.args.get("network", "airqo") | ||||||||||||||
|
||||||||||||||
json_data = request.get_json() | ||||||||||||||
sites = filter_non_private_sites(sites=json_data.get("sites", {})).get( | ||||||||||||||
"sites", [] | ||||||||||||||
) | ||||||||||||||
sites = filter_non_private_sites_devices( | ||||||||||||||
filter_type="sites", filter_value=json_data.get("sites", {}) | ||||||||||||||
).get("data", []) | ||||||||||||||
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the default value for sites filter. Using an empty dict as the default value for - sites = filter_non_private_sites_devices(
- filter_type="sites", filter_value=json_data.get("sites", {})
- ).get("data", [])
+ sites = filter_non_private_sites_devices(
+ filter_type="sites", filter_value=json_data.get("sites", [])
+ ).get("data", []) 📝 Committable suggestion
Suggested change
|
||||||||||||||
start_date = json_data["startDate"] | ||||||||||||||
end_date = json_data["endDate"] | ||||||||||||||
frequency = json_data["frequency"] | ||||||||||||||
|
@@ -59,11 +56,10 @@ def post(self): | |||||||||||||
|
||||||||||||||
colors = ["#7F7F7F", "#E377C2", "#17BECF", "#BCBD22", "#3f51b5"] | ||||||||||||||
|
||||||||||||||
events_model = EventsModel(tenant) | ||||||||||||||
events_model = EventsModel(network) | ||||||||||||||
data = events_model.get_chart_events( | ||||||||||||||
sites, start_date, end_date, pollutant, frequency | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
chart_datasets = [] | ||||||||||||||
chart_labels = [] | ||||||||||||||
|
||||||||||||||
|
@@ -189,17 +185,18 @@ def _get_validated_filter(self, json_data): | |||||||||||||
filter_value = json_data.get(filter_type) | ||||||||||||||
|
||||||||||||||
if filter_type in sites: | ||||||||||||||
validated_value = filter_non_private_sites(filter_type, filter_value) | ||||||||||||||
validated_value = filter_non_private_sites_devices( | ||||||||||||||
filter_type, filter_value | ||||||||||||||
) | ||||||||||||||
elif filter_type in devices: | ||||||||||||||
validated_value = filter_non_private_devices(filter_type, filter_value) | ||||||||||||||
validated_value = filter_non_private_sites_devices( | ||||||||||||||
filter_type, filter_value | ||||||||||||||
) | ||||||||||||||
Comment on lines
+188
to
+194
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add filter type validation for device registry calls. The filter_type is passed directly from the provided_filters list without ensuring it matches the expected values ("devices" or "sites"): + def normalize_filter_type(filter_type: str) -> str:
+ if filter_type in sites:
+ return "sites"
+ if filter_type in devices:
+ return "devices"
+ return filter_type
if filter_type in sites:
validated_value = filter_non_private_sites_devices(
- filter_type, filter_value
+ normalize_filter_type(filter_type), filter_value
)
elif filter_type in devices:
validated_value = filter_non_private_sites_devices(
- filter_type, filter_value
+ normalize_filter_type(filter_type), filter_value
)
🧰 Tools🪛 Ruff (0.8.2)187-194: Combine Combine (SIM114) |
||||||||||||||
else: | ||||||||||||||
return filter_type, filter_value, None | ||||||||||||||
|
||||||||||||||
if validated_value and validated_value.get("status") == "success": | ||||||||||||||
# TODO This should be cleaned up. | ||||||||||||||
validated_data = validated_value.get("data", {}).get( | ||||||||||||||
"sites" if filter_type in sites else "devices", [] | ||||||||||||||
) | ||||||||||||||
validated_data = validated_value.get("data", []) | ||||||||||||||
else: | ||||||||||||||
error_message = validated_value.get("message", "Validation failed") | ||||||||||||||
|
||||||||||||||
|
@@ -274,9 +271,9 @@ def post(self): | |||||||||||||
pollutant = json_data["pollutant"] | ||||||||||||||
start_date = json_data["startDate"] | ||||||||||||||
end_date = json_data["endDate"] | ||||||||||||||
sites = filter_non_private_sites(sites=json_data.get("sites", {})).get( | ||||||||||||||
"sites", [] | ||||||||||||||
) | ||||||||||||||
sites = filter_non_private_sites_devices( | ||||||||||||||
filter_type="sites", filter_value=json_data.get("sites", {}) | ||||||||||||||
).get("data", []) | ||||||||||||||
Comment on lines
+274
to
+276
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the default value for sites filter. Using an empty dict as the default value for - sites = filter_non_private_sites_devices(
- filter_type="sites", filter_value=json_data.get("sites", {})
- ).get("data", [])
+ sites = filter_non_private_sites_devices(
+ filter_type="sites", filter_value=json_data.get("sites", [])
+ ).get("data", []) 📝 Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
events_model = EventsModel(tenant) | ||||||||||||||
site_model = SiteModel(tenant) | ||||||||||||||
|
@@ -333,15 +330,16 @@ class DailyAveragesResource2(Resource): | |||||||||||||
"devices|optional:list", | ||||||||||||||
) | ||||||||||||||
def post(self): | ||||||||||||||
tenant = request.args.get("tenant", "airqo") | ||||||||||||||
network = request.args.get("network", "airqo") | ||||||||||||||
json_data = request.get_json() | ||||||||||||||
pollutant = json_data["pollutant"] | ||||||||||||||
start_date = json_data["startDate"] | ||||||||||||||
end_date = json_data["endDate"] | ||||||||||||||
devices = filter_non_private_devices(devices=json_data.get("devices", {})).get( | ||||||||||||||
"devices", [] | ||||||||||||||
) | ||||||||||||||
events_model = EventsModel(tenant) | ||||||||||||||
devices = filter_non_private_sites_devices( | ||||||||||||||
filter_type="devices", filter_value=json_data.get("devices", {}) | ||||||||||||||
).get("data", []) | ||||||||||||||
Comment on lines
+338
to
+340
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the default value for devices filter. Using an empty dict as the default value for - devices = filter_non_private_sites_devices(
- filter_type="devices", filter_value=json_data.get("devices", {})
- ).get("data", [])
+ devices = filter_non_private_sites_devices(
+ filter_type="devices", filter_value=json_data.get("devices", [])
+ ).get("data", []) 📝 Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
events_model = EventsModel(network) | ||||||||||||||
data = events_model.get_device_averages_from_bigquery( | ||||||||||||||
start_date, end_date, pollutant, devices=devices | ||||||||||||||
) | ||||||||||||||
|
@@ -391,9 +389,9 @@ def post(self): | |||||||||||||
standard = json_data["standard"] | ||||||||||||||
start_date = json_data["startDate"] | ||||||||||||||
end_date = json_data["endDate"] | ||||||||||||||
sites = filter_non_private_sites(sites=json_data.get("sites", {})).get( | ||||||||||||||
"sites", [] | ||||||||||||||
) | ||||||||||||||
sites = filter_non_private_sites_devices( | ||||||||||||||
filter_type="sites", filter_value=json_data.get("sites", {}) | ||||||||||||||
).get("data", []) | ||||||||||||||
Comment on lines
+392
to
+394
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the default value for sites filter. Using an empty dict as the default value for - sites = filter_non_private_sites_devices(
- filter_type="sites", filter_value=json_data.get("sites", {})
- ).get("data", [])
+ sites = filter_non_private_sites_devices(
+ filter_type="sites", filter_value=json_data.get("sites", [])
+ ).get("data", []) 📝 Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
exc_model = ExceedanceModel(tenant) | ||||||||||||||
data = exc_model.get_exceedances( | ||||||||||||||
|
@@ -427,9 +425,9 @@ def post(self): | |||||||||||||
standard = json_data["standard"] | ||||||||||||||
start_date = json_data["startDate"] | ||||||||||||||
end_date = json_data["endDate"] | ||||||||||||||
devices = filter_non_private_devices(devices=json_data.get("devices", {})).get( | ||||||||||||||
"devices", [] | ||||||||||||||
) | ||||||||||||||
devices = filter_non_private_sites_devices( | ||||||||||||||
filter_type="devices", filter_value=json_data.get("devices", {}) | ||||||||||||||
).get("data", []) | ||||||||||||||
Comment on lines
+428
to
+430
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the default value for devices filter. Using an empty dict as the default value for - devices = filter_non_private_sites_devices(
- filter_type="devices", filter_value=json_data.get("devices", {})
- ).get("data", [])
+ devices = filter_non_private_sites_devices(
+ filter_type="devices", filter_value=json_data.get("devices", [])
+ ).get("data", []) 📝 Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
events_model = EventsModel(tenant) | ||||||||||||||
data = events_model.get_device_readings_from_bigquery( | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance input validation and error handling.
The implementation could be more robust with additional validations and consistent error handling:
📝 Committable suggestion