-
Notifications
You must be signed in to change notification settings - Fork 0
/
D2M-01-usage-collection.py
150 lines (118 loc) · 4.82 KB
/
D2M-01-usage-collection.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
#!/usr/bin/python
import os
import sys
import logging
import config
import ConfigParser
import datasift
import time
import calendar
from datetime import datetime
from env import MySqlTask
if config.logLevel == "info":
logging.basicConfig(format='%(asctime)s | %(levelname)s | %(filename)s | %(message)s', level=logging.INFO, filename=config.logFile)
else:
logging.basicConfig(format='%(asctime)s | %(levelname)s | %(filename)s | %(message)s', level=logging.DEBUG, filename=config.logFile)
if __name__ == "__main__":
_table_name = 'usage_reporting'
# Create object for internal database methods (mySQL)
mysql = MySqlTask(config.mysql_username, config.mysql_password, config.mysql_host, config.mysql_database)
retry = 3
while retry:
try:
logging.info("Getting valid columns from MySQL.")
valid_sources = mysql.return_columns(_table_name)
valid_sources = [ col for col in valid_sources if col not in ('intID', 'username', 'start', 'end', 'stream_type', 'stream_hash', 'seconds') ]
logging.debug("Columns found: {0}".format(valid_sources))
retry = 0
except Exception, err:
#print err #2013: Lost connection to MySQL server during query
logging.error(err)
retry -= 1
logging.warning("Retries left: {0}".format(retry))
time.sleep(2) # Sleep 2 seconds before retrying
user_list = ConfigParser.ConfigParser()
user_list.optionxform = str
path = os.path.dirname(os.path.realpath(__file__))
user_list.read(path+"/user_list.ini")
print "Users to query /usage: {0}".format(user_list.options("Users"))
for username in user_list.options("Users"):
api_key = user_list.get("Users", username)
# Connect to the mysql database
mysql.connect()
# Create object for DataSift methods
ds = datasift.User(username, api_key)
retry = 3
while retry:
try:
usage = ds.get_usage('day')
logging.info("Getting /usage for user: {0}".format(username))
print "Getting /usage for user: {0}".format(username)
retry = 0
except Exception, err:
logging.error("Encountered getting /usage for user: {0}. Error message: {1}".format(username, err))
retry -= 1
logging.warning("Retries left: {0}".format(retry))
time.sleep(5) # Sleep 5 seconds before retrying
date_format = "%a, %d %b %Y %H:%M:%S +0000"
start = time.strptime(usage['start'], date_format)
end = time.strptime(usage['end'], date_format)
unix_start = calendar.timegm(start)
unix_end = calendar.timegm(end)
insert_string = ''
if len(usage['streams']):
for stream in usage['streams']:
if len(stream) == 32:
stream_type = "stream"
else:
stream_type = "historic"
seconds = usage['streams'][stream]['seconds']
data = {
'username' : username,
'start' : unix_start,
'startDate' : datetime.utcfromtimestamp(unix_start),
'end' : unix_end,
'endDate' : datetime.utcfromtimestamp(unix_end),
'stream_type' : stream_type,
'stream_hash' : str(stream),
'seconds' : seconds
}
licenses = usage['streams'][stream]['licenses']
if len(licenses):
headers = []
for license_type, license_value in licenses.items():
# Only add licenses for columns that exist in the database
if any(str(license_type) in x for x in valid_sources):
data[str(license_type)] = license_value
headers.append(str(license_type))
fields_string = ", ".join([ "`{0}`".format(k) for k in headers ])
values_string = ", ".join([ "%({0})s".format(k) for k in headers ])
insert_query = ("""
INSERT INTO {0}
(`username`, `start`, `startDate`, `end`, `endDate`, `stream_type`, `stream_hash`, `seconds`, {1})
VALUES ('%(username)s', %(start)s, '%(startDate)s', %(end)s, '%(endDate)s', '%(stream_type)s', '%(stream_hash)s', %(seconds)s, {2});
""").format(_table_name, fields_string, values_string)
# Different MySQL Query if there is no license consumption
else:
insert_query = ("""
INSERT INTO {0}
(`username`, `start`, `startDate`, `end`, `endDate`, `stream_type`, `stream_hash`, `seconds`)
VALUES ('%(username)s', %(start)s, '%(startDate)s', %(end)s, '%(endDate)s', '%(stream_type)s', '%(stream_hash)s', %(seconds)s);
""").format(_table_name)
# Concatenate all the INSERT statements
insert_string += " ".join(insert_query.split()) % data
try:
insert_count= 0
cursor = mysql.execute_many(insert_string)
for insert in cursor:
insert_count += 1
# Commit the inserts for the user (if there are results)
if insert_count: mysql.commit()
else: mysql.close()
except Exception, err:
logging.exception(err)
logging.error("Query: {0}".format(insert_string))
continue
else:
logging.info("No streams consumed in the past 24 hours for user: {0}".format(username))
logging.info("Tasks completed.")