Commit 5d4a5f51 by John Doe

added visibility property to dataviews

parent ece8d62d
...@@ -38,11 +38,11 @@ class DataViewsController < ApplicationController ...@@ -38,11 +38,11 @@ class DataViewsController < ApplicationController
private private
def data_view_params def data_view_params
params.permit(:name, :description, :image, :redux_json) params.permit(:name, :description, :visibility, :image, :redux_json)
end end
def updatable_data_view_params def updatable_data_view_params
params.permit(:name, :description) params.permit(:name, :description, :visibility)
end end
def set_data_view def set_data_view
......
...@@ -10,6 +10,9 @@ class DataView < ApplicationRecord ...@@ -10,6 +10,9 @@ class DataView < ApplicationRecord
#---Validations----- #---Validations-----
validates :name, :presence => true validates :name, :presence => true
TYPES = %w(public private hidden)
validates :visibility, :inclusion => {:in => TYPES}
#return all DataViews that can be loaded by this user #return all DataViews that can be loaded by this user
def self.find_viewable(user) def self.find_viewable(user)
...@@ -18,14 +21,20 @@ class DataView < ApplicationRecord ...@@ -18,14 +21,20 @@ class DataView < ApplicationRecord
nilms = r[:admin]+r[:owner]+r[:viewer] nilms = r[:admin]+r[:owner]+r[:viewer]
#find all DataViewsNilms that are not allowed #find all DataViewsNilms that are not allowed
prohibited = DataViewsNilm.where.not(nilm_id: nilms.pluck(:id)) prohibited = DataViewsNilm.where.not(nilm_id: nilms.pluck(:id))
#find the *other* DataViewsNilms (allowed) #find the *other* DataViewsNilms (allowed)
allowed_ids = DataViewsNilm.where.not(id: prohibited.pluck(:id)) allowed_ids = DataViewsNilm.where.not(id: prohibited.pluck(:id))
.pluck(:data_view_id).uniq .pluck(:data_view_id).uniq
DataView.find(allowed_ids) #find all visible data views
visible_ids = DataView.where(owner: user).
or(DataView.where(visibility: 'public')).
pluck(:id)
#permitted views must be both visible and allowed
DataView.where(id: allowed_ids&visible_ids)
end end
def self.json_keys def self.json_keys
[:id, :name, :description, :image, :redux_json] [:id, :name, :description, :image, :redux_json, :visibility]
end end
end end
...@@ -69,6 +69,7 @@ class LoadStreamData ...@@ -69,6 +69,7 @@ class LoadStreamData
@data = __build_decimated_data(decimateable_elements, resp) + @data = __build_decimated_data(decimateable_elements, resp) +
__build_intervals_from_decimated_data(interval_elements, resp) __build_intervals_from_decimated_data(interval_elements, resp)
end end
self
end end
#===Description #===Description
......
...@@ -9,23 +9,28 @@ ...@@ -9,23 +9,28 @@
# total time: <%=distance_of_time_in_words(@legend[:end_time]/1e6-@legend[:start_time]/1e6)%> # total time: <%=distance_of_time_in_words(@legend[:end_time]/1e6-@legend[:start_time]/1e6)%>
# total rows: <%=@legend[:num_rows]%> # total rows: <%=@legend[:num_rows]%>
# decimation factor: <%=@legend[:decimation_factor]%> # decimation factor: <%=@legend[:decimation_factor]%>
# notes: <%=@legend[:notes]%>
# legend:
<%@legend[:columns].each do |col|%>
# Column <%=col[:index]%>: <%=col[:name]%> (<%=col[:units]%>)
<%end%>
#
# --------- MATLAB INSTRUCTIONS ------------
# #
# this file can be loaded directly into MATLAB # this file can be loaded directly into MATLAB
# #
# dataset = importdata('thisfilename.csv') # >> x = importdata('~/Downloads/filename.csv')
# x =
# data: [1737x15 double] % the data
# textdata: {41x1 cell} % this help text
# #
# dataset.textdata: this help text # --------- NILMTOOL INSTRUCTIONS ----------
# dataset.data: the data
# #
# raw data can be accessed using nilmtool, run: # raw data can be accessed using nilmtool, run:
# nilmtool -u <%=@db.url%> extract -s @<%=@legend[:start_time]%> -e @<%=@legend[:end_time]%> <%=@db_stream.path%>
# #
# <%=@legend[:notes]%> # $> nilmtool -u <%=@db.url%> extract -s @<%=@legend[:start_time]%> -e @<%=@legend[:end_time]%> <%=@db_stream.path%>
# ----------- LEGEND --------------- #
<%@legend[:columns].each do |col|%> # ------------------------------------------
# Column <%=col[:index]%>: <%=col[:name]%> (<%=col[:units]%>)
<%end%>
# -----------------------------------
# #
<% @data.each do |row|%> <% @data.each do |row|%>
<%= row.join(", ")%> <%= row.join(", ")%>
......
class AddVisibilityToDataViews < ActiveRecord::Migration[5.1]
def change
add_column :data_views, :visibility, :string
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170512013633) do ActiveRecord::Schema.define(version: 20170524005258) do
create_table "data_views", force: :cascade do |t| create_table "data_views", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
...@@ -20,6 +20,7 @@ ActiveRecord::Schema.define(version: 20170512013633) do ...@@ -20,6 +20,7 @@ ActiveRecord::Schema.define(version: 20170512013633) do
t.text "redux_json" t.text "redux_json"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "visibility"
end end
create_table "data_views_nilms", force: :cascade do |t| create_table "data_views_nilms", force: :cascade do |t|
......
...@@ -17,25 +17,24 @@ RSpec.describe DataViewsController, type: :request do ...@@ -17,25 +17,24 @@ RSpec.describe DataViewsController, type: :request do
other_stream = create(:db_stream, db: other_db) other_stream = create(:db_stream, db: other_db)
other_user = create(:user) other_user = create(:user)
service = CreateDataView.new service = CreateDataView.new
allowed_view = service.run( service.run(
{name: 'allowed'}, [viewed_streams.first.id], other_user) {name: 'allowed', visibility: 'public'}, [viewed_streams.first.id], other_user)
prohibited_view = service.run( service.run(
{name: 'prohibited'}, [other_stream.id], other_user) {name: 'prohibited', visibility: 'public'}, [other_stream.id], other_user)
my_view = service.run( service.run(
{name: 'created'}, viewed_streams.map{|x| x.id}, viewer) {name: 'private', visibility: 'private'}, [viewed_streams.first.id], other_user)
service.run(
{name: 'my_public', visibility: 'public'}, viewed_streams.map{|x| x.id}, viewer)
service.run(
{name: 'my_private', visibility: 'private'}, viewed_streams.map{|x| x.id}, viewer)
#viewer should receive 'allowed' and 'created' #viewer should receive 'allowed' and 'created'
@auth_headers = viewer.create_new_auth_token @auth_headers = viewer.create_new_auth_token
get "/data_views.json", headers: @auth_headers get "/data_views.json", headers: @auth_headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
body = JSON.parse(response.body) body = JSON.parse(response.body)
expect(body.length).to eq 2 names = body.map {|view| view['name']}
body.each do |view| expect(names).to contain_exactly('allowed','my_public','my_private')
if view['name']=='created'
expect(view['owner']).to be true
else
expect(view['owner']).to be false
end
end
end end
end end
context 'without sign-in' do context 'without sign-in' do
...@@ -53,7 +52,7 @@ RSpec.describe DataViewsController, type: :request do ...@@ -53,7 +52,7 @@ RSpec.describe DataViewsController, type: :request do
post "/data_views.json", post "/data_views.json",
params: { params: {
name: 'test', description: '', image: '', redux_json: '', name: 'test', description: '', image: '', redux_json: '',
stream_ids: viewed_streams.map {|x| x.id} visibility: 'public', stream_ids: viewed_streams.map {|x| x.id}
}, headers: @auth_headers }, headers: @auth_headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(response).to have_notice_message expect(response).to have_notice_message
...@@ -66,7 +65,7 @@ RSpec.describe DataViewsController, type: :request do ...@@ -66,7 +65,7 @@ RSpec.describe DataViewsController, type: :request do
post "/data_views.json", post "/data_views.json",
params: { params: {
description: 'missing name', image: '', redux_json: '', description: 'missing name', image: '', redux_json: '',
stream_ids: viewed_streams.map {|x| x.id} visibility: 'public', stream_ids: viewed_streams.map {|x| x.id}
}, headers: @auth_headers }, headers: @auth_headers
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_error_message expect(response).to have_error_message
...@@ -84,7 +83,7 @@ RSpec.describe DataViewsController, type: :request do ...@@ -84,7 +83,7 @@ RSpec.describe DataViewsController, type: :request do
before do before do
service = CreateDataView.new service = CreateDataView.new
service.run( service.run(
{name: 'created'}, viewed_streams.map{|x| x.id}, viewer) {name: 'created', visibility: 'public'}, viewed_streams.map{|x| x.id}, viewer)
@my_view = service.data_view @my_view = service.data_view
end end
context 'with view owner' do context 'with view owner' do
......
...@@ -2,6 +2,7 @@ FactoryGirl.define do ...@@ -2,6 +2,7 @@ FactoryGirl.define do
factory :data_view do factory :data_view do
name { Faker::Lorem.words(3).join(' ') } name { Faker::Lorem.words(3).join(' ') }
description { Faker::Lorem.sentence } description { Faker::Lorem.sentence }
visibility "public"
redux_json "auto generated from factory" redux_json "auto generated from factory"
end end
end end
...@@ -8,6 +8,7 @@ RSpec.describe DataView, type: :model do ...@@ -8,6 +8,7 @@ RSpec.describe DataView, type: :model do
specify { expect(data_view).to respond_to(:redux_json) } specify { expect(data_view).to respond_to(:redux_json) }
specify { expect(data_view).to respond_to(:owner) } specify { expect(data_view).to respond_to(:owner) }
specify { expect(data_view).to respond_to(:nilms) } specify { expect(data_view).to respond_to(:nilms) }
specify { expect(data_view).to respond_to(:visibility) }
it 'is deleted when nilm is destroyed' do it 'is deleted when nilm is destroyed' do
nilm1 = create(:nilm) nilm1 = create(:nilm)
......
...@@ -13,6 +13,7 @@ describe 'CreateDataView service' do ...@@ -13,6 +13,7 @@ describe 'CreateDataView service' do
it 'creates a dataview' do it 'creates a dataview' do
params = { params = {
name: 'test', name: 'test',
visibility: 'public',
description: '', description: '',
image: '', image: '',
redux_json: ''} redux_json: ''}
......
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