Commit 5d4a5f51 by John Doe

added visibility property to dataviews

parent ece8d62d
......@@ -38,11 +38,11 @@ class DataViewsController < ApplicationController
private
def data_view_params
params.permit(:name, :description, :image, :redux_json)
params.permit(:name, :description, :visibility, :image, :redux_json)
end
def updatable_data_view_params
params.permit(:name, :description)
params.permit(:name, :description, :visibility)
end
def set_data_view
......
......@@ -10,6 +10,9 @@ class DataView < ApplicationRecord
#---Validations-----
validates :name, :presence => true
TYPES = %w(public private hidden)
validates :visibility, :inclusion => {:in => TYPES}
#return all DataViews that can be loaded by this user
def self.find_viewable(user)
......@@ -18,14 +21,20 @@ class DataView < ApplicationRecord
nilms = r[:admin]+r[:owner]+r[:viewer]
#find all DataViewsNilms that are not allowed
prohibited = DataViewsNilm.where.not(nilm_id: nilms.pluck(:id))
#find the *other* DataViewsNilms (allowed)
allowed_ids = DataViewsNilm.where.not(id: prohibited.pluck(:id))
.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
def self.json_keys
[:id, :name, :description, :image, :redux_json]
[:id, :name, :description, :image, :redux_json, :visibility]
end
end
......@@ -69,6 +69,7 @@ class LoadStreamData
@data = __build_decimated_data(decimateable_elements, resp) +
__build_intervals_from_decimated_data(interval_elements, resp)
end
self
end
#===Description
......
......@@ -9,23 +9,28 @@
# total time: <%=distance_of_time_in_words(@legend[:end_time]/1e6-@legend[:start_time]/1e6)%>
# total rows: <%=@legend[:num_rows]%>
# 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
#
# 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
# dataset.data: the data
# --------- NILMTOOL INSTRUCTIONS ----------
#
# 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]%>
# ----------- LEGEND ---------------
<%@legend[:columns].each do |col|%>
# Column <%=col[:index]%>: <%=col[:name]%> (<%=col[:units]%>)
<%end%>
# -----------------------------------
# $> nilmtool -u <%=@db.url%> extract -s @<%=@legend[:start_time]%> -e @<%=@legend[:end_time]%> <%=@db_stream.path%>
#
# ------------------------------------------
#
<% @data.each do |row|%>
<%= row.join(", ")%>
......
class AddVisibilityToDataViews < ActiveRecord::Migration[5.1]
def change
add_column :data_views, :visibility, :string
end
end
......@@ -10,16 +10,17 @@
#
# 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|
t.integer "user_id"
t.string "name"
t.string "description"
t.text "image"
t.text "redux_json"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "name"
t.string "description"
t.text "image"
t.text "redux_json"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "visibility"
end
create_table "data_views_nilms", force: :cascade do |t|
......@@ -30,77 +31,77 @@ ActiveRecord::Schema.define(version: 20170512013633) do
end
create_table "db_decimations", force: :cascade do |t|
t.integer "start_time", limit: 8
t.integer "end_time", limit: 8
t.integer "total_rows", limit: 8
t.integer "total_time", limit: 8
t.integer "db_stream_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "level", limit: 8
t.string "data_type"
t.integer "start_time", limit: 8
t.integer "end_time", limit: 8
t.integer "total_rows", limit: 8
t.integer "total_time", limit: 8
t.integer "db_stream_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "level", limit: 8
t.string "data_type"
end
create_table "db_elements", force: :cascade do |t|
t.string "name"
t.string "units"
t.integer "column"
t.float "default_max"
t.float "default_min"
t.float "scale_factor"
t.float "offset"
t.integer "db_stream_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "plottable"
t.string "display_type"
t.string "name"
t.string "units"
t.integer "column"
t.float "default_max"
t.float "default_min"
t.float "scale_factor"
t.float "offset"
t.integer "db_stream_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "plottable"
t.string "display_type"
end
create_table "db_folders", force: :cascade do |t|
t.string "name"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "parent_id"
t.string "path"
t.boolean "hidden"
t.integer "db_id"
t.integer "start_time", limit: 8
t.integer "end_time", limit: 8
t.integer "size_on_disk", limit: 8
t.string "name"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "parent_id"
t.string "path"
t.boolean "hidden"
t.integer "db_id"
t.integer "start_time", limit: 8
t.integer "end_time", limit: 8
t.integer "size_on_disk", limit: 8
end
create_table "db_streams", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "db_folder_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "path"
t.integer "start_time", limit: 8
t.integer "end_time", limit: 8
t.integer "total_rows", limit: 8
t.integer "total_time", limit: 8
t.string "data_type"
t.string "name_abbrev"
t.boolean "delete_locked"
t.boolean "hidden"
t.integer "size_on_disk", limit: 8
t.integer "db_id"
t.string "name"
t.string "description"
t.integer "db_folder_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "path"
t.integer "start_time", limit: 8
t.integer "end_time", limit: 8
t.integer "total_rows", limit: 8
t.integer "total_time", limit: 8
t.string "data_type"
t.string "name_abbrev"
t.boolean "delete_locked"
t.boolean "hidden"
t.integer "size_on_disk", limit: 8
t.integer "db_id"
end
create_table "dbs", force: :cascade do |t|
t.string "url"
t.integer "db_folder_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "nilm_id"
t.integer "size_total", limit: 8
t.integer "size_db", limit: 8
t.integer "size_other", limit: 8
t.string "version"
t.integer "max_points_per_plot", default: 3600
t.boolean "available"
t.string "url"
t.integer "db_folder_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "nilm_id"
t.integer "size_total", limit: 8
t.integer "size_db", limit: 8
t.integer "size_other", limit: 8
t.string "version"
t.integer "max_points_per_plot", default: 3600
t.boolean "available"
end
create_table "memberships", force: :cascade do |t|
......@@ -111,60 +112,60 @@ ActiveRecord::Schema.define(version: 20170512013633) do
end
create_table "nilms", force: :cascade do |t|
t.string "name"
t.string "description"
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "description"
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "permissions", force: :cascade do |t|
t.integer "nilm_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "user_group_id"
t.string "role"
t.integer "nilm_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "user_group_id"
t.string "role"
end
create_table "user_groups", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "owner_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "description"
t.integer "owner_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "provider", default: "email", null: false
t.string "uid", default: "", null: false
t.string "encrypted_password", default: ""
t.string "reset_password_token"
t.string "provider", default: "email", null: false
t.string "uid", default: "", null: false
t.string "encrypted_password", default: ""
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.string "first_name"
t.string "last_name"
t.string "email"
t.text "tokens"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "invitation_token"
t.string "unconfirmed_email"
t.string "first_name"
t.string "last_name"
t.string "email"
t.text "tokens"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "invitation_token"
t.datetime "invitation_created_at"
t.datetime "invitation_sent_at"
t.datetime "invitation_accepted_at"
t.integer "invitation_limit"
t.integer "invited_by_id"
t.string "invited_by_type"
t.string "invitation_url"
t.integer "invitation_limit"
t.integer "invited_by_id"
t.string "invited_by_type"
t.string "invitation_url"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
......
......@@ -17,25 +17,24 @@ RSpec.describe DataViewsController, type: :request do
other_stream = create(:db_stream, db: other_db)
other_user = create(:user)
service = CreateDataView.new
allowed_view = service.run(
{name: 'allowed'}, [viewed_streams.first.id], other_user)
prohibited_view = service.run(
{name: 'prohibited'}, [other_stream.id], other_user)
my_view = service.run(
{name: 'created'}, viewed_streams.map{|x| x.id}, viewer)
service.run(
{name: 'allowed', visibility: 'public'}, [viewed_streams.first.id], other_user)
service.run(
{name: 'prohibited', visibility: 'public'}, [other_stream.id], other_user)
service.run(
{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'
@auth_headers = viewer.create_new_auth_token
get "/data_views.json", headers: @auth_headers
expect(response).to have_http_status(:ok)
body = JSON.parse(response.body)
expect(body.length).to eq 2
body.each do |view|
if view['name']=='created'
expect(view['owner']).to be true
else
expect(view['owner']).to be false
end
end
names = body.map {|view| view['name']}
expect(names).to contain_exactly('allowed','my_public','my_private')
end
end
context 'without sign-in' do
......@@ -53,7 +52,7 @@ RSpec.describe DataViewsController, type: :request do
post "/data_views.json",
params: {
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
expect(response).to have_http_status(:ok)
expect(response).to have_notice_message
......@@ -66,7 +65,7 @@ RSpec.describe DataViewsController, type: :request do
post "/data_views.json",
params: {
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
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_error_message
......@@ -84,7 +83,7 @@ RSpec.describe DataViewsController, type: :request do
before do
service = CreateDataView.new
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
end
context 'with view owner' do
......
......@@ -2,6 +2,7 @@ FactoryGirl.define do
factory :data_view do
name { Faker::Lorem.words(3).join(' ') }
description { Faker::Lorem.sentence }
visibility "public"
redux_json "auto generated from factory"
end
end
......@@ -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(:owner) }
specify { expect(data_view).to respond_to(:nilms) }
specify { expect(data_view).to respond_to(:visibility) }
it 'is deleted when nilm is destroyed' do
nilm1 = create(:nilm)
......
......@@ -13,6 +13,7 @@ describe 'CreateDataView service' do
it 'creates a dataview' do
params = {
name: 'test',
visibility: 'public',
description: '',
image: '',
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