-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export model data to JS instead of letting the JS compilation query t…
…he database (#9674) * Add Frontend JS export routine * Use stored JSON data in frontend compilation * Add Rake task to generate frontend files during Docker spinup * Add data files in raw JSON form * Refactor into abstract data loader * Add migration to remove primary key from championship ISO2 table * Add basic StaticData module for easy loading inclusion * Add backend loader model * Adjust frontend data loader to use exported file format * Replace database call on Competition.non_future_years * Fix DB access in EligibleIso2 validator * Run frontend data export in GitHub CI * Consistently use camelCase in data loading * Share derived property computation between JS and Rails * Remove frontend compilation task * Redirect model listener to lib/static_files folder * Fix data compilation errors * Remove direct AR instantiations from model code * Symbolize keys upon static_file JSON parsing * Fix 'official' computation of Events in frontend * Let static data handle their own export * Remove static data listener * Dump only fictive countries for Country static_data * Don't deliver duplicated data files through Webpacker * Regenerate data files after backend refactor * Add custom PreferredFormat dumper * Rename loader method * Fix column sanitizing for static data * Remove cellName from Events table * Fix upsert call in old migrations * Add isOfficial to Events serialization format * Hard-code competition model length validations * Add rake task for handling static data load/dump * Fix dump/load consistency in events.json * Fix classloader cyclic dependency * Avoid constant initialization DB access in SolveTime * Use compact export format for PreferredFormat * Remove legacy test case for 333ft
- Loading branch information
Showing
44 changed files
with
958 additions
and
219 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
module StaticData | ||
extend ActiveSupport::Concern | ||
|
||
DATA_FOLDER = Rails.root.join('lib/static_data') | ||
|
||
class_methods do | ||
def parse_json_file(file_path, symbolize_names: true) | ||
::JSON.parse(File.read(file_path), symbolize_names: symbolize_names) | ||
end | ||
|
||
def write_json_file(file_path, hash_data) | ||
json_output = ::JSON.pretty_generate(hash_data.as_json) | ||
|
||
# Don't rewrite the file if it already exists and has the same content. | ||
# It helps the asset pipeline or webpack understand that file wasn't changed. | ||
if File.exist?(file_path) && File.read(file_path) == json_output | ||
return | ||
end | ||
|
||
File.write(file_path, json_output) | ||
end | ||
end | ||
|
||
included do | ||
after_commit :write_json_data! if Rails.env.development? | ||
|
||
def self.data_file_handle | ||
self.name.pluralize.underscore | ||
end | ||
|
||
def self.data_file_path | ||
DATA_FOLDER.join("#{self.data_file_handle}.json") | ||
end | ||
|
||
def self.static_json_data | ||
self.parse_json_file(self.data_file_path) | ||
end | ||
|
||
def self.all_raw | ||
self.static_json_data | ||
end | ||
|
||
def self.all_raw_sanitized | ||
column_symbols = self.column_names.map(&:to_sym) | ||
self.all_raw.map { |attributes| attributes.slice(*column_symbols) } | ||
end | ||
|
||
def self.all_static | ||
self.all_raw_sanitized.map { |attributes| self.new(**attributes) } | ||
end | ||
|
||
def self.dump_static | ||
self.all.as_json | ||
end | ||
|
||
def self.load_json_data! | ||
self.upsert_all(self.all_raw_sanitized) | ||
end | ||
|
||
def self.write_json_data! | ||
self.write_json_file(self.data_file_path, self.dump_static) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
class EligibleCountryIso2ForChampionship < ApplicationRecord | ||
include StaticData | ||
|
||
self.table_name = "eligible_country_iso2s_for_championship" | ||
|
||
belongs_to :championship, foreign_key: :championship_type, primary_key: :championship_type, optional: true | ||
|
||
validates :eligible_country_iso2, uniqueness: { scope: :championship_type, case_sensitive: false }, | ||
inclusion: { in: Country.all.map(&:iso2) } | ||
inclusion: { in: Country::ALL_STATES_RAW.pluck(:iso2) } | ||
|
||
def self.data_file_handle | ||
"championship_eligible_iso2" | ||
end | ||
|
||
def self.all_raw | ||
self.static_json_data.flat_map do |type, iso2_list| | ||
iso2_list.map do |iso2| | ||
{ championship_type: type, eligible_country_iso2: iso2 } | ||
end | ||
end | ||
end | ||
|
||
def self.dump_static | ||
self.all | ||
.group_by(&:championship_type) | ||
.transform_values { |el| el.pluck(:eligible_country_iso2) } | ||
.as_json | ||
end | ||
|
||
def self.championship_types | ||
pluck(:championship_type).uniq | ||
all_raw.pluck(:championship_type).uniq | ||
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
Oops, something went wrong.