Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata #1642

Merged
7 commits merged into from
May 23, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 46 additions & 48 deletions cosmos-cmd-tlm-api/app/controllers/metadata_controller.rb
Original file line number Diff line number Diff line change
@@ -18,26 +18,25 @@
# copyright holder

require 'cosmos/models/metadata_model'
require 'time'

class MetadataController < ApplicationController
def initialize
@model_class = Cosmos::MetadataModel
end

def parse_time_input(x_start:, x_stop:)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? Why does it default to +/- 7 days?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know but I didn't change it. It doesn't match the comment either which says start is minus 12 hrs and end is plus 2 days. I'm honestly not sure when the search feature is used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

now = DateTime.now.new_offset(0)
start = x_start.nil? ? (now - 7) : DateTime.parse(x_start) # minus 7 days
stop = x_stop.nil? ? (now + 7) : DateTime.parse(x_stop) # plus 7 days
return start.strftime('%s%3N').to_i, stop.strftime('%s%3N').to_i
now = Time.now
start = x_start.nil? ? (now - 7.days) : Time.parse(x_start) # minus 7 days
stop = x_stop.nil? ? (now + 7.days) : Time.parse(x_stop) # plus 7 days
return start.to_i, stop.to_i
end

# Returns a single metadata in json
#
# name [String] the target or name of metadata, `INST`
# scope [String] the scope of the chronicle, `DEFAULT`
# target [String] (required) The key in the metadata
# scope [String] the scope of the metadata, `DEFAULT`
# @return [String] the current metadata converted into json format.
def get
def latest
begin
authorize(permission: 'system', scope: params[:scope], token: request.headers['HTTP_AUTHORIZATION'])
rescue Cosmos::AuthError => e
@@ -46,22 +45,24 @@ def get
render(:json => { :status => 'error', :message => e.message }, :status => 403) and return
end
begin
model = @model_class.get_current_value(target: params[:name], scope: params[:scope])
if model.nil?
json = @model_class.get_current_value(scope: params[:scope])
if json.nil?
render :json => { :status => 'error', :message => 'not found' }, :status => 204
return
end
render :json => model.as_json(), :status => 200
render :json => json, :status => 200
rescue StandardError => e
render :json => { :status => 'error', :message => e.message, :type => e.class, :backtrace => e.backtrace }, :status => 400
end
end

# Returns an array/list of metadata in json. With optional start_time and end_time parameters
#
# scope [String] the scope of the chronicle, `DEFAULT`
# start [String] (optional) The start time of the search window for chronicle to return. Recommend in ISO format, `2031-04-16T01:02:00+00:00`. If not provided start_time is equal to 12 hours before the request is made.
# stop [String] (optional) The stop time of the search window for chronicle to return. Recommend in ISO format, `2031-04-16T01:02:00+00:00`. If not provided end_time is equal to 2 days after the request is made.
# scope [String] the scope of the metadata, `DEFAULT`
# start [String] (optional) The start time of the search window for metadata to return.
# Recommend in ISO format, `2031-04-16T01:02:00+00:00`. If not provided start_time is equal to 12 hours before the request is made.
# stop [String] (optional) The stop time of the search window for metadata to return.
# Recommend in ISO format, `2031-04-16T01:02:00+00:00`. If not provided end_time is equal to 2 days after the request is made.
# @return [String] the array of entries converted into json format.
def index
begin
@@ -73,7 +74,7 @@ def index
end
begin
start, stop = parse_time_input(x_start: params[:start], x_stop: params[:stop])
json_array = @model_class.get(scope: params[:scope], start: start, stop: stop)
json_array = @model_class.range(scope: params[:scope], start: start, stop: stop)
render :json => json_array, :status => 200
rescue ArgumentError
render :json => { :status => 'error', :message => 'Invalid input provided' }, :status => 400
@@ -84,9 +85,11 @@ def index

