Skip to content

Commit

Permalink
Everything works, just need to add few more test for new jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasdrga committed Jun 20, 2024
1 parent 7e08743 commit 809426c
Show file tree
Hide file tree
Showing 38 changed files with 403 additions and 341 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ ROLLBAR_ACCESS_TOKEN=
GOOGLE_APPLICATION_CREDENTIALS=
GOOGLE_SHEET_ID=
GOOGLE_SHEET_EXPORT_ID=
GOOGLE_SHEET_SCRIPT_URL=
19 changes: 19 additions & 0 deletions app/assets/javascripts/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,22 @@ $(document).on('ready turbolinks:load', function () {
$('[data-toggle="tooltip"]').tooltip();
$('.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();
}
});

document.addEventListener("turbolinks:before-render", function() {
window.printCalled = false;
});
});
8 changes: 4 additions & 4 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def publish
{
column_names: {"preparation" => "RF web príprava", "product" => "RF web produkt"},
page_type: @page.page_type,
published_value: "https://redflags.slovensko.digital/admin/pages/#{@page.id}"
published_value: %(=HYPERLINK("https://redflags.slovensko.digital/admin/pages/#{@page.id}"; "Admin link"))
}
]
UpdateMultipleSheetColumnsJob.perform_now(@page.id, updates)
ExportTopicIntoSheetJob.perform_now(new_revision)
UpdateMultipleSheetColumnsJob.perform_later(@page.id, updates)
ExportTopicIntoSheetJob.perform_later(new_revision)

redirect_back fallback_location: { action: :index }
end
Expand Down Expand Up @@ -98,7 +98,7 @@ def unpublish
published_value: ""
}
]
UpdateMultipleSheetColumnsJob.perform_now(@page.id, updates)
UpdateMultipleSheetColumnsJob.perform_later(@page.id, updates)

redirect_back fallback_location: { action: :index }
end
Expand Down
26 changes: 16 additions & 10 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class ProjectsController < ApplicationController
def show
@project = Project.find_by!(id: params[:project_id])
@revision_type = params[:revision_type]
revision_type_map = { 'hodnotenie-pripravy' => 'preparation', 'hodnotenie-produktu' => 'product' }
revision_type = revision_type_map[params[:revision_type]] || params[:revision_type]

revision_types = { 'preparation' => 0, 'product' => 1 }
page_type = revision_types[@revision_type]
page_type = revision_types[revision_type]

@project_revisions = @project.revisions.once_published.joins(revision: :page).where(pages: { page_type: page_type }).order(updated_at: :desc)
@project_revision = @project.published_revisions.joins(revision: :page).find_by(pages: { page_type: page_type })
Expand All @@ -19,10 +20,11 @@ def show

def pdf
@project = Project.find_by!(id: params[:project_id])
@revision_type = params[:revision_type]
revision_type_map = { 'hodnotenie-pripravy' => 'preparation', 'hodnotenie-produktu' => 'product' }
revision_type = revision_type_map[params[:revision_type]] || params[:revision_type]

revision_types = { 'preparation' => 0, 'product' => 1 }
page_type = revision_types[@revision_type]
page_type = revision_types[revision_type]

@project_revision = @project.published_revisions.joins(revision: :page).find_by(pages: { page_type: page_type })

Expand All @@ -41,15 +43,19 @@ def pdf
end

def show_history
@project = Project.find_by!(id: params[:id])
@project = Project.find_by!(id: params[:project_id])
@revision_type = params[:revision_type]

revision_types = { 'preparation' => 0, 'product' => 1 }
revision_types = { 'preparation' => 0, 'product' => 1, 'hodnotenie-pripravy' => 0, 'hodnotenie-produktu' => 1}
page_type = revision_types[@revision_type]

page_id = params[:page_id]
version = params[:version]

@project_revision = @project.revisions.once_published.joins(revision: :page).find_by(pages: { page_type: page_type, id: page_id })
@project = Project.find_by!(id: params[:project_id])
@project.revisions
@project_revision = @project.revisions.joins(revision: :page)
.once_published
.where('revisions.version = ? AND pages.page_type = ?', version, page_type).first

if @project_revision
@revision = @project_revision.revision
Expand All @@ -74,9 +80,9 @@ def index

