-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ingesting parent segment configuration function to monitoring wor…
…kflow
- Loading branch information
Yuu Ohmura
committed
Jul 10, 2024
1 parent
83bf2e8
commit d086ce7
Showing
4 changed files
with
70 additions
and
1 deletion.
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
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
37 changes: 37 additions & 0 deletions
37
scenarios/monitoring/cdp_monitoring/scripts/ingest_ps_configuration.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,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) | ||
|
||
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') |