From 2d2d80d5cf3893564683e050805049fd89b8ccf4 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Tue, 10 Apr 2018 14:43:27 -0500 Subject: [PATCH] Add `scenic` gem for view management --- Gemfile | 1 + Gemfile.lock | 4 ++ app/models/application_record.rb | 1 + ...180404165355_create_sequence_tools_view.rb | 34 ++------------- .../20180410192539_create_in_use_resources.rb | 16 ++++++++ db/schema.rb | 31 +++++++++++++- db/seeds.rb | 41 +++++++++---------- db/views/in_use_points_v01.sql | 14 +++++++ db/views/in_use_tools_v01.sql | 10 +++++ 9 files changed, 98 insertions(+), 54 deletions(-) create mode 100644 db/migrate/20180410192539_create_in_use_resources.rb create mode 100644 db/views/in_use_points_v01.sql create mode 100644 db/views/in_use_tools_v01.sql diff --git a/Gemfile b/Gemfile index fef5ee347..8e77a0a0f 100755 --- a/Gemfile +++ b/Gemfile @@ -28,6 +28,7 @@ gem "secure_headers" gem "valid_url" gem "font-awesome-rails" gem "discard", "~> 1.0" +gem "scenic" group :development, :test do gem "hashdiff" diff --git a/Gemfile.lock b/Gemfile.lock index 360123d8d..9c68cee11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -282,6 +282,9 @@ GEM rspec-support (3.7.1) ruby-graphviz (1.2.3) rubyzip (1.2.1) + scenic (1.4.1) + activerecord (>= 4.0.0) + railties (>= 4.0.0) secure_headers (5.0.5) useragent (>= 0.15.0) selenium-webdriver (3.11.0) @@ -369,6 +372,7 @@ DEPENDENCIES rollbar rspec rspec-rails + scenic secure_headers selenium-webdriver simplecov diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 920a650ad..a96992c03 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -59,6 +59,7 @@ class ApplicationRecord < ActiveRecord::Base end def broadcast! + # `espeak "ding"` AutoSyncJob.perform_later(broadcast_payload, Device.current.id, chan_name, diff --git a/db/migrate/20180404165355_create_sequence_tools_view.rb b/db/migrate/20180404165355_create_sequence_tools_view.rb index f85304c16..6828b4b35 100644 --- a/db/migrate/20180404165355_create_sequence_tools_view.rb +++ b/db/migrate/20180404165355_create_sequence_tools_view.rb @@ -1,35 +1,7 @@ class CreateSequenceToolsView < ActiveRecord::Migration[5.1] - def up - execute 'CREATE VIEW in_use_tools AS - SELECT - tools.id as tool_id, - tools.name as tool_name, - sequences.name as sequence_name, - sequences.id as sequence_id, - sequences.device_id as device_id - FROM "edge_nodes" - INNER JOIN "sequences" ON sequences.id=sequences.id - INNER JOIN "tools" ON (edge_nodes.value)::int=tools.id - WHERE "edge_nodes"."kind" = \'tool_id\';' - - execute 'CREATE VIEW in_use_points AS - SELECT - points.x as x, - points.y as y, - points.z as z, - (edge_nodes.value)::int as point_id, - points.pointer_type as pointer_type, - points.name as pointer_name, - sequences.id as sequence_id, - sequences.name as sequence_name, - edge_nodes.id as edge_node_id - FROM "edge_nodes" - INNER JOIN "sequences" ON edge_nodes.sequence_id=sequences.id - INNER JOIN "points" ON (edge_nodes.value)::int=points.id - WHERE "edge_nodes"."kind" = \'pointer_id\';' - end - - def down + def change + # I goofed up on this migration and deployed to staging before I could fix. + # See later migration that creates a new view using the `scenic` gem. execute "DROP VIEW IF EXISTS in_use_tools;" execute "DROP VIEW IF EXISTS in_use_points;" end diff --git a/db/migrate/20180410192539_create_in_use_resources.rb b/db/migrate/20180410192539_create_in_use_resources.rb new file mode 100644 index 000000000..1b0cd6ca9 --- /dev/null +++ b/db/migrate/20180410192539_create_in_use_resources.rb @@ -0,0 +1,16 @@ +class CreateInUseResources < ActiveRecord::Migration[5.1] + # I goofed up on this migration and deployed to staging before I could fix. + # See later migration that creates a new view using the `scenic` gem. - RC + def up + execute "DROP VIEW IF EXISTS in_use_tools;" + execute "DROP VIEW IF EXISTS in_use_points;" + + create_view :in_use_tools + create_view :in_use_points + end + + def down + drop_view :in_use_tools + drop_view :in_use_points + end +end diff --git a/db/schema.rb b/db/schema.rb index 3ba5c6dee..7ed41166c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180410180929) do +ActiveRecord::Schema.define(version: 20180410192539) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -483,4 +483,33 @@ ActiveRecord::Schema.define(version: 20180410180929) do add_foreign_key "sensor_readings", "devices" add_foreign_key "sensors", "devices" add_foreign_key "token_issuances", "devices" + + create_view "in_use_tools", sql_definition: <<-SQL + SELECT tools.id AS tool_id, + tools.name AS tool_name, + sequences.name AS sequence_name, + sequences.id AS sequence_id, + sequences.device_id + FROM ((edge_nodes + JOIN sequences ON ((sequences.id = sequences.id))) + JOIN tools ON (((edge_nodes.value)::integer = tools.id))) + WHERE ((edge_nodes.kind)::text = 'tool_id'::text); + SQL + + create_view "in_use_points", sql_definition: <<-SQL + SELECT points.x, + points.y, + points.z, + (edge_nodes.value)::integer AS point_id, + points.pointer_type, + points.name AS pointer_name, + sequences.id AS sequence_id, + sequences.name AS sequence_name, + edge_nodes.id AS edge_node_id + FROM ((edge_nodes + JOIN sequences ON ((edge_nodes.sequence_id = sequences.id))) + JOIN points ON (((edge_nodes.value)::integer = points.id))) + WHERE ((edge_nodes.kind)::text = 'pointer_id'::text); + SQL + end diff --git a/db/seeds.rb b/db/seeds.rb index 9cda14679..db173e016 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -9,8 +9,8 @@ unless Rails.env == "production" LogDispatch.destroy_all User.destroy_all - POINT_COUNT = 2 - PLANT_COUNT = 2 + POINT_COUNT = 8 + PLANT_COUNT = 8 DATE_RANGE_LO = 1..3 DATE_RANGE_HI = 3..8 ENV['MQTT_HOST'] = "blooper.io" @@ -51,29 +51,26 @@ unless Rails.env == "production" end PLANT_COUNT.times do - Point.create( - device: u.device, - x: rand(40...970), - y: rand(40...470), - radius: rand(10...50), - name: Faker::StarWars.call_sign, - pointer: Plant.new( - openfarm_slug: ["tomato", "carrot", "radish", "garlic"].sample - )) + Plant.create(device: u.device, + x: rand(40...970), + y: rand(40...470), + radius: rand(10...50), + name: Faker::StarWars.call_sign, + pointer_id: 0, + openfarm_slug: ["tomato", "carrot", "radish", "garlic"].sample) end POINT_COUNT.times do - Point.create( - device: u.device, - x: rand(40...970) + rand(40...970), - y: rand(40...470) + rand(40...470), - z: 5, - radius: (rand(1...150) + rand(1...150)) / 20, - pointer: GenericPointer.new(), - meta: { - created_by: "plant-detection", - color: (Sequence::COLORS + [nil]).sample - }) + GenericPointer.create(device: u.device, + x: rand(40...970) + rand(40...970), + y: rand(40...470) + rand(40...470), + z: 5, + radius: (rand(1...150) + rand(1...150)) / 20, + pointer_id: 0, + meta: { + created_by: "plant-detection", + color: (Sequence::COLORS + [nil]).sample + }) end s = Sequences::Create.run!(device: u.device, diff --git a/db/views/in_use_points_v01.sql b/db/views/in_use_points_v01.sql new file mode 100644 index 000000000..d1623facd --- /dev/null +++ b/db/views/in_use_points_v01.sql @@ -0,0 +1,14 @@ +SELECT + points.x as x, + points.y as y, + points.z as z, + (edge_nodes.value)::int as point_id, + points.pointer_type as pointer_type, + points.name as pointer_name, + sequences.id as sequence_id, + sequences.name as sequence_name, + edge_nodes.id as edge_node_id +FROM "edge_nodes" + INNER JOIN "sequences" ON edge_nodes.sequence_id=sequences.id + INNER JOIN "points" ON (edge_nodes.value)::int=points.id +WHERE "edge_nodes"."kind" = 'pointer_id'; diff --git a/db/views/in_use_tools_v01.sql b/db/views/in_use_tools_v01.sql new file mode 100644 index 000000000..70034c0aa --- /dev/null +++ b/db/views/in_use_tools_v01.sql @@ -0,0 +1,10 @@ +SELECT + tools.id as tool_id, + tools.name as tool_name, + sequences.name as sequence_name, + sequences.id as sequence_id, + sequences.device_id as device_id +FROM "edge_nodes" +INNER JOIN "sequences" ON sequences.id=sequences.id +INNER JOIN "tools" ON (edge_nodes.value)::int=tools.id +WHERE "edge_nodes"."kind" = 'tool_id';