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
......
......@@ -39,7 +39,7 @@ module Joule
# 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 }
required_keys.all? {|s| info.key? s}
Rails.logger.warn "Error #{@url} is not a Joule node"
return nil
end
......@@ -71,7 +71,7 @@ module Joule
required_keys = %w(name inputs outputs)
items.each do |item|
unless item.respond_to?(:has_key?) &&
required_keys.all? { |s| item.key? s }
required_keys.all? {|s| item.key? s}
Rails.logger.warn "Error #{@url} is not a Joule node"
return nil
end
......@@ -89,8 +89,8 @@ module Joule
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)
......@@ -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?
......@@ -153,41 +153,41 @@ module Joule
display_type: elem.display_type}
end
attrs = { name: db_stream.name,
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)
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,
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,7 +200,7 @@ module Joule
'end': annotation.end_time}
begin
resp = self.class.post("#{@url}/annotation.json",
headers: { 'Content-Type' => 'application/json' },
headers: {'Content-Type' => 'application/json'},
body: data.to_json)
raise "error creating annotations #{resp.body}" unless resp.success?
rescue
......@@ -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