forked from all-of-us/raw-data-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticipant_dump.py
66 lines (53 loc) · 2.33 KB
/
participant_dump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Print debugging information about a participant.
Usage: run_client.sh --account [email protected] --project all-of-us-rdr-prod %(prog)s P12345
"""
import httplib
import json
import logging
import pprint
import subprocess
from client import Client
from main_util import configure_logging, get_parser
# Server logs are searchable for at most 30 days (see DA-247).
_SERVER_LOG_FRESHNESS = '30d'
def log_debug_info(client, participant_id, project):
# basic info: signup time, withdrawal
logging.info(pprint.pformat(client.request_json('Participant/%s' % participant_id)))
# Questionnaires available: TODO implement list for Questionnaire
# QuestionnaireResponses: TODO implement list for QRs on a participant
logging.info(
'Server logs for %s from the last %s (oldest first)\n%s',
participant_id,
_SERVER_LOG_FRESHNESS,
'\n'.join(['\t' + line for line in _get_app_log_lines(participant_id, project)]))
def _get_app_log_lines(participant_id, project):
# Alternate format for easy reading on the CLI:
# --format="value(timestamp,severity,protoPayload.status,protoPayload.resource)"
log_json_data = subprocess.check_output([
'gcloud',
'beta',
'logging',
'read',
('resource.type="gae_app"'
+ ' logName="projects/%(project)s/logs/appengine.googleapis.com%%2Frequest_log"'
+ ' "%(participant_id)s"') % {'participant_id': participant_id, 'project': project},
'--format', 'json',
'--freshness', _SERVER_LOG_FRESHNESS,
])
log_data = json.loads(log_json_data)
formatted_lines = []
for msg in reversed(log_data):
payload = msg['protoPayload']
formatted_lines.append('%(startTime)s %(method)s %(status)d %(resource)s' % payload)
# Only print messages logged on the server for errors (status != 200 or log severity != INFO).
if (payload['status'] != httplib.OK or
any([line['severity'] != 'INFO' for line in payload['line']])):
for line in payload['line']:
formatted_lines.append('\t%(time)s %(severity)s %(logMessage)s' % line)
return formatted_lines
if __name__ == '__main__':
configure_logging()
parser = get_parser()
parser.add_argument('participant_id', help='P12345 format participant ID to look up.')
rdr_client = Client(parser=parser)
log_debug_info(rdr_client, rdr_client.args.participant_id, rdr_client.args.project)