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

Ml klok import into fulfillment pt 2 #215

Merged
merged 9 commits into from
Oct 14, 2016
30 changes: 27 additions & 3 deletions app/models/klok/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ def end_time_stamp=(value)
end

def decimal_duration
minutes = duration/60000
minutes = duration/60000.0
minutes/60.0
end

def local_protocol_includes_service service
local_protocol.organization.inclusive_child_services(:one_time_fee, false).include? service
end

#### Error Msgs

def klok_project_present
unless self.klok_project.present?
self.errors[:base] << 'klok project not present'
Expand All @@ -61,7 +67,7 @@ def klok_project_ssr_id
end

def klok_project_ssr_id_regex_error
unless self.klok_project.ssr_id.match(/\d\d\d\d-\d\d\d\d/)
unless ( /\d\d\d\d-\d\d\d\d/ === self.klok_project.ssr_id )
self.errors[:base] << 'improper format - correct format is 1234-0001'
end
end
Expand All @@ -72,12 +78,26 @@ def local_project_error
end
end

def service_id_not_ssr_id
unless ( /\A\d+\z/ === self.klok_project.code )
self.errors[:base] << 'must have service id, not ssr id'
end
end

def service_error
unless self.service.present?
self.errors[:base] << 'no service present'
end
end

def service_not_available_to_protocol_error
if self.local_protocol && self.service
unless self.local_protocol_includes_service(self.service)
self.errors[:base] << 'service not available to protocol'
end
end
end

def klok_person_error
unless self.klok_person.present?
self.errors[:base] << 'no klok person present'
Expand All @@ -95,7 +115,9 @@ def error_messages
klok_project_ssr_id
klok_project_ssr_id_regex_error
local_project_error
service_id_not_ssr_id
service_error
service_not_available_to_protocol_error
klok_person_error
local_identity_error
return self.errors[:base]
Expand All @@ -104,9 +126,11 @@ def error_messages
def is_valid?
self.klok_project.present? &&
self.klok_project.ssr_id &&
self.klok_project.ssr_id.match(/\d\d\d\d-\d\d\d\d/) &&
( /\d\d\d\d-\d\d\d\d/ === self.klok_project.ssr_id ) && #### validate we have a valid SSR id (comes from parent project)
self.local_protocol.present? &&
( /\A\d+\z/ === self.klok_project.code ) && #### validate we actually have a service id and not a SSR id
self.service.present? &&
self.local_protocol_includes_service(self.service) &&
self.klok_person.present? &&
self.local_identity.present?
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/klok/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Klok::Project < ActiveRecord::Base
include KlokShard

self.primary_key = 'project_id'

has_many :klok_entries, class_name: 'Klok::Entry', foreign_key: :project_id
has_many :klok_people, class_name: 'Klok::Person', foreign_key: :resource_id, through: :klok_entries
belongs_to :parent_project, class_name: 'Klok::Project', foreign_key: :parent_id
Expand Down
13 changes: 8 additions & 5 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Organization < ActiveRecord::Base

has_many :services,
-> {where(is_available: true)}

has_many :all_services, class_name: "Service" # include services regardless of is_available

has_many :sub_service_requests
has_many :protocols, through: :sub_service_requests
has_many :pricing_setups
Expand Down Expand Up @@ -54,10 +57,10 @@ def effective_pricing_setup_for_date(date=Date.today)
return pricing_setup
end

def inclusive_child_services(scope)
services.
def inclusive_child_services(scope, is_available=true)
(is_available ? services : all_services).
send(scope).
push(all_child_services(scope)).
push(all_child_services(scope, is_available)).
flatten.
sort_by(&:name)
end
Expand Down Expand Up @@ -88,7 +91,7 @@ def all_child_organizations_with_non_process_ssrs
].flatten
end

def all_child_services(scope)
all_child_organizations_with_non_process_ssrs.map { |child| child.services.send(scope) }
def all_child_services(scope, is_available=true)
all_child_organizations_with_non_process_ssrs.map { |child| child.send(is_available ? :services : :all_services).send(scope) }
end
end
78 changes: 48 additions & 30 deletions lib/tasks/import_klok_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,68 @@ task import_klok: :environment do
STDIN.gets.strip
end