case params[:sort]
when 'newest'
@projects = Project.all.sort_by { |project| project.published_revisions.maximum(:published_at) }.reverse
@projects = Project.all.select { |p| p.published_revisions.any? && p.published_revisions.last.published }.sort_by { |p| p.published_revisions.maximum(:published_at) || Time.at(0) }.reverse
when 'oldest'
@projects = Project.all.sort_by { |project| project.published_revisions.maximum(:published_at) }
@projects = Project.all.select { |p| p.published_revisions.any? && p.published_revisions.last.published }.sort_by { |p| p.published_revisions.maximum(:published_at) || Time.at(0) }
when 'alpha'
@projects = Project.joins(:published_revisions)
.select('DISTINCT ON (projects.id) projects.*, project_revisions.title AS rev_title')
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ def substitue_iframes(body)
end
doc.to_html
end

def map_page_type_to_route(page_type)
route_map = { 'preparation' => 'hodnotenie-pripravy', 'product' => 'hodnotenie-produktu' }
route_map[page_type] || page_type
end
end
118 changes: 87 additions & 31 deletions app/jobs/export_topic_into_sheet_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,43 @@ class ExportTopicIntoSheetJob < ApplicationJob
queue_as :default

COLUMN_NAMES = [
'Projekt', 'Projekt ID', 'ID hodnotenia', 'Typ hodnotenia', 'Názov', 'Popis', 'Náklady', 'Garant', 'Zhrnutie',
'Stanovisko S.D', 'Aktuálny stav', 'Čo sa deje', 'Reforma VS body', 'Reforma VS', 'Merateľné ciele (KPI) body',
'Merateľné ciele (KPI)', 'Postup dosiahnutia cielov body', 'Postup dosiahnutia cielov', 'Súlad s KRIS body',
'Súlad s KRIS', 'Biznis prínos body', 'Biznis prínos', 'Príspevok v informatizácií body', 'Príspevok v informatizácií',
'Štúdia uskutočniteľnosti body', 'Štúdia uskutočniteľnosti', 'Alternatívy body', 'Alternatívy', 'Kalkulácia efektívnosti body',
'Kalkulácia efektívnosti', 'Participácia na príprave projektu body', 'Participácia na príprave projektu', 'Súlad s požiadavkami body',
'Súlad s požiadavkami', 'Elektronické služby body', 'Elektronické služby', 'Identifikácia, autentifikácia, autorizácia (IAA) body',
'Identifikácia, autentifikácia, autorizácia (IAA)', 'Riadenie údajov body', 'Riadenie údajov', 'OpenData body', 'OpenData',
'MyData body', 'MyData', 'OpenApi body', 'OpenApi', 'Zdrojový kód body', 'Zdrojový kód'
'Projekt', 'Projekt ID', 'ID hodnotenia prípravy', 'Link na hodnotenie prípravy', 'ID hodnotenia produktu', 'Link na hodnotenie produktu',
'Názov-Príprava', 'Garant-Príprava', 'Stručný opis-Príprava', 'Náklady na projekt-Príprava', 'Aktuálny stav projektu-Príprava',
'Č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',
'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',
'Súlad s KRIT body-Produkt', 'Súlad s KRIT-Produkt', 'Biznis prínos body-Produkt', 'Biznis prínos-Produkt', 'Príspevok v informatizácii body-Produkt',
'Príspevok v informatizácii-Produkt', 'Kalkulácia efektívnosti body-Produkt', 'Kalkulácia efektívnosti-Produkt', 'Transparentnosť a participácia body-Produkt',
'Transparentnosť a participácia-Produkt', 'Súlad s požiadavkami body-Produkt', 'Súlad s požiadavkami-Produkt', 'Elektronické služby body-Produkt',
'Elektronické služby-Produkt', 'Identifikácia, autentifikácia, autorizácia (IAA) body-Produkt', 'Identifikácia, autentifikácia, autorizácia (IAA)-Produkt',
'Riadenie údajov body-Produkt', 'Riadenie údajov-Produkt', 'OpenData body-Produkt', 'OpenData-Produkt', 'MyData body-Produkt', 'MyData-Produkt',
'OpenAPI body-Produkt', 'OpenAPI-Produkt', 'Zdrojový kód body-Produkt', 'Zdrojový kód-Produkt'
].freeze

RATINGS_CONSTANT = {
"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 KRIT" => "Súlad s KRIT body",
"Biznis prínos" => "Biznis prínos body",
"Príspevok v informatizácii" => "Príspevok v informatizácii body",
"Kalkulácia efektívnosti" => "Kalkulácia efektívnosti body",
"Transparentnosť a participácia" => "Transparentnosť a participácia 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",
"Riadenie údajov" => "Riadenie údajov body",
"OpenData" => "OpenData body",
"MyData" => "MyData body",
"OpenAPI" => "OpenAPI body",
"Zdrojový kód" => "Zdrojový kód body"
}

