Skip to content
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

[CAPT-1797] Ops reports spike #3284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions app/controllers/admin/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Admin
class ReportsController < BaseAdminController
before_action :ensure_service_operator

def index
@reports = Report.order(created_at: :desc)
end

def show
respond_to do |format|
format.csv {
report = Report.find(params[:id])
send_data report.csv, filename: "#{report.name.parameterize(separator: "_")}_#{report.created_at.iso8601}.csv"
}
end
end
end
end
14 changes: 14 additions & 0 deletions app/jobs/reports_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ReportsJob < CronJob
self.cron_expression = "0 6 * * 2#2" # second Tuesday of the month

def perform
Rails.logger.info "Generating Ops reports"

csv = Reports::DuplicateClaims.new.to_csv
Report.create!(name: Reports::DuplicateClaims::NAME, csv: csv, number_of_rows: csv.lines.count - 1)
csv = Reports::FailedQualificationClaims.new.to_csv
Report.create!(name: Reports::FailedQualificationClaims::NAME, csv: csv, number_of_rows: csv.lines.count - 1)
csv = Reports::FailedProviderCheckClaims.new.to_csv
Report.create!(name: Reports::FailedProviderCheckClaims::NAME, csv: csv, number_of_rows: csv.lines.count - 1)
end
end
2 changes: 2 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Report < ApplicationRecord
end
47 changes: 47 additions & 0 deletions app/models/reports/duplicate_claims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "csv"
require "excel_utils"

module Reports
class DuplicateClaims
include Admin::ClaimsHelper

NAME = "Duplicate Approved Claims"
HEADERS = [
"Claim reference",
"Teacher reference number",
"Full name",
"Policy name",
"Claim amount",
"Claim status",
"Decision date",
"Decision agent"
].freeze

def initialize
@claims = Claim.approved.select { |claim| Claim::MatchingAttributeFinder.new(claim).matching_claims.any? }
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@claims = Claim.approved.select { |claim| Claim::MatchingAttributeFinder.new(claim).matching_claims.any? }
@claims = Claim.approved.includes(:eligibility).select { |claim| Claim::MatchingAttributeFinder.new(claim).matching_claims.any? }

Not sure if we want to include decisions too or if it's less work to filter first and make the additional trips to the db

end

def to_csv
CSV.generate(write_headers: true, headers: HEADERS) do |csv|
@claims.each do |claim|
csv << row(
claim.reference,
claim.eligibility.teacher_reference_number,
claim.full_name,
claim.policy,
claim.award_amount,
status(claim),
claim.latest_decision.created_at,
claim.latest_decision.created_by.full_name
)
end
end
end

private

def row(*entries)
entries.map { |entry| ExcelUtils.escape_formulas(entry) }
end
end
end
72 changes: 72 additions & 0 deletions app/models/reports/failed_provider_check_claims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require "csv"
require "excel_utils"

module Reports
class FailedProviderCheckClaims
include Admin::ClaimsHelper

def self.provider_verification_label(field)
I18n.t("further_education_payments.admin.task_questions.provider_verification.#{field}.label")
end
private_class_method :provider_verification_label

NAME = "Claims with failed provider check"
HEADERS = [
"Claim reference",
"Teacher reference number",
"Full name",
"Claim amount",
"Claim status",
"Decision date",
"Decision agent",
"Provider response: #{provider_verification_label("contract_type")}",
"Provider response: #{provider_verification_label("teaching_responsibilities")}",
"Provider response: #{provider_verification_label("further_education_teaching_start_year")}",
"Provider response: #{provider_verification_label("teaching_hours_per_week")}",
"Provider response: #{provider_verification_label("half_teaching_hours")}",
"Provider response: #{provider_verification_label("subjects_taught")}",
"Provider response: #{provider_verification_label("taught_at_least_one_term")}",
"Provider response: #{provider_verification_label("teaching_hours_per_week_next_term")}"
].freeze

def initialize
@claims = Claim.includes(:tasks)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@claims = Claim.includes(:tasks)
@claims = Claim.includes(:tasks, :eligibility)

.where(eligibility_type: "Policies::FurtherEducationPayments::Eligibility", tasks: {name: "provider_verification", passed: false})
.approved
end

def to_csv
CSV.generate(write_headers: true, headers: HEADERS) do |csv|
@claims.each do |claim|
csv << row(
claim.reference,
claim.eligibility.teacher_reference_number,
claim.full_name,
claim.award_amount,
status(claim),
claim.latest_decision.created_at,
claim.latest_decision.created_by.full_name,
verification_assertion(claim, "contract_type"),
verification_assertion(claim, "teaching_responsibilities"),
verification_assertion(claim, "further_education_teaching_start_year"),
verification_assertion(claim, "teaching_hours_per_week"),
verification_assertion(claim, "half_teaching_hours"),
verification_assertion(claim, "subjects_taught"),
verification_assertion(claim, "taught_at_least_one_term"),
verification_assertion(claim, "teaching_hours_per_week_next_term")
)
end
end
end

private

def row(*entries)
entries.map { |entry| ExcelUtils.escape_formulas(entry) }
end

