Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/jobs/provider_region_data_job.rb
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",
Copy link
Collaborator Author

@dmitrytrager dmitrytrager Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filename TBD

Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will wait

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
5 changes: 5 additions & 0 deletions app/services/json_generator/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class JsonGenerator::Base
def perform
json_content
end
end
20 changes: 20 additions & 0 deletions app/services/json_generator/provider_regions.rb
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 } }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Field names TBD

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a sample file to send them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like locally generated, for example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English language:

"[{\"name\":\"Provided by the government\",\"prefix\":\"pref\",\"regions\":[{\"id\":1,\"name\":\"Africa\",\"created_at\":\"2025-02-03T16:02:20.957Z\",\"updated_at\":\"2025-10-13T21:19:47.221Z\"},{\"id\":2,\"name\":\"Asia\",\"created_at\":\"2025-10-13T21:19:53.908Z\",\"updated_at\":\"2025-10-13T21:19:53.908Z\"}]}]"

Spanish language:

"[{\"name\":\"Provided by the government\",\"prefix\":\"pref\",\"regions\":[{\"id\":1,\"name\":\"Africa\",\"created_at\":\"2025-02-03T16:02:20.957Z\",\"updated_at\":\"2025-10-13T21:19:47.221Z\"},{\"id\":2,\"name\":\"Asia\",\"created_at\":\"2025-10-13T21:19:53.908Z\",\"updated_at\":\"2025-10-13T21:19:53.908Z\"}]}]"

Second file, if parsed, looks like this:

[{"name" => "Provided by the government",
  "prefix" => "pref",
  "regions" =>
   [{"id" => 1, "name" => "Africa", "created_at" => "2025-02-03T16:02:20.957Z", "updated_at" => "2025-10-13T21:19:47.221Z"},
    {"id" => 2, "name" => "Asia", "created_at" => "2025-10-13T21:19:53.908Z", "updated_at" => "2025-10-13T21:19:53.908Z"}]}]

.to_json
end

def scope
language.providers.includes(:regions)
end
end
5 changes: 5 additions & 0 deletions app/services/provider_region_data_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ProviderRegionDataBuilder
def perform
ProviderRegionDataJob.perform_later
end
end
4 changes: 4 additions & 0 deletions config/recurring.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schedule TBD

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down
21 changes: 21 additions & 0 deletions spec/jobs/provider_region_data_job_spec.rb
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
42 changes: 42 additions & 0 deletions spec/services/json_generator/provider_regions_spec.rb
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
2 changes: 1 addition & 1 deletion spec/services/text_generator/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
before do
tag_names = [ "cold", "flu", "cough" ]
tag_names.each do |tag_name|
tag = create(:tag, name: tag_name)
create(:tag, name: tag_name)
end
topic.tag_list.add(tag_names)
topic.save
Expand Down