def perform(new_revision)
if new_revision.published?
update_sheet(new_revision)
Expand All @@ -24,32 +50,51 @@ def perform(new_revision)
private

def update_sheet(new_revision)
suffix = new_revision.revision.page.page_type == 'preparation' ? '-Príprava' : '-Produkt'

result = extract_content_from_html(new_revision.body_html)
ratings = new_revision.revision.ratings.includes(:rating_type).index_by { |rating| rating.rating_type.name }

result["Projekt"] = new_revision.title
result["Projekt ID"] = new_revision.project_id
result["ID hodnotenia"] = new_revision.revision.page.id
result["Typ hodnotenia"] = new_revision.revision.page.page_type
result["Názov"] = new_revision.title
result["Popis"] = new_revision.description
result["Náklady"] = new_revision.budget
result["Garant"] = new_revision.guarantor
result["Zhrnutie"] = new_revision.summary
result["Stanovisko S.D"] = new_revision.recommendation
result["Aktuálny stav"] = new_revision.stage_id
result["Čo sa deje"] = new_revision.current_status
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["Čo sa práve deje"] = new_revision.current_status
result["Zhrnutie hodnotenia Red Flags"] = new_revision.summary
result["Stanovisko Slovensko.Digital"] = new_revision.recommendation

RATINGS_CONSTANT.each do |type_name, result_field_name|
result[result_field_name] = ratings[type_name]&.score || 'N/A'
end

result.transform_keys! { |k| k + suffix }

result["Reforma VS body"] = new_revision.revision.ratings.where(rating_type_id: 1).first&.score || 'N/A'
result["Merateľné ciele (KPI) body"] = new_revision.revision.ratings.where(rating_type_id: 2).first&.score || 'N/A'
result["Projekt"] = new_revision.title
result["Projekt ID"] = new_revision.project_id

if new_revision.revision.page.page_type == 'preparation'
result["ID hodnotenia prípravy"] = new_revision.revision.page.id
result["Link na hodnotenie prípravy"] = %(=HYPERLINK("https://redflags.slovensko.digital/admin/pages/#{new_revision.revision.page.id}"; "Hodnotenie v adminovi"))
else
result["ID hodnotenia produktu"] = new_revision.revision.page.id
result["Link na hodnotenie produktu"] = %(=HYPERLINK("https://redflags.slovensko.digital/admin/pages/#{new_revision.revision.page.id}"; "Hodnotenie v adminovi"))
end

sheets_service = GoogleApiService.get_sheets_service
response = sheets_service.get_spreadsheet_values(ENV['GOOGLE_SHEET_EXPORT_ID'], 'A:BA')
response = sheets_service.get_spreadsheet_values(ENV['GOOGLE_SHEET_EXPORT_ID'], 'A:CA')
header_row = response.values[2]
current_row_count = response.values.size
current_row_count = response.values.count
column_indices = COLUMN_NAMES.map { |name| header_row.index(name) }

values = COLUMN_NAMES.map { |name| result[name] || "-" }
update_google_sheet(sheets_service, ENV['GOOGLE_SHEET_EXPORT_ID'], column_indices, values, current_row_count)
values = COLUMN_NAMES.map { |name| result[name] }

if find_row_index_by_project_id(response.values[3..], header_row, new_revision.project_id)
range = "Hárok1!#{column_letter(column_indices.min + 1)}#{current_row_count}:#{column_letter(column_indices.max + 1)}#{current_row_count}"
else
range = "Hárok1!#{column_letter(column_indices.min + 1)}#{current_row_count + 1}:#{column_letter(column_indices.max + 1)}#{current_row_count + 1}"
end
update_google_sheet(sheets_service, ENV['GOOGLE_SHEET_EXPORT_ID'], column_indices, values, range)
end

def delete_row(new_revision)
Expand Down Expand Up @@ -77,12 +122,9 @@ def extract_content_from_html(body_html)
result
end

def update_google_sheet(sheets_service, google_sheet_id, column_indices, values, current_row_count)
values.each_with_index do |value, i|
range = "Hárok1!#{(column_indices[i] + 65).chr}#{current_row_count + 1}"
value_range_object = Google::Apis::SheetsV4::ValueRange.new(values: [[value]])
sheets_service.update_spreadsheet_value(google_sheet_id, range, value_range_object, value_input_option: 'USER_ENTERED')
end
def update_google_sheet(sheets_service, google_sheet_id, column_indices, values, range)
value_range_object = Google::Apis::SheetsV4::ValueRange.new(values: [values])
sheets_service.update_spreadsheet_value(google_sheet_id, range, value_range_object, value_input_option: 'USER_ENTERED')
end

