-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add GoogleAnalyticsReportingToPostgresOperator & dimensionFilterClauses option #10
Open
smowden
wants to merge
9
commits into
airflow-plugins:master
Choose a base branch
from
smowden:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9b2afae
add option to specify a dimension filter
smowden a05c023
add GoogleAnalyticsReportingToPostgresOperator
smowden b088f50
add new operator to plugin
smowden 0b7f17a
fix key name
smowden 6852535
dont hardcode key file name
smowden be66b63
template destination_table
smowden 904a09a
add log
smowden 03dcc7d
add more debug info
smowden eb89999
use settings.AIRFLOW_HOME (support newer airflow version)
smowden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
178 changes: 178 additions & 0 deletions
178
operators/google_analytics_reporting_to_postgres_operator.py
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,178 @@ | ||
from datetime import datetime | ||
|
||
from airflow.hooks.postgres_hook import PostgresHook | ||
from airflow.models import BaseOperator | ||
import pandas as pd | ||
|
||
from google_analytics_plugin.hooks.google_analytics_hook import GoogleAnalyticsHook | ||
|
||
|
||
class GoogleAnalyticsReportingToPostgresOperator(BaseOperator): | ||
""" | ||
Google Analytics Reporting To S3 Operator | ||
|
||
:param google_analytics_conn_id: The Google Analytics connection id. | ||
:type google_analytics_conn_id: string | ||
:param view_id: The view id for associated report. | ||
:type view_id: string/array | ||
:param since: The date up from which to pull GA data. | ||
This can either be a string in the format | ||
of '%Y-%m-%d %H:%M:%S' or '%Y-%m-%d' | ||
but in either case it will be | ||
passed to GA as '%Y-%m-%d'. | ||
:type since: string | ||
:param until: The date up to which to pull GA data. | ||
This can either be a string in the format | ||
of '%Y-%m-%d %H:%M:%S' or '%Y-%m-%d' | ||
but in either case it will be | ||
passed to GA as '%Y-%m-%d'. | ||
:type until: string | ||
|
||
|
||
:type postgres_conn_id: string | ||
:param google_analytics_conn_id The Postgres connection id | ||
|
||
:type destination_table: string | ||
:param destination_table: Table to be created in the database | ||
|
||
:type destination_table_dtypes: dict | ||
:param destination_table_dtypes Dictionary containing column/sqlalchemy type mapping | ||
|
||
:type if_exists: string | ||
:param if_exists What to do if the table exists. Options: fail,replace,append. | ||
See pandas documetation for to_sql for more | ||
|
||
:type destination_schema string | ||
:param destination_schema Database schema where to create the table | ||
""" | ||
|
||
template_fields = ('since', | ||
'until', | ||
'destination_table') | ||
|
||
def __init__(self, | ||
google_analytics_conn_id, | ||
view_id, | ||
since, | ||
until, | ||
dimensions, | ||
metrics, | ||
postgres_conn_id, | ||
destination_table, | ||
destination_schema=None, | ||
if_exists='fail', | ||
destination_table_dtypes=None, | ||
page_size=1000, | ||
include_empty_rows=True, | ||
sampling_level=None, | ||
dimension_filter_clauses=None, | ||
key_file=None, | ||
*args, | ||
**kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self.google_analytics_conn_id = google_analytics_conn_id | ||
self.view_id = view_id | ||
self.since = since | ||
self.until = until | ||
self.sampling_level = sampling_level | ||
self.dimensions = dimensions | ||
self.metrics = metrics | ||
self.page_size = page_size | ||
self.include_empty_rows = include_empty_rows | ||
self.postgres_conn_id = postgres_conn_id | ||
self.destination_schema = destination_schema | ||
self.destination_table = destination_table | ||
self.destination_table_dtypes = destination_table_dtypes | ||
self.if_exists = if_exists | ||
self.dimension_filter_clauses = dimension_filter_clauses | ||
self.key_file=key_file | ||
|
||
self.metricMap = { | ||
'METRIC_TYPE_UNSPECIFIED': 'varchar(255)', | ||
'CURRENCY': 'decimal(20,5)', | ||
'INTEGER': 'int(11)', | ||
'FLOAT': 'decimal(20,5)', | ||
'PERCENT': 'decimal(20,5)', | ||
'TIME': 'time' | ||
} | ||
|
||
if self.page_size > 10000: | ||
raise Exception('Please specify a page size equal to or lower than 10000.') | ||
|
||
if not isinstance(self.include_empty_rows, bool): | ||
raise Exception('Please specificy "include_empty_rows" as a boolean.') | ||
|
||
def execute(self, context): | ||
ga_conn = GoogleAnalyticsHook(self.google_analytics_conn_id, key_file=self.key_file) | ||
try: | ||
since_formatted = datetime.strptime(self.since, '%Y-%m-%d %H:%M:%S').strftime( | ||
'%Y-%m-%d') | ||
except: | ||
since_formatted = str(self.since) | ||
try: | ||
until_formatted = datetime.strptime(self.until, '%Y-%m-%d %H:%M:%S').strftime( | ||
'%Y-%m-%d') | ||
except: | ||
until_formatted = str(self.until) | ||
report = ga_conn.get_analytics_report(self.view_id, | ||
since_formatted, | ||
until_formatted, | ||
self.sampling_level, | ||
self.dimensions, | ||
self.metrics, | ||
self.page_size, | ||
self.include_empty_rows, | ||
dimension_filter_clauses=self.dimension_filter_clauses | ||
) | ||
|
||
columnHeader = report.get('columnHeader', {}) | ||
# Right now all dimensions are hardcoded to varchar(255), will need a map if any non-varchar dimensions are used in the future | ||
# Unfortunately the API does not send back types for Dimensions like it does for Metrics (yet..) | ||
dimensionHeaders = [ | ||
{'name': header.replace('ga:', ''), 'type': 'varchar(255)'} | ||
for header | ||
in columnHeader.get('dimensions', []) | ||
] | ||
metricHeaders = [ | ||
{'name': entry.get('name').replace('ga:', ''), | ||
'type': self.metricMap.get(entry.get('type'), 'varchar(255)')} | ||
for entry | ||
in columnHeader.get('metricHeader', {}).get('metricHeaderEntries', []) | ||
] | ||
|
||
print('samples read', report.get('data', {}).get('samplesReadCounts', 0)) | ||
print('total read', report.get('data', {}).get('totals', 0)) | ||
print('row count', report.get('data', {}).get('rowCount', 0)) | ||
|
||
rows = report.get('data', {}).get('rows', []) | ||
all_data = [] | ||
for row_counter, row in enumerate(rows): | ||
root_data_obj = {} | ||
dimensions = row.get('dimensions', []) | ||
metrics = row.get('metrics', []) | ||
|
||
for index, dimension in enumerate(dimensions): | ||
header = dimensionHeaders[index].get('name').lower() | ||
root_data_obj[header] = dimension | ||
for metric in metrics: | ||
data = {} | ||
data.update(root_data_obj) | ||
|
||
for index, value in enumerate(metric.get('values', [])): | ||
header = metricHeaders[index].get('name').lower() | ||
data[header] = value | ||
|
||
data['viewid'] = self.view_id | ||
data['timestamp'] = self.since | ||
|
||
all_data.append(data) | ||
|
||
df_google_data = pd.DataFrame(all_data) | ||
postgres_hook = PostgresHook(self.postgres_conn_id) | ||
df_google_data.to_sql(name=self.destination_table, | ||
con=postgres_hook.get_sqlalchemy_engine(), | ||
dtype=self.destination_table_dtypes, | ||
if_exists=self.if_exists, | ||
schema=self.destination_schema | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just a typo: :param postgres_conn_id