Skip to content

Commit

Permalink
Merge branch 'main' into further-fix-mail-job-scope
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-coggin committed Sep 29, 2023
2 parents 4dc3ab2 + 3f7cc1f commit 5e86650
Show file tree
Hide file tree
Showing 23 changed files with 464 additions and 49 deletions.
164 changes: 164 additions & 0 deletions .github/workflows/tf-azure-deploy-stage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
name: 'Terraform Deploy [Azure - STAGE]'

on:
workflow_dispatch:

defaults:
run:
working-directory: ./terraform-azure

# Permissions for OIDC authentication
permissions:
id-token: write
contents: write

env:
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
ARM_USE_OIDC: true
ARM_SKIP_PROVIDER_REGISTRATION: true

jobs:
terraform-plan:
name: 'Terraform Plan'
runs-on: ubuntu-latest
environment: staging
outputs:
tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }}

steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v4

# Install the latest version of the Terraform CLI
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.5.6
terraform_wrapper: false

# Initialise a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
run: >
terraform init
-backend-config="resource_group_name=${{ secrets.TERRAFORM_STATE_RESOURCE_GROUP }}"
-backend-config="storage_account_name=${{ secrets.TERRAFORM_STATE_STORAGE_ACCOUNT_NAME }}"
-backend-config="container_name=${{ secrets.TERRAFORM_STATE_STORAGE_CONTAINER_NAME }}"
-backend-config="key=${{ secrets.TERRAFORM_STATE_KEY }}"
# Checks that all Terraform configuration files adhere to a canonical format
# Will fail the build if not
- name: Terraform Format
run: terraform fmt -check

# Generates Terraform input variables
- name: Generate Terraform Variables
shell: bash
env:
WEB_SECRETS: ${{ toJSON(secrets) }}
WEB_VARS: ${{ toJSON(vars) }}
run: |
printf '%s\n' "$WEB_SECRETS" > tmp-secrets.json
printf '%s\n' "$WEB_VARS" > tmp-vars.json
jq 'with_entries(.key |= ascii_downcase)' tmp-secrets.json > web-secrets.auto.tfvars.json
jq 'with_entries(.key |= ascii_downcase)' tmp-vars.json > web-vars.auto.tfvars.json
# Generates an execution plan for Terraform
# An exit code of 0 indicated no changes, 1 a terraform failure, 2 there are pending changes.
- name: Terraform Plan
id: tf-plan
run: |
export exitcode=0
terraform plan -detailed-exitcode -no-color -out tfplan || export exitcode=$?
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
if [ $exitcode -eq 1 ]; then
echo Terraform Plan Failed!
exit 1
else
exit 0
fi
# Save Terraform Plan
- name: Publish Terraform Plan
uses: actions/upload-artifact@v3
with:
name: tfplan
path: terraform-azure/tfplan

