This repository has been archived by the owner on Oct 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_aggregate_activity_stats.py
217 lines (142 loc) · 5.74 KB
/
get_aggregate_activity_stats.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from concurrent import futures
import psycopg2, requests, json, time, io
with open('config.json', 'r') as configFile:
config = json.load(configFile)
api_config = config['API']
db_config = config['Database']
sql_config = config['SQL']
def execute_ddl(db, ddl):
print('Executing DDL statement on database...')
start = time.time()
pg_cursor = db.cursor()
pg_cursor.execute(ddl)
db.commit()
end = time.time()
ddl_duration = end - start
print('Database Execution: {0:.2f}s'.format(ddl_duration))
def get_characters(db):
print('Getting characters...')
character_sql = sql_config['characterSelect']
pg_cursor = db.cursor()
pg_cursor.execute(character_sql)
characters = pg_cursor.fetchall()
characters = list(map(list, characters))
return characters
def build_requests(characters):
print('Building requests...')
requests = []
for character in characters:
#print(character[0])
url = [api_config['url'].format(character[0]['destiny_membership_type'], character[0]['destiny_id'], character[0]['character_id'])]
character[0]['requestUrl'] = url
#print(json.dumps(character))
requests.append(character)
return requests
def process_requests(characters):
print('Processing requests...')
stats = []
character_count = len(characters)
print('Requests {0}'.format(character_count))
chunk_size = 25
start = 0
api_start_time = time.time()
for start in range(0, character_count, chunk_size):
end = start + chunk_size - 1
if end > character_count:
end = character_count - 1
#print('({0},{1})'.format(start, end))
print('Processing chunk {0} - {1}'.format(str(start), str(end)))
chunk = end - start + 1
with futures.ThreadPoolExecutor(chunk) as executor:
future_to_url = {executor.submit(get_stats, character): character for character in characters[start:(end + 1)]}
for response in futures.as_completed(future_to_url):
stats.append(response.result())
api_end_time = time.time()
api_duration = api_end_time - api_start_time
print('API Execution: {0:.2f}s'.format(api_duration))
return stats
def get_stats(character):
# print('Getting stats...')
x_api_key = api_config['xApiKey']
#print(character[0]['requestUrl'][0])
response = requests.get(character[0]['requestUrl'][0], headers={'X-API-Key': x_api_key})
stats = response.text
#print(stats)
character[0]['stats'] = json.loads(stats)
#print(json.dumps(character))
# print(json.loads(stats)['ErrorStatus'])
return character
def build_inserts(character_stats):
print('Building database inserts...')
inserts = []
for character in character_stats:
activities = character[0]['stats']['Response']['activities']
for activity in activities:
activityHash = [activity['activityHash']]
stats = activity['values']
for stat in stats:
data = stats[stat]
#print(character[0])
insert = [character[0]['group_id'], character[0]['clan_id'], character[0]['member_id'], character[0]['character_id']] + activityHash + [stat] + [json.dumps(data)]
#print(insert)
inserts.append(insert)
return inserts
def load_data(db, data):
insert_count = len(data)
print(insert_count)
chunk_size = 10000
if chunk_size > insert_count:
chunk_size = insert_count
start = 0
total_inserts = 0
db_start_time = time.time()
print(insert_count)
for start in range(0, insert_count, chunk_size):
end = start + chunk_size - 1
if end > insert_count:
end = insert_count - 1
#if (i % chunkSize == 0 and i > 0) or i == (insert_count - 1):
print('Processing chunk {0} - {1}'.format(str(start), str(end)))
chunk = data[start:(end+1)]
file_data = ''
for row in chunk:
#print(row)
file_data += '\t'.join(str(value) for value in row) + '\n'
#print(file_data)
total_inserts += 1
buffer = io.StringIO()
buffer.write(file_data)
buffer.seek(0)
pg_cursor = db.cursor()
pg_cursor.copy_from(buffer, 'stats.t_aggregate_activity_stats', sep='\t', columns=('group_id', 'clan_id', 'member_id', 'character_id', 'activity_hash', 'stat_id', 'stat'))
db.commit()
#start = i + 1
db_end_time = time.time()
db_duration = db_end_time - db_start_time
print('Database Loading Execution: {0:.2f}s'.format(db_duration))
return total_inserts
def handler(event, context):
# Open database connection
#pg = pg8000.connect(host=dbConfig['host'], port=dbConfig['port'], database=dbConfig['database'], user=dbConfig['user'], password=dbConfig['password'])
pg = psycopg2.connect(host=db_config['host'], port=db_config['port'], database=db_config['database'], user=db_config['user'], password=db_config['password'])
# Truncate table
execute_ddl(pg, sql_config['truncateActivity'])
# Retrieve characters
characters = get_characters(pg)
# Build requests
characters = build_requests(characters)
# Process requests
character_stats = process_requests(characters)
# Build inserts
inserts = build_inserts(character_stats)
# Load data
counts = load_data(pg, inserts)
print('Inserts: {0}'.format(counts))
# Post load activities
# Analyze base table after loading
execute_ddl(pg, sql_config['analyzeActivityTable'])
# Refresh materialized view
execute_ddl(pg, sql_config['refreshActivity'])
# Analyze materialized view after refresh
execute_ddl(pg, sql_config['analyzeActivityView'])
pg.close()