- Timestamp:
- May 4, 2020, 8:37:16 PM (7 years ago)
- Branches:
- debian/bullseye, master
- Children:
- 91c5ab7
- Parents:
- ac05b52
- File:
-
- 1 edited
-
src/gnuboat/common/bin/gnuboat-log-uploader (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/gnuboat/common/bin/gnuboat-log-uploader
rac05b52 r48dcf77 21 21 import sys 22 22 import json 23 import requests23 import getpass 24 24 import argparse 25 from urllib3.exceptions import RequestError 26 27 from gnuboat.mapservice_client.configuration import Configuration as MsConfiguration 28 from gnuboat.mapservice_client.api_client import ApiClient as MsApiClient 29 from gnuboat.mapservice_client.exceptions import ApiException as MsApiException 30 from gnuboat.mapservice_client.api.logs_api import LogsApi as MsLogsApi 31 from gnuboat.mapservice_client.models.log_creation import LogCreation as MsLogCreation 32 from gnuboat.mapservice_client.api.devices_api import DevicesApi as MsDevicesApi 25 33 26 34 … … 63 71 64 72 65 def header(api_key): 66 return { 67 'X-API-KEY': api_key 68 # 'Content-type': 'multipart/form-data' 69 } 70 71 72 def create_log(log, url, api_key): 73 response = requests.post( 74 url, json=log.log_creation(), headers=header(api_key) 75 ) 76 77 if response.status_code != 200: 78 print( 79 'ERROR: Could not create LogMetaData for logfile {}'.format( 80 log.logfile 73 def mapservice_configuration(username, password, api_key, base_url = None): 74 configuration = MsConfiguration() 75 if base_url is not None: 76 configuration.host = base_url 77 if api_key: 78 configuration.api_key['X-API-KEY'] = api_key 79 else: 80 configuration.username = username 81 configuration.password = password 82 return configuration 83 84 85 def handle_mapservice_error(exception, error_message): 86 if isinstance(exception, MsApiException): 87 response_body = json.loads(exception.body) 88 print(error_message) 89 print(' Status code: {}'.format(response_body['status'])) 90 print(' Details: {}'.format(response_body['detail'])) 91 elif isinstance(exception, RequestError): 92 print('Could not connect to server {}:{}/{}'.format( 93 exception.pool.host, exception.pool.port, exception.url 94 )) 95 else: 96 print('An unhandled error occured: {}'.format(repr(exception))) 97 sys.exit(5) 98 99 100 def display_devices(configuration): 101 with MsApiClient(configuration) as api_client: 102 try: 103 devices = MsDevicesApi(api_client).devices_get() 104 except Exception as e: 105 handle_mapservice_error(e, 'ERROR: Could not get devices') 106 107 if len(devices) < 0: 108 print('You have no devices at {}').format(configuration.host) 109 return 110 111 print('Id Name Sensor Depth Comment') 112 for device in devices: 113 print('{:6d} {:30s} {:11f} {}'.format( 114 device.device_id, device.name, 115 device.sensor_depth, device.comment 116 )) 117 118 def create_log(log, configuration): 119 with MsApiClient(configuration) as api_client: 120 try: 121 log_metadata = MsLogsApi(api_client).logs_log_create(log_creation = log.log_creation()) 122 except Exception as e: 123 error_message = ('ERROR: Could not create Log entry for ' 124 'logfile {}').format(log.logfile) 125 handle_mapservice_error(e, error_message) 126 return log_metadata.log_id 127 128 129 def upload_log(log_id, log, configuration): 130 with open(log.logfile, 'rb') as logfilehandle: 131 file_content = logfilehandle.read() 132 with MsApiClient(configuration) as api_client: 133 try: 134 log_metadata = MsLogsApi(api_client).logs_log_upload(log_id = log_id, body = file_content) 135 except MsApiException as e: 136 handle_mapservice_error(e, log) 137 138 139 def load_and_upload_logs(input_file_path, configuration): 140 logs_checked = [] 141 print('Checking input from {}'.format(input_file_path)) 142 with open(input_file_path, 'r') as input_file: 143 logs = json.load(input_file) 144 for log in logs: 145 if not 'water' in log: 146 print('ERROR: No water in log {}'.format(json.dumps(log))) 147 sys.exit(4) 148 water_id = to_int(log['water']) 149 if not water_id: 150 print( 151 'ERROR: Could not convert water to int {}'.format( 152 log['water'] 153 ) 154 ) 155 sys.exit(4) 156 157 if not 'gauge' in log: 158 print('ERROR: No gauge in log {}'.format(json.dumps(log))) 159 sys.exit(4) 160 gauge_id = to_int(log['gauge']) 161 if not gauge_id: 162 print( 163 'ERROR: Could not convert gauge to int {}'.format( 164 log['gauge'] 165 ) 166 ) 167 sys.exit(4) 168 169 if not 'device' in log: 170 print('ERROR: No device in log {}'.format(json.dumps(log))) 171 sys.exit(4) 172 device_id = to_int(log['device']) 173 if not device_id: 174 print( 175 'ERROR: Could not convert device to int {}'.format( 176 log['device'] 177 ) 178 ) 179 sys.exit(4) 180 181 if not 'gaugeHeight' in log: 182 print( 183 'ERROR: No gaugeHeight in log {}'.format(json.dumps(log)) 184 ) 185 sys.exit(4) 186 gauge_height = to_float(log['gaugeHeight']) 187 if not gauge_height: 188 print( 189 'ERROR: Could not convert gaugeHeight to float {}'.format( 190 log['gaugeHeight'] 191 ) 192 ) 193 sys.exit(4) 194 195 if not 'file' in log: 196 print('ERROR: No file in log {}'.format(json.dumps(log))) 197 sys.exit(4) 198 199 comment = '' 200 if 'comment' in log: 201 comment = log['comment'] 202 203 logfile = os.path.join(os.path.dirname(__file__), log['file']) 204 if not os.path.exists(logfile): 205 logfile_rel = os.path.join( 206 os.path.dirname(input_file_path), log['file'] 207 ) 208 if not os.path.exists(logfile_rel): 209 print( 210 'ERROR: Logfile not found at {} or {}'.format( 211 logfile, logfile_rel 212 ) 213 ) 214 sys.exit(4) 215 else: 216 logfile = logfile_rel 217 logs_checked.append( 218 Log( 219 water_id, device_id, gauge_id, gauge_height, comment, 220 logfile 221 ) 81 222 ) 82 ) 83 print(' Response status code: {}'.format(response.status_code)) 84 print(' Response: \n{}'.format(response.text)) 85 print(' Request headers: {}'.format(response.request.headers)) 86 print(' Request body: {}'.format(response.request.body)) 87 sys.exit(5) 88 89 data = response.json() 90 if not 'logId' in data: 91 print('ERROR: No log id returned') 92 sys.exit(6) 93 return data["logId"] 94 95 96 def upload_log(log_id, log, url, api_key): 97 fileContent = open(log.logfile, 'rb') 98 headers = header(api_key) 99 headers["Accept"] = "application/json" 100 response = requests.post( 101 "{}/{}".format(url, log_id), data=fileContent, headers=headers 102 ) 103 if response.status_code != 200: 104 print('ERROR: Could not upload log for logfile {}'.format(log.logfile)) 105 print("Response status code: {}".format(response.status_code)) 106 print("Response: \n{}".format(response.text)) 107 sys.exit(7) 223 224 print('Uploading {} logs'.format(len(logs_checked))) 225 for log in logs_checked: 226 log_id = create_log(log, configuration) 227 upload_log(log_id, log, configuration) 228 print(' Finished {}'.format(log.logfile)) 108 229 109 230 110 231 def main(argv): 111 232 input_file_path = None 112 url = None 233 base_url = None 234 username = None 235 password = None 113 236 api_key = None 114 237 … … 120 243 metavar='input_file_path', 121 244 type=str, 245 nargs='?', 122 246 help= 123 247 'JSON file with water, gauge, device, logfiles and correction data to be processed' 124 248 ) 125 249 parser.add_argument( 126 '- s',127 '-- server',128 dest=' server',250 '-b', 251 '--base', 252 dest='base_url', 129 253 type=str, 130 help='Server host and port to upload the logsfiles to' 254 default=MsConfiguration().host, 255 help='Server base url to upload the logsfiles to' 131 256 ) 132 257 parser.add_argument( … … 137 262 help='API Key for authorisation' 138 263 ) 264 parser.add_argument( 265 '-d', 266 '--devices', 267 dest='devices', 268 action='store_true', 269 help='Display a list of your devices on the server' 270 ) 139 271 arguments = parser.parse_args(argv) 272 273 274 # handle base parameter 275 if arguments.base_url is not None: 276 if arguments.base_url.endswith('/'): 277 base_url = arguments.base_url[:-1] 278 else: 279 base_url = arguments.base_url 280 281 if arguments.api_key is None: 282 username = input('Username: ') 283 if not username: 284 print('ERROR: No username given') 285 sys.exit(2) 286 password = getpass.getpass() 287 if not password: 288 print('ERROR: No password given') 289 sys.exit(2) 290 else: 291 api_key = arguments.api_key 292 configuration = mapservice_configuration(username, password, api_key, base_url) 293 294 if arguments.devices: 295 display_devices(configuration) 296 sys.exit(0) 140 297 141 298 if arguments.input_file is None: … … 152 309 input_file_path = arguments.input_file 153 310 154 if arguments.server is None: 155 print('ERROR: No server specified') 156 sys.exit(2) 157 else: 158 url = 'http://{}/mapservice/v1/logs'.format(arguments.server) 159 160 if arguments.api_key is None: 161 print('ERROR: No API key specified') 162 sys.exit(2) 163 else: 164 api_key = arguments.api_key 165 166 logs_checked = [] 167 print('Checking input from {}'.format(input_file_path)) 168 with open(input_file_path, 'r') as input_file: 169 logs = json.load(input_file) 170 for log in logs: 171 if not 'water' in log: 172 print('ERROR: No water in log {}'.format(json.dumps(log))) 173 sys.exit(4) 174 water_id = to_int(log['water']) 175 if not water_id: 176 print( 177 'ERROR: Could not convert water to int {}'.format( 178 log['water'] 179 ) 180 ) 181 sys.exit(4) 182 183 if not 'gauge' in log: 184 print('ERROR: No gauge in log {}'.format(json.dumps(log))) 185 sys.exit(4) 186 gauge_id = to_int(log['gauge']) 187 if not gauge_id: 188 print( 189 'ERROR: Could not convert gauge to int {}'.format( 190 log['gauge'] 191 ) 192 ) 193 sys.exit(4) 194 195 if not 'device' in log: 196 print('ERROR: No device in log {}'.format(json.dumps(log))) 197 sys.exit(4) 198 device_id = to_int(log['device']) 199 if not device_id: 200 print( 201 'ERROR: Could not convert device to int {}'.format( 202 log['device'] 203 ) 204 ) 205 sys.exit(4) 206 207 if not 'gaugeHeight' in log: 208 print( 209 'ERROR: No gaugeHeight in log {}'.format(json.dumps(log)) 210 ) 211 sys.exit(4) 212 gauge_height = to_float(log['gaugeHeight']) 213 if not gauge_height: 214 print( 215 'ERROR: Could not convert gaugeHeight to float {}'.format( 216 log['gaugeHeight'] 217 ) 218 ) 219 sys.exit(4) 220 221 if not 'file' in log: 222 print('ERROR: No file in log {}'.format(json.dumps(log))) 223 sys.exit(4) 224 225 comment = '' 226 if 'comment' in log: 227 comment = log['comment'] 228 229 logfile = os.path.join(os.path.dirname(__file__), log['file']) 230 if not os.path.exists(logfile): 231 logfile_rel = os.path.join( 232 os.path.dirname(input_file_path), log['file'] 233 ) 234 if not os.path.exists(logfile_rel): 235 print( 236 'ERROR: Logfile not found at {} or {}'.format( 237 logfile, logfile_rel 238 ) 239 ) 240 sys.exit(4) 241 else: 242 logfile = logfile_rel 243 logs_checked.append( 244 Log( 245 water_id, device_id, gauge_id, gauge_height, comment, 246 logfile 247 ) 248 ) 249 250 print('Uploading {} logs'.format(len(logs_checked))) 251 for log in logs_checked: 252 log_id = create_log(log, url, api_key) 253 upload_log(log_id, log, url, api_key) 254 print(' Finished {}'.format(log.logfile)) 311 load_and_upload_logs(input_file_path, configuration) 255 312 256 313
Note:
See TracChangeset
for help on using the changeset viewer.