# Create string output of Terraform Plan
- name: Create String Output
id: tf-plan-string
run: |
TERRAFORM_PLAN=$(terraform show -no-color tfplan)
delimiter="$(openssl rand -hex 8)"
echo "summary<<${delimiter}" >> $GITHUB_OUTPUT
echo "## Terraform Plan Output" >> $GITHUB_OUTPUT
echo "<details><summary>Click to expand</summary>" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo '```terraform' >> $GITHUB_OUTPUT
echo "$TERRAFORM_PLAN" >> $GITHUB_OUTPUT
echo '```' >> $GITHUB_OUTPUT
echo "</details>" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
# Publish Terraform Plan as task summary
- name: Publish Terraform Plan to Task Summary
env:
SUMMARY: ${{ steps.tf-plan-string.outputs.summary }}
run: |
echo "$SUMMARY" >> $GITHUB_STEP_SUMMARY
# terraform-apply:
# name: 'Terraform Apply'
# if: needs.terraform-plan.outputs.tfplanExitCode == 2
# runs-on: ubuntu-latest
# environment: staging
# needs: [terraform-plan]
#
# steps:
# # Checkout the repository to the GitHub Actions runner
# - name: Checkout
# uses: actions/checkout@v4
#
# # Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
# - name: Setup Terraform
# uses: hashicorp/setup-terraform@v2
# with:
# terraform_version: 1.5.6
#
# # Initialise a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
# - name: Terraform Init
# run: >
# terraform init
# -backend-config="resource_group_name=${{ secrets.TERRAFORM_STATE_RESOURCE_GROUP }}"
# -backend-config="storage_account_name=${{ secrets.TERRAFORM_STATE_STORAGE_ACCOUNT_NAME }}"
# -backend-config="container_name=${{ secrets.TERRAFORM_STATE_STORAGE_CONTAINER_NAME }}"
# -backend-config="key=${{ secrets.TERRAFORM_STATE_KEY }}"
#
# # Generates Terraform input variables
# - name: Generate Terraform Variables
# shell: bash
# env:
# WEB_SECRETS: ${{ toJSON(secrets) }}
# WEB_VARS: ${{ toJSON(vars) }}
# run: |
# printf '%s\n' "$WEB_SECRETS" > tmp-secrets.json
# printf '%s\n' "$WEB_VARS" > tmp-vars.json
# jq 'with_entries(.key |= ascii_downcase)' tmp-secrets.json > web-secrets.auto.tfvars.json
# jq 'with_entries(.key |= ascii_downcase)' tmp-vars.json > web-vars.auto.tfvars.json
#
# # Download saved plan from artifacts
# - name: Download Terraform Plan
# uses: actions/download-artifact@v3
# with:
# name: tfplan
# path: terraform-azure/tfplan
#
# # Terraform Apply
# - name: Terraform Apply
# run: terraform apply -auto-approve tfplan/tfplan
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ GEM
google-cloud-core (~> 1.6)
googleauth (>= 0.16.2, < 2.a)
mini_mime (~> 1.0)
google-protobuf (3.24.0)
google-protobuf (3.24.0-aarch64-linux)
google-protobuf (3.24.0-arm64-darwin)
google-protobuf (3.24.0-x86_64-darwin)
Expand Down Expand Up @@ -330,6 +331,7 @@ GEM
memoist (0.16.2)
method_source (1.0.0)
mini_mime (1.1.5)
mini_portile2 (2.8.4)
minitest (5.20.0)
msgpack (1.6.0)
multi_json (1.15.0)
Expand All @@ -343,6 +345,9 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.4)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.15.4-aarch64-linux)
racc (~> 1.4)
nokogiri (1.15.4-arm64-darwin)
Expand Down Expand Up @@ -795,6 +800,7 @@ PLATFORMS
arm64-darwin-20
arm64-darwin-21
arm64-darwin-22
ruby
x86_64-darwin-19
x86_64-darwin-20
x86_64-darwin-21
Expand Down
23 changes: 16 additions & 7 deletions app/controllers/training/responses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def save_response!
correct_answers = content.confidence_question? ? true : content.correct_answers.eql?(user_answers)

if ENV['DISABLE_USER_ANSWER'].present?
current_user_response.update(answers: user_answers, correct: correct_answers)
current_user_response.update(answers: user_answers, correct: correct_answers, schema: content.schema)
else
current_user_response.update(answer: user_answers, correct: correct_answers)
end
Expand All @@ -62,12 +62,21 @@ def redirect

# @return [Ahoy::Event] Update action
def track_question_answer
track('questionnaire_answer',
uid: content.id,
mod_uid: mod.id,
type: content.assessments_type, # TODO: will be replaced with content.page_type
success: current_user_response.correct?,
answers: current_user_response.answers)
if ENV['DISABLE_USER_ANSWER'].present?
track('questionnaire_answer',
uid: content.id,
mod_uid: mod.id,
type: content.question_type,
success: current_user_response.correct?,
answers: current_user_response.answers)
else
track('questionnaire_answer',
uid: content.id,
mod_uid: mod.id,
type: content.assessments_type, # TODO: will be replaced with content.page_type
success: current_user_response.correct?,
answers: current_user_response.answers)
end
end
end
end
36 changes: 36 additions & 0 deletions app/models/data/confidence_check_scores.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Data
class ConfidenceCheckScores
include ToCsv

class << self
# @return [Array<String>]
def column_names
%w[
Module
Question
Answers
Count
]
end

