-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhousekeeping.py
More file actions
executable file
·165 lines (134 loc) · 5.53 KB
/
housekeeping.py
File metadata and controls
executable file
·165 lines (134 loc) · 5.53 KB
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
from flask import request
from flask import Blueprint
from flask_restful import Resource, Api
from marshmallow import ValidationError
from datetime import datetime
import json
from groundstation.backend_api.models import Housekeeping, AdcsHK, AthenaHK, \
EpsHK, EpsStartupHK, IrisHK, UhfHK, SbandHK, HyperionHK, CharonHK, DfgmHK, \
NorthernSpiritHK
from groundstation import db
from groundstation.backend_api.utils import create_context, login_required, \
dynamic_filters_housekeeping
from groundstation.backend_api.validators import HousekeepingValidator
from werkzeug.datastructures import MultiDict
from sqlalchemy import desc
housekeeping_blueprint = Blueprint('housekeeping', __name__)
api = Api(housekeeping_blueprint)
class HousekeepingLog(Resource):
@create_context
def get(self, housekeeping_id):
"""Endpoint for getting a specific housekeeping log
:param int housekeeping_id: The housekeeping_id
:returns: response_object, status_code
:rtype: tuple (dict, int)
"""
response_object = {
'status': 'fail',
'message': 'Housekeeping Log does not exist'
}
housekeeping = Housekeeping.query.filter_by(id=housekeeping_id).first()
if not housekeeping:
return response_object, 404
else:
response_object = {
'status': 'success',
'data': housekeeping.to_json()
}
return response_object, 200
class HousekeepingLogList(Resource):
def __init__(self):
self.validator = HousekeepingValidator()
super(HousekeepingLogList, self).__init__()
@create_context
@login_required
def post(self, local_data=None):
"""Endpoint for creating a new Housekeeping log
:param json_string local_data: This should be used in place of the POST body that would be used through HTTP, used for local calls.
:returns: response_object, status_code
:rtype: tuple (dict, int)
"""
if not local_data:
post_data = request.get_json()
else:
post_data = json.loads(local_data)
try:
validated_data = self.validator.load(post_data)
except ValidationError as err:
response_object = {
'status': 'fail',
'message': 'Invalid payload',
'errors': err.messages
}
return response_object, 400
adcs_data = validated_data.pop('adcs')
athena_data = validated_data.pop('athena')
eps_data = validated_data.pop('eps')
eps_startup_data = validated_data.pop('eps_startup')
uhf_data = validated_data.pop('uhf')
sband_data = validated_data.pop('sband')
hyperion_data = validated_data.pop('hyperion')
charon_data = validated_data.pop('charon')
dfgm_data = validated_data.pop('dfgm')
northern_spirit_data = validated_data.pop('northern_spirit')
iris_data = validated_data.pop('iris')
subsystems = {
'adcs': AdcsHK(**adcs_data),
'athena': AthenaHK(**athena_data),
'eps': EpsHK(**eps_data),
'eps_startup': EpsStartupHK(**eps_startup_data),
'uhf': UhfHK(**uhf_data),
'sband': SbandHK(**sband_data),
'hyperion': HyperionHK(**hyperion_data),
'charon': CharonHK(**charon_data),
'dfgm': DfgmHK(**dfgm_data),
'northern_spirit': NorthernSpiritHK(**northern_spirit_data),
'iris': IrisHK(**iris_data)
}
housekeeping = Housekeeping(**validated_data, **subsystems)
db.session.add(housekeeping)
db.session.commit()
response_object = {
'status': 'success',
'message': f'Housekeeping Log with timestamp {housekeeping.timestamp} was added!'
}
return response_object, 201
@create_context
def get(self, local_args=None):
"""Endpoint for getting a list of housekeeping logs
:param dict local_args: This should be used in place of the QUERY PARAMS that would be used through HTTP, used for local calls.
:returns: response_object, status_code
:rtype: tuple (dict, int)
"""
if not local_args:
query_limit = request.args.get('limit', None)
newest_first = request.args.get('newest-first', None)
args = dynamic_filters_housekeeping(
request.args, ignore_keys=['limit', 'newest-first'])
else:
local_args = MultiDict(local_args)
query_limit = local_args.get('limit', None)
newest_first = local_args.get('newest-first', None)
args = dynamic_filters_housekeeping(
local_args, ignore_keys=['limit', 'newest-first'])
if args is None:
response_object = {
'status': 'fail',
'message': 'Invalid query params',
}
return response_object, 400
if newest_first in ("true", True):
ordering = desc(Housekeeping.timestamp)
else:
ordering = Housekeeping.timestamp
logs = Housekeeping.query.filter(
*args).order_by(ordering).limit(query_limit).all()
response_object = {
'status': 'success',
'data': {
'logs': [log.to_json() for log in logs]
}
}
return response_object, 200
api.add_resource(HousekeepingLog, '/api/housekeepinglog/<housekeeping_id>')
api.add_resource(HousekeepingLogList, '/api/housekeepinglog')