Commit a8c0ce57 by source_reader

added annotations, working on tests

parent 1995cc7f
......@@ -45,7 +45,7 @@ module Joule
#
# raw data can be accessed using the joule cli, run:
#
# $> joule -u #{@backend.url} data read -s #{start_time} -e #{end_time} #{db_stream.path}
# $> joule -n #{@backend.url} data read -s #{start_time} -e #{end_time} #{db_stream.path}
#
# ------------------------------------------"
end
......@@ -58,6 +58,22 @@ module Joule
@backend.module_post_interface(joule_module, req)
end
def create_annotation(annotation)
@backend.create_annotation(annotation)
end
def get_annotations(db_stream)
resp = @backend.get_annotations(db_stream.joule_id)
annotations = resp[:result]
annotations.each do |annotation|
annotation[:db_stream_id] = db_stream.id
end
end
def delete_annotation(annotation_id)
resp = @backend.delete_annotation(annotation_id)
end
def node_type
'joule'
end
......
......@@ -173,5 +173,54 @@ module Joule
end
{ error: false, msg: 'success' }
end
def create_annotation(annotation)
data = {
'stream_id': annotation.db_stream.joule_id,
'title': annotation.title,
'content': annotation.content,
'start': annotation.start_time,
'end': annotation.end_time}
begin
response = self.class.post("#{@url}/annotation.json",
headers: { 'Content-Type' => 'application/json' },
body: data.to_json)
rescue
return { error: true, msg: 'cannot contact Joule server' }
end
unless response.success?
puts "#########"
puts response.body
return { error: true, msg: "error creating annotation" }
end
annotation.id = response.parsed_response["id"]
{ error: false, msg: 'success' }
end
def get_annotations(stream_id)
query = {'stream_id': stream_id}
options = { query: query}
begin
resp = self.class.get("#{@url}/annotations.json", options)
return {success: false, result: resp.body} unless resp.success?
rescue
return {success: false, result: "connection error"}
end
annotations = resp.parsed_response
{success: true, result: annotations}
end
def delete_annotation(annotation_id)
query = {'id': annotation_id}
options = { query: query}
begin
resp = self.class.delete("#{@url}/annotation.json",
options)
return {success: false, result: resp.body} unless resp.success?
rescue
return {success: false, result: "connection error"}
end
end
end
end
class AnnotationsController < ApplicationController
before_action :authenticate_user!
before_action :set_stream
before_action :create_adapter
before_action :authorize_owner, except: [:index]
# GET /stream/:stream_id/annotations.json
def index
annotations = @node_adapter.get_annotations(@db_stream)
@service = StubService.new
render json: annotations, status: @service.success? ? :ok : :unprocessable_entity
end
# POST /annotations.json
def create
@annotation = Annotation.new
@annotation.title = params[:title]
@annotation.content = params[:content]
@annotation.db_stream = @db_stream
@annotation.start_time = params[:start]
@annotation.end_time = params[:end]
status = @node_adapter.create_annotation(@annotation)
@service = StubService.new
render :show, status: @service.success? ? :ok : :unprocessable_entity
end
# PATCH/PUT /annotations/1.json
def update
@service = EditAnnotation.new(@node_adapter)
@service.run(params[:id], annotation_params)
render status: @service.success? ? :ok : :unprocessable_entity
end
# DELETE /annotations/1.json
def destroy
status = @node_adapter.delete_annotation(params[:id])
@service = StubService.new
render 'helpers/empty_response', status: :ok
end
private
def annotation_params
params.permit(:title, :content, :start, :end, :db_stream_id)
end
def set_stream
@db_stream = DbStream.find(params[:db_stream_id])
@db = @db_stream.db
@nilm = @db.nilm
end
# authorization based on nilms
def authorize_owner
head :unauthorized unless current_user.owns_nilm?(@nilm)
end
def create_adapter
@node_adapter = NodeAdapterFactory.from_nilm(@nilm)
if @node_adapter.nil?
@service = StubService.new
@service.add_error("Cannot contact installation")
render 'helpers/empty_response', status: :unprocessable_entity
end
end
end
class Annotation
attr_accessor :id
attr_accessor :title
attr_accessor :content
attr_accessor :start_time
attr_accessor :end_time
attr_accessor :db_stream
end
\ No newline at end of file
# frozen_string_literal: true
class CreateAnnotation
include ServiceStatus
attr_reader :nilm
def initialize(node_adapter)
super()
@node_adapter = node_adapter
end
def run(db_stream, annotation)
status = @node_adapter.create_annotation(@annotation)
# if there was an error don't save the model
if status[:error]
add_error(status[:msg])
return self
end
add_warnings(msgs.errors + msgs.warnings)
add_notice('Created annotation')
self
end
end
json.data do
json.id @annotation.id
json.title @annotation.title
json.content @annotation.content
json.start @annotation.start_time
json.end @annotation.end_time
json.db_stream_id @db_stream.id
end
json.partial! "helpers/messages", service: @service
\ No newline at end of file
......@@ -35,7 +35,7 @@ module ControlPanel
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
# Add folders under the services and adapters directory
%w(data nilm db db_folder db_stream permission user_group user data_view joule_modules).each do |service|
%w(annotations data nilm db db_folder db_stream permission user_group user data_view joule_modules).each do |service|
config.autoload_paths << Rails.root.join("app/services/#{service}")
end
config.autoload_paths << Rails.root.join("app/adapters/nilmdb")
......
......@@ -16,10 +16,12 @@ Rails.application.routes.draw do
resources :db_folders, only: [:show, :update]
resources :db_streams, only: [:index, :update] do
resources :annotations, except: [:show]
member do
post 'data'
end
end
resources :db_elements, only: [:index] do
collection do
get 'data'
......
require 'rails_helper'
RSpec.describe AnnotationsController, type: :controller do
end
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