# @return [Array<Hash{Symbol => Mixed}>]
def dashboard
confidence_check_scores.map do |(module_name, question_name, answers), count|
{
module_name: module_name,
question_name: question_name,
answers: answers,
count: count,
}
end
end

private

# @return [Hash{Array<String, String, String> => Integer}]
def confidence_check_scores
Response.confidence.group(:training_module, :question_name, :answers).count
end
end
end
end
16 changes: 12 additions & 4 deletions app/models/data/high_fail_questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ def dashboard

private

# @return [Hash{Array<String, String> => Integer}]
# @return [Hash{Array<String> => Integer}]
def question_attempts
UserAnswer.summative.group(:module, :name).count
if ENV['DISABLE_USER_ANSWER'].present?
Response.summative.group(:training_module, :question_name).count
else
UserAnswer.summative.group(:module, :name).count
end
end

# @return [Hash{Array<String, String> => Integer}]
# @return [Hash{Array<String> => Integer}]
def question_failures
UserAnswer.summative.where(correct: false).group(:module, :name).count
if ENV['DISABLE_USER_ANSWER'].present?
Response.summative.where(correct: false).group(:training_module, :question_name).count
else
UserAnswer.summative.where(correct: false).group(:module, :name).count
end
end

# @return [Integer]
Expand Down
3 changes: 3 additions & 0 deletions app/models/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Response < ApplicationRecord
validates :answers, presence: true

scope :unarchived, -> { where(archived: false) }
scope :formative, -> { where('schema->>4 = ?', 'formative') }
scope :summative, -> { where('schema->>4 = ?', 'summative') }
scope :confidence, -> { where('schema->>4 = ?', 'confidence') }

delegate :to_partial_path, :legend, to: :question

Expand Down
9 changes: 7 additions & 2 deletions app/models/training/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def incorrect_answers
incorrect_options.map(&:id)
end

# @return [Hash{Symbol => Array<Integer>}]
# @return [Hash{Symbol => Array<Hash, Integer>}]
def schema
{ correct: correct_answers, incorrect: incorrect_answers }
{ options: options.map(&:schema), correct: correct_answers, incorrect: incorrect_answers }
end

# @param disabled [Boolean]
Expand Down Expand Up @@ -91,6 +91,11 @@ class Option < Dry::Struct
alias_method :correct?, :correct
alias_method :checked?, :checked
alias_method :disabled?, :disabled

# @return [Hash{Symbol => nil, String}]
def schema
{ id: id, label: label }
end
end
end
end
11 changes: 10 additions & 1 deletion app/models/training/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def true_false?
answer.options.map(&:label).sort.eql? %w[False True]
end

# TODO: remove once user_answers is removed
# @return [String]
def assessments_type
{
Expand All @@ -66,9 +67,17 @@ def assessments_type
}.fetch(page_type.to_sym)
end

def question_type
{
formative_questionnaire: 'formative',
summative_questionnaire: 'summative',
confidence_questionnaire: 'confidence',
}.fetch(page_type.to_sym)
end

# @return [Array<String, Hash>]
def schema
[name, page_type, body, answer.schema]
[name, page_type, body, answer.schema, question_type]
end

# @return [String]
Expand Down
1 change: 1 addition & 0 deletions app/services/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Dashboard
{ model: 'User', folder: 'userdata', file: 'users' },
{ model: 'UserAnswer', folder: 'useranswers', file: 'user_answers' },
{ model: 'Response', folder: 'useranswers', file: 'responses' },
{ model: 'Data::ConfidenceCheckScores', folder: 'useranswers', file: 'confidence_check_scores' },
{ model: 'UserAssessment', folder: 'userassessments', file: 'user_assessments' },
{ model: 'Data::LocalAuthorityUser', folder: 'localauthorities', file: 'local_authority_users' },
{ model: 'Data::AveragePassScores', folder: 'summativequiz', file: 'average_pass_scores' },
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20230724160222_add_schema_to_responses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddSchemaToResponses < ActiveRecord::Migration[7.0]
def change
change_table :responses, bulk: true do |t|
t.jsonb :schema
end
end
end
1 change: 1 addition & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5e86650

Please sign in to comment.