# Returns an array/list of metadata in json. With optional start_time and end_time parameters
#
# scope [String] the scope of the chronicle, `DEFAULT`
# start [String] (optional) The start time of the search window for chronicle to return. Recommend in ISO format, `2031-04-16T01:02:00+00:00`. If not provided start_time is equal to 12 hours before the request is made.
# stop [String] (optional) The stop time of the search window for chronicle to return. Recommend in ISO format, `2031-04-16T01:02:00+00:00`. If not provided end_time is equal to 2 days after the request is made.
# scope [String] the scope of the metadata, `DEFAULT`
# start [String] (optional) The start time of the search window for metadata to return.
# Recommend in ISO format, `2031-04-16T01:02:00+00:00`. If not provided start_time is equal to 12 hours before the request is made.
# stop [String] (optional) The stop time of the search window for metadata to return.
# Recommend in ISO format, `2031-04-16T01:02:00+00:00`. If not provided end_time is equal to 2 days after the request is made.
# key [String] (required) The key in the metadata
# value [String] (required) The value equal to the value of the key in the metadata
# @return [String] the array of entries converted into json format.
@@ -100,14 +103,14 @@ def search
end
begin
start, stop = parse_time_input(x_start: params[:start], x_stop: params[:stop])
json_array = @model_class.get(scope: params[:scope], start: start, stop: stop)
json_array = @model_class.range(scope: params[:scope], start: start, stop: stop)
key, value = [params[:key], params[:value]]
raise MetadataInputError "Must include key, value in metadata search" if key.nil? || value.nil?
raise Cosmos::SortedInputError "Must include key, value in metadata search" if key.nil? || value.nil?
selected_array = json_array.select { | json_model | json_model['metadata'][key] == value }
render :json => selected_array, :status => 200
rescue ArgumentError => e
render :json => { :status => 'error', :message => 'Invalid input provided' }, :status => 400
rescue Cosmos::MetadataError => e
rescue Cosmos::SortedError => e
render :json => { :status => 'error', :message => e.message, :type => e.class }, :status => 400
rescue StandardError => e
render :json => { :status => 'error', :message => e.message, :type => e.class, :backtrace => e.backtrace }, :status => 400
@@ -116,7 +119,7 @@ def search

# Returns an object/hash the contains `count` as a key in json.
#
# scope [String] the scope of the chronicle, `DEFAULT`
# scope [String] the scope of the metadata, `DEFAULT`
# @return [String] the object/hash converted into json format
# Request Headers
# ```json
@@ -143,7 +146,7 @@ def count

# Returns an object/hash of a single metadata in json.
#
# scope [String] the scope of the chronicle, `DEFAULT`
# scope [String] the scope of the metadata, `DEFAULT`
# id [String] the id of the entry, `1620248449`
# @return [String] the metadata as a object/hash converted into json format
# Request Headers
@@ -162,20 +165,20 @@ def show
render(:json => { :status => 'error', :message => e.message }, :status => 403) and return
end
begin
model_hash = @model_class.score(score: params[:id], scope: params[:scope])
if model_hash.nil?
model_hash = @model_class.get(start: params[:id], scope: params[:scope])
if model_hash
render :json => model_hash, :status => 200
else
render :json => { :status => 'error', :message => 'not found' }, :status => 404
return
end
render :json => model_hash, :status => 200
rescue StandardError => e
render :json => { :status => 'error', :message => e.message, :type => e.class, :backtrace => e.backtrace }, :status => 400
end
end

