-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathload-status.py
executable file
·116 lines (98 loc) · 3.79 KB
/
load-status.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
#!/usr/bin/env python
# OOI Data Team Portal
# Tool to check and load operational status
import datateam
import argparse
import csv
import requests
from datetime import datetime, timedelta
# Command Line Arguments
parser = argparse.ArgumentParser(description='OOI Data Team Portal - Operational Status')
parser.add_argument('-s','--server',
choices=['production','development'],
default='development',
help='Database server to use')
args = parser.parse_args()
def load_m2m_config():
"""Load M2M configuration"""
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open('config.cfg'))
return {
'username': config.get('ooinet','username'),
'password': config.get('ooinet','password')
}
def load_instrument_list():
"""Load instruments list from CSV file"""
with open("infrastructure/instruments.csv", 'rb') as csvfile:
reader = csv.DictReader(csvfile)
instruments=[]
for row in reader:
instruments.append(row)
return instruments
def create_url(reference_designator,stream):
"""Create uFrame M2M url for last 24 hours"""
site = reference_designator[:8]
node = reference_designator[9:14]
inst = reference_designator[15:]
if site[0:2] == 'RS':
delivery_method = 'streamed'
elif site in ['CE02SHBP','CE04OSBP']:
delivery_method = 'streamed'
else:
delivery_method = 'telemetered'
end_date = datetime.utcnow()
start_date = end_date - timedelta(2)
url = ('https://ooinet.oceanobservatories.org/api/m2m/12576/sensor/inv/{site}/{node}/{inst}'
'/{delivery_method}/{stream}?beginDT={start_time}&endDT={end_time}&limit=1000¶meters=7').format(
site=site, node=node, inst=inst,
delivery_method=delivery_method, stream=stream,
start_time = start_date.strftime('%Y-%m-%dT%H:%M:%S.000Z'),
end_time = end_date.strftime('%Y-%m-%dT%H:%M:%S.000Z'))
return url
def save_csv(data):
"""Save status data to CSV file"""
filename = 'instrument_status_%s.csv' % datetime.now().strftime('%Y%m%d')
with open(filename, 'w') as csvfile:
fieldnames = ['reference_designator', 'current_status']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow({'reference_designator': row['reference_designator'], 'current_status': row.get('current_status')})
def load_instrument_status(db):
"""Load instruments, check status, and update database"""
# Load config information
config = load_m2m_config()
# Load Instrument List
instruments = load_instrument_list()
# Loop over each instrument
for row in instruments:
if not row['current_status'] and row['preferred_stream']:
print 'Checking ' + row['reference_designator']
url = create_url(row['reference_designator'],row['preferred_stream'])
# Grab Cassandra data from API
data = requests.get(url, auth=(config['username'],config['password']))
# Check current status
if data.status_code == 200 and len(data.json())>2:
row['current_status'] = 'Operational'
else:
print " Response: %s" % ( data.text )
row['current_status'] = 'Not Operational'
# Save to database
print " Code: %d Length: %d Status: %s" % ( data.status_code, len(data.json()), row['current_status'] )
datateam.designators.save(db, 'instruments', row, ['reference_designator', 'current_status'])
else:
print 'Skipping: ' + row['reference_designator']
# Save to CSV
save_csv(instruments)
def main():
"""Main function for command line execution"""
db = datateam.MysqlPython()
db.load_config(args.server)
db.open_connection()
load_instrument_status(db)
datateam.import_log.log(db,'status_check')
db.close_connection()
# Run main function when in comand line mode
if __name__ == '__main__':
main()