-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Defines the logic for calculating average scores, based on the EvaluationProgram's program_type
- Loading branch information
1 parent
7e7577d
commit 79792d8
Showing
30 changed files
with
453 additions
and
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ all rspec | |
rspec | ||
all rspec | ||
all | ||
EvaluatioEvaluai | ||
all |
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
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: project_program_summaries | ||
# | ||
# id :bigint not null, primary key | ||
# average_score :float default(0.0), not null | ||
# latest_increase_percent :float | ||
# evaluation_program_id :bigint not null | ||
# project_id :bigint not null | ||
# scores_summary :jsonb not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class ProjectProgramSummary < ApplicationRecord | ||
belongs_to :evaluation_program | ||
belongs_to :project | ||
|
||
validates :project_id, uniqueness: { scope: :evaluation_program_id } | ||
validates_numericality_of :average_score, | ||
less_than_or_equal_to: EvaluationProgram::MAXIMUM_VALID_SCORE, | ||
greater_than_or_equal_to: 0 | ||
|
||
delegate :program_type, to: :evaluation_program | ||
|
||
def project_evaluations | ||
project.project_evaluations.where( | ||
evaluation_program_id: evaluation_program.id | ||
) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
# | ||
# This class manages the logic behind determining how a Summary's scores (JSONB) should | ||
# be updated, and then performs the update. | ||
# | ||
class ProjectProgramSummaryUpdater | ||
PROGRAM_TYPE_METHODS = { | ||
project_follow_up: :take_latest_evaluation, | ||
competition: :take_all_evaluations | ||
}.with_indifferent_access | ||
|
||
def initialize(project_id, evaluation_program_id) | ||
@summary = ProjectProgramSummary.find_by( | ||
project_id: project_id, evaluation_program_id: evaluation_program_id | ||
) | ||
return unless @summary | ||
|
||
@type = @summary.program_type | ||
end | ||
|
||
def run | ||
assign_summary_scores | ||
assign_average_score | ||
@summary.save! | ||
end | ||
|
||
private | ||
|
||
def assign_summary_scores | ||
@summary.scores_summary['evaluation_count'] = evaluations.count | ||
@summary.scores_summary['criteria'] = generate_criteria_summary | ||
end | ||
|
||
def assign_average_score | ||
@summary.average_score = evaluations.average(:total_score) | ||
end | ||
|
||
def evaluations | ||
@evaluations ||= send(PROGRAM_TYPE_METHODS[@type]) | ||
end | ||
|
||
def take_latest_evaluation | ||
@summary.project_evaluations.order(timestamp: :desc).limit(1) | ||
end | ||
|
||
def take_all_evaluations | ||
@summary.project_evaluations | ||
end | ||
|
||
def generate_criteria_summary | ||
sql = <<-SQL | ||
SELECT | ||
evaluation_criteria.name, | ||
program_criteria.weight, | ||
evaluation_programs.criteria_scale_max AS maximum, | ||
evaluation_programs.criteria_scale_min AS minimum, | ||
AVG(evaluation_scores.total) AS total, | ||
AVG(evaluation_scores.weighed_total) AS weighed_total | ||
FROM evaluation_scores | ||
LEFT JOIN program_criteria | ||
ON evaluation_scores.program_criterium_id = program_criteria.id | ||
LEFT JOIN evaluation_programs | ||
ON program_criteria.evaluation_program_id = evaluation_programs.id | ||
LEFT JOIN evaluation_criteria | ||
ON program_criteria.evaluation_criterium_id = evaluation_criteria.id | ||
WHERE evaluation_scores.project_evaluation_id IN (#{evaluations.pluck(:id).join(', ')}) | ||
GROUP BY | ||
evaluation_criteria.name, program_criteria.weight, | ||
evaluation_programs.criteria_scale_max, | ||
evaluation_programs.criteria_scale_min | ||
ORDER BY | ||
evaluation_criteria.name | ||
SQL | ||
scores = EvaluationScore.connection.execute(sql).to_a | ||
scores.inject({}) { |hash, score| hash.merge(score['name'] => score) } | ||
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
6 changes: 3 additions & 3 deletions
6
...49_create_project_evaluation_summaries.rb → ...91113010649_create_project_evaluations.rb
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
15 changes: 15 additions & 0 deletions
15
db/migrate/20200102223447_create_project_program_summaries.rb
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateProjectProgramSummaries < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :project_program_summaries do |t| | ||
t.float :average_score, null: false, default: 0.0 | ||
t.float :latest_increase_percent | ||
t.references :evaluation_program, null: false, foreign_key: true | ||
t.references :project, null: false, foreign_key: true | ||
t.jsonb :scores_summary, null: false, default: {} | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.