-
Notifications
You must be signed in to change notification settings - Fork 6
Add builder, job and generator for json data with provider regions #444 #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| class ProviderRegionDataJob < ApplicationJob | ||
| limits_concurrency to: 1, key: ->(language_id) { "hard-limit" } | ||
|
|
||
| def perform(language_id) | ||
| language = Language.find(language_id) | ||
| deliver_provider_region_data(language) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def deliver_provider_region_data(language) | ||
| provider_region_data(language).tap { |file| deliver(file) } | ||
| end | ||
|
|
||
| def provider_region_data(language) | ||
| FileToUpload.new( | ||
| content: JsonGenerator::ProviderRegions.new(language).perform, | ||
| name: "#{language.file_storage_prefix}provider_region_data.json", | ||
| path: "#{language.file_storage_prefix}CMES-v2", | ||
| ) | ||
| end | ||
|
|
||
| def deliver(file) | ||
| FileWorker.new( | ||
| share: ENV["AZURE_STORAGE_SHARE_NAME"], | ||
| name: file.name, | ||
| path: file.path, | ||
| file: file.content, | ||
| ).send | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class JsonGenerator::Base | ||
| def perform | ||
| json_content | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class JsonGenerator::ProviderRegions < JsonGenerator::Base | ||
| def initialize(language, **args) | ||
| @language = language | ||
| @args = args | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :language, :args | ||
|
|
||
| def json_content | ||
| scope | ||
| .map { |provider| { name: provider.name, prefix: provider.file_name_prefix, regions: provider.regions } } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Field names TBD
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get a sample file to send them?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like locally generated, for example?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. English language: Spanish language: Second file, if parsed, looks like this: |
||
| .to_json | ||
| end | ||
|
|
||
| def scope | ||
| language.providers.includes(:regions) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class ProviderRegionDataBuilder | ||
| def perform | ||
| ProviderRegionDataJob.perform_later | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,10 @@ default: &default | |
| command: LanguageFilesScheduler.new.perform | ||
| queue: default | ||
| schedule: "@hourly" | ||
| provider_region_data: | ||
| command: ProviderRegionDataBuilder.new.perform | ||
| queue: default | ||
| schedule: "@daily" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Schedule TBD
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't change very often. Daily should be fine. |
||
| clear_solid_queue_finished_jobs: | ||
| command: "SolidQueue::Job.clear_finished_in_batches(sleep_between_batches: 0.3)" | ||
| schedule: every hour at minute 12 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe ProviderRegionDataJob, type: :job do | ||
| let(:language) { create(:language) } | ||
|
|
||
| describe "#perform" do | ||
| it "generates provider region data" do | ||
| generator = instance_double(JsonGenerator::ProviderRegions) | ||
| allow(JsonGenerator::ProviderRegions).to receive(:new).with(language).and_return(generator) | ||
| expect(generator).to receive(:perform) | ||
|
|
||
| described_class.perform_now(language.id) | ||
| end | ||
|
|
||
| context "when language does not exist" do | ||
| it "raises an error" do | ||
| expect { described_class.perform_now(-1) }.to raise_error(ActiveRecord::RecordNotFound) | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe JsonGenerator::ProviderRegions do | ||
| subject { described_class.new(language) } | ||
|
|
||
| let(:language) { create(:language) } | ||
|
|
||
| it "generates empty json" do | ||
| expect(subject.perform).to eq("[]") | ||
| end | ||
|
|
||
| context "when providers exist" do | ||
| let!(:provider) { create(:provider) } | ||
|
|
||
| before do | ||
| create(:topic, provider:, language:) | ||
| end | ||
|
|
||
| it "generates json with provider data" do | ||
| expect(subject.perform).to eq([ | ||
| { | ||
| name: provider.name, | ||
| prefix: provider.file_name_prefix, | ||
| regions: provider.regions, | ||
| }, | ||
| ].to_json) | ||
| end | ||
| end | ||
|
|
||
| context "when provider does not belong to language" do | ||
| let(:other_language) { create(:language) } | ||
| let!(:provider) { create(:provider) } | ||
|
|
||
| before do | ||
| create(:topic, provider:, language: other_language) | ||
| end | ||
|
|
||
| it "generates empty json" do | ||
| expect(subject.perform).to eq("[]") | ||
| end | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filename TBD
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They'll get back to us on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, will wait