def verification_assertion(claim, name)
claim.eligibility["assertions"].find { |assertion| assertion["name"] == name }["outcome"]
end
end
end
77 changes: 77 additions & 0 deletions app/models/reports/failed_qualification_claims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require "csv"
require "excel_utils"

module Reports
class FailedQualificationClaims
NAME = "Claims with failed qualification status"
HEADERS = [
"Claim reference",
"Teacher reference number",
"Policy name",
"Decision date",
"Decision agent",
"Answered qualification",
"Answered ITT start year",
"Answered ITT subject",
"DQT ITT subjects",
"DQT ITT start year",
"DQT QTS award date",
"DQT qualification name"
].freeze

def initialize
@claims = Claim.includes(:tasks).where(tasks: {name: "qualifications", passed: false}).approved
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@claims = Claim.includes(:tasks).where(tasks: {name: "qualifications", passed: false}).approved
@claims = Claim.includes(:tasks, :eligibility).where(tasks: {name: "qualifications", passed: false}).approved

end

def to_csv
CSV.generate(write_headers: true, headers: HEADERS) do |csv|
@claims.each do |claim|
csv << row(
claim.reference,
claim.eligibility.teacher_reference_number,
claim.policy,
claim.latest_decision.created_at,
claim.latest_decision.created_by.full_name,
claim.eligibility.qualification,
claim.eligibility.itt_academic_year.start_year,
claim.eligibility.eligible_itt_subject,
dqt_itt_subjects(claim),
dqt_itt_start_date(claim),
dqt_qts_date(claim),
dqt_qts_qualification_name(claim)
)
end
end
end

private

def row(*entries)
entries.map { |entry| ExcelUtils.escape_formulas(entry) }
end

def dqt_itt_subjects(claim)
unless claim.dqt_teacher_status.empty?
claim.dqt_teacher_status["initial_teacher_training"].fetch_values("subject1", "subject2", "subject3").compact.join(",")
end
end

def dqt_itt_start_date(claim)
unless claim.dqt_teacher_status.empty?
claim.dqt_teacher_status["initial_teacher_training"]["programme_start_date"]
end
end

def dqt_qts_date(claim)
unless claim.dqt_teacher_status.empty?
claim.dqt_teacher_status["qualified_teacher_status"]["qts_date"]
end
end

def dqt_qts_qualification_name(claim)
unless claim.dqt_teacher_status.empty?
claim.dqt_teacher_status["qualified_teacher_status"]["name"]
end
end
end
end
32 changes: 32 additions & 0 deletions app/views/admin/reports/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<% content_for(:page_title) { page_title("Download Reports") } %>

<% content_for :back_link do %>
<%= govuk_back_link href: admin_root_path %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<h1 class="govuk-heading-xl">
Reports
</h1>

<table class="govuk-table">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header">Name</th>
<th scope="col" class="govuk-table__header">Created At</th>
<th scope="col" class="govuk-table__header">Number of rows</th>
</tr>
</thead>
<tbody class="govuk-table__body">
<% @reports.each do |report| %>
<tr class="govuk-table__row">
<td class="govuk-table__cell"><%= govuk_link_to report.name, admin_report_path(report, format: :csv) %></td>
<td class="govuk-table__cell"><%= l(report.created_at) %></td>
<td class="govuk-table__cell"><%= report.number_of_rows %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
3 changes: 3 additions & 0 deletions app/views/application/_admin_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<li class="govuk-header__navigation-item">
<%= link_to "Manage services", admin_journey_configurations_path, class: "govuk-header__link" %>
</li>
<li class="govuk-header__navigation-item">
<%= link_to "Reports", admin_reports_path, class: "govuk-header__link" %>
</li>
<% end %>
<li class="govuk-header__navigation-item">
<%= link_to "Sign out", admin_sign_out_path, method: :delete, class: "govuk-header__link" %>
Expand Down
7 changes: 7 additions & 0 deletions config/analytics_blocklist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,10 @@
- verification
:journeys_sessions:
- answers
:reports:
- id
- name
- csv
- created_at
- updated_at
- number_of_rows
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def matches?(request)
resources :levelling_up_premium_payments_awards, only: [:index, :create]
resource :eligible_ey_providers, only: [:create, :show], path: "eligible-early-years-providers"
resource :eligible_fe_providers, only: [:create, :show], path: "eligible-further-education-providers"
resources :reports, only: [:index, :show]

get "refresh-session", to: "sessions#refresh", as: :refresh_session

Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20241007141638_create_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateReports < ActiveRecord::Migration[7.0]
def change
create_table :reports, id: :uuid do |t|
t.string :name
t.text :csv
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice approach 👍, very simple and gets the job done!

t.integer :number_of_rows

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_09_24_113642) do
ActiveRecord::Schema[7.0].define(version: 2024_10_07_141638) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -419,6 +419,14 @@
t.string "itt_subject"
end

create_table "reports", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.text "csv"
t.integer "number_of_rows"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "school_workforce_censuses", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "teacher_reference_number"
t.datetime "created_at", null: false
Expand Down