From c9520c5453722de91a2eb8d1de35c9acd45992d1 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 7 Aug 2024 16:15:59 +0200 Subject: [PATCH] refactor --- Gemfile.lock | 30 +++---- app/controllers/islets_controller.rb | 56 ------------- .../infrastructures_controller.rb | 46 ++++++++++- .../network_capacities_controller.rb | 17 +++- app/models/bay.rb | 2 +- app/models/filter.rb | 8 +- ...ture.html.erb => _infrastructure.html.erb} | 49 ++++++------ ...ty.html.erb => _network_capacity.html.erb} | 13 +-- .../_modal_add_element_in_frame.html.erb | 1 - .../infrastructures/show.html.erb | 79 ++++++++++--------- .../network_capacities/show.html.erb | 75 +++++++++--------- config/routes.rb | 5 -- ...40709134050_create_external_app_records.rb | 2 + 13 files changed, 183 insertions(+), 200 deletions(-) rename app/views/islets/{infrastructure.html.erb => _infrastructure.html.erb} (88%) rename app/views/islets/{network_capacity.html.erb => _network_capacity.html.erb} (94%) delete mode 100644 app/views/servers/_modal_add_element_in_frame.html.erb diff --git a/Gemfile.lock b/Gemfile.lock index 0b64908b7..37e68ccfb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -101,7 +101,7 @@ GEM bootstrap (5.3.2) autoprefixer-rails (>= 9.1.0) popper_js (>= 2.11.8, < 3) - builder (3.2.4) + builder (3.3.0) byebug (11.1.3) capybara (3.40.0) addressable @@ -114,7 +114,7 @@ GEM xpath (~> 3.2) coercible (1.0.0) descendants_tracker (~> 0.0.1) - concurrent-ruby (1.3.1) + concurrent-ruby (1.3.3) connection_pool (2.4.1) content_disposition (1.0.0) crass (1.0.6) @@ -159,7 +159,7 @@ GEM drb (2.2.1) email_validator (2.2.4) activemodel - erubi (1.12.0) + erubi (1.13.0) exception_notification (4.5.0) actionmailer (>= 5.2, < 8) activesupport (>= 5.2, < 8) @@ -197,7 +197,7 @@ GEM activesupport (>= 6.0.0) railties (>= 6.0.0) io-console (0.7.2) - irb (1.13.1) + irb (1.14.0) rdoc (>= 4.0.0) reline (>= 0.4.2) jbuilder (2.11.5) @@ -267,7 +267,7 @@ GEM mini_mime (1.1.5) mini_racer (0.12.0) libv8-node (~> 21.7.2.0) - minitest (5.23.1) + minitest (5.24.1) msgpack (1.7.2) mutex_m (0.2.0) net-http (0.4.1) @@ -282,11 +282,11 @@ GEM net-smtp (0.5.0) net-protocol nio4r (2.7.1) - nokogiri (1.16.5-arm64-darwin) + nokogiri (1.16.7-arm64-darwin) racc (~> 1.4) - nokogiri (1.16.5-x86_64-darwin) + nokogiri (1.16.7-x86_64-darwin) racc (~> 1.4) - nokogiri (1.16.5-x86_64-linux) + nokogiri (1.16.7-x86_64-linux) racc (~> 1.4) omniauth (2.1.2) hashie (>= 3.4.6) @@ -331,8 +331,8 @@ GEM public_suffix (5.0.5) puma (6.4.2) nio4r (~> 2.0) - racc (1.8.0) - rack (3.0.11) + racc (1.8.1) + rack (3.1.7) rack-mini-profiler (3.3.1) rack (>= 1.2.0) rack-oauth2 (2.2.1) @@ -393,13 +393,13 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) - rdoc (6.6.3.1) + rdoc (6.7.0) psych (>= 4.0.0) record_tag_helper (1.0.1) actionview (>= 5) redcarpet (3.6.0) regexp_parser (2.9.1) - reline (0.5.6) + reline (0.5.9) io-console (~> 0.5) responders (3.1.1) actionpack (>= 5.2) @@ -511,7 +511,7 @@ GEM railties (>= 6.0.0) store_attribute (1.2.0) activerecord (>= 6.0) - stringio (3.1.0) + stringio (3.1.1) strscan (3.1.0) swd (2.0.3) activesupport (>= 3) @@ -526,7 +526,7 @@ GEM thread_safe (0.3.6) tilt (2.3.0) timeout (0.4.1) - turbo-rails (2.0.5) + turbo-rails (2.0.6) actionpack (>= 6.0.0) activejob (>= 6.0.0) railties (>= 6.0.0) @@ -566,7 +566,7 @@ GEM xpath (3.2.0) nokogiri (~> 1.8) yard (0.9.36) - zeitwerk (2.6.13) + zeitwerk (2.6.17) PLATFORMS arm64-darwin-22 diff --git a/app/controllers/islets_controller.rb b/app/controllers/islets_controller.rb index 195f59005..54b483316 100644 --- a/app/controllers/islets_controller.rb +++ b/app/controllers/islets_controller.rb @@ -94,62 +94,6 @@ def destroy end end - # TODO - def infrastructure - @room = Room.friendly.find(params[:room_id]) - @islet = @room.islets.find(params[:id]) - - @servers = Server.includes(:frame, :stack, :ports, :cards => [:ports]) - .where.not(network_types: []) - # .includes(:cards, :ports => [:connection => [:port, :cable =>[:connections => [:port => :card]]]]). - @concentrateurs_ids = [383, 384, 1043, 1044] - @concentrateurs = Server.where(id: @concentrateurs_ids).includes(:ports => :connection, :cards => [:ports => :connection]) - @switchs_lan_ids = @concentrateurs_ids | @servers.pluck(:id) # Switch LAN - # TODO: Remove hard-coded values - @hubs = {} - - unless Rails.env.test? - @hubs = { "gbe" => { 4 => Server.find(383), 3 => Server.find(384) }, "10gbe" => { 4 => Server.find(1043), 3 => Server.find(1044) } } # Concentrateurs per room - end - - @connections = {} - @stacks = @servers.map(&:stack).uniq.compact - @servers.each do |server| - @connections[server.id] = server.directly_connected_servers_ids_with_color.reject { |conn| @switchs_lan_ids.exclude?(conn[:server_id]) } - end - @concentrateurs.each do |hub| - @connections[hub.id] = hub.connected_servers_ids_through_twin_cards_with_color.reject { |conn| @switchs_lan_ids.exclude?(conn[:server_id]) } - end - - # TODO: remove when hard-coded system will be removed - @network_types = Modele::Network::TYPES.excluding("fiber") - @network = params[:network_type] # TODO: take from params and raise error if not good - - if @room.id == 4 || @room.id == 3 - @hub = @hubs[@network][@room.id] - @second_room = Room.find(@room.id == 4 ? 3 : 4) - @second_hub = @hubs[@network][@second_room.id] - end - - # TODO: only respond to turbo frame or stream - respond_to do |format| - format.html - end - end - - def network_capacity - @room = Room.friendly.find(params[:room_id]) - @islet = @room.islets.find(params[:id]) - @network = params[:network_type] # TODO: take from params and raise error if not good - - @bays = @islet.bays.sorted - - # TODO: only respond to turbo frame or stream - respond_to do |format| - format.html - end - end - private # Use callbacks to share common setup or constraints between actions. diff --git a/app/controllers/visualization/infrastructures_controller.rb b/app/controllers/visualization/infrastructures_controller.rb index 576c49cdd..762d53744 100644 --- a/app/controllers/visualization/infrastructures_controller.rb +++ b/app/controllers/visualization/infrastructures_controller.rb @@ -6,11 +6,51 @@ def show @filter = Filter.new(params, %i[network_type islet_id]) unless @filter.filled? - @filter.islet_id = Islet.sorted.not_empty.has_name.distinct.first.id - @filter.network_type = :gbe + islet_id = Islet.sorted.not_empty.has_name.distinct.first.id + + redirect_to islet_id:, network_type: :gbe end - @islet = Islet.find(@filter.islet_id) if @filter.filled? + load_data! if turbo_frame_request? + end + + private + + def load_data! + @islet = Islet.find(@filter.islet_id) + @room = @islet.room + + @servers = Server.includes(:frame, :stack, :ports, :cards => [:ports]) + .where.not(network_types: []) + # .includes(:cards, :ports => [:connection => [:port, :cable =>[:connections => [:port => :card]]]]). + @concentrateurs_ids = [383, 384, 1043, 1044] + @concentrateurs = Server.where(id: @concentrateurs_ids).includes(:ports => :connection, :cards => [:ports => :connection]) + @switchs_lan_ids = @concentrateurs_ids | @servers.pluck(:id) # Switch LAN + # TODO: Remove hard-coded values + @hubs = {} + + unless Rails.env.test? + @hubs = { "gbe" => { 4 => Server.find(383), 3 => Server.find(384) }, "10gbe" => { 4 => Server.find(1043), 3 => Server.find(1044) } } # Concentrateurs per room + end + + @connections = {} + @stacks = @servers.map(&:stack).uniq.compact + @servers.each do |server| + @connections[server.id] = server.directly_connected_servers_ids_with_color.reject { |conn| @switchs_lan_ids.exclude?(conn[:server_id]) } + end + @concentrateurs.each do |hub| + @connections[hub.id] = hub.connected_servers_ids_through_twin_cards_with_color.reject { |conn| @switchs_lan_ids.exclude?(conn[:server_id]) } + end + + # TODO: remove when hard-coded system will be removed + @network_types = Modele::Network::TYPES.excluding("fiber") + @network = @filter.network_type # TODO: take from params and raise error if not good + + return unless @room.id == 4 || @room.id == 3 + + @hub = @hubs[@network][@room.id] + @second_room = Room.find(@room.id == 4 ? 3 : 4) + @second_hub = @hubs[@network][@second_room.id] end end end diff --git a/app/controllers/visualization/network_capacities_controller.rb b/app/controllers/visualization/network_capacities_controller.rb index f172a4fe2..a832612af 100644 --- a/app/controllers/visualization/network_capacities_controller.rb +++ b/app/controllers/visualization/network_capacities_controller.rb @@ -4,13 +4,24 @@ module Visualization class NetworkCapacitiesController < ApplicationController def show @filter = Filter.new(params, %i[network_type islet_id]) + @filter_filled = @filter.filled? unless @filter.filled? - @filter.islet_id = Islet.sorted.not_empty.has_name.distinct.first.id - @filter.network_type = :gbe + islet_id = Islet.sorted.not_empty.has_name.distinct.first.id + + redirect_to islet_id:, network_type: :gbe end - @islet = Islet.find(@filter.islet_id) if @filter.filled? + load_data! if turbo_frame_request? + end + + private + + def load_data! + @islet = Islet.find(@filter.islet_id) + @room = @islet.room + @network = @filter.network_type # TODO: take from params and raise error if not good + @bays = @islet.bays.sorted end end end diff --git a/app/models/bay.rb b/app/models/bay.rb index 2bd086895..40b00e964 100644 --- a/app/models/bay.rb +++ b/app/models/bay.rb @@ -16,7 +16,7 @@ class Bay < ApplicationRecord scope :sorted_by_room, -> { joins(:room, :islet).order(:site_id, 'rooms.position', 'rooms.name', 'islets.name', :lane, 'bays.position') } def to_s - frames.map { _1.name }.sort.join('/') + frames.map(&:name).sort.join('/') end def detailed_name diff --git a/app/models/filter.rb b/app/models/filter.rb index a1d79c6be..a6bde2b51 100644 --- a/app/models/filter.rb +++ b/app/models/filter.rb @@ -44,10 +44,10 @@ def filled_attributes def attributes @attributes ||= if @params.respond_to?(:permit) - @params.permit(*attribute_names).to_h - else - @params.with_indifferent_access - end + @params.permit(*attribute_names).to_h + else + @params.with_indifferent_access + end end alias to_h attributes diff --git a/app/views/islets/infrastructure.html.erb b/app/views/islets/_infrastructure.html.erb similarity index 88% rename from app/views/islets/infrastructure.html.erb rename to app/views/islets/_infrastructure.html.erb index 9f1a7c6b5..23dae48f6 100644 --- a/app/views/islets/infrastructure.html.erb +++ b/app/views/islets/_infrastructure.html.erb @@ -1,37 +1,34 @@ -<% hide_inventories_sidebar! %> - -<%= turbo_frame_tag "infrastructure_room_islet" do %>
<% # TODO: move html here? %>
- <% if @room.id == 4 || @room.id == 3 %> + <% if room.id == 4 || room.id == 3 %>
- <% bgColorHub1 = define_background_color(server: @hub, mode: params["bg"]) %> -
+
- <%= link_to server_path(@hub), class: "link-dark", target: :_blank, rel: :noopener do %> + data-url="<%= server_path(hub) %>"> + <%= link_to server_path(hub), class: "link-dark", target: :_blank, rel: :noopener do %> Concentrateur
- <%= @hub %> + <%= hub %> <% end %>
- <% bgColorHub2 = define_background_color(server: @second_hub, mode: params["bg"]) %> - <%= @second_room %> -
+ <%= second_room %> +
- <%= link_to server_path(@second_hub), class: "link-dark", + id="hub2-<%= room.id %>-<%= islet %>"> + <%= link_to server_path(second_hub), class: "link-dark", target: :_blank, rel: :noopener do %> Concentrateur
- <%= @second_hub %> + <%= second_hub %> <% end %>
@@ -39,12 +36,12 @@ <% end %>
- <% @islet.bays.sorted.each do |bay| %> + <% islet.bays.sorted.each do |bay| %>
; grid-row: <%= "#{bay.lane.to_i}/#{bay.lane.to_i+1}" %>;"> <% bay.frames.sorted.each_with_index do |frame, index| %> <% servers = frame.servers.includes(:bay, :modele, :stack) - .where("servers.network_types @> ?", "{#{@network}}") # Switch LAN %> + .where("servers.network_types @> ?", "{#{network}}") # Switch LAN %>
0) %>" data-bay-id="<%= bay.id %>" data-frame-id="<%= frame.id %>"> @@ -53,9 +50,9 @@ odd: bay.lane.to_i.odd?) %>" style="z-index: -3000;">.
- <%= link_to frame.name, room_path(@room, + <%= link_to frame.name, room_path(room, view: params[:view], - islet: @islet.name, + islet: islet.name, "bay-id": bay.id, "frame-id": frame, bg: params[:bg]), @@ -108,6 +105,10 @@ var lines = [] var current_connections = [] $(document).ready(function () { + $("svg.leader-line").each(function () { + $(this).remove() + }) + drawCurrentLines() resetLinesPositionOnScroll() }) @@ -152,9 +153,9 @@ function deleteOldLines() { function drawCurrentLines() { var disabled_stacks = disabled_colors() - <% @connections.each do |server_id, connections| %> + <% connections.each do |server_id, connections| %> <% connections.each do |connection| %> - <% unless @concentrateurs_ids.include?(connection[:server_id]) && @concentrateurs_ids.exclude?(server_id) %> + <% unless concentrateurs_ids.include?(connection[:server_id]) && concentrateurs_ids.exclude?(server_id) %> if(disabled_stacks.indexOf(colors_hash['<%= connection[:cable_color] %>']) < 0) { drawInterSwitchsConnections('<%= server_id %>', '<%= connection[:server_id] %>', '<%= connection[:cable_color] %>') } @@ -262,5 +263,3 @@ function setParamsAndDraw(start_element, end_element, color) { drawLine(start_element, end_element, params) } - -<% end %> diff --git a/app/views/islets/network_capacity.html.erb b/app/views/islets/_network_capacity.html.erb similarity index 94% rename from app/views/islets/network_capacity.html.erb rename to app/views/islets/_network_capacity.html.erb index 94ac977b7..13b591ffa 100644 --- a/app/views/islets/network_capacity.html.erb +++ b/app/views/islets/_network_capacity.html.erb @@ -1,6 +1,3 @@ -<% hide_inventories_sidebar! %> - -<%= turbo_frame_tag "network_capacity_room_islet" do %> <% # TODO: create a component to represent a bay %>
@@ -9,7 +6,7 @@
- <% @bays.each do |bay| %> + <% bays.each do |bay| %> <% if (frames = bay.frames.sorted).any? %>
; grid-row: <%= "#{bay.lane.to_i}/#{bay.lane.to_i+1}" %>;"> @@ -17,14 +14,14 @@ <% # TODO: move in the action or a model method %> <% servers = frame.servers.includes(:bay, :stack, modele: { enclosures: :composants }, cards: [{ card_type: :port_type }, { ports: :cable }]) - .where("servers.network_types @> ?", "{#{@network}}") # Switch LAN + .where("servers.network_types @> ?", "{#{network}}") # Switch LAN %>
- <%= link_to frame.name, room_path(@room, + <%= link_to frame.name, room_path(room, view: params[:view], - islet: @islet.name, + islet: islet.name, 'bay-id': bay.id, 'frame-id': frame), class: "link-body-emphasis", target: :_top %> @@ -32,7 +29,6 @@
    <% servers.each do |server| %> - <%# TODO: remove or fix cache %> <% cache ["server", server.id, server.updated_at, Port.maximum(:updated_at)] do %> <% data = { :url => server_url(server) } %> <%= tag.li class: "server_#{server.id} @@ -94,4 +90,3 @@
-<% end %> diff --git a/app/views/servers/_modal_add_element_in_frame.html.erb b/app/views/servers/_modal_add_element_in_frame.html.erb deleted file mode 100644 index 8b1378917..000000000 --- a/app/views/servers/_modal_add_element_in_frame.html.erb +++ /dev/null @@ -1 +0,0 @@ - diff --git a/app/views/visualization/infrastructures/show.html.erb b/app/views/visualization/infrastructures/show.html.erb index 481894af7..c4f5bf5c2 100644 --- a/app/views/visualization/infrastructures/show.html.erb +++ b/app/views/visualization/infrastructures/show.html.erb @@ -6,51 +6,52 @@ "Infrastructure" => nil, } %> -
- <%= render CardComponent.new(type: :primary) do |card| %> - <%= form_with model: @filter, url: "", method: :get, class: "row g-3 align-items-center", id: :filters, data: { controller: "form-update" } do |f| %> -
-
- <%= f.select :network_type, Modele::Network::TYPES.map { |t| [Modele.human_attribute_name("network_types.short_#{t}"), t] }, {}, { class: "form-control", data: { action: "change->form-update#update" } } %> - <%= f.label :network_type %> -
-
-
-
- <%= f.select :islet_id, IsletDecorator.grouped_by_sites_options_for_select, { prompt: true }, { class: "form-control", data: { action: "change->form-update#update" } } %> - <%= f.label :islet_id %> -
-
+
+
+ <%= render CardComponent.new(type: :primary) do |card| %> + <%= form_with model: @filter, url: "", method: :get, class: "row g-3 align-items-center", id: :filters, + data: { controller: "form-update" , turbo_frame: :infrastructure_islet, turbo_action: :advance } do |f| %> +
+
+ <%= f.select :network_type, Modele::Network::TYPES.excluding("fiber").map { |t| [Modele.human_attribute_name("network_types.short_#{t}"), t] }, {}, { class: "form-control", data: { action: "change->form-update#update" } } %> + <%= f.label :network_type %> +
+
-
- <%= f.submit "Appliquer", class: "btn btn-primary btn-lg", form: :filters %> -
+
+
+ <%= f.select :islet_id, IsletDecorator.grouped_by_sites_options_for_select, { prompt: true }, { class: "form-control", data: { action: "change->form-update#update" } } %> + <%= f.label :islet_id %> +
+
+ +
+ <%= f.submit "Appliquer", class: "btn btn-primary btn-lg", form: :filters %> +
+ <% end %> <% end %> - <% end %> -
+
-<% if @filter.filled? %> - <%= turbo_frame_tag "infrastructure_room_islet", src: infrastructure_room_islet_path(@islet.room, @islet, network_type: @filter.network_type) do %> -
-
-
-
- Loading... - + <% if turbo_frame_request? %> + <%= turbo_frame_tag "infrastructure_islet" do %> + <%= render partial: "islets/infrastructure", locals: { + bays: @bays, network: @filter.network_type, room: @room, islet: @islet, hub: @hub, + second_hub: @second_hub, second_room: @second_room, connections: @connections, concentrateurs_ids: @concentrateurs_ids + } %> + <% end %> + <% else %> + <%= turbo_frame_tag "infrastructure_islet", src: visualization_infrastructure_path(**@filter.to_h) do %> +
+
+
+
+ Loading... + +
-
+ <% end %> <% end %> -<% else %> -
-
-
-
-

Please choose an islet

-
-
-
-<% end %> diff --git a/app/views/visualization/network_capacities/show.html.erb b/app/views/visualization/network_capacities/show.html.erb index b8531b2de..cab9ccad7 100644 --- a/app/views/visualization/network_capacities/show.html.erb +++ b/app/views/visualization/network_capacities/show.html.erb @@ -6,51 +6,48 @@ t("rooms.capacity_view.title") => nil, } %> -
- <%= render CardComponent.new(type: :primary) do |card| %> - <%= form_with model: @filter, url: "", method: :get, class: "row g-3 align-items-center", id: :filters, data: { controller: "form-update" } do |f| %> -
-
- <%= f.select :network_type, Modele::Network::TYPES.map { |t| [Modele.human_attribute_name("network_types.short_#{t}"), t] }, {}, { class: "form-control", data: { action: "change->form-update#update" } } %> - <%= f.label :network_type %> -
-
+
+
+ <%= render CardComponent.new(type: :primary) do |card| %> + <%= form_with model: @filter, url: "", method: :get, class: "row g-3 align-items-center", id: :filters, + data: { controller: "form-update" , turbo_frame: :network_capacity_islet, turbo_action: :advance } do |f| %> +
+
+ <%= f.select :network_type, Modele::Network::TYPES.map { |t| [Modele.human_attribute_name("network_types.short_#{t}"), t] }, {}, { class: "form-control", data: { action: "change->form-update#update" } } %> + <%= f.label :network_type %> +
+
-
-
- <%= f.select :islet_id, IsletDecorator.grouped_by_sites_options_for_select, { prompt: true }, { class: "form-control", data: { action: "change->form-update#update" } } %> - <%= f.label :islet_id %> -
-
+
+
+ <%= f.select :islet_id, IsletDecorator.grouped_by_sites_options_for_select, { prompt: true }, { class: "form-control", data: { action: "change->form-update#update" } } %> + <%= f.label :islet_id %> +
+
-
- <%= f.submit "Appliquer", class: "btn btn-primary btn-lg", form: :filters %> -
+
+ <%= f.submit "Appliquer", class: "btn btn-primary btn-lg", form: :filters %> +
+ <% end %> <% end %> - <% end %> -
+
-<% if @filter.filled? %> - <%= turbo_frame_tag "network_capacity_room_islet", src: network_capacity_room_islet_path(@islet.room, @islet, network_type: @filter.network_type) do %> -
-
-
-
- Loading... - + <% if turbo_frame_request? %> + <%= turbo_frame_tag "network_capacity_islet" do %> + <%= render partial: "islets/network_capacity", locals: { bays: @bays, network: @filter.network_type, room: @room, islet: @islet } %> + <% end %> + <% else %> + <%= turbo_frame_tag "network_capacity_islet", src: visualization_network_capacity_path(**@filter.to_h) do %> +
+
+
+
+ Loading... + +
-
+ <% end %> <% end %> -<% else %> -
-
-
-
-

Please choose an islet

-
-
-
-<% end %> diff --git a/config/routes.rb b/config/routes.rb index 45662c834..543901b0f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,11 +71,6 @@ collection do get :overview end - - resources :islets, only: [] do - get :network_capacity, on: :member - get :infrastructure, on: :member - end end resources :bays resources :gestions diff --git a/db/migrate/20240709134050_create_external_app_records.rb b/db/migrate/20240709134050_create_external_app_records.rb index 6b067a16e..cea0e07fa 100644 --- a/db/migrate/20240709134050_create_external_app_records.rb +++ b/db/migrate/20240709134050_create_external_app_records.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateExternalAppRecords < ActiveRecord::Migration[7.1] def change create_table :external_app_records do |t|