-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtimeline_controller.rb
More file actions
224 lines (214 loc) · 7.19 KB
/
timeline_controller.rb
File metadata and controls
224 lines (214 loc) · 7.19 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
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
# encoding: ascii-8bit
# Copyright 2022 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2024, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
require 'openc3/models/timeline_model'
class TimelineController < ApplicationController
def initialize
super()
@model_class = OpenC3::TimelineModel
end
# Returns an array/list of timeline values in json.
#
# scope [String] the scope of the timeline, `TEST`
# @return [String] the array of timeline names converted into json format
def index
return unless authorization('system')
timelines = @model_class.all
ret = []
timelines.each do |timeline, value|
if params[:scope] == timeline.split('__')[0]
ret << value
end
end
render json: ret
end
# Create a new timeline returns object/hash of the timeline in json.
#
# scope [String] the scope of the timeline, `TEST`
# json [String] The json of the timeline name (see below)
# @return [String] the timeline converted into json format
# Request Headers
#```json
# {
# "Authorization": "token/password",
# "Content-Type": "application/json"
# }
#```
# Request Post Body
#```json
# {
# "name": "system42",
# "color": "#FFFFFF"
# }
#```
def create
return unless authorization('script_run')
begin
model = @model_class.new(name: params['name'], color: params['color'], scope: params[:scope])
model.create()
model.deploy()
OpenC3::Logger.info("Timeline created: #{params['name']}", scope: params[:scope], user: username())
render json: model.as_json(), status: 201
rescue RuntimeError, JSON::ParserError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
rescue TypeError => e
log_error(e)
render json: { status: 'error', message: 'Invalid json object', type: e.class }, status: 400
rescue OpenC3::TimelineInputError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
end
end
# Returns a timeline in json.
#
# name [String] the name of the timeline, `TEST`
# scope [String] the scope of the timeline, `DEFAULT`
# @return [String] timeline converted into json format
def show
return unless authorization('system')
begin
model = @model_class.get(name: params['name'], scope: params[:scope])
if model.nil?
render json: { status: 'error', message: 'not found' }, status: 404
else
render json: model.as_json()
end
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, e: e.to_s }, status: 400
end
end
# Change the color returns object/hash of the timeline in json.
#
# name [String] the timeline name, `system42`
# scope [String] the scope of the timeline, `TEST`
# json [String] The json of the timeline name (see below)
# @return [String] the timeline converted into json format
# Request Headers
#```json
# {
# "Authorization": "token/password",
# "Content-Type": "application/json"
# }
#```
# Request Post Body
#```json
# {
# "color": "#FFFFFF"
# }
#```
def color
return unless authorization('script_run')
model = @model_class.get(name: params[:name], scope: params[:scope])
if model.nil?
render json: {
status: 'error',
message: "failed to find timeline: #{params[:name]}",
}, status: 404
return
end
begin
# Model color=(value) method handles the color validation
model.color = params['color']
model.update()
model.notify(kind: 'updated')
render json: model.as_json()
rescue RuntimeError, JSON::ParserError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
rescue TypeError => e
log_error(e)
render json: { status: 'error', message: 'Invalid json object', type: e.class }, status: 400
rescue OpenC3::TimelineInputError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
end
end
# Set the timeline execution status
#
# name [String] the timeline name, `system42`
# scope [String] the scope of the timeline, `TEST`
# json [String] The json of the timeline name (see below)
# @return [String] the timeline converted into json format
# Request Headers
#```json
# {
# "Authorization": "token/password",
# "Content-Type": "application/json"
# }
#```
# Request Post Body
#```json
# {
# "enable": "false"
# }
#```
def execute
return unless authorization('script_run')
model = @model_class.get(name: params[:name], scope: params[:scope])
if model.nil?
render json: {
status: 'error',
message: "failed to find timeline: #{params[:name]}",
}, status: 404
return
end
begin
# Model execute=(value) method handles the conversion of the string to a boolean
model.execute = params['enable']
model.update()
model.notify(kind: 'updated')
render json: model.as_json()
rescue RuntimeError, JSON::ParserError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
rescue TypeError => e
log_error(e)
render json: { status: 'error', message: 'Invalid json object', type: e.class }, status: 400
rescue OpenC3::TimelineInputError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
end
end
# Delete timeline
#
# name [String] the timeline name, `system42`
# scope [String] the scope of the timeline, `TEST`
# @return [String] hash/object of timeline name in json with a 200 status code
def destroy
return unless authorization('script_run')
model = @model_class.get(name: params[:name], scope: params[:scope])
if model.nil?
render json: { status: 'error', message: "failed to find timeline: #{params[:name]}" }, status: 404
else
begin
use_force = params[:force].nil? == false && params[:force] == 'true'
@model_class.delete(name: params[:name], scope: params[:scope], force: use_force)
model.undeploy()
model.notify(kind: 'deleted')
OpenC3::Logger.info("Timeline destroyed: #{params[:name]}", scope: params[:scope], user: username())
render json: { name: params[:name]}
rescue OpenC3::TimelineError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
end
end
end
end