# Record metadata and returns an object/hash of in json.
#
# scope [String] the scope of the chronicle, `DEFAULT`
# scope [String] the scope of the metadata, `DEFAULT`
# json [String] The json of the activity (see below)
# @return [String] the metadata converted into json format
# Request Headers
@@ -188,7 +191,6 @@ def show
# Request Post Body
# ```json
# {
# "target": "INST",
# "start": "2031-04-16T01:02:00.001+00:00",
# "color": "#FF0000",
# "metadata": {"version"=>"v1234567"}
@@ -203,11 +205,11 @@ def create
render(:json => { :status => 'error', :message => e.message }, :status => 403) and return
end
begin
hash = params.to_unsafe_h.slice(:target, :start, :color, :metadata).to_h
hash = params.to_unsafe_h.slice(:start, :color, :metadata).to_h
if hash['start'].nil?
hash['start'] = DateTime.now.strftime('%s%3N').to_i
hash['start'] = Time.now.to_i
else
hash['start'] = DateTime.parse(hash['start']).strftime('%s%3N').to_i
hash['start'] = Time.parse(hash['start']).to_i
end
model = @model_class.from_json(hash.symbolize_keys, scope: params[:scope])
model.create()
@@ -220,9 +222,9 @@ def create
rescue ArgumentError, TypeError => e
message = "Invalid input: #{JSON.generate(hash)}"
render :json => { :status => 'error', :message => message, :type => e.class }, :status => 400
rescue Cosmos::MetadataInputError => e
rescue Cosmos::SortedInputError => e
render :json => { :status => 'error', :message => e.message, :type => e.class }, :status => 400
rescue Cosmos::MetadataError => e
rescue Cosmos::SortedError => e
render :json => { :status => 'error', :message => e.message, :type => e.class }, :status => 418
rescue StandardError => e
render :json => { :status => 'error', :message => e.message, :type => e.class, :backtrace => e.backtrace }, :status => 400
@@ -232,7 +234,7 @@ def create
# Update metadata and returns an object/hash of in json.
#
# id [String] the id or start value, `12345667`
# scope [String] the scope of the chronicle, `TEST`
# scope [String] the scope of the metadata, `TEST`
# json [String] The json of the activity (see below)
# @return [String] the activity converted into json format
# Request Headers
@@ -245,7 +247,6 @@ def create
# Request Post Body
# ```json
# {
# "target": "target",
# "start": "2031-04-16T01:02:00.001+00:00",
# "metadata": {"version"=>"v1234567"}
# "color": "#FF0000",
@@ -260,15 +261,15 @@ def update
render(:json => { :status => 'error', :message => e.message }, :status => 403) and return
end
begin
hash = @model_class.score(score: params[:id], scope: params[:scope])
hash = @model_class.get(start: params[:id], scope: params[:scope])
if hash.nil?
render :json => { :status => 'error', :message => 'not found' }, :status => 404
return
end
model = @model_class.from_json(hash.symbolize_keys, scope: params[:scope])