if prompt("Would you like to refresh the KlokShard with data found in tmp/klok.xml? (Y/N) ") == 'Y'
begin
Klok::Entry.destroy_all
Klok::Project.destroy_all
Klok::Person.destroy_all
CSV.open("tmp/klok_import_#{Time.now.strftime('%m%d%Y')}.csv", "wb") do |csv|
dup_entry_header = true
if prompt("Would you like to refresh the KlokShard with data found in tmp/klok.xml? (Y/N) ") == 'Y'
puts "Data refresh initiated"
begin
Klok::Entry.destroy_all
Klok::Project.destroy_all
Klok::Person.destroy_all

d = File.read(Rails.root.join('tmp/klok.xml'))

h = Hash.from_xml(d)

h['report']['people']['person'].each do |person|
d = Klok::Person.new
d.attributes = person.reject{|k,v| !d.attributes.keys.member?(k.to_s)}
d.save
end

d = File.read(Rails.root.join('tmp/klok.xml'))
h['report']['projects']['project'].each do |project|
d = Klok::Project.new
d.attributes = project.reject{|k,v| !d.attributes.keys.member?(k.to_s)}
d.save
end

h = Hash.from_xml(d)
h['report']['entries']['entry'].each do |entry|

h['report']['people']['person'].each do |person|
d = Klok::Person.new
d.attributes = person.reject{|k,v| !d.attributes.keys.member?(k.to_s)}
d.save
end
if entry['enabled'] == 'false' # only solution for duplicate entries with same entry_id
if dup_entry_header
csv << ['', 'Duplicate entries'] + entry.keys
dup_entry_header = false
end

h['report']['projects']['project'].each do |project|
d = Klok::Project.new
d.attributes = project.reject{|k,v| !d.attributes.keys.member?(k.to_s)}
d.save
end
csv << ["N/A", "Duplicate entry"] + entry.values
next
end

h['report']['entries']['entry'].each do |entry|
next if entry['enabled'] == 'false' # only solution for duplicate entries with same entry_id
d = Klok::Entry.new
d.attributes = entry.reject{|k,v| !d.attributes.keys.member?(k.to_s)}
d.save
end

d = Klok::Entry.new
d.attributes = entry.reject{|k,v| !d.attributes.keys.member?(k.to_s)}
d.save
rescue Exception => e
puts e.inspect
puts e.backtrace.inspect
end
rescue Exception => e
puts e.inspect
puts e.backtrace.inspect
else
puts "### Data NOT refreshed from tmp/klok.xml ###"
end
end

####### now that we have populated the KlokShard we can bring the same data in as line items ########
####### now that we have populated the KlokShard we can bring the same data in as line items ########

CSV.open("tmp/klok_import_#{Time.now.strftime('%m%d%Y')}.csv", "wb") do |csv|
csv << ['']
csv << ["ssr_id", "reason", "created_at", "project_id", "resource_id", "rate", "date", "start_time_stamp_formatted",
"start_time_stamp", "entry_id", "duration", "submission_id", "device_id", "comments", "end_time_stamp_formatted",
"end_time_stamp", "rollup_to"
]

puts "Populating data from KlokShard"

Klok::Entry.all.each do |entry|

if entry.is_valid?

local_protocol = entry.local_protocol
Expand All @@ -95,12 +113,12 @@ task import_klok: :environment do

if fulfillment.valid?
fulfillment.save
csv << ["SRID: #{fulfillment.protocol.srid}"] + ["Success (Fulfillment ID: #{fulfillment.id})"] + entry.attributes.values
csv << ["SRID: #{fulfillment.protocol.srid}", "Success (Fulfillment ID: #{fulfillment.id})"] + entry.attributes.values
else
csv << [fulfillment.errors.messages.to_s] + entry.attributes.values
end
else
csv << ["N/A"] + ["Entry not valid - Reasoning: #{entry.error_messages.to_sentence}"] + entry.attributes.values
csv << ["N/A", "Entry not valid - Reasoning: #{entry.error_messages.to_sentence}"] + entry.attributes.values
end
end
end
Expand Down