Skip to content
Merged
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
4 changes: 2 additions & 2 deletions app/controllers/results_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ def records

if @is_only
@years_condition_competition = "AND YEAR(competitions.start_date) = #{@year}"
@years_condition_result = "AND results.year = #{@year}"
@years_condition_result = "AND results.reg_year = #{@year}"
elsif @is_until
@years_condition_competition = "AND YEAR(competitions.start_date) <= #{@year}"
@years_condition_result = "AND results.year <= #{@year}"
@years_condition_result = "AND results.reg_year <= #{@year}"
else
@years_condition_competition = ""
@years_condition_result = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class ChangeMonthAndDayFromConciseTables < ActiveRecord::Migration[8.1]
def change
change_table :concise_single_results, bulk: true do |t|
t.remove :month, type: :integer, limit: 2, default: 0, null: false, unsigned: true
t.remove :day, type: :integer, limit: 2, default: 0, null: false, unsigned: true

t.rename :year, :reg_year
end

change_table :concise_average_results, bulk: true do |t|
t.remove :month, type: :integer, limit: 2, default: 0, null: false, unsigned: true
t.remove :day, type: :integer, limit: 2, default: 0, null: false, unsigned: true

t.rename :year, :reg_year
end
end
end
8 changes: 2 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,11 @@
t.integer "average", default: 0, null: false
t.string "continent_id", limit: 50, default: "", null: false
t.string "country_id", limit: 50, default: "", null: false
t.integer "day", limit: 2, default: 0, null: false, unsigned: true
t.string "event_id", limit: 6, default: "", null: false
t.integer "id", default: 0, null: false
t.integer "month", limit: 2, default: 0, null: false, unsigned: true
t.string "person_id", limit: 10, default: "", null: false
t.integer "reg_year", limit: 2, default: 0, null: false, unsigned: true
t.bigint "value_and_id"
t.integer "year", limit: 2, default: 0, null: false, unsigned: true
t.index ["event_id", "average"], name: "mixed_records_speedup"
t.index ["event_id", "country_id", "average"], name: "regional_records_speedup"
end
Expand All @@ -486,13 +484,11 @@
t.integer "best", default: 0, null: false
t.string "continent_id", limit: 50, default: "", null: false
t.string "country_id", limit: 50, default: "", null: false
t.integer "day", limit: 2, default: 0, null: false, unsigned: true
t.string "event_id", limit: 6, default: "", null: false
t.integer "id", default: 0, null: false
t.integer "month", limit: 2, default: 0, null: false, unsigned: true
t.string "person_id", limit: 10, default: "", null: false
t.integer "reg_year", limit: 2, default: 0, null: false, unsigned: true
t.bigint "value_and_id"
t.integer "year", limit: 2, default: 0, null: false, unsigned: true
t.index ["event_id", "best"], name: "mixed_records_speedup"
t.index ["event_id", "country_id", "best"], name: "regional_records_speedup"
end
Expand Down
6 changes: 2 additions & 4 deletions lib/auxiliary_data_computation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def self.compute_concise_results
].each do |field, table_name|
DbHelper.with_temp_table(table_name) do |temp_table_name|
ActiveRecord::Base.connection.execute <<~SQL.squish
INSERT INTO #{temp_table_name} (id, #{field}, value_and_id, person_id, event_id, country_id, continent_id, year, month, day)
INSERT INTO #{temp_table_name} (id, #{field}, value_and_id, person_id, event_id, country_id, continent_id, reg_year)
SELECT
results.id,
#{field},
Expand All @@ -24,9 +24,7 @@ def self.compute_concise_results
event_id,
countries.id country_id,
continent_id,
YEAR(start_date) year,
MONTH(start_date) month,
DAY(start_date) day
YEAR(start_date) reg_year
FROM (
SELECT MIN(#{field} * 1000000000 + results.id) valueAndId
FROM results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,40 @@ WITH round_dates AS (SELECT sa.round_id,
csr.event_id,
MIN(csr.best) AS old_NR_single
FROM concise_single_results csr
WHERE csr.year < 2019
WHERE csr.reg_year < 2019
GROUP BY country_id, event_id),
-- Fetches the NR averages of each country as of the end of the previous year.
old_nr_averages AS (SELECT car.country_id,
car.event_id,
MIN(car.average) AS old_NR_average
FROM concise_average_results car
WHERE car.year < 2019
WHERE car.reg_year < 2019
GROUP BY country_id, event_id),
-- Fetches the CR singles of each continent as of the end of the previous year.
old_cr_singles AS (SELECT csr.continent_id,
csr.event_id,
MIN(csr.best) AS old_CR_single
FROM concise_single_results csr
WHERE csr.year < 2019
WHERE csr.reg_year < 2019
GROUP BY continent_id, event_id),
-- Fetches the CR averages of each continent as of the end of the previous year.
old_cr_averages AS (SELECT car.continent_id,
car.event_id,
MIN(car.average) AS old_CR_average
FROM concise_average_results car
WHERE car.year < 2019
WHERE car.reg_year < 2019
GROUP BY continent_id, event_id),
-- Fetches WR singles as of the end of the previous year.
old_wr_singles AS (SELECT csr.event_id,
MIN(csr.best) AS old_WR_single
FROM concise_single_results csr
WHERE csr.year < 2019
WHERE csr.reg_year < 2019
GROUP BY event_id),
-- Fetches WR averages as of the end of the previous year.
old_wr_averages AS (SELECT car.event_id,
MIN(car.average) AS old_WR_average
FROM concise_average_results car
WHERE car.year < 2019
WHERE car.reg_year < 2019
GROUP BY event_id),
-- Joins round date to results table and filters out rows that are not <= NRs from previous years. Assigns ranking 1 to each single or average that is the best for that country for that day.
t1 AS (SELECT r.id AS results_id,
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/auxiliary_data_computation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
create(:result, event_id: "222", best: 100, average: 150, competition: competition_2017, person: person)
AuxiliaryDataComputation.compute_concise_results
# Concise single results
concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, year, best FROM concise_single_results"
concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, reg_year, best FROM concise_single_results"
expect(concise_single_results).to contain_exactly(["333", person.wca_id, 2016, 700], ["333", person.wca_id, 2017, 800], ["222", person.wca_id, 2017, 100])
# Concise average results
concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, year, average FROM concise_average_results"
concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, reg_year, average FROM concise_average_results"
expect(concise_average_results).to contain_exactly(["333", person.wca_id, 2016, 800], ["333", person.wca_id, 2017, 900], ["222", person.wca_id, 2017, 150])
end

Expand All @@ -32,10 +32,10 @@
create(:result, event_id: "333", best: 750, average: 850, competition: next_competition_2016, person: person)
AuxiliaryDataComputation.compute_concise_results
# Concise single results
concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, year, best FROM concise_single_results"
concise_single_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, reg_year, best FROM concise_single_results"
expect(concise_single_results).to contain_exactly(["333", person.wca_id, "China", 2016, 700], ["333", person.wca_id, "Chile", 2016, 750])
# Concise average results
concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, year, average FROM concise_average_results"
concise_average_results = ActiveRecord::Base.connection.execute "SELECT event_id, person_id, country_id, reg_year, average FROM concise_average_results"
expect(concise_average_results).to contain_exactly(["333", person.wca_id, "China", 2016, 800], ["333", person.wca_id, "Chile", 2016, 850])
end
end
Expand Down