Skip to content

Commit

Permalink
activejob improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Dec 4, 2018
1 parent 7344840 commit c04d3fc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
8 changes: 8 additions & 0 deletions app/controllers/concerns/helpeable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def subset_exist? report_id, compressed_subset
false
end

def add_subsets
if @report.present? && safe_params[:compressed].present?
unless subset_exist?(@report.uid, safe_params[:compressed])
@report.report_subsets << ReportSubset.new(compressed: safe_params[:compressed])
end
end
end

def get_month date
Date.strptime(date,"%Y-%m-%d").month.to_s
end
Expand Down
17 changes: 11 additions & 6 deletions app/jobs/validation_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ class ValidationJob < ActiveJob::Base
queue_as :sashimi


def perform(item, options={})
def perform(id, options={})
logger = Logger.new(STDOUT)

full_report = ActiveSupport::Gzip.decompress(item.compressed)
subset = ReportSubset.where(id: id).first


full_report = ActiveSupport::Gzip.decompress(subset.compressed)
parsed =JSON.parse((full_report))
header = parsed.dig("report-header")
header["report-datasets"] = parsed.dig("report-datasets")

report = Report.where(uid: item.report.uid).first
report = Report.where(uid: subset.report.uid).first

valid = item.validate_this_sushi(header)
valid = subset.validate_this_sushi(header)
if valid.empty?
message = "A subset of Usage Report #{item.report.uid} successfully validated and ready to Push"
message = "[ValidationJob] A subset of Usage Report #{subset.report.uid} successfully validated and ready to Push"
# item.push_report
report.push_report
subset.update_column(:aasm, "valid")
else
message = "A subset of Usage Report #{item.report.uid} fail validation. Needs to be updated, there are #{valid.size} errors. For example: #{valid.first[:message]}"
message = "[ValidationJob] A subset of Usage Report #{subset.report.uid} fail validation. Needs to be updated, there are #{valid.size} errors. For example: #{valid.first[:message]}"
report.exceptions = valid
subset.update_column(:aasm, "not_valid")
end
logger.info message
return report.exceptions unless valid.empty?
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/metadatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Metadatable
included do

def validate_sushi
puts "this is being validated"
puts "report validation"
schema = load_schema
report = self.attributes.except("compressed")
report.transform_keys! { |key| key.tr('_', '-') }
Expand All @@ -17,7 +17,7 @@ def validate_sushi
end

def validate_this_sushi sushi
puts "this is being validated"
puts "subset validation"
schema = load_schema
JSON::Validator.fully_validate(schema, sushi.to_json, :errors_as_objects => true)
end
Expand Down
11 changes: 2 additions & 9 deletions app/models/report_subset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@ class ReportSubset < ApplicationRecord
validates_presence_of :report_id
after_validation :make_checksum
before_create :set_id

# include validation methods for sushi
include Metadatable
after_commit :validate_report_job
after_commit :validate_report_job, on: :create


def validate_report_job
ValidationJob.perform_later(self)
ValidationJob.perform_later(id)
end

def gzip
::Base64.strict_encode64(compressed)
end

def pelon
ActiveSupport::Gzip.decompress(compressed)

end


def make_checksum
write_attribute(:checksum, Digest::SHA256.hexdigest(compressed))
end
Expand Down
24 changes: 16 additions & 8 deletions spec/jobs/validation_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@

context "good metadata" do
let(:bigly) {file_fixture('small_dataone.json').read}
# let(:report) {create(:report)}
# let(:report_id) {report.uid}

let(:gzipped) do
ActiveSupport::Gzip.compress(bigly)
end

let(:compresed_report) { create(:report_subset, compressed: gzipped) }

subject(:job) { ValidationJob.perform_later(gzipped) }
# subject(:job) { ValidationJob.perform_later(gzipped, {report_id: report_id}) }
subject(:job) { ValidationJob.perform_later(compresed_report.id) }

it 'queues the job' do
expect { job }.to have_enqueued_job(ValidationJob)
.on_queue("test_sashimi")
# expect { job }.to have_enqueued_job(ValidationJob)
# .on_queue("test_sashimi")
end

it 'execute further call' do
response = perform_enqueued_jobs do
ValidationJob.new.perform(compresed_report)
# ValidationJob.new.perform(gzipped, {report_id: report_id})
ValidationJob.new.perform(compresed_report.id)
end
expect(response).not_to be_a(Array)
end
Expand All @@ -34,23 +38,27 @@

context "bad metadata" do
let(:bigly) {file_fixture('DSR-D1-2012-07-10.json').read}
# let(:report) {create(:report)}
# let(:report_id) {report.uid}

let(:gzipped) do
ActiveSupport::Gzip.compress(bigly)
end

let(:compresed_report) { create(:report_subset, compressed: gzipped) }

subject(:job) { ValidationJob.perform_later(gzipped) }
subject(:job) { ValidationJob.perform_later(compresed_report.id) }
# subject(:job) { ValidationJob.perform_later(gzipped, {report_id: report_id}) }

it 'queues the job' do
expect { job }.to have_enqueued_job(ValidationJob)
.on_queue("test_sashimi")
# expect { job }.to have_enqueued_job(ValidationJob)
# .on_queue("test_sashimi")
end

it 'execute further call' do
response = perform_enqueued_jobs do
ValidationJob.new.perform(compresed_report)
# ValidationJob.new.perform(gzipped, {report_id: report_id})
ValidationJob.new.perform(compresed_report.id)
end
expect(response).to be_a(Array)
end
Expand Down

0 comments on commit c04d3fc

Please sign in to comment.