Skip to content

feat: exclude fields #528

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

Merged
merged 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions integrations/server/test_covidcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,31 @@ def test_fields(self):
'message': 'success',
})


# limit exclude fields
response = requests.get(BASE_URL, params={
'endpoint': 'covidcast',
'data_source': 'src',
'signal': 'sig',
'time_type': 'day',
'geo_type': 'county',
'time_values': 20200414,
'geo_value': '01234',
'fields': '-value,-stderr,-sample_size,-direction,-issue,-lag,-signal'
})
response.raise_for_status()
response = response.json()

# assert that the right data came back
self.assertEqual(response, {
'result': 1,
'epidata': [{
'time_value': 20200414,
'geo_value': '01234'
}],
'message': 'success',
})

def test_location_wildcard(self):
"""Select all locations with a wildcard query."""

Expand Down
30 changes: 24 additions & 6 deletions src/server/_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,21 @@ def filter_fields(generator: Iterable[Dict[str, Any]]):
if not fields:
yield from generator
else:
exclude_fields = {f[1:] for f in fields if f.startswith("-")}
include_fields = [f for f in fields if not f.startswith("-") and f not in exclude_fields]

for row in generator:
filtered = dict()
for field in fields:
if field in row:
filtered[field] = row[field]
if include_fields:
# positive list
for field in include_fields:
if field in row:
filtered[field] = row[field]
elif exclude_fields:
# negative list
for k, v in row.items():
if k not in exclude_fields:
filtered[k] = v
yield filtered


Expand Down Expand Up @@ -252,9 +262,17 @@ def execute_queries(

fields_to_send = set(extract_strings("fields") or [])
if fields_to_send:
fields_string = [v for v in fields_string if v in fields_to_send]
fields_int = [v for v in fields_int if v in fields_to_send]
fields_float = [v for v in fields_float if v in fields_to_send]
exclude_fields = {f[1:] for f in fields_to_send if f.startswith("-")}
include_fields = {f for f in fields_to_send if not f.startswith("-") and f not in exclude_fields}

if include_fields:
fields_string = [v for v in fields_string if v in include_fields]
fields_int = [v for v in fields_int if v in include_fields]
fields_float = [v for v in fields_float if v in include_fields]
if exclude_fields:
fields_string = [v for v in fields_string if v not in exclude_fields]
fields_int = [v for v in fields_int if v not in exclude_fields]
fields_float = [v for v in fields_float if v not in exclude_fields]

query_list = list(queries)

Expand Down