def delete_google_sheet_row(sheets_service, google_sheet_id, row_index)
Expand Down Expand Up @@ -112,4 +154,18 @@ def find_row_index(rows, header_row, page_id)
id_index = header_row.index('ID hodnotenia')
rows.find_index { |row| row[id_index] == page_id.to_s }
end

def find_row_index_by_project_id(rows, header_row, project_id)
project_id_index = header_row.index('Projekt ID')
rows.find_index { |row| row[project_id_index] == project_id.to_s }
end

def column_letter(number)
letter = ''
while number > 0
number, remainder = (number - 1).divmod(26)
letter = (65 + remainder).chr + letter
end
letter
end
end
23 changes: 16 additions & 7 deletions app/jobs/initialization_of_topics_to_sheets_job.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
class InitializationOfTopicsToSheetsJob < ApplicationJob
queue_as :default

COLUMN_NAMES = ['Projekt', 'Platforma', 'Draft prípravy', 'ID prípravy', 'Príprava publikovaná?'].freeze
COLUMN_NAMES = ['Projekt', 'Projekt ID', 'Platforma', '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)

def perform(topic_id, found_page)
sheets_service = GoogleApiService.get_sheets_service
response_values = fetch_response_values(sheets_service)
column_indices = get_column_indices(response_values[2])

values = construct_values(topic_id, found_page)
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)
end

private

def fetch_response_values(sheets_service)
response = sheets_service.get_spreadsheet_values(ENV['GOOGLE_SHEET_ID'], 'A:ZZ')
response = sheets_service.get_spreadsheet_values(ENV['GOOGLE_SHEET_ID'], 'A:Z')
response.values
end

def get_column_indices(header_row)
COLUMN_NAMES.map { |name| header_row.index(name) }
end

def construct_values(topic_id, found_page)
def construct_values(topic_id, found_page, project)
title_parametrized = found_page.latest_revision.title.parameterize
[
found_page.latest_revision.title,
"https://platforma.slovensko.digital/t/#{title_parametrized}/#{topic_id}",
project.id,
%(=HYPERLINK("https://platforma.slovensko.digital/t/#{title_parametrized}/#{topic_id}"; "Platforma link")),
'',
'',
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.present? ? %(=HYPERLINK("https://redflags.slovensko.digital/admin/pages/#{found_page.id}"; "Admin link")) : ''
]
end

Expand Down
16 changes: 4 additions & 12 deletions app/jobs/sync_all_topics_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class SyncAllTopicsJob < ApplicationJob
queue_as :default

COLUMN_NAMES = ['Projekt', 'Projekt ID', 'Platforma', 'Draft prípravy', 'ID prípravy', 'Draft produktu', 'ID produktu'].freeze
COLUMN_NAMES = ['Projekt', 'Projekt ID', 'Platforma', 'ID draft prípravy', 'ID prípravy', 'ID draft produktu', 'ID produktu'].freeze

def perform
sheets_service = GoogleApiService.get_sheets_service
Expand All @@ -24,27 +24,19 @@ def process_row(row, indices)
project_name = row[indices["Projekt"]]
project_id = row[indices["Projekt ID"]]
platform_link = row[indices["Platforma"]]
preparation_document_id = extract_id_from_link(row[indices["Draft prípravy"]])
preparation_document_id = row[indices["ID draft prípravy"]]
preparation_page_id = row[indices["ID prípravy"]]
product_document_id = extract_id_from_link(row[indices["Draft produktu"]])
product_document_id = row[indices["ID draft produktu"]]
product_page_id = row[indices["ID produktu"]]

if valid_link?(platform_link)
if platform_link != ''
SyncTopicJob.perform_later(project_id, preparation_page_id)
else
enqueue_job_for_update("#{project_name} - Príprava", project_id, preparation_document_id, preparation_page_id, 0)
enqueue_job_for_update("#{project_name} - Produkt", project_id, product_document_id, product_page_id, 1)
end
end

def valid_link?(link)
link.present? && (link.start_with?('http://') || link.start_with?('https://'))
end

def extract_id_from_link(link)
link&.split('/')&.[](5)
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)
end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/sync_category_topics_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def perform(category_slug, page = 0, sync_topic_job: SyncTopicJob)
SyncCategoryTopicsJob.perform_later(category_slug, page + 1) if topics.count >= 30

topics.each do |topic|
sync_topic_job.perform_later(topic.fetch('id'))
sync_topic_job.perform_later(nil, topic.fetch('id'))
end
end
end
Loading

0 comments on commit 809426c

Please sign in to comment.