forked from nanego/my-dcim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
163 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
class ExternalAppRecordsController < ApplicationController | ||
# frozen_string_literal: true | ||
|
||
class ExternalAppRecordsController < ApplicationController | ||
def index | ||
@external_app_records = ExternalAppRecord.includes(:server => :frame).order('servers.name') | ||
@external_app_records = ExternalAppRecord.includes(server: :frame).order("servers.name") | ||
@filter = ProcessorFilter.new(@external_app_records, params) | ||
@external_app_records = @filter.results | ||
@servers_count = Server.no_pdus.count | ||
end | ||
|
||
def sync_all_servers_with_glpi | ||
if ExternalAppRequest.where(status: ['pending', 'in_progress'], external_app_name: 'glpi').exists? | ||
render json: { error: 'Another request is already in progress' } | ||
if ExternalAppRequest.exists?(status: ["pending", "in_progress"], external_app_name: "glpi") | ||
render json: { error: "Another request is already in progress" } | ||
else | ||
request = ExternalAppRequest.create!(status: :pending, user: current_user, external_app_name: 'glpi') | ||
request = ExternalAppRequest.create!(status: :pending, user: current_user, external_app_name: "glpi") | ||
SyncWithGlpiJob.perform_later | ||
|
||
render json: { request_id: request.id, status: request.status, progress: request.progress } | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
class SyncWithGlpiJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(*args) | ||
|
||
def perform | ||
request = ExternalAppRequest.find_by(status: 'pending', external_app_name: 'glpi') | ||
request.update(status: :in_progress) | ||
|
||
begin | ||
|
||
client = GlpiClient.new | ||
servers = Server.no_pdus | ||
servers_count = servers.count | ||
|
||
Rails.logger.info "Synchronizing #{servers_count} servers with GLPI records:" | ||
puts "Synchronizing #{servers_count} servers with GLPI records:" | ||
Rails.logger.debug { "Synchronizing #{servers_count} servers with GLPI records:" } | ||
|
||
servers.each_with_index do |server, index| | ||
ExternalAppRecord.sync_server_with_glpi(server, client) | ||
|
||
puts "Processed #{index + 1} servers so far" if (index + 1) % 25 == 0 | ||
Rails.logger.debug { "Processed #{index + 1} servers so far" } if ((index + 1) % 25).zero? | ||
request.update(progress: (index + 1) * 100 / servers_count) | ||
end | ||
|
||
request.update(status: :completed, progress: 100) | ||
|
||
rescue StandardError => e | ||
request.update(status: :failed) | ||
Rails.logger.error "ExternalAppRequest failed: #{e.message}" | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :sync_glpi_data do | ||
|
||
desc "Sync all servers with GLPI records" | ||
task :sync_all_servers => :environment do | ||
|
||
client = GlpiClient.new | ||
|
||
servers = Server.no_pdus | ||
|
||
puts "Synchronizing #{servers.count} servers with GLPI records:" | ||
|
||
servers.each_with_index do |server, index| | ||
puts "Processed #{index + 1} servers so far" if (index + 1) % 25 == 0 | ||
puts "Processed #{index + 1} servers so far" if ((index + 1) % 25).zero? | ||
ExternalAppRecord.sync_server_with_glpi(server, client) | ||
end | ||
|
||
end | ||
|
||
desc "Clear all ExternalAppRecord" | ||
task :clear_all_external_records => :environment do | ||
ExternalAppRecord.destroy_all | ||
ExternalAppRecord.destroy_all | ||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe SyncWithGlpiJob do | ||
let(:request) { ExternalAppRequest.create(user: User.first, external_app_name: "glpi") } | ||
|
||
before { request } | ||
|
||
it "queues the job" do | ||
expect do | ||
described_class.perform_later | ||
end.to have_enqueued_job(described_class).on_queue("default") | ||
end | ||
|
||
it "executes perform" do | ||
expect do | ||
perform_enqueued_jobs { described_class.perform_later } | ||
end.to change { request.reload.progress }.from(0).to(100) | ||
end | ||
|
||
describe "updates the request status and progress correctly" do | ||
before do | ||
perform_enqueued_jobs { described_class.perform_later } | ||
request.reload | ||
end | ||
|
||
it { expect(request.status).to eq("completed") } | ||
it { expect(request.progress).to eq(100) } | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe "ExternalAppRecords" do | ||
let(:ext_app_rec) { external_app_records(:one) } | ||
|
||
before do | ||
sign_in users(:one) | ||
|
||
ext_app_rec | ||
end | ||
|
||
describe "GET #index" do | ||
subject(:response) do | ||
get external_app_records_path | ||
|
||
@response # rubocop:disable RSpec/InstanceVariable | ||
end | ||
|
||
it { expect(response).to have_http_status(:success) } | ||
it { expect(response).to render_template(:index) } | ||
it { expect(response.body).to include(ext_app_rec.server.numero) } | ||
end | ||
|
||
describe "PUT #sync_all_servers_with_glpi" do | ||
subject(:response) do | ||
put sync_with_glpi_external_app_records_path | ||
|
||
# NOTE: used to simplify usage and custom test done in final spec file. | ||
@response # rubocop:disable RSpec/InstanceVariable | ||
end | ||
|
||
it "creates a new request and enqueues the job" do | ||
expect do | ||
response | ||
end.to change(ExternalAppRequest, :count).by(1) | ||
end | ||
|
||
it "enqueues the job" do | ||
expect do | ||
response | ||
end.to have_enqueued_job(SyncWithGlpiJob) | ||
end | ||
|
||
it do | ||
expect(response.parsed_body).to include("request_id" => ExternalAppRequest.last.id, | ||
"status" => "pending", | ||
"progress" => 0) | ||
end | ||
end | ||
end |
Oops, something went wrong.