Skip to content
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

GTC-2659: "include_attribute" query param does not behave as expected #130

Merged
merged 28 commits into from
Jan 23, 2024

Conversation

gtempus
Copy link
Contributor

@gtempus gtempus commented Jan 11, 2024

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Make sure you are requesting to pull a topic/feature/bugfix branch (right side). Don't request your master!
  • Make sure you are making a pull request against the develop branch (left side). Also you should start your branch off our develop.
  • Check the commit's or even all commits' message styles matches our requested structure.
  • Check your code additions will fail neither code linting checks nor unit test.

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Nasa Viirs Fire Alerts Tile Endpoint

Two main issues surfaced when tasked with supporting FLAG-914

  1. Stale Attributes
    The include_attribute query param lists all of the possible attributes for the latest version of the dataset at the time the server was started. The uptime of the server should not be a factor in what data is available. I belive this design was implemented to populate the developer docs with proper attributes in the form of an enumeration. However, it also turned out to be misleading for clients of this endpoint and leading to the second issue.
  2. Unsupported Attributes
    This was the biggest point of confusion for clients. When the dynamic tile is created, it's possible that several of the points can represent more than one detected fire. Each point has an attribute: count, and it's a value from 1 to n.
    When multiple points are involved, aggregation of any included attributes must be calculated. For example, latitutude and longitude (if included in the include_attributes URL as a query param) need to be averaged.
    There was only a subset of all possible attributes with these types of aggregation rules and they were hard-coded into a dict in the source.
    Even though an attribute was advertised as available to include (see issue one, above) it would cause the API to return a 500 Server Error when a client tried to pass it as a value to the include_attribute query param.

Issue Number: GTC-2659

What is the new behavior?

The advertised attributes to include are now only the ones in which aggregation rules exist. Those SupportedAttributes are an enhanced enum that stores the aggregation code with the enumeration so there can be no error when submitting a request with include_attribute.
For future requests to add another attribute, an enum will be added to the collection with a corresponding aggregation rule in one place in the code. No other changes will be necessary.
Also, attributes for the version are validated per request (requests are still cached) instead of when the server is stood up. Therefore, should older versions be requested, error responses can be much more helpful to the developer.

Does this introduce a breaking change?

  • Yes
  • No

Testing

There is an updated nasa_viirs_fire_alerts dataset in staging. Here is an example request that uses several include_attribute params:
https://staging-tiles.globalforestwatch.org/nasa_viirs_fire_alerts/v20231227/dynamic/0/0/0.pbf?start_date=2023-12-01&end_date=2023-12-31&high_confidence_only=false&force_date_range=True&include_attribute=alert__date&include_attribute=alert__time_utc&include_attribute=bright_ti4__K&include_attribute=frp__MW&include_attribute=latitude&include_attribute=longitude&include_attribute=confidence__cat&include_attribute=umd_tree_cover_density_2000__threshold&include_attribute=umd_tree_cover_density__threshold

Other Notes

I will follow up this PR with more developer experience (DX) improvements to documentation and I'll also add appropriate unit tests. The existing tests are far too dependent on the database and other technologies and need to be decoupled.

@gtempus gtempus force-pushed the gtc-2663--agg-rules-for-validation branch from 151c3fa to 8490bf0 Compare January 18, 2024 16:30
@codecov-commenter
Copy link

codecov-commenter commented Jan 18, 2024

Codecov Report

Attention: 37 lines in your changes are missing coverage. Please review.

Comparison is base (2894f01) 79.49% compared to head (8071e29) 78.74%.

Files Patch % Lines
app/utils/data_api.py 24.13% 22 Missing ⚠️
...ud/async_db/vector_tiles/nasa_viirs_fire_alerts.py 22.22% 7 Missing ⚠️
app/crud/sync_db/tile_cache_assets.py 57.14% 3 Missing ⚠️
app/routes/dynamic_vector_tiles.py 33.33% 2 Missing ⚠️
app/routes/nasa_viirs_fire_alerts/__init__.py 50.00% 2 Missing ⚠️
...ors/nasa_viirs_fire_alerts/supported_attributes.py 94.44% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #130      +/-   ##
==========================================
- Coverage   79.49%   78.74%   -0.76%     
==========================================
  Files          53       53              
  Lines        1780     1778       -2     
==========================================
- Hits         1415     1400      -15     
- Misses        365      378      +13     
Flag Coverage Δ
unittests 78.74% <47.88%> (-0.76%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gtempus gtempus force-pushed the gtc-2663--agg-rules-for-validation branch from 44db630 to 563b71a Compare January 18, 2024 22:31
@gtempus gtempus force-pushed the gtc-2663--agg-rules-for-validation branch from 1db2c5a to d143171 Compare January 19, 2024 16:02
@gtempus gtempus marked this pull request as ready for review January 19, 2024 16:39
@gtempus gtempus requested a review from dmannarino January 19, 2024 16:39
@gtempus gtempus changed the title Gtc 2663 agg rules for validation GTC-2659: "include_attribute" query param does not behave as expected Jan 19, 2024
elif len(rows) == 0:
logger.warning("Zero rows. There are no tile caches registered with the API")
else:
logger.warning(f"Found {len(rows)} rows")
# tile_caches = rows
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll admit having no tile caches is a rare situation (likely only in dev) but, in the interest of being thorough, let's clean up this bit which I vaguely recall we added while pairing. :)
The problem is that we check if rows is None (which it looks like it might be if there are no rows: Gino returns None, SQA would return an empty iterable IIRC), but then go on to do a for "for row in rows". If rows is None this will break because None is not iterable. So I think I would reduce this selection to logging a warning and then exiting early. For example:
if rows is None:
logger.warning("There are no tile caches registered with the API")
return []
for row in rows:
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, @dmannarino! 💪
I hadn't looked at that code since we paired on it.

I did refactor it. However, the function returns a dict. What I wanted to ensure is that rows are iterable as you suggest. Then the list comprehension never runs and the function returns the tile cache dict as before.

What do you think? My changes are in this commit: 8071e29

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, that looks good! I forgot the return value was a dictionary.

Copy link
Member

@dmannarino dmannarino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@gtempus gtempus merged commit 55c01b1 into dev Jan 23, 2024
3 of 4 checks passed
@gtempus gtempus deleted the gtc-2663--agg-rules-for-validation branch April 30, 2024 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants