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

Google Analytics runner - iterate over keys the Python 3 way #4538

Merged
merged 2 commits into from
Jan 13, 2020

Conversation

rauchy
Copy link
Contributor

@rauchy rauchy commented Jan 12, 2020

What type of PR is this? (check all applicable)

  • Bug Fix

Description

Following #4181, Google Analytics queries were broken since dict .keys() returns dict_keys, which would in turn yield wrong results when iterating over param values, resulting in errors such as

Parameter "end_date" value "y,e,s,t,e,r,d,a,y" does not match the pattern "[0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo)"

Related Tickets & Documents

#4181

@rauchy rauchy requested a review from arikfr January 12, 2020 21:50
@@ -164,7 +164,7 @@ def run_query(self, query, user):
params = json_loads(query)
except:
params = parse_qs(urlparse(query).query, keep_blank_values=True)
for key in params.keys():
for key in [*params]:
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this is the issue:

>>> d = {'ab': 'yesterday', 'cd': 2}
>>> for k in d.keys():
...     print(k)
... 
ab
cd

(this shows that the keys are iterated properly)

But, check the following:

>>> ",".join("yesterday")
'y,e,s,t,e,r,d,a,y'

So the issue is actually line 168 which applies ",".join on any value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The real problem is line 170, which messes around with the dict_keys iterator, something that didn't happen in Python 2. Populating a new list of keys with [*params] solves this.

Copy link
Member

@arikfr arikfr left a comment

Choose a reason for hiding this comment

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

Ok, I understood the real problem and the solution. To help people like myself I've added a few comments as suggested commits.

@@ -164,7 +164,7 @@ def run_query(self, query, user):
params = json_loads(query)
except:
params = parse_qs(urlparse(query).query, keep_blank_values=True)
for key in params.keys():
for key in [*params]:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for key in [*params]:
# Because we change keys list on line 170, we need to create a copy of the keys
for key in [*params]:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another option that would make fiddling with iterators (and the need to explain it) redundant is:

 query_string = parse_qs(urlparse(query).query, keep_blank_values=True) 
 params = {k.replace('-', '_'): ",".join(v) for k,v in query_string.items() }

We'll still have to explain ",".join, but maybe we don't have to.

Copy link
Member

Choose a reason for hiding this comment

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

I like this 👍

@@ -164,7 +164,7 @@ def run_query(self, query, user):
params = json_loads(query)
except:
params = parse_qs(urlparse(query).query, keep_blank_values=True)
for key in params.keys():
for key in [*params]:
params[key] = ",".join(params[key])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
params[key] = ",".join(params[key])
# parse_qs returns every value as a list
params[key] = ",".join(params[key])

@rauchy rauchy merged commit fe06f7f into master Jan 13, 2020
@rauchy rauchy deleted the google-analytics-params-python-3-fix branch January 13, 2020 12:41
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.

2 participants