-
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 the function for ingesting revision data
- Loading branch information
Yuu Ohmura
committed
Jan 26, 2024
1 parent
0ce91c8
commit 547663d
Showing
3 changed files
with
55 additions
and
0 deletions.
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
40 changes: 40 additions & 0 deletions
40
scenarios/monitoring/workflow_monitoring/scripts/ingest_revision.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,40 @@ | ||
import requests | ||
import os | ||
import pytd | ||
import pandas as pd | ||
import json | ||
|
||
def get_revision_info(base_url, headers, ids): | ||
l = [] | ||
for i in ids: | ||
url = base_url % i | ||
print(url) | ||
res = requests.get(url=url, headers=headers) | ||
if res.status_code != requests.codes.ok: | ||
res.raise_for_status() | ||
revisions = res.json()['revisions'] | ||
for r in revisions: | ||
r['projectid'] = i | ||
l.extend(revisions) | ||
return l | ||
|
||
def insert_revision_info(import_unixtime, endpoint, apikey, dest_db, dest_table, revisions): | ||
df = pd.DataFrame(revisions) | ||
df['time'] = int(import_unixtime) | ||
df['userInfo'] = df['userInfo'].apply(json.dumps) | ||
client = pytd.Client(apikey=apikey, endpoint=endpoint, database=dest_db) | ||
client.load_table_from_dataframe(df, dest_table, if_exists='append', fmt='msgpack') | ||
|
||
def run(session_unixtime, dest_db, dest_table, project_ids, api_endpoint='api.treasuredata.com', workflow_endpoint='api-workflow.treasuredata.com'): | ||
id_list = project_ids[1:-1].split(',') | ||
if len(id_list) == 0: | ||
print('no project id') | ||
return | ||
|
||
workflow_url = 'https://%s/api/projects' % workflow_endpoint + '/%s/revisions' | ||
headers = {'Authorization': 'TD1 %s' % os.environ['TD_API_KEY']} | ||
l = get_revision_info(workflow_url, headers, id_list) | ||
if len(l) == 0: | ||
print('no insert record') | ||
return | ||
insert_revision_info(session_unixtime, 'https://%s' % api_endpoint, os.environ['TD_API_KEY'], dest_db, dest_table, l) |