forked from ombegov/dcoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.py
403 lines (323 loc) · 15.1 KB
/
validate.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
from __future__ import print_function
import csv
import sys
import itertools
import re
import io
# Remove the script from our arguments.
arguments = sys.argv[1:]
errorsOnly = False
# Check for our flags, and strip from our arguments.
i = 0
while i < len(arguments):
if arguments[i] == '--errors-only':
errorsOnly = True
del arguments[i]
i += 1
# Our remaining argument should be the filename.
try:
filename = arguments[0]
except IndexError:
print ('No filename specified!')
exit()
print ('Filename: ', filename)
# Constant to define allowed values for a field
VALID_VALUES = {
"Record Validity": ['Invalid Facility', 'Valid Facility'],
"Ownership Type": ['Agency Owned', 'Colocation', 'Outsourcing', 'Using Cloud Provider'],
"Inter-Agency Shared Services Position": ['Provider', 'Tenant', 'None'],
"Country": ['U.S.', 'Outside U.S.'],
"Data Center Tier": ['Non-Tiered', 'Tier 1', 'Tier 2', 'Tier 3', 'Tier 4', 'Unknown', 'Using Cloud Provider'],
"Key Mission Facility": ['Yes', 'No'],
"Key Mission Facility Type": ['Mission', 'Processing', 'Control', 'Location', 'Legal', 'Other'],
"Electricity Is Metered": ['Yes', 'No'],
"Closing Fiscal Year": [str(i) for i in range(2010, 2022)], # 2010 - 2021
"Closing Quarter": ['Q1', 'Q2', 'Q3', 'Q4'],
"Closing Stage": ['Closed', 'Migration Execution', 'Not closing'],
}
VALID_TIERS = ['Tier 1', 'Tier 2', 'Tier 3', 'Tier 4']
# Constant to define functions to check field value format
VALID_FUNCTIONS = {
'Gross Floor Area': ['is_integer', 'greater_0'],
'Avg Electricity Usage': ['is_decimal', 'greater_0'],
'Avg IT Electricity Usage': ['is_decimal', 'greater_0'],
'Underutilized Servers': ['is_integer', 'equal_greater_0'],
'Actual Hours of Facility Downtime': ['is_decimal', 'equal_greater_0'],
'Planned Hours of Facility Availability': ['is_decimal', 'equal_greater_0'],
'Rack Count': ['is_integer', 'equal_greater_0'],
'Total Mainframes' : ['is_integer', 'equal_greater_0'],
'Total HPC Cluster Nodes': ['is_integer', 'equal_greater_0'],
'Total Virtual Hosts': ['is_integer', 'equal_greater_0'],
}
# Variables we will re-use
hasErrors = False
hasWarnings = False
hasValidFacilities = False
allRecordWarnings = {
'underutilized servers': 'No facilities were listed having underutilized servers. Please verify this information.',
'facility downtime': 'No facilities were listed with any downtime. Please verify this information.',
'key mission facilities': 'No key mission facilities were listed. Please verify this information.'
}
# Lowercase the field keys by updating the header row, for maximum compatiblity.
def lower_headings(iterator):
return itertools.chain([next(iterator).lower()], iterator)
# must be an integer, e.g. "10", "-7"
def is_integer(value):
try:
int(value)
except ValueError:
return "must be an integer value"
# must be a float, e.g. "10", "-7.6"
def is_decimal(value):
try:
float(value)
except ValueError:
return "must be an float value"
# must be greater than 0, e.g. "0.1", "5"
def greater_0(value):
try:
assert float(value) > 0
except ValueError:
return "must be greater than 0"
except AssertionError:
return "must be greater than 0"
# must be equal or greater than 0, e.g. "0", "0.0", "10"
def equal_greater_0(value):
try:
assert float(value) >= 0
except ValueError:
return "must be greater than or equal to 0"
except AssertionError:
return "must be greater than or equal to 0"
# check field to against its valid_values/_functions
def validate_values(data, field, msg=''):
errors = []
value = data.get(field.lower(), '')
# this function is not responsible to blank check
if not value:
return []
# check against a list of values first
if VALID_VALUES.get(field):
values = VALID_VALUES.get(field)
if value.lower() not in map(str.lower, values):
msg = msg or 'If not blank, {} value must be one of "{}"; "{}" is given.'.format(field, '", "'.join(values), value)
errors.append(msg)
# then check with a list of functions
elif VALID_FUNCTIONS.get(field):
funcs = VALID_FUNCTIONS.get(field)
errs = []
for func in funcs:
if not func:
continue
elif not isinstance(func, str):
print('Provide a function name in function list for field "{}". {} is given.'.format(field, type(func)))
exit()
try:
errs.append(eval(func)(value))
except NameError:
print('Function "{}" is not defined for field "{}".'.format(func, field))
exit()
# remove empty ones
errs = [x for x in errs if x]
if errs:
msg = msg or field + ' ' + ', '.join(errs) + '. "' + value + '" is given.'
errors.append(msg)
return errors
# Check field for required
def validate_required(data, field, msg=''):
errors = []
# check required implies check valid values first
errors.extend(validate_values(data, field, msg))
if len(errors) == 0 and not data.get(field.lower()):
errors.append(msg or '{} must not be blank.'.format(field))
return errors
# Main function starts
with io.open(filename, 'r', encoding='utf-8-sig', errors='replace') as datafile:
reader = csv.DictReader(lower_headings(datafile))
stats = {
'record_total': 0,
'record_error': 0,
'record_warning': 0,
'error': 0,
'warning': 0
}
for row in reader:
num = reader.line_num
errors = []
warnings = []
applicable = False
###
# Data acceptance rules. These should match the IDC instructions.
###
###
# Required fields.
###
required_fields = ['agency abbreviation', 'component', 'record validity', #'data center name',
'ownership type', 'gross floor area', 'data center tier', 'key mission facility', 'electricity is metered',
'underutilized servers', 'actual hours of facility downtime', 'planned hours of facility availability',
'rack count', 'total servers', 'total mainframes', 'total hpc cluster nodes', 'total virtual hosts', 'closing stage'
]
if row.get('record validity', '').lower() == 'invalid facility':
required_fields = ['agency abbreviation', 'component', 'record validity']
elif row.get('ownership type', '').lower() != 'agency owned':
required_fields = ['agency abbreviation', 'component', 'record validity', 'closing stage']
elif row.get('inter-agency shared services position', '').lower() == 'tenant':
required_fields = ['agency abbreviation', 'component', 'record validity', 'closing stage', 'ownership type']
elif row.get('key mission facility', '').lower() == 'yes':
required_fields = ['agency abbreviation', 'component', 'record validity', 'closing stage', 'ownership type', 'key mission facility type']
applicable = True
hasValidFacilities = True
elif row.get('closing stage', '').lower() == 'closed':
required_fields = ['agency abbreviation', 'component', 'record validity', 'ownership type', 'gross floor area', 'data center tier', 'closing stage']
else:
applicable = True
hasValidFacilities = True
for required_field in required_fields:
errors.extend(validate_required(row, required_field))
###
# If we don't have a valid data center, don't check any more errors.
###
if not applicable:
continue
# Common optional value checks
#
errors.extend(validate_values(row, 'Country'))
# Other checks
#
if row.get('data center id') and not (re.match(r"DCOI-DC-\d+$", row.get('data center id'))):
errors.append('Data Center ID must be DCOI-DC-##### or leave blank for new data centers.')
if row.get('ownership type', '').lower() == 'Using Cloud Provider'.lower() and row.get('data center tier', '').lower() != 'Using Cloud Provider'.lower():
errors.append('Data Center Tier must be "Using Cloud Provider" if Ownership Type is "Using Cloud Provider".')
if row.get('ownership type', '').lower() == 'Colocation'.lower():
msg = 'Inter-Agency Shared Services Position must not be blank if Ownership Type is "Colocation".'
errors.extend(validate_required(row, 'Inter-Agency Shared Services Position', msg))
else:
errors.extend(validate_values(row, 'Inter-Agency Shared Services Position'))
if row.get('key mission facility', '').lower() == 'yes':
msg = 'Key Mission Facilities must have a Key Mission Facility Type.'
errors.extend(validate_required(row, 'Key Mission Facility Type', msg))
else:
errors.extend(validate_values(row, 'Key Mission Facility Type'))
if row.get('closing stage', '').lower() in ['closed', 'migration execution']:
msg = 'Closing Fiscal Year must not be blank if Closing Stage is "Closed" or "Migration Execution".'
errors.extend(validate_required(row, 'Closing Fiscal Year', msg))
msg = 'Closing Quarter must not be blank if Closing Stage is "Closed" or "Migration Execution".'
errors.extend(validate_required(row, 'Closing Quarter', msg))
else:
errors.extend(validate_values(row, 'Closing Fiscal Year'))
errors.extend(validate_values(row, 'Closing Quarter'))
if row.get('planned hours of facility availability') and float(row.get('planned hours of facility availability')) == 0:
errors.append('Planned Hours of Facility Availability must be greater than 0.')
###
# Data validation rules. This should catch any bad data.
###
if (row.get('record validity', '').lower() == 'valid facility' and
row.get('closing stage', '').lower() != 'closed' and
row.get('ownership type', '').lower() == 'agency owned' and
row.get('data center tier', '').lower() not in map(str.lower, VALID_TIERS)):
warnings.append('Only tiered data centers need to be reported, marked as "{}"'.format(row.get('data center tier')))
# Impossible PUEs
# PUE = 1.0:
if (row.get('avg electricity usage') and
row.get('avg it electricity usage') and
row.get('avg electricity usage').replace('.','',1).isdigit() and
row.get('avg it electricity usage').replace('.','',1).isdigit() and
float(row.get('avg electricity usage')) <= float(row.get('avg it electricity usage'))):
warnings.append(
'Avg Electricity Usage ({}) for a facility should be greater than Avg IT Electricity Usage ({})'
.format(row.get('avg electricity usage'), row.get('avg it electricity usage'))
)
if row.get('electricity is metered', '').lower() == 'yes':
msg = 'Avg Electricity Usage should not be blank if Electricity is Metered.'
warnings.extend(validate_required(row, 'Avg Electricity Usage', msg))
msg = 'Avg IT Electricity Usage should not be blank if Electricity is Metered.'
warnings.extend(validate_required(row, 'Avg IT Electricity Usage', msg))
# Check for incorrect KMF reporting
if row.get('key mission facility type') and row.get('key mission facility', '').lower() != 'yes':
warnings.append('Key Mission Facility Type should only be present if Key Mission Facility is "Yes"')
if row.get('key mission facility', '').lower() == 'yes':
if row.get('data center tier', '').lower() not in map(str.lower, VALID_TIERS):
warnings.append('Key Mission Facilities should not be non-tiered data centers.')
if row.get('ownership type', '').lower() != 'agency owned':
warnings.append('Key Mission Facilities should only be agency-owned.')
if row.get('record validity', '').lower() != 'valid facility':
warnings.append('Invalid facilities should not be Key Mission Facilities.')
if row.get('closing stage', '').lower() != 'not closing':
warnings.append('Key Mission Facilities should be "Not Closing" for Closing Stage.')
# Total Servers vs Total Virtual Hosts
#
if (row.get('total servers') and row.get('total virtual hosts') and row.get('total mainframes') and
int(row.get('total virtual hosts')) > (int(row.get('total servers')) + int(row.get('total mainframes')))
):
warnings.append('Total Virtual Hosts should not be greater than Total Servers plus Total Mainframes. Total Virtual Hosts represents the physical servers or mainframes dedicated to providing a virtualization layer to guest operating systems. These should be also included in the Total counts. Total Virtual Hosts is not a count of the guest operating systems (Total Virtual OS in previous collections).')
# Flags for all-records. Checks to see if agencies are generally following our guidance.
#
if applicable:
# Downtime
if row.get('actual hours of facility downtime') and float(row.get('actual hours of facility downtime')) > 0:
if 'facility downtime' in allRecordWarnings:
del allRecordWarnings['facility downtime']
# Underutilized Servers
if row.get('underutilized servers') and int(row.get('underutilized servers')) > 0:
if 'underutilized servers' in allRecordWarnings:
del allRecordWarnings['underutilized servers']
# Key Mission Facilities
if row.get('key mission facility') and row.get('key mission facility').lower() == 'yes':
if 'key mission facilities' in allRecordWarnings:
del allRecordWarnings['key mission facilities']
###
# Print our results.
###
if len(errors) or (len(warnings) and not errorsOnly):
# Print some sort of name to look up, even if we don't have one.
dcName = []
if row.get('agency abbreviation'):
dcName.append(row.get('agency abbreviation'))
if row.get('component'):
dcName.append(row.get('component'))
if row.get('data center id'):
dcName.append(row.get('data center id'))
else:
dcName.append('Line Number {}'.format(num))
print(' - '.join(dcName))
if len(errors) > 0:
hasErrors = True
print(' Errors:', "\n ", "\n ".join(errors))
if len(warnings) > 0 and not errorsOnly:
hasWarnings = True
print(' Warnings:', "\n ", "\n ".join(warnings))
stats['record_total'] += 1
stats['record_error'] += 1 if len(errors) else 0
stats['record_warning'] += 1 if len(warnings) else 0
stats['error'] += len(errors)
stats['warning'] += len(warnings)
###
# Print our final validation results.
###
if len(allRecordWarnings):
print('')
print('General Warnings')
for warn in allRecordWarnings.values():
print(' ' + warn)
print('')
print('********************************************************************************')
print('* Total records in file: %d.' % stats['record_total'])
if hasErrors or hasWarnings:
print('*')
print('*', end=" ")
if hasErrors:
print('%d errors found in %d records.' % (stats['error'], stats['record_error']), end=" ")
if hasWarnings and not errorsOnly:
print('%d warnings found in %d records.' % (stats['warning'], stats['record_warning']))
else:
print('')
print('*')
if hasErrors:
print('* Any errors must be corrected before the data file will be accepted.')
if hasWarnings and not errorsOnly:
print('* The warnings above _should_ be corrected before submitting this data, but it ')
print('* is not required.')
if not hasErrors and not hasWarnings:
print('* The file had no problems or errors.')
print('********************************************************************************')
print('')