forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvTableStoreResults.py
186 lines (157 loc) · 7.2 KB
/
csvTableStoreResults.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
#!/usr/bin/env python
import httplib, json, csv, time, smtplib, os, traceback
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
from dbs.apis.dbsClient import DbsApi
mailingList = ['[email protected]']
#mailingList = ['[email protected]']
def main():
# Get workflows data from the server
url = 'cmsweb.cern.ch'
print 'INFO: Getting workflows data from ' + url
header = {'Content-type': 'application/json',
'Accept': 'application/json'}
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'),
key_file = os.getenv('X509_USER_PROXY'))
conn.request("GET",
'/couchdb/wmdatamining/_design/WMDataMining/_view/storeresults_summary',
headers= header)
response = conn.getresponse()
data = response.read()
conn.close()
# CSV writer needs utf-8 data format
myString = data.decode('utf-8')
workflows = json.loads(myString)['rows']
# Create the CSV table and json File
filename = 'tableSR.csv'
jsonFile = 'tableSR.json'
params = 'excel'
workflows_dict = {}
print 'INFO: Creating workflows table: ' + filename + ' with dialect: ' + params
print 'INFO: Creating workflows json table: ' + jsonFile
# Get missing request events from DBS input dataset information
dbsUrl = 'https://cmsweb.cern.ch/dbs/prod/global/DBSReader/'
dbsApi = DbsApi(url = dbsUrl)
for entry in workflows:
if 'lcontrer' in entry['id']:
continue
info = entry['value']
if info[9] and type(info[9][0]) == list:
info[9] = [info[9][x][0] for x in xrange(0, len(info[9]))]
workflow_dict = {
'Campaign' : info[0],
'Tier' : info[1],
'Task type' : info[2],
'Status' : info[3],
'Priority' : info[4],
'Requested events' : info[5],
'% Complete' : info[6],
'Completed events' : 0,
'Request date' : time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(info[7])),
'Processing dataset name' : '',
'Input Dataset' : info[8],
'Output Datasets' : info[9],
}
if 'archived' in workflow_dict['Status']:
continue
if workflow_dict['Requested events'] == 0:
events = 0
datasetName = workflow_dict['Input Dataset']
if datasetName != "":
response = dbsApi.listFileSummaries(dataset= datasetName)
if response:
events = response[0]['num_event']
workflow_dict['Requested events'] = events
workflow_dict['Completed events'] = int(int(workflow_dict['Requested events']) * int(workflow_dict['% Complete']) / 100)
if workflow_dict['Output Datasets'] != []:
proccesedDataset = workflow_dict['Output Datasets'][0].split('/')[2]
proccesingName = proccesedDataset.split('-')[-2]
workflow_dict['Processing dataset name'] = proccesingName
# Add to main dictionary
workflows_dict[entry['id']] = workflow_dict
jsonfile = open(jsonFile,'w+')
jsonfile.write(json.dumps(workflows_dict, sort_keys=True, indent=3))
jsonfile.close()
print 'INFO: json table created'
max_lenght = 1
for workflow_name in workflows_dict.keys():
wfData = workflows_dict[workflow_name]
if len(wfData['Output Datasets']) > max_lenght:
max_lenght = len(wfData['Output Datasets'])
with open(filename, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, dialect = params)
fisrtRow = ['Workflow', 'Campaign', 'Tier', 'Task type', 'Status', 'Priority',
'Requested events', '% Complete', 'Completed events',
'Request date', 'Processing dataset name', 'Input Dataset']
fisrtRow.extend(['Output Dataset ' + str(x) for x in xrange(1, max_lenght + 1)])
csvwriter.writerow(fisrtRow)
for workflow_name in workflows_dict.keys():
wfData = workflows_dict[workflow_name]
entry = [workflow_name, wfData['Campaign'], wfData['Tier'], wfData['Task type'],
wfData['Status'], wfData['Priority'], wfData['Requested events'],
wfData['% Complete'], wfData['Completed events'], wfData['Request date'],
wfData['Processing dataset name'], wfData['Input Dataset']]
entry.extend(wfData['Output Datasets'])
csvwriter.writerow(entry)
print 'INFO: csv table created'
#print 'Sending mail to: ', mailingList
#body_text = 'Hi,\n\nAttached please find the summary table of the current state of the workflows in the system,\n\n'
#body_text += 'Thanks,\nLuis C\n\n'
#send_mail('[email protected]',
# mailingList,
# 'Workflows Table',
# body_text,
# [filename])
print 'INFO: Script ran successfully'
def getInputEvents(datasetName):
"""
This uses DBS web interface instead dbs API
I am not using this right now, it can be switched.
"""
events = 0
if datasetName == "":
return events
url = 'cmsweb.cern.ch'
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'),
key_file = os.getenv('X509_USER_PROXY'))
conn.request("GET",
'/dbs/prod/global/DBSReader/filesummaries?dataset='+datasetName)
response = conn.getresponse()
data = response.read()
conn.close()
events = data[0]['num_event']
return events
def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
assert isinstance(send_to, list)
assert isinstance(files, list)
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
if __name__ == "__main__":
try:
main()
except Exception, ex:
mssg = 'There was an error while executing csvTable.py on cmst2 acrontab, please have a look to the following error:\n'
mssg += 'Exception: '+str(ex)+'\n'
mssg += 'Traceback: '+str(traceback.format_exc())
send_mail('[email protected]',
mailingList,
'Error executing cvsTable.py on cmst2 acrontab',
mssg)
print 'ERROR: The script was not executed successfully\n\n'
print mssg