Skip to content
Draft
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
2 changes: 1 addition & 1 deletion app/controllers/setup/jobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Setup::JobsController < PrivateController
class JobsSummary < Struct.new(:scheduled_jobs, :last_jobs); end

def index
pyramid = current_project.project_anchor.nearest_pyramid_for(Date.new)
pyramid = current_project.project_anchor.nearest_pyramid_for(Time.zone.now)

@jobs = JobsSummary.new(
Jobs::ScheduledJobsService.new(current_project, pyramid).jobs,
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/setup/project_rules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ def index
project = current_project(
raise_if_published: false
)
# TODO: Check with Stephan, changed Date.new to Time.zone.now
data_compound = project.project_anchor
.nearest_data_compound_for(Date.new) || DataCompound.from(project)
.nearest_data_compound_for(Time.zone.now) || DataCompound.from(project)
@orbf_project = MapProjectToOrbfProject.new(
project,
data_compound.indicators,
Expand Down
16 changes: 16 additions & 0 deletions app/models/dhis2_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ class Dhis2Snapshot < ApplicationRecord
indicators
category_combos].freeze

KINDS.each do |kind|
scope kind, -> { where(kind: kind) }
end

scope :with_snapshotted_at_lt_eq, -> (date) {
end_of_month = Arel.sql("(to_date(concat(year, month),'YYYYMM') + interval '1 month' - interval '1 day')")
order_by = Arel.sql("'#{date}' - #{end_of_month}")
where(end_of_month.lteq(date)).order(order_by.asc)
}

scope :with_snapshotted_at_gt, -> (date) {
end_of_month = Arel.sql("(to_date(concat(year, month),'YYYYMM') + interval '1 month' - interval '1 day')")
order_by = Arel.sql("#{end_of_month} - '#{date}'")
where(end_of_month.gt(date)).order(order_by.asc)
}

has_many :dhis2_snapshot_changes, dependent: :destroy

attr_accessor :disable_tracking
Expand Down
51 changes: 16 additions & 35 deletions app/models/project_anchor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ def latest_draft

def nearest_pyramid_snapshot_for(date)
kinds = %i[organisation_units organisation_unit_groups organisation_unit_group_sets]
pyramid_snapshots = dhis2_snapshots.select("id, year, month, kind").where(kind: kinds)

candidates = pyramid_snapshots.sort_by { |snap| [snap.kind, [snap.year, snap.month].join("-")] }
pyramid_snapshots = dhis2_snapshots.where(kind: kinds)

final_candidates = kinds.map do |kind|
kind_method = "kind_#{kind}?".to_sym
[kind, nearest(candidates.select(&kind_method), date)]
[kind, nearest(pyramid_snapshots.public_send(kind), date)]
end.to_h

final_snapshots = final_candidates.map { |kind, candidate| [kind, candidate ? dhis2_snapshots.find(candidate.id) : nil] }
Expand All @@ -65,16 +62,16 @@ def nearest_pyramid_for(date)
new_pyramid(final_snapshots)
end

def latest_data_compound()
def latest_data_compound()
kinds = %i[data_elements data_element_groups indicators category_combos]
pyramid_snapshots = dhis2_snapshots.select("id, year, month, kind").where(kind: kinds)
pyramid_snapshots = dhis2_snapshots.where(kind: kinds)

candidates = pyramid_snapshots.sort_by { |snap| [snap.kind, [snap.year, snap.month].join("-")] }
candidates = pyramid_snapshots

data_elements = latest_candidates(candidates.select(&:kind_data_elements?))
data_element_groups = latest_candidates(candidates.select(&:kind_data_element_groups?))
indicators = latest_candidates(candidates.select(&:kind_indicators?))
category_combos = latest_candidates(candidates.select(&:kind_category_combos?))
data_elements = candidates.data_elements.last
data_element_groups = candidates.data_element_groups.last
indicators = candidates.indicators.last
category_combos = candidates.category_combos.last

return nil unless data_elements || data_element_groups || indicators

Expand All @@ -90,14 +87,12 @@ def latest_data_compound()

def nearest_data_compound_for(date)
kinds = %i[data_elements data_element_groups indicators category_combos]
pyramid_snapshots = dhis2_snapshots.select("id, year, month, kind").where(kind: kinds)

candidates = pyramid_snapshots.sort_by { |snap| [snap.kind, [snap.year, snap.month].join("-")] }
pyramid_snapshots = dhis2_snapshots.where(kind: kinds)

data_elements = nearest(candidates.select(&:kind_data_elements?), date)
data_element_groups = nearest(candidates.select(&:kind_data_element_groups?), date)
indicators = nearest(candidates.select(&:kind_indicators?), date)
category_combos = nearest(candidates.select(&:kind_category_combos?), date)
data_elements = nearest(pyramid_snapshots.data_elements, date)
data_element_groups = nearest(pyramid_snapshots.data_element_groups, date)
indicators = nearest(pyramid_snapshots.indicators, date)
category_combos = nearest(pyramid_snapshots.category_combos, date)

return nil unless data_elements || data_element_groups || indicators

Expand All @@ -109,28 +104,15 @@ def nearest_data_compound_for(date)

new_data_compound(data_elements, data_element_groups, indicators, category_combos)
end


def latest_candidates(snapshots)

sorted = snapshots.sort_by { |snap| [snap.kind, [snap.year, snap.month.to_s.ljust(2,"0")].join("-")] }
sorted[-1]
end

def nearest(snapshots, date)
# there should be a better way
time = date.to_time
past_candidates = snapshots.select { |snapshot| snapshot.snapshoted_at <= date }
.sort_by { |snapshot| time - snapshot.snapshoted_at.to_time }

past_candidates = snapshots.with_snapshotted_at_lt_eq(date)
past_candidate = past_candidates.first
return past_candidate if past_candidate

futur_candidates = snapshots.select { |snapshot| snapshot.snapshoted_at > date }
.sort_by do |snapshot|
snapshot.snapshoted_at.to_time - time
end

futur_candidates = snapshots.with_snapshotted_at_gt(date)
futur_candidates.first
end

Expand Down Expand Up @@ -204,4 +186,3 @@ def update_token_if_needed(force_refresh: false)
token
end
end