| [ac11efe] | 1 | '''
|
|---|
| [ec9e811] | 2 | Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| [ac11efe] | 3 |
|
|---|
| 4 | This program is free software: you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 16 | '''
|
|---|
| 17 | import connexion
|
|---|
| 18 |
|
|---|
| [306f18a] | 19 | from ..database.Model import User, Log, LogEntry, Water, Device, Gauge, ProcessingStatus, Queue, db
|
|---|
| [ac11efe] | 20 | from ..HttpErrors import MapserviceBadRequest, MapserviceUnauthorized, MapserviceNotFound
|
|---|
| [5e16955] | 21 | from ..tasks.tasks import log_process
|
|---|
| [ec9e811] | 22 |
|
|---|
| [7be94aa] | 23 |
|
|---|
| [ec9e811] | 24 | def logs_get(submitter=None, order_by=None, offset=None, limit=None):
|
|---|
| [7be94aa] | 25 | query = None
|
|---|
| 26 |
|
|---|
| 27 | if submitter is not None:
|
|---|
| 28 | submitter = User.query.filter_by(username=submitter).all()
|
|---|
| 29 | if len(submitter) != 1:
|
|---|
| 30 | raise MapserviceNotFound(
|
|---|
| 31 | detail="Could not find the submitter with id {}".
|
|---|
| 32 | format(submitter)
|
|---|
| 33 | )
|
|---|
| 34 | submitter = submitter[0]
|
|---|
| 35 |
|
|---|
| 36 | query = None
|
|---|
| 37 | if submitter is not None:
|
|---|
| 38 | query = Log.query.filter_by(submitter=submitter)
|
|---|
| 39 |
|
|---|
| 40 | if query is None:
|
|---|
| 41 | query = Log.query
|
|---|
| 42 |
|
|---|
| 43 | result = []
|
|---|
| 44 | for log in query.offset(offset).limit(limit):
|
|---|
| 45 | result.append(log.meta_data())
|
|---|
| 46 | return result
|
|---|
| 47 |
|
|---|
| [ec9e811] | 48 |
|
|---|
| [ac11efe] | 49 | def logs_log_get_by_id(id):
|
|---|
| [7be94aa] | 50 | log = Log.query.filter_by(id=id).all()
|
|---|
| 51 | if len(log) != 1:
|
|---|
| 52 | raise MapserviceNotFound
|
|---|
| 53 | if connexion.request.headers['Accept'] == "application/geo+json":
|
|---|
| 54 | return log[0].to_geojson(), 200, {
|
|---|
| 55 | 'Content-Type': 'application/geo+json; charset=utf-8'
|
|---|
| 56 | }
|
|---|
| 57 | return log[0].log()
|
|---|
| 58 |
|
|---|
| [ac11efe] | 59 |
|
|---|
| [ec9e811] | 60 | def logs_log_create(log_creation):
|
|---|
| [7be94aa] | 61 | user = User.loggedin_user()
|
|---|
| 62 | if user is None:
|
|---|
| 63 | raise MapserviceUnauthorized
|
|---|
| 64 |
|
|---|
| 65 | water = Water.query.filter_by(id=log_creation.water).all()
|
|---|
| 66 | if len(water) != 1:
|
|---|
| 67 | raise MapserviceBadRequest(
|
|---|
| 68 | detail='Water {} not found database'.format(log_creation.water)
|
|---|
| 69 | )
|
|---|
| 70 | water = water[0]
|
|---|
| 71 |
|
|---|
| 72 | device = Device.query.filter_by(id=log_creation.device).all()
|
|---|
| 73 | if len(device) != 1:
|
|---|
| 74 | raise MapserviceBadRequest(
|
|---|
| 75 | detail='Device {} not found in database'.
|
|---|
| 76 | format(log_creation.device)
|
|---|
| 77 | )
|
|---|
| 78 | device = device[0]
|
|---|
| 79 |
|
|---|
| 80 | gauge = Gauge.query.filter_by(id=log_creation.gauge).all()
|
|---|
| 81 | if len(gauge) != 1:
|
|---|
| 82 | raise MapserviceBadRequest(
|
|---|
| 83 | detail='Gauge {} not found in database'.format(log_creation.gauge)
|
|---|
| 84 | )
|
|---|
| 85 | gauge = gauge[0]
|
|---|
| 86 |
|
|---|
| 87 | if log_creation.gauge_height is None:
|
|---|
| 88 | raise MapserviceBadRequest(detail='Gauge Height (Stage) missing')
|
|---|
| 89 | else:
|
|---|
| 90 | try:
|
|---|
| 91 | float(log_creation.gauge_height)
|
|---|
| 92 | except ValueError:
|
|---|
| 93 | raise MapserviceBadRequest(
|
|---|
| 94 | detail='Gauge Height (Stage) {} is no number'.
|
|---|
| 95 | format(log_creation.gauge_height)
|
|---|
| 96 | )
|
|---|
| 97 |
|
|---|
| 98 | if log_creation.comment is None:
|
|---|
| 99 | log_creation.comment = ""
|
|---|
| 100 |
|
|---|
| 101 | log = Log(
|
|---|
| 102 | submitter=user,
|
|---|
| 103 | water=water,
|
|---|
| 104 | device=device,
|
|---|
| 105 | gauge=gauge,
|
|---|
| 106 | gauge_height=log_creation.gauge_height,
|
|---|
| 107 | start=None,
|
|---|
| 108 | end=None,
|
|---|
| 109 | comment=log_creation.comment
|
|---|
| 110 | )
|
|---|
| 111 | db.session.add(log)
|
|---|
| 112 | db.session.commit()
|
|---|
| 113 | return log.meta_data()
|
|---|
| 114 |
|
|---|
| [ac11efe] | 115 |
|
|---|
| [ec9e811] | 116 | def logs_log_upload(log_id, body=None):
|
|---|
| [7be94aa] | 117 | user = User.loggedin_user()
|
|---|
| 118 | if user is None:
|
|---|
| 119 | raise MapserviceUnauthorized
|
|---|
| 120 |
|
|---|
| 121 | if body is None or not body:
|
|---|
| 122 | raise MapserviceBadRequest(detail='No log data')
|
|---|
| 123 |
|
|---|
| 124 | processing_state = ProcessingStatus.query.filter_by(
|
|---|
| 125 | label='Queued for Parsing'
|
|---|
| 126 | ).all()
|
|---|
| 127 | if len(processing_state) != 1:
|
|---|
| 128 | raise MapserviceBadRequest(
|
|---|
| 129 | detail='Processing status Queued for Parsing not found database'
|
|---|
| 130 | )
|
|---|
| 131 | processing_state = processing_state[0]
|
|---|
| 132 |
|
|---|
| 133 | log = Log.query.filter_by(id=log_id).all()
|
|---|
| 134 | if len(log) != 1:
|
|---|
| 135 | raise MapserviceBadRequest(
|
|---|
| 136 | detail='Log {} not found database'.format(log_id)
|
|---|
| 137 | )
|
|---|
| 138 | log = log[0]
|
|---|
| 139 |
|
|---|
| 140 | # check if Log has already log entries
|
|---|
| 141 | if LogEntry.query.filter_by(log=log).count() > 0:
|
|---|
| 142 | raise MapserviceBadRequest(
|
|---|
| 143 | detail='Log {} has already log entries. Aborting'.format(log_id)
|
|---|
| 144 | )
|
|---|
| 145 |
|
|---|
| 146 | # Add Log to Queued table and change the processing state of Log
|
|---|
| 147 | queue = Queue(log=log)
|
|---|
| 148 | db.session.add(queue)
|
|---|
| 149 | log.processingstatus = processing_state
|
|---|
| 150 | db.session.commit()
|
|---|
| 151 |
|
|---|
| 152 | # request log parsing
|
|---|
| 153 | log_process.delay(log.id, body.decode())
|
|---|
| 154 |
|
|---|
| 155 | return log.meta_data()
|
|---|
| 156 |
|
|---|
| [ec9e811] | 157 |
|
|---|
| [ac11efe] | 158 | def logs_log_delete(id):
|
|---|
| [7be94aa] | 159 | user = User.loggedin_user()
|
|---|
| 160 | if user is None:
|
|---|
| 161 | raise MapserviceUnauthorized
|
|---|
| 162 |
|
|---|
| 163 | if not Log.query.filter_by(submitter=user).filter_by(id=id).delete():
|
|---|
| 164 | raise MapserviceBadRequest(
|
|---|
| 165 | detail="Could not delete log with id {}".format(id)
|
|---|
| 166 | )
|
|---|
| 167 | db.session.commit()
|
|---|
| [ac11efe] | 168 |
|
|---|
| [7be94aa] | 169 | query = Log.query.filter_by(submitter=user)
|
|---|
| 170 | result = []
|
|---|
| 171 | for log in query:
|
|---|
| 172 | result.append(log.meta_data())
|
|---|
| 173 | return result
|
|---|