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

Add ingesting parent segment configuration to monitoring workflow #407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions scenarios/monitoring/cdp_monitoring/common/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ td:
journey_activation_history: journey_activation_history
activations: activations
activations_history: activations_history
parent_segments_configuration: parent_segments_configuration
parent_segments_configuration_history: parent_segments_configuration_history
api_endpoint: api.treasuredata.com
cdp_api_endpoint: api-cdp.treasuredata.com

18 changes: 18 additions & 0 deletions scenarios/monitoring/cdp_monitoring/incremental_ingest.dig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ schedule:
_env:
TD_API_KEY: ${secret:td.apikey}

+incremental_ingest_ps_configuration:
+append_ps_configuration_history:
td>:
query: select * from ${td.tables.parent_segments_configuration}
insert_into: ${td.tables.parent_segments_configuration_history}
database: ${td.database}
+ingest_ps_configuration:
py>: scripts.ingest_ps_configuration.run
session_unixtime: ${session_unixtime}
dest_db: ${td.database}
dest_table: ${td.tables.parent_segments_configuration}
api_endpoint: ${td.api_endpoint}
cdp_api_endpoint: ${td.cdp_api_endpoint}
docker:
image: "digdag/digdag-python:3.10.1"
_env:
TD_API_KEY: ${secret:td.apikey}

+incremental_ingest_entities:
+append_entities_history:
td>:
Expand Down
26 changes: 19 additions & 7 deletions scenarios/monitoring/cdp_monitoring/initial_ingest.dig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ _export:
api_endpoint: ${td.api_endpoint}
cdp_api_endpoint: ${td.cdp_api_endpoint}
docker:
image: "digdag/digdag-python:3.9"
image: "digdag/digdag-python:3.10.1"
_env:
TD_API_KEY: ${secret:td.apikey}

+initial_ingest_ps_configuration:
py>: scripts.ingest_ps_configuration.run
session_unixtime: ${session_unixtime}
dest_db: ${td.database}
dest_table: ${td.tables.parent_segments_configuration}
api_endpoint: ${td.api_endpoint}
cdp_api_endpoint: ${td.cdp_api_endpoint}
docker:
image: "digdag/digdag-python:3.10.1"
_env:
TD_API_KEY: ${secret:td.apikey}

Expand All @@ -36,7 +48,7 @@ _export:
api_endpoint: ${td.api_endpoint}
cdp_api_endpoint: ${td.cdp_api_endpoint}
docker:
image: "digdag/digdag-python:3.9"
image: "digdag/digdag-python:3.10.1"
_env:
TD_API_KEY: ${secret:td.apikey}

Expand All @@ -55,7 +67,7 @@ _export:
api_endpoint: ${td.api_endpoint}
cdp_api_endpoint: ${td.cdp_api_endpoint}
docker:
image: "digdag/digdag-python:3.9"
image: "digdag/digdag-python:3.10.1"
_env:
TD_API_KEY: ${secret:td.apikey}

Expand All @@ -76,7 +88,7 @@ _export:
api_endpoint: ${td.api_endpoint}
cdp_api_endpoint: ${td.cdp_api_endpoint}
docker:
image: "digdag/digdag-python:3.9"
image: "digdag/digdag-python:3.10.1"
_env:
TD_API_KEY: ${secret:td.apikey}

Expand All @@ -96,7 +108,7 @@ _export:
api_endpoint: ${td.api_endpoint}
cdp_api_endpoint: ${td.cdp_api_endpoint}
docker:
image: "digdag/digdag-python:3.9"
image: "digdag/digdag-python:3.10.1"
_env:
TD_API_KEY: ${secret:td.apikey}

Expand All @@ -116,6 +128,6 @@ _export:
api_endpoint: ${td.api_endpoint}
cdp_api_endpoint: ${td.cdp_api_endpoint}
docker:
image: "digdag/digdag-python:3.9"
image: "digdag/digdag-python:3.10.1"
_env:
TD_API_KEY: ${secret:td.apikey}
TD_API_KEY: ${secret:td.apikey}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import requests
import pandas as pd
import pytd
import os
import json

def convert_to_json(s):
return json.dumps(s)

def get_all_parent_segment_configuration(url, headers):
print(url)
res = requests.get(url=url, headers=headers)
if res.status_code != requests.codes.ok:
res.raise_for_status()

data = res.json()
for d in data:
for k in d.keys():
if type(d[k]) is dict:
d[k] = json.dumps(d[k])

return data

def run(session_unixtime, dest_db, dest_table, api_endpoint='api.treasuredata.com', cdp_api_endpoint='api-cdp.treasuredata.com'):
url = 'https://%s/audiences' % cdp_api_endpoint
headers = {'Authorization': 'TD1 %s' % os.environ['TD_API_KEY']}
l = get_all_parent_segment_configuration(url, headers)
if len(l) == 0:
print('no import record')
return
df = pd.DataFrame(l)
df['time'] = int(session_unixtime)
df['attributes'] = df['attributes'].apply(convert_to_json)
df['behaviors'] = df['behaviors'].apply(convert_to_json)
Copy link
Member

Choose a reason for hiding this comment

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

Does the API return schedule and query engine (Presto and Hive, and Hive only for example)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, to obtain detailed settings, the ParentSegment ID must be specified.
So, we will need to issue as many REST APIs as there are ParentSegments.
https://api-docs.treasuredata.com/pages/audience_api_v1/operation/audiences_show/


client = pytd.Client(apikey=os.environ['TD_API_KEY'], endpoint='https://%s' % api_endpoint, database=dest_db)
client.load_table_from_dataframe(df, dest_table, if_exists='overwrite', fmt='msgpack')