Commit 8201bc3b by source_reader

added edit endpoints for annotations, better logging, and bug fixes

parent 1765b262
......@@ -55,8 +55,8 @@ module Joule
@backend.module_interface(joule_module, req)
end
def module_post_interface(joule_module, req)
@backend.module_post_interface(joule_module, req)
def module_post_interface(joule_module, req, body)
@backend.module_post_interface(joule_module, req, body)
end
# === ANNOTATIONS ===
......@@ -88,6 +88,20 @@ module Joule
# returns nil
@backend.delete_annotation(annotation.id)
end
def edit_annotation(id, title, content, stream)
json = @backend.edit_annotation(id, title, content)
annotation = Annotation.new
annotation.id = json["id"]
annotation.title = json["title"]
annotation.content = json["content"]
annotation.start_time = json["start"]
annotation.end_time = json["end"]
# ignore joule stream_id parameter
# use the db_stream model instead
annotation.db_stream = stream
annotation
end
# === END ANNOTATIONS ===
def node_type
......
......@@ -18,87 +18,87 @@ module Joule
end
def dbinfo
begin
resp = self.class.get("#{@url}/version")
if not resp.success?
Rails.logger.warn "Error retrieving /version for #{@url}: [#{resp.body}]"
begin
resp = self.class.get("#{@url}/version")
if not resp.success?
Rails.logger.warn "Error retrieving /version for #{@url}: [#{resp.body}]"
return nil
end
version = resp.parsed_response
resp = self.class.get("#{@url}/dbinfo")
if not resp.success?
Rails.logger.warn "Error retrieving /dbinfo for #{@url}: [#{resp.body}]"
return nil
end
info = resp.parsed_response
rescue StandardError => e
Rails.logger.warn "Error retrieving dbinfo for #{@url}: [#{e}]"
return nil
end
version = resp.parsed_response
resp = self.class.get("#{@url}/dbinfo")
if not resp.success?
Rails.logger.warn "Error retrieving /dbinfo for #{@url}: [#{resp.body}]"
return nil
end
info = resp.parsed_response
rescue StandardError => e
Rails.logger.warn "Error retrieving dbinfo for #{@url}: [#{e}]"
return nil
end
# if the site exists but is not a nilm...
required_keys = %w(size other free reserved)
unless info.respond_to?(:has_key?) &&
required_keys.all? { |s| info.key? s }
Rails.logger.warn "Error #{@url} is not a Joule node"
return nil
end
{
version: version,
size_db: info['size'],
size_other: info['other'],
size_total: info['size'] + info['other'] + info['free'] + info['reserved']
}
end
# if the site exists but is not a nilm...
required_keys = %w(size other free reserved)
unless info.respond_to?(:has_key?) &&
required_keys.all? {|s| info.key? s}
Rails.logger.warn "Error #{@url} is not a Joule node"
return nil
end
{
version: version,
size_db: info['size'],
size_other: info['other'],
size_total: info['size'] + info['other'] + info['free'] + info['reserved']
}
end
def db_schema
begin
resp = self.class.get("#{@url}/streams.json")
return nil unless resp.success?
rescue StandardError => e
Rails.logger.warn "Error retrieving db_schema for #{@url}: [#{e}]"
return nil
end
resp.parsed_response.deep_symbolize_keys
begin
resp = self.class.get("#{@url}/streams.json")
return nil unless resp.success?
rescue StandardError => e
Rails.logger.warn "Error retrieving db_schema for #{@url}: [#{e}]"
return nil
end
resp.parsed_response.deep_symbolize_keys
end
def module_schemas
begin
resp = self.class.get("#{@url}/modules.json?statistics=1")
return nil unless resp.success?
items = resp.parsed_response
# if the site exists but is not a joule server...
required_keys = %w(name inputs outputs)
items.each do |item|
unless item.respond_to?(:has_key?) &&
required_keys.all? { |s| item.key? s }
Rails.logger.warn "Error #{@url} is not a Joule node"
return nil
end
item.symbolize_keys!
end
rescue StandardError => e
Rails.logger.warn "Error retrieving module_schemas for #{@url}: [#{e}]"
return nil
end
items
begin
resp = self.class.get("#{@url}/modules.json?statistics=1")
return nil unless resp.success?
items = resp.parsed_response
# if the site exists but is not a joule server...
required_keys = %w(name inputs outputs)
items.each do |item|
unless item.respond_to?(:has_key?) &&
required_keys.all? {|s| item.key? s}
Rails.logger.warn "Error #{@url} is not a Joule node"
return nil
end
item.symbolize_keys!
end
rescue StandardError => e
Rails.logger.warn "Error retrieving module_schemas for #{@url}: [#{e}]"
return nil
end
items
end
def module_interface(joule_module, req)
self.class.get("#{@url}/interface/#{joule_module.joule_id}/#{req}")
self.class.get("#{@url}/interface/#{joule_module.joule_id}/#{req}")
end
def module_post_interface(joule_module, req)
self.class.post("#{@url}/interface/#{joule_module.joule_id}/#{req}")
def module_post_interface(joule_module, req, body)
self.class.post("#{@url}/interface/#{joule_module.joule_id}/#{req}", body: body)
end
def stream_info(joule_id)
begin
resp = self.class.get("#{@url}/stream.json?id=#{joule_id}")
return nil unless resp.success?
resp = self.class.get("#{@url}/stream.json?id=#{joule_id}")
return nil unless resp.success?
rescue
return nil
return nil
end
resp.parsed_response.deep_symbolize_keys
end
......@@ -107,10 +107,10 @@ module Joule
query = {'id': joule_id, 'max-rows': resolution}
query['start'] = start_time unless start_time.nil?
query['end'] = end_time unless end_time.nil?
options = { query: query}
options = {query: query}
begin
resp = self.class.get("#{@url}/data.json", options)
if resp.code==400 and resp.body.include?('decimated data is not available')
if resp.code == 400 and resp.body.include?('decimated data is not available')
return {success: false, result: "decimation error"}
end
return {success: false, result: resp.body} unless resp.success?
......@@ -124,7 +124,7 @@ module Joule
query = {'id': joule_id}
query['start'] = start_time unless start_time.nil?
query['end'] = end_time unless end_time.nil?
options = { query: query}
options = {query: query}
begin
resp = self.class.get("#{@url}/data/intervals.json", options)
return {success: false, result: resp.body} unless resp.success?
......@@ -143,51 +143,51 @@ module Joule
def update_stream(db_stream)
elements = []
db_stream.db_elements.each do |elem|
elements << {name: elem.name,
plottable: elem.plottable,
units: elem.units,
default_min: elem.default_min,
default_max: elem.default_max,
scale_factor: elem.scale_factor,
offset: elem.offset,
display_type: elem.display_type}
end
attrs = { name: db_stream.name,
description: db_stream.description,
elements: elements
}
elements << {name: elem.name,
plottable: elem.plottable,
units: elem.units,
default_min: elem.default_min,
default_max: elem.default_max,
scale_factor: elem.scale_factor,
offset: elem.offset,
display_type: elem.display_type}
end
attrs = {name: db_stream.name,
description: db_stream.description,
elements: elements
}
begin
response = self.class.put("#{@url}/stream.json",
headers: { 'Content-Type' => 'application/json' },
headers: {'Content-Type' => 'application/json'},
body: {
id: db_stream.joule_id,
stream: attrs}.to_json)
id: db_stream.joule_id,
stream: attrs}.to_json)
rescue
return { error: true, msg: 'cannot contact Joule server' }
return {error: true, msg: 'cannot contact Joule server'}
end
unless response.success?
return { error: true, msg: "error updating #{db_stream.path} metadata" }
return {error: true, msg: "error updating #{db_stream.path} metadata"}
end
{ error: false, msg: 'success' }
{error: false, msg: 'success'}
end
def update_folder(db_folder)
attrs = { name: db_folder.name,
description: db_folder.description}
attrs = {name: db_folder.name,
description: db_folder.description}
begin
response = self.class.put("#{@url}/folder.json",
headers: { 'Content-Type' => 'application/json' },
headers: {'Content-Type' => 'application/json'},
body: {
id: db_folder.joule_id,
folder: attrs}.to_json)
rescue
return { error: true, msg: 'cannot contact Joule server' }
return {error: true, msg: 'cannot contact Joule server'}
end
unless response.success?
return { error: true, msg: "error updating #{db_folder.path} metadata" }
return {error: true, msg: "error updating #{db_folder.path} metadata"}
end
{ error: false, msg: 'success' }
{error: false, msg: 'success'}
end
# === ANNOTATION METHODS ===
......@@ -200,8 +200,8 @@ module Joule
'end': annotation.end_time}
begin
resp = self.class.post("#{@url}/annotation.json",
headers: { 'Content-Type' => 'application/json' },
body: data.to_json)
headers: {'Content-Type' => 'application/json'},
body: data.to_json)
raise "error creating annotations #{resp.body}" unless resp.success?
rescue
raise "connection error"
......@@ -212,7 +212,7 @@ module Joule
def get_annotations(stream_id)
query = {'stream_id': stream_id}
options = { query: query}
options = {query: query}
begin
resp = self.class.get("#{@url}/annotations.json", options)
raise "error loading annotations #{resp.body}" unless resp.success?
......@@ -224,7 +224,7 @@ module Joule
def delete_annotation(annotation_id)
query = {'id': annotation_id}
options = { query: query}
options = {query: query}
begin
resp = self.class.delete("#{@url}/annotation.json",
options)
......@@ -234,6 +234,21 @@ module Joule
end
end
def edit_annotation(annotation_id, title, content)
begin
resp = self.class.put("#{@url}/annotation.json",
headers: {'Content-Type' => 'application/json'},
body: {id: annotation_id,
title: title,
content: content}.to_json)
raise "error updating annotation #{resp.body}" unless resp.success?
rescue
raise "connection error"
end
resp.parsed_response
end
# === END ANNOTATION METHODS ===
end
end
......@@ -95,5 +95,24 @@ module Nilmdb
end
@backend.write_annotations(path, updated_annotations)
end
def edit_annotation(id, title, content, stream)
path = stream.path
json = @backend.read_annotations(path)
index = json.index{ |item| item['id']== id.to_i}
raise "error, invalid annotation id" if index.nil?
# find the specified id
json[index]['title'] = title
json[index]['content'] = content
@backend.write_annotations(path, json)
annotation = Annotation.new
annotation.id = json[index]["id"]
annotation.title = json[index]["title"]
annotation.content = json[index]["content"]
annotation.start_time = json[index]["start"]
annotation.end_time = json[index]["end"]
annotation.db_stream = stream
annotation
end
end
end
......@@ -38,6 +38,22 @@ class AnnotationsController < ApplicationController
render :index
end
# PATCH/PUT /annotations/1.json
def update
@service = StubService.new
begin
annotation = @node_adapter.edit_annotation(params[:id],
params[:title],
params[:content],
@db_stream)
rescue RuntimeError => e
@service.add_error("Cannot update annotation [#{e}]")
render 'helpers/empty_response', status: :unprocessable_entity and return
end
@annotations = [annotation]
render :index
end
# DELETE /annotations/1.json
def destroy
annotation = Annotation.new
......
......@@ -47,7 +47,7 @@ class InterfacesController < ActionController::Base
def post
path = create_proxy_path(request.fullpath, @joule_module.id)
proxied_response = @node_adapter.module_post_interface(@joule_module,path)
proxied_response = @node_adapter.module_post_interface(@joule_module,path, request.raw_post)
render plain: proxied_response.body
proxied_response.headers.each do |key,value|
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment