Skip to content

Commit

Permalink
Merge pull request #146 from slovensko-digital/SD-2693/Change-migrati…
Browse files Browse the repository at this point in the history
…ons-project-register

SD-2693/Change-migrations-project-register
  • Loading branch information
celuchmarek authored Jun 27, 2024
2 parents 3fb5c10 + b58d997 commit b92ae49
Show file tree
Hide file tree
Showing 25 changed files with 214 additions and 106 deletions.
22 changes: 7 additions & 15 deletions app/assets/javascripts/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ $(document).on('ready turbolinks:load', function () {
$('.tag-tooltip').tooltip();
});

$(document).ready(function(){
$('[data-toggle="collapse"]').on('click', function() {
$(this).find('.collapsed,.expanded').toggleClass('d-none');
});
});

document.addEventListener("turbolinks:load", function() {
document.addEventListener("turbolinks:load", function() {
if (!window.printCalled && window.location.pathname.endsWith("/pdf")) {
window.printCalled = true;
window.print();
}
});
if (!window.printCalled && window.location.pathname.endsWith("/pdf")) {
window.printCalled = true;
window.print();
}
});

document.addEventListener("turbolinks:before-render", function() {
window.printCalled = false;
});
document.addEventListener("turbolinks:before-render", function() {
window.printCalled = false;
});
12 changes: 12 additions & 0 deletions app/assets/stylesheets/projects.scss
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,15 @@ img.emoji {
height: 10em;
}
}

.accordion:after {
font-family: 'Merriweather';
font-size: $h3-font-size;
content: "-";
}

.accordion.collapsed:after {
font-family: 'Merriweather';
font-size: $h3-font-size;
content: "+";
}
20 changes: 9 additions & 11 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@ class Admin::PagesController < AdminController
before_action :load_page, only: [:show, :preview, :publish, :unpublish, :sync_one]

def index
@pages = Page.includes(phase: :project).order(id: :desc)
@projects = @pages.map { |page| page.phase.project }.uniq
@pages = Page.includes(phase: :project).order(created_at: :desc)
@projects = @pages.map { |page| page.phase&.project }.uniq
end

def show
@revisions = @page.revisions.order(version: :desc).page(params[:page])
@project = @page.phase.project
@project = @page.phase ? @page.phase.project : Project.find_by(id: @page.id)
end

def preview
@phase = @page.phase

if @project.present?
if @phase.present?
if params['version'] == 'latest'
@phase_revision = @phase.revisions.find_by!(revision: @page.latest_revision)
@phase_revision = @phase.revisions.find_by(revision: @page.latest_revision)
else
@phase_revision = @phase.revisions.find_by!(revision: @page.revisions.where(version: params['version']))
@phase_revision = @phase.revisions.find_by(revision: @page.revisions.find_by(version: params['version']))
end
@ratings_by_type = @phase_revision.ratings.index_by(&:rating_type)

@ratings_by_type = @phase.revisions.ratings.index_by(&:rating_type)
else
if params['version'] == 'latest'
@phase_revision = PhaseRevision.find_by(revision: @page.latest_revision)
@phase_revision = @page.latest_revision
else
@phase_revision = PhaseRevision.joins(:revision)
.find_by(revisions: { version: params['version'] })
@phase_revision = @page.revisions.find_by(version: params['version'])
end
end
end
Expand Down
12 changes: 9 additions & 3 deletions app/jobs/export_topic_into_sheet_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class ExportTopicIntoSheetJob < ApplicationJob
'Čo sa práve deje-Príprava', 'Zhrnutie hodnotenia Red Flags-Príprava', 'Stanovisko Slovensko.Digital-Príprava', 'Reforma VS body-Príprava',
'Reforma VS-Príprava', 'Merateľné ciele (KPI) body-Príprava', 'Merateľné ciele (KPI)-Príprava', 'Postup dosiahnutia cieľov body-Príprava',
'Postup dosiahnutia cieľov-Príprava', 'Súlad s KRIT body-Príprava', 'Súlad s KRIT-Príprava', 'Biznis prínos body-Príprava',
'Biznis prínos-Príprava', 'Príspevok v informatizácii body-Príprava', 'Príspevok v informatizácii-Príprava', 'Kalkulácia efektívnosti body-Príprava',
'Kalkulácia efektívnosti-Príprava', 'Transparentnosť a participácia body-Príprava', 'Transparentnosť a participácia-Príprava', 'Názov-Produkt',
'Biznis prínos-Príprava', 'Príspevok v informatizácii body-Príprava', 'Príspevok v informatizácii-Príprava', 'Štúdia uskutočniteľnosti body-Príprava',
'Štúdia uskutočniteľnosti-Príprava', 'Alternatívy body-Príprava', 'Alternatívy-Príprava', 'Kalkulácia efektívnosti body-Príprava',
'Kalkulácia efektívnosti-Príprava', 'Transparentnosť a participácia body-Príprava', 'Transparentnosť a participácia-Príprava',
'Participácia na príprave projektu body-Príprava', 'Participácia na príprave projektu-Príprava', 'Názov-Produkt',
'Garant-Produkt', 'Stručný opis-Produkt', 'Náklady na projekt-Produkt', 'Aktuálny stav projektu-Produkt', 'Čo sa práve deje-Produkt',
'Zhrnutie hodnotenia Red Flags-Produkt', 'Stanovisko Slovensko.Digital-Produkt', 'Reforma VS body-Produkt', 'Reforma VS-Produkt',
'Merateľné ciele (KPI) body-Produkt', 'Merateľné ciele (KPI)-Produkt', 'Postup dosiahnutia cieľov body-Produkt', 'Postup dosiahnutia cieľov-Produkt',
Expand All @@ -24,11 +26,15 @@ class ExportTopicIntoSheetJob < ApplicationJob
"Reforma VS" => "Reforma VS body",
"Merateľné ciele (KPI)" => "Merateľné ciele (KPI) body",
"Postup dosiahnutia cieľov" => "Postup dosiahnutia cieľov body",
"Súlad s KRIS" => "Súlad s KRIT body",
"Súlad s KRIT" => "Súlad s KRIT body",
"Biznis prínos" => "Biznis prínos body",
"Príspevok v informatizácii" => "Príspevok v informatizácii body",
"Štúdia uskutočniteľnosti" => "Štúdia uskutočniteľnosti body",
"Alternatívy" => "Alternatívy body",
"Kalkulácia efektívnosti" => "Kalkulácia efektívnosti body",
"Transparentnosť a participácia" => "Transparentnosť a participácia body",
"Participácia na príprave projektu" => "Participácia na príprave projektu body",
"Súlad s požiadavkami" => "Súlad s požiadavkami body",
"Elektronické služby" => "Elektronické služby body",
"Identifikácia, autentifikácia, autorizácia (IAA)" => "Identifikácia, autentifikácia, autorizácia (IAA) body",
Expand Down Expand Up @@ -59,7 +65,7 @@ def update_sheet(new_revision)
result["Garant"] = new_revision.guarantor
result["Stručný opis"] = new_revision.description
result["Náklady na projekt"] = new_revision.budget
result["Aktuálny stav projektu"] = new_revision.stage.name
result["Aktuálny stav projektu"] = new_revision.stage&.name || 'N/A'
result["Čo sa práve deje"] = new_revision.current_status
result["Zhrnutie hodnotenia Red Flags"] = new_revision.summary
result["Stanovisko Slovensko.Digital"] = new_revision.recommendation
Expand Down
51 changes: 44 additions & 7 deletions app/jobs/initialization_of_topics_to_sheets_job.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
class InitializationOfTopicsToSheetsJob < ApplicationJob
queue_as :default

COLUMN_NAMES = ['Projekt', 'Projekt ID', 'Platforma', 'Dátum poslednej aktualizácie', 'Draft prípravy', 'ID draft prípravy', 'ID prípravy', 'Príprava publikovaná?', 'Dátum publikácie prípravy', 'RF web príprava'].freeze
COLUMN_NAMES = ['Projekt', 'Projekt ID', 'Gestor', 'Platforma', 'Dátum poslednej aktualizácie', 'Draft prípravy', 'ID draft prípravy', 'ID prípravy', 'Príprava publikovaná?', 'Dátum publikácie prípravy', 'RF web príprava'].freeze

def perform(topic_id, found_page, project)
uri = URI(ENV['GOOGLE_SHEET_SCRIPT_URL'])
Net::HTTP.get(uri)
create_new_row

sheets_service = GoogleApiService.get_sheets_service

new_row_index = poll_for_new_row(sheets_service)

if new_row_index.nil?
Rails.logger.error("Failed to create a new row in Google Sheets.")
return
end

response_values = fetch_response_values(sheets_service)
column_indices = get_column_indices(response_values[2])

values = construct_values(topic_id, found_page, project)

update_sheet_cells(sheets_service, column_indices, response_values.count, values)

ExportTopicIntoSheetJob.set(wait: 15.seconds).perform_later(found_page.published_revision.phase_revision) if found_page.published_revision.present?
ExportTopicIntoSheetJob.set(wait: 30.seconds).perform_later(found_page.published_revision.phase_revision) if found_page.published_revision.present?
end

private

def create_new_row
uri = URI(ENV['GOOGLE_SHEET_SCRIPT_URL'])
Net::HTTP.get(uri)
end

def fetch_response_values(sheets_service)
response = sheets_service.get_spreadsheet_values(ENV['GOOGLE_SHEET_ID'], 'A:Z')
response.values
Expand All @@ -31,16 +43,22 @@ def get_column_indices(header_row)

def construct_values(topic_id, found_page, project)
title_parametrized = found_page.latest_revision.title.parameterize

phase_revision_title = found_page.latest_revision.phase_revision.nil? ?
nil : found_page.latest_revision.phase_revision.title
phase_revision_guarantor = found_page.latest_revision.phase_revision&.guarantor

[
found_page.latest_revision.title,
phase_revision_title || found_page.latest_revision.title,
project.id,
phase_revision_guarantor || '',
%(=HYPERLINK("https://platforma.slovensko.digital/t/#{title_parametrized}/#{topic_id}"; "Platforma link")),
found_page.published_revision&.updated_at&.in_time_zone('Europe/Bratislava')&.strftime('%H:%M %d.%m.%Y') || '',
found_page.published_revision&.created_at&.in_time_zone('Europe/Bratislava')&.strftime('%H:%M %d.%m.%Y') || '',
'',
'',
topic_id.to_s,
found_page.published_revision.present? ? 'Áno' : 'Nie',
found_page.published_revision&.updated_at&.in_time_zone('Europe/Bratislava')&.strftime('%H:%M %d.%m.%Y') || '',
found_page.published_revision&.created_at&.in_time_zone('Europe/Bratislava')&.strftime('%H:%M %d.%m.%Y') || '',
found_page.published_revision.present? ? %(=HYPERLINK("https://redflags.slovensko.digital/admin/pages/#{found_page.id}"; "Admin link")) : ''
]
end
Expand All @@ -52,4 +70,23 @@ def update_sheet_cells(sheets_service, indices, current_row_count, values)
sheets_service.update_spreadsheet_value(ENV['GOOGLE_SHEET_ID'], range, value_range_object, value_input_option: 'USER_ENTERED')
end
end

def poll_for_new_row(sheets_service, max_attempts = 10, sleep_duration = 3)
max_attempts.times do
response_values = fetch_response_values(sheets_service)
new_row_index = response_values.count

if initial_values_in_last_row?(response_values.last)
return new_row_index
end

sleep(sleep_duration)
end
nil
end

def initial_values_in_last_row?(last_row)
initial_values = ['Nie', 'Nie']
(last_row & initial_values).any?
end
end
9 changes: 8 additions & 1 deletion app/jobs/sync_all_topics_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def process_row(row, indices)
end

def enqueue_job_for_update(name, project_id, document_id, page_id, page_type)
SyncGoogleDocumentJob.perform_later(name, project_id, document_id, page_id, page_type)
unless has_template_name?(document_id)
SyncGoogleDocumentJob.perform_now(name, project_id, document_id, page_id, page_type)
end
end

