-
Notifications
You must be signed in to change notification settings - Fork 3
/
monitor.py
executable file
·291 lines (226 loc) · 9.18 KB
/
monitor.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
import sys
import argparse
import pymongo
import logging
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from urllib.parse import urlparse
from dateutil.relativedelta import relativedelta
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
# establish basic logging
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format="[%(asctime)s] %(levelname)s %(message)s",
)
class Logger:
def __init__(self):
self.text = ""
def info(self, message):
logging.info(message)
self.text += message + "\n"
def error(self, message):
logging.error(message)
self.text += message + "\n"
def send_email(sender_email, recipients, subject, body, smtp_info):
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = ', '.join(recipients)
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))
with smtplib.SMTP(smtp_info['hostname'], smtp_info['port']) as server:
server.starttls()
if smtp_info['username'] and smtp_info['password']:
server.login(smtp_info['username'], smtp_info['password'])
server.sendmail(sender_email, recipients, message.as_string())
print("Email sent successfully.")
def plot(df):
df['date'] = pd.to_datetime(df[['year', 'month']].assign(day=1))
# Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8), sharex=True)
# Plot for User Actions
ax1.plot(df['date'], df['user_actions'], marker='o', color='blue')
ax1.set_ylabel('Count')
ax1.grid(True)
ax1.set_title('Total User Actions')
# Plot for Recommendations
ax2.plot(df['date'], df['recommendations'], marker='o', color='orange')
ax2.set_ylabel('Count')
ax2.grid(True)
ax2.set_title('Total Recommendations')
# Format x-axis ticks as YEAR-MONTH
date_format = DateFormatter('%Y-%m')
plt.gca().xaxis.set_major_formatter(date_format)
plt.gca().xaxis.set_major_locator(plt.MaxNLocator(
len(df['date'].unique())))
# Rotate x-axis labels for better visibility
plt.gcf().autofmt_xdate()
min_date = df['date'].min().strftime('%Y_%m')
max_date = df['date'].max().strftime('%Y_%m')
# Show the plot
plt.savefig('capacity_{}_{}.pdf'.format(min_date, max_date),
bbox_inches='tight')
def capacity(args, db):
data = []
current = args.starttime
while current <= args.endtime:
current = current.replace(day=1)
next_current = current + relativedelta(months=1)
time_filter = {}
time_filter["timestamp"] = {}
time_filter["timestamp"]["$gte"] = current
time_filter["timestamp"]["$lt"] = next_current
data.append([current.year,
current.month,
db["user_actions"].count_documents(time_filter),
db["recommendations"].count_documents(time_filter)])
# Move to the next month
current = next_current
df = pd.DataFrame(data, columns=['year', 'month', 'user_actions',
'recommendations'])
# Set display options to show all rows and columns
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
print(df)
if args.plot:
plot(df)
def main(args):
# create the logger
logger = Logger()
try:
# connect to the datastore
mongo = pymongo.MongoClient(args.datastore,
uuidRepresentation="pythonLegacy")
rsmetrics_db = mongo[args.datastore.split("/")[-1]]
# check if datastore is alive
if mongo.rsmetrics_db.command('ping') == {u'ok': 1.0}:
logger.info("Connected succesfully to {}".format(args.datastore))
except Exception as e:
logger.error("Cannot connect to {}: {}".format(args.datastore, e))
return
time_filter = {}
if args.starttime:
args.starttime = datetime.fromisoformat(args.starttime)
if "timestamp" not in time_filter:
time_filter["timestamp"] = {}
time_filter["timestamp"]["$gte"] = args.starttime
if args.endtime:
edt = datetime.fromisoformat(args.endtime)
args.endtime = datetime.combine(edt, datetime.min.time())
if "timestamp" not in time_filter:
time_filter["timestamp"] = {}
time_filter["timestamp"]["$lt"] = args.endtime
if args.starttime and args.endtime:
if args.endtime < args.starttime:
logger.error("End date must be older than start date")
return
logger.info("Searching for the period {} - {}".format(args.starttime,
args.endtime))
if args.capacity:
capacity(args, rsmetrics_db)
return
for col in args.collection:
try:
doc_count = rsmetrics_db[col].count_documents(time_filter)
logger.info("> Collection '{}' has {} entries".format(col,
doc_count))
for item_type in rsmetrics_db[col].distinct('type', time_filter):
if col != 'resources':
count = rsmetrics_db[col].count_documents({**time_filter,
"type":
item_type})
else:
count = len(rsmetrics_db[col].distinct('id',
{**time_filter,
"type":
item_type}))
logger.info("\t* '{}'\thas {} entries".format(item_type,
count))
except Exception as e:
logger.error("Cannot retrieve entries from collection '{}'\n{}"
.format(col, e))
if args.email:
smtp_info = parse_smtp_uri(args.smtp_uri)
send_email(args.sender_email, args.recipients,
'RSeval Report for {}'.format(args.starttime
.strftime("%Y-%m-%d")),
logger.text, smtp_info)
def parse_smtp_uri(smtp_uri):
parsed_uri = urlparse(smtp_uri)
if parsed_uri.scheme != 'smtp':
raise ValueError('Invalid SMTP URI. Scheme must be "smtp".')
username, password = None, None
if parsed_uri.username:
username = parsed_uri.username
if parsed_uri.password:
password = parsed_uri.password
return {
'hostname': parsed_uri.hostname,
'port': parsed_uri.port,
'username': username,
'password': password
}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="RS Monitor")
parser.add_argument(
"-c",
"--collection",
metavar="STRING",
nargs="+",
help=("collection to monitor, default all "
"(i.e., user_actions recommendations resources)"),
default=["user_actions", "recommendations", "resources"],
)
parser.add_argument(
"-d",
"--datastore",
metavar="STRING",
help="datastore uri",
required=True,
dest="datastore",
)
parser.add_argument(
"-s",
"--starttime",
metavar=("DATETIME"),
help=("filter search from given datetime in ISO format (UTC) "
"e.g. YYYY-MM-DD"),
nargs="?",
default=None,
)
parser.add_argument(
"-e",
"--endtime",
metavar=("DATETIME"),
help=("filter search to given datetime in ISO format (UTC) "
"e.g. YYYY-MM-DD"),
nargs="?",
default=None,
)
# Add optional argument to enable email-related arguments
parser.add_argument('--email', action='store_true',
help='Send email using SMTP URI')
# Create a mutually exclusive group for email-related arguments
email_group = parser.add_argument_group('Email Options')
# Add email-related arguments to the group
email_group.add_argument('smtp_uri', nargs='?',
help='SMTP URI for the mail server')
email_group.add_argument('sender_email', nargs='?',
help='Sender email address')
email_group.add_argument('recipients', nargs='*',
help='Recipient email addresses (at least one)')
# Add optional argument to enable email-related arguments
parser.add_argument('--capacity', action='store_true',
help='Export output for capacity info. CSV format of \
YEAR, MONTH, TOTAL USER ACTIONS, TOTAL \
RECOMMENDATIONS')
capacity_group = parser.add_argument_group('Capacity Options')
capacity_group.add_argument('--plot', action='store_true',
help='Plot to file')
# Pass the arguments to main method
sys.exit(main(parser.parse_args()))