hash = params.to_unsafe_h.slice(:target, :start, :color, :metadata).to_h
hash['start'] = DateTime.parse(hash['start']).strftime('%s%3N').to_i
hash = params.to_unsafe_h.slice(:start, :color, :metadata).to_h
hash['start'] = Time.parse(hash['start']).to_i
model.update(start: hash['start'], color: hash['color'], metadata: hash['metadata'])
Cosmos::Logger.info(
"Metadata updated: #{model}",
@@ -279,9 +280,9 @@ def update
rescue ArgumentError, TypeError => e
message = "Invalid input: #{JSON.generate(hash)}"
render :json => { :status => 'error', :message => message, :type => e.class }, :status => 400
rescue Cosmos::MetadataInputError => e
rescue Cosmos::SortedInputError => e
render :json => { :status => 'error', :message => e.message, :type => e.class }, :status => 400
rescue Cosmos::MetadataError => e
rescue Cosmos::SortedError => e
render :json => { :status => 'error', :message => e.message, :type => e.class }, :status => 418
rescue StandardError => e
render :json => { :status => 'error', :message => e.message, :type => e.class, :backtrace => e.backtrace }, :status => 400
@@ -309,24 +310,21 @@ def delete
render(:json => { :status => 'error', :message => e.message }, :status => 403) and return
end
begin
hash = @model_class.score(score: params[:id], scope: params[:scope])
if hash.nil?
count = @model_class.destroy(start: params[:id], scope: params[:scope])
if count == 0
render :json => { :status => 'error', :message => 'not found' }, :status => 404
return
end
model = @model_class.from_json(hash.symbolize_keys, scope: params[:scope])
model.destroy()
Cosmos::Logger.info(
"Metadata destroyed: #{params[:id]}",
scope: params[:scope],
user: user_info(request.headers['HTTP_AUTHORIZATION'])
)
render :json => { "status" => 1 }, :status => 204
rescue Cosmos::MetadataError => e
render :json => { "status" => count }, :status => 204
rescue Cosmos::SortedError => e
render :json => { :status => 'error', :message => e.message, :type => e.class }, :status => 400
rescue StandardError => e
render :json => { :status => 'error', :message => e.message, :type => e.class, :backtrace => e.backtrace }, :status => 400
end
end

end
18 changes: 8 additions & 10 deletions cosmos-cmd-tlm-api/app/controllers/narrative_controller.rb
Original file line number Diff line number Diff line change
@@ -17,11 +17,11 @@
# enterprise edition license of COSMOS if purchased from the
# copyright holder

require 'cosmos/models/narrative_model'
require 'cosmos/models/note_model'

class NarrativeController < ApplicationController
def initialize
@model_class = Cosmos::NarrativeModel
@model_class = Cosmos::NoteModel
end

def parse_time_input(x_start:, x_stop:)
@@ -79,7 +79,7 @@ def search
start, stop = parse_time_input(x_start: params[:start], x_stop: params[:stop])
q = params[:q]
raise NarrativeInputError "Must include q value" if q.nil?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still Narrative

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I haven't made it up the chain yet to the controller and frontend

model_array = @model_class.get(scope: params[:scope], start: start, stop: stop)
model_array = @model_class.range(scope: params[:scope], start: start, stop: stop)
model_array.find { |model| model.description.include? q }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is q?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment says "The string to contain in the narrative description". Worst variable name ever though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

render :json => model_array, :status => 200
rescue ArgumentError
@@ -139,7 +139,7 @@ def show
render(:json => { :status => 'error', :message => e.message }, :status => 403) and return
end
begin
model_hash = @model_class.score(score: params[:id], scope: params[:scope])
model_hash = @model_class.get(score: params[:id], scope: params[:scope])
if model_hash.nil?
render :json => { :status => 'error', :message => 'not found' }, :status => 404
return
@@ -238,7 +238,7 @@ def update
render(:json => { :status => 'error', :message => e.message }, :status => 403) and return
end
begin
hash = @model_class.score(score: params[:id], scope: params[:scope])
hash = @model_class.get(score: params[:id], scope: params[:scope])
if hash.nil?
render :json => { :status => 'error', :message => 'not found' }, :status => 404
return
@@ -295,19 +295,17 @@ def delete
render(:json => { :status => 'error', :message => e.message }, :status => 403) and return
end
begin
hash = @model_class.score(score: params[:id], scope: params[:scope])
if hash.nil?
count = @model_class.destroy(start: params[:id], scope: params[:scope])
if count == 0
render :json => { :status => 'error', :message => 'not found' }, :status => 404
return
end
model = @model_class.from_json(hash.symbolize_keys, scope: params[:scope])
model.destroy()
Cosmos::Logger.info(
"Narrative destroyed: #{model}",
scope: params[:scope],
user: user_info(request.headers['HTTP_AUTHORIZATION'])
)
render :json => { "status" => 1 }, :status => 204
render :json => { "status" => count }, :status => 204
rescue Cosmos::NarrativeError => e
render :json => { :status => 'error', :message => e.message, :type => e.class }, :status => 400
rescue StandardError => e
2 changes: 1 addition & 1 deletion cosmos-cmd-tlm-api/config/routes.rb
Original file line number Diff line number Diff line change
@@ -125,8 +125,8 @@

get '/metadata', to: 'metadata#index'
post '/metadata', to: 'metadata#create'
get '/metadata/latest', to: 'metadata#latest', name: /[^\/]+/
get '/metadata/_search', to: 'metadata#search'
get '/metadata/_get/:name', to: 'metadata#get', name: /[^\/]+/
get '/metadata/:id', to: 'metadata#show', id: /[^\/]+/
match '/metadata/:id', to: 'metadata#update', id: /[^\/]+/, via: [:patch, :put]
delete '/metadata/:id', to: 'metadata#delete', id: /[^\/]+/
Loading