def has_template_name?(document_id)
document_name = GoogleApiService.get_document(document_id)&.title
document_name == 'Kópia dokumentu RF-priprava-template' || document_name == 'Kópia dokumentu RF-produkt-template'
end
end
1 change: 1 addition & 0 deletions app/jobs/sync_google_document_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def create_or_update_page(project_name, project_id, google_document_id, page_id,
phase_type = PhaseType.find_by(name: phase_type_name)
new_phase = Phase.find_or_create_by(project: new_project, phase_type: phase_type)
new_page.phase = new_phase
new_page.save!
end

drive_service = GoogleApiService.get_drive_service
Expand Down
21 changes: 14 additions & 7 deletions app/jobs/sync_one_topic_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@ def process_row(row, indices, target_id)
product_document_id = row[indices["ID draft produktu"]]
product_page_id = row[indices["ID produktu"]]

if platform_link != ''
SyncTopicJob.perform_later(project_id, preparation_page_id)
else
if target_id == preparation_page_id.to_i
if target_id == preparation_page_id.to_i
if platform_link != ''
SyncTopicJob.perform_now(project_id, preparation_page_id)
else
enqueue_job_for_update("#{project_name} - Príprava", project_id, preparation_document_id, preparation_page_id, 'Prípravná fáza')
else target_id == product_page_id.to_i
enqueue_job_for_update("#{project_name} - Produkt", project_id, product_document_id, product_page_id, 'Fáza produkt')
end
else target_id == product_page_id.to_i
enqueue_job_for_update("#{project_name} - Produkt", project_id, product_document_id, product_page_id, 'Fáza produkt')
end
end

def enqueue_job_for_update(name, project_id, document_id, page_id, page_type)
SyncGoogleDocumentJob.perform_later(name, project_id, document_id, page_id, page_type)
unless has_template_name?(document_id)
SyncGoogleDocumentJob.perform_now(name, project_id, document_id, page_id, page_type)
end
end

def has_template_name?(document_id)
document_name = GoogleApiService.get_document(document_id)&.title
document_name == 'Kópia dokumentu RF-priprava-template' || document_name == 'Kópia dokumentu RF-produkt-template'
end
end
7 changes: 5 additions & 2 deletions app/jobs/sync_topic_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ def perform(project_id, topic_id)

Page.transaction do
page = Page.find_or_create_by!(id: topic_id) do |new_page|
project = Project.find_or_create_by(id: project_id)
project = Project.find_or_create_by(id: new_page.latest_revision&.phase_revision&.phase&.project&.id || project_id)
phase_type = PhaseType.find_by(name: 'Prípravná fáza')
new_phase = Phase.find_or_create_by(project: project, phase_type: phase_type)
new_page.phase = new_phase
new_page.save!
end

project ||= Project.find_by(id: page.latest_revision&.phase_revision&.phase&.project&.id || project_id || page.id)

version = topic['post_stream']['posts'].first['version']

revision = page.revisions.find_or_initialize_by(version: version)
Expand All @@ -28,6 +31,6 @@ def perform(project_id, topic_id)
end

# For initial import of current topics into Google Sheets
#InitializationOfTopicsToSheetsJob.set(wait: 15.seconds).perform_later(topic_id, page, project)
InitializationOfTopicsToSheetsJob.set(wait: 30.seconds).perform_later(topic_id, page, project) unless project.nil?
end
end
17 changes: 11 additions & 6 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

class Page < ApplicationRecord
belongs_to :phase
belongs_to :phase, optional: true

has_many :revisions

Expand Down Expand Up @@ -59,6 +59,11 @@ def update_associated_phase_revision(revision)

def build_publish_updates(revision)
[
{
column_names: { "project" => "Dátum poslednej aktualizácie" },
page_type: "project",
published_value: revision.published_at.in_time_zone('Europe/Bratislava').strftime('%H:%M %d.%m.%Y')
},
{
column_names: { "Prípravná fáza" => "Príprava publikovaná?", "Fáza produkt" => "Produkt publikovaný?" },
page_type: revision.phase.phase_type.name,
Expand All @@ -69,11 +74,6 @@ def build_publish_updates(revision)
page_type: revision.phase.phase_type.name,
published_value: revision.published_at.in_time_zone('Europe/Bratislava').strftime('%H:%M %d.%m.%Y')
},
{
column_names: { "project" => "Dátum poslednej aktualizácie" },
page_type: "project",
published_value: revision.published_at.in_time_zone('Europe/Bratislava').strftime('%H:%M %d.%m.%Y')
},
{
column_names: { "Prípravná fáza" => "RF web príprava", "Fáza produkt" => "RF web produkt" },
page_type: revision.phase.phase_type.name,
Expand All @@ -91,6 +91,11 @@ def unpublish_and_enqueue_jobs(revision_id)

def build_unpublish_updates
[
{
column_names: { "project" => "Dátum poslednej aktualizácie" },
page_type: "project",
published_value: Time.now.in_time_zone('Europe/Bratislava').strftime('%H:%M %d.%m.%Y')
},
{
column_names: { "Prípravná fáza" => "Príprava publikovaná?", "Fáza produkt" => "Produkt publikovaný?" },
page_type: phase.phase_type.name,
Expand Down
Loading

0 comments on commit b92ae49

Please sign in to comment.