Skip to content
Browse files

caching working well now

  • Loading branch information...
1 parent 6d8feb3 commit 3da54658b7542c52ea1644095eb50d9639dbc32b Siddharth committed Sep 25, 2011
View
6 app/controllers/browse.rb
@@ -6,14 +6,18 @@ class Browse < Application
Line = Struct.new(:ip, :date_time, :method, :model, :url, :status, :response_time)
def index
- debugger
+ render
+ end
+
+ def today
@date = params[:date] ? Date.parse(params[:date]) : Date.today
@caches = BranchCache.all(:date => @date)
@branch_data = @caches.map{|c| [c.model_id, c]}.to_hash
@branch_names = Branch.all.aggregate(:name, :id).to_hash
display [@branch_data, @branch_names], @template
end
+
def branches
redirect resource(:branches)
end
View
4 app/controllers/dashboard.rb
@@ -9,11 +9,13 @@ def index
render
end
- def today
+ def old_today
@date = params[:date].blank? ? Date.today : Date.parse(params[:date])
render
end
+
+
def branch
case params[:id]
when "centers"
View
2 app/models/bookmark.rb
@@ -8,7 +8,7 @@ class Bookmark
property :title, String, :length => 100, :nullable => false
property :route, Text, :nullable => false
property :type, Enum.send('[]', *Types), :nullable => false, :default => :system, :index => true
- property :method, Enum.send('[]', *Methods), :nullable => false, :default => :get, :index => true
+# property :method, Enum.send('[]', *Methods), :nullable => false, :default => :get, :index => true
property :params, Text, :nullable => true
property :user_id, Integer, :nullable => false, :index => true
property :share_with, Flag.send('[]', *User::ROLES), :nullable => false, :default => :none, :index => true
View
6 app/models/cacher.rb
@@ -73,7 +73,7 @@ def self.get_stale(what)
# get the absolutely lowest last update time
last_updated_at = cacher_update_times.map{|x| x[1]}.min
# get all payments after this time and return an array [[model_id, created_at]....]
- models_with_payment_update = Payment.all(:created_at.gt => last_updated_at, "#{what}_id".to_sym => model_ids).aggregate("#{what}_id".to_sym, :created_at.max).to_hash
+ models_with_payment_update = Payment.all(:created_at.gt => last_updated_at, "c_#{what}_id".to_sym => model_ids).aggregate("c_#{what}_id".to_sym, :created_at.max).to_hash
# then check created_at.max against each cacher's updated_at
models_with_payment_update = models_with_payment_update.select{|k,v| cacher_update_times[k] < v}
@@ -126,8 +126,8 @@ def consolidate (other)
def + (other)
# this adds all attributes and uses the latest date to add two cachers together
return self if other.nil?
- raise ArgumentError "cannot add cacher to something that is not a cacher" unless other.is_a? Cacher
- raise ArgumentError "cannot add cachers of different classes" unless self.class == other.class
+ raise ArgumentError.new("cannot add cacher to something that is not a cacher") unless other.is_a? Cacher
+# raise ArgumentError "cannot add cachers of different classes" unless self.class == other.class
date = (self.date > other.date ? self.date : other.date)
me = self.attributes; other = other.attributes;
attrs = me + other; attrs[:date] = date; attrs[:id] = -1; attrs[:model_name] = "Sum";
View
36 app/models/loan.rb
@@ -1022,7 +1022,7 @@ def update_history_caller
def update_history(forced=false)
t = Time.now
reload
- puts "RELOAD: #{Time.now - t} secs"
+ Merb.logger.info "RELOAD: #{Time.now - t} secs"
extend_loan
return true if Mfi.first.dirty_queue_enabled and DirtyLoan.add(self) and not forced
return if @already_updated and not forced
@@ -1037,6 +1037,12 @@ def update_history(forced=false)
Merb.logger.info "LOAN CACHE UPDATE TIME: #{(Time.now - t).round(4)} secs"
end
+ def update_history_only
+ t = Time.now
+ update_history_bulk_insert
+ Merb.logger.info "HISTORY EXEC TIME: #{(Time.now - t).round(4)} secs"
+ end
+
def loan_outstanding_on?(date)
[:disbursed, :outstanding].include?(get_status(date))
end
@@ -1069,9 +1075,9 @@ def calculate_history
int = interest_received_on(date).round(2)
total_interest_paid += int
st = get_status(date)
- scheduled_principal_due = i_num > 0 ? scheduled_principal_for_installment(i_num) : 0
- scheduled_interest_due = i_num > 0 ? scheduled_interest_for_installment(i_num) : 0
- outstanding = loan_outstanding_on?(date)
+ scheduled_principal_due = i_num > 0 ? scheduled[:principal] : 0
+ scheduled_interest_due = i_num > 0 ? scheduled[:interest] : 0
+ outstanding = [:disbursed, :outstanding].include?(st)
principal_due = outstanding ? actual[:balance].round(2) - scheduled[:balance].round(2) : 0
interest_due = outstanding ? actual[:total_balance].round(2) - scheduled[:total_balance].round(2) - (actual[:balance].round(2) - scheduled[:balance].round(2)) : 0
total_principal_due += scheduled[:principal].round(2)
@@ -1084,8 +1090,13 @@ def calculate_history
fees_due_today = ap_fees[date] || 0
fees_paid_today = fee_payments[date] || 0
- funder_id = funding_line.funder.id
-
+ last_loan_history = @loan_history.last || nil
+
+ principal_in_default = (date <= Date.today) ? [0,total_principal_paid.round(2) - total_principal_due.round(2)].min : 0
+ interest_in_default = (date <= Date.today) ? [0,total_interest_paid.round(2) - total_interest_due.round(2)].min : 0
+
+ days_overdue = ((principal_in_default > 0 or interest_in_default > 0) and last_loan_history) ? last_loan_history[:days_overdue] + (date - last_loan_history[:date]) : 0
+
@history_array << {
:loan_id => self.id,
:date => date,
@@ -1095,8 +1106,8 @@ def calculate_history
:actual_outstanding_principal => outstanding ? actual[:balance].round(2) : 0,
:actual_outstanding_total => outstanding ? actual[:total_balance].round(2) : 0,
:amount_in_default => actual[:balance].round(2) - scheduled[:balance].round(2),
- :principal_in_default => date <= Date.today ? [0,total_principal_paid.round(2) - total_principal_due.round(2)].min : 0,
- :interest_in_default => date <= Date.today ? [0,total_interest_paid.round(2) - total_interest_due.round(2)].min : 0,
+ :principal_in_default => principal_in_default,
+ :interest_in_default => interest_in_default,
:scheduled_principal_due => scheduled_principal_due,
:scheduled_interest_due => scheduled_interest_due,
:principal_due => principal_due.round(2),
@@ -1113,8 +1124,8 @@ def calculate_history
:advance_principal_paid_today => (appt = @history_array.last ? [0,advance_principal_paid - (@history_array.last[:advance_principal_paid] || 0)].max : 0),
:advance_interest_paid_today => (aipt = @history_array.last ? [0,advance_interest_paid - (@history_array.last[:advance_interest_paid] || 0)].max : 0),
:total_advance_paid_today => appt + aipt,
- :advance_principal_adjusted => @history_array.last ? [0,@history_array.last[:advance_principal_paid] - advance_principal_paid].max : 0,
- :advance_interest_adjusted => @history_array.last ? [0,@history_array.last[:advance_interest_paid] - advance_interest_paid].max : 0,
+ :advance_principal_adjusted => last_loan_history ? [0,last_loan_history[:advance_principal_paid] - advance_principal_paid].max : 0,
+ :advance_interest_adjusted => last_loan_history ? [0,last_loan_history[:advance_interest_paid] - advance_interest_paid].max : 0,
:total_fees_due => total_fees_due,
:total_fees_paid => total_fees_paid,
:fees_due_today => fees_due_today,
@@ -1124,8 +1135,8 @@ def calculate_history
:center_id => c_center_id,
:created_at => now,
:funding_line_id => funding_line_id,
- :funder_id => funder_id,
- :loan_product_id => loan_product_id
+ :loan_product_id => loan_product_id,
+ :days_overdue => days_overdue
}
end
@@ -1165,6 +1176,7 @@ def update_history_bulk_insert
t = Time.now
Merb.logger.error! "could not destroy the history" unless self.loan_history.destroy!
sql = get_bulk_insert_sql("loan_history",calculate_history)
+ t = Time.now
repository.adapter.execute(sql)
Merb.logger.info "update_history_bulk_insert done in #{Time.now - t}"
return true
View
10 app/models/payment.rb
@@ -24,16 +24,14 @@ class Payment
property :verified_by_user_id, Integer, :nullable => true, :index => true
property :loan_id, Integer, :nullable => true, :index => true
property :client_id, Integer, :nullable => true, :index => true
- property :center_id, Integer, :nullable => true, :index => true
- property :branch_id, Integer, :nullable => false, :index => true
+ property :c_center_id, Integer, :nullable => true, :index => true
+ property :c_branch_id, Integer, :nullable => false, :index => true
property :fee_id, Integer, :nullable => true, :index => true
property :desktop_id, Integer
property :origin, String, :default => DEFAULT_ORIGIN
belongs_to :loan, :nullable => true
belongs_to :client
- belongs_to :center, :nullable => true
- belongs_to :branch, :nullable => true
belongs_to :fee
belongs_to :created_by, :child_key => [:created_by_user_id], :model => 'User'
belongs_to :received_by, :child_key => [:received_by_staff_id], :model => 'StaffMember'
@@ -56,8 +54,8 @@ class Payment
validates_with_method :verified_by_user_id, :method => :verified_cannot_be_deleted, :if => Proc.new{|p| p.deleted_at != nil and p.deleted_by!=nil}
def add_center_and_branch
- self.center = self.loan.client.center
- self.branch = self.loan.client.center.branch
+ self.c_center_id = self.client.center.id
+ self.c_branch_id = self.client.center.branch.id
end
def self.from_csv(row, headers, loans)
View
121 app/views/browse/index.html.haml
@@ -1,92 +1,31 @@
-= js_include_tag 'Jit/jit-yc'
-= js_include_tag :treemap, :hbarchart
+%div.right_pane
+ %div.result
+ %script
+ $.get("/browse/hq_tab", function(data){$('.result').html(data);})
+ = random_chart
+ %br
-= css_include_tag 'jit-base','treemap'
-:javascript
- var date = new Date('#{@date.strftime("%Y-%m-%d")}')
- var date_string = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
- function get_charts(q) {
- $.get('/graph_data/tm_collections?' + q,'', function(data){
- init_barchart($.parseJSON(data).pmts_barchart);
- init_treemap($.parseJSON(data).treemap);
- });
- }
-
- $(document).ready(function(){ get_charts('date=' + date_string)});
-
-- debugger
-- bnames = @branch_names.keys.sort
-%script{:type => "text/javascript"}
- == names=['#{bnames.join("','")}']
- == values = ['#{@branch_names.values.join("','")}']
-#stats
- - total = @caches.reduce(nil){|s,h| h + s}
- %table#menu{:style => "float:left; position: relative; left: 1px;"}
- %tr.selected
- %th
- %h1
- today
- %td.number
- %h1.green
- = total.fees_paid_today + total.principal_paid + total.interest_paid
- %p collected
- %td.number
- %h1
- = total.total_fees_due + total.principal_due + total.interest_due
- %p due
- %td.number
- %h1.red
- 0
- %p
- missed
-
-
- .detail
- #today
- %table.new-report{:style => "margin: auto; width: 90%"}
- %thead
- %tr
- %th.purple
- - ["adv", "prin", "int", "fees","default","total"].each do |k|
- %th
- = k
- %tr
- %th
- Paid
- - t = 0
- - keys = [:total_advance_paid, :principal_paid, :interest_paid, :fees_paid_today, :noop]
- - keys.map do |k|
- %td.green
- - value = total.send(k) || 0
- - t += value
- = value.to_currency.split(".")[0]
- %th
- = t.to_currency.split(".")[0]
- %tr
- %th
- Due
- - t = 0
- - keys = [:noop, :principal_due, :interest_due, :fees_due_today, :total_default]
- - keys.map do |k|
- %td.red
- - value = total.send(k) || 0
- - t += value
- = value.to_currency.split(".")[0]
- %th
- = t.to_currency.split(".")[0]
- %h1.green
- = link_to "See a more detailed report", url(:controller => :browse, :action => :centers_paying_today)
- #hbarchart{:style => "width: 92%; height: 30px; margin-left: 3%"}
- #infovis{:style => "width: 90%; height: 400px; margin: auto"}
-
-
-
- #staff
- %h1 Staff Members
-
- %table
- %tr
- %th
-
-
-
+%div.left_pane
+ %h1 Home
+ %h3
+ You can navigate to the most commonly used screens from here
+ %div
+ %ul
+ %li
+ = link_to_with_rights('Branches', url(:browse, :action => 'branches'), :class => 'edit')
+ %li
+ = link_to_with_rights('Staff members', resource(:staff_members), :class => 'edit')
+ %li
+ = link_to_with_rights('Centers meeting today', url(:browse, :centers_paying_today), :class => 'edit')
+ %li
+ = link_to_with_rights('Areas', resource(:areas), :class => 'edit')
+ %li
+ = link_to_with_rights('Regions', resource(:regions), :class => 'edit')
+ %li
+ = link_to_with_rights 'Verify data', resource(:verifications), :class => 'edit'
+ %li
+ = link_to_with_rights 'Documents', resource(:documents), :class => 'edit'
+ %li
+ = link_to 'Funder page', url(:funders), :class => 'edit' if session.user.role == :funder
+ %li
+ = link_to_with_rights 'Book keeping', resource(:accounts), :class => 'edit'
View
128 app/views/browse/index.html.old
@@ -0,0 +1,128 @@
+= js_include_tag 'Jit/jit-yc'
+= js_include_tag :treemap, :hbarchart
+
+= css_include_tag 'jit-base','treemap'
+:javascript
+ var date = new Date('#{@date.strftime("%Y-%m-%d")}')
+ var date_string = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
+ function get_charts(q) {
+ $.get('/graph_data/tm_collections?' + q,'', function(data){
+ init_barchart($.parseJSON(data).pmts_barchart);
+ init_treemap($.parseJSON(data).treemap);
+ });
+ }
+
+ $(document).ready(function(){ get_charts('date=' + date_string)});
+
+- bnames = @branch_names.keys.sort
+- bvalues = @branch_names.map{|name, id| @branch_data[id][:principal_paid] if @branch_data[id]}
+- keys = [:advance_principal_paid, :advance_interest_paid, :principal_paid, :principal_due, :interest_paid, :interest_due]
+%script{:type => "text/javascript"}
+ == names=['#{bnames.join("','")}']
+ == values = ['#{bvalues.join("','")}']
+#stats
+ - array = @branch_data.reduce({}){|s,a| s + a[1]}
+ %table#menu{:style => "float:left; position: relative; left: 1px;"}
+ %tr.selected
+ %th
+ %h1
+ today
+ %td.number
+ %h1.green
+ = keys.select{|k| k.to_s.match(/paid/)}.map{|k| array[k] || 0}.reduce(:+).to_currency
+ %p collected
+ %td.number
+ %h1
+ = keys.select{|k| k.to_s.match(/due/)}.map{|k| array[k] || 0}.reduce(:+).to_currency
+ %p due
+ %td.number
+ %h1.red
+ 1,745
+ %p
+ missed
+
+ %tr
+ %th
+ %h1 meetings
+ - pt = Center.paying_today(session.user)
+ %td.number
+ %h1
+ = pt.count
+ %p
+ mtngs
+ %br
+ today
+ %td.number
+ %h1
+ = pt.select{|c| c.meeting_time_hours.to_i <= DateTime.now.hour}.count
+ %p
+ mtngs
+ %br
+ by now
+ %td.number
+ %h1
+ = Payment.all(:received_on => @date, :center_id => pt.aggregate(:id)).aggregate(:center_id).count
+ %p
+ meetings
+ %br
+ done
+ %tr
+ %th
+ %h1 ytd
+ %td.number{:colspan => "4"}
+ %h1 0
+ %p PAR
+
+
+
+ .detail
+ #today
+ %table.new-report{:style => "margin: auto; width: 90%"}
+ %thead
+ %tr
+ %th.purple
+ - ["adv", "prin", "int", "fees","default"].each do |k|
+ %th
+ = k
+ %tr
+ %th
+ Paid
+ %td.green
+ = (array[:advance_principal_paid] + array[:advance_interest_paid]).to_currency
+ %td.green
+ = (array[:principal_paid]).to_currency
+ %td.green
+ = (array[:interest_paid]).to_currency
+ %td.green
+ = (array[:fees_paid]).to_currency
+ %td.green
+ = (-array[:principal_in_default] + array[:interest_in_default]).to_currency
+ %tr
+ %th
+ Due
+ %td.red
+ = "-"
+ %td.red
+ = (array[:principal_due]).to_currency
+ %td.red
+ = (array[:interest_due]).to_currency
+ %td.red
+ = (array[:fees_due]).to_currency
+ %td.red
+ = "-"
+ %h1.green
+ See a more detailed report
+ #hbarchart{:style => "width: 92%; height: 30px; margin-left: 3%"}
+ #infovis{:style => "width: 90%; height: 400px; margin: auto"}
+
+
+
+ #staff
+ %h1 Staff Members
+
+ %table
+ %tr
+ %th
+
+
+
View
95 app/views/browse/today.html.haml
@@ -1,3 +1,92 @@
-.today
- %h1
- = Center.all(:meeting_day => @date.weekday).count
+= js_include_tag 'Jit/jit-yc'
+= js_include_tag :treemap, :hbarchart
+
+= css_include_tag 'jit-base','treemap'
+:javascript
+ var date = new Date('#{@date.strftime("%Y-%m-%d")}')
+ var date_string = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
+ function get_charts(q) {
+ $.get('/graph_data/tm_collections?' + q,'', function(data){
+ init_barchart($.parseJSON(data).pmts_barchart);
+ init_treemap($.parseJSON(data).treemap);
+ });
+ }
+
+ $(document).ready(function(){ get_charts('date=' + date_string)});
+
+- debugger
+- bnames = @branch_names.keys.sort
+%script{:type => "text/javascript"}
+ == names=['#{bnames.join("','")}']
+ == values = ['#{@branch_names.values.join("','")}']
+#stats
+ - total = @caches.reduce(nil){|s,h| h + s}
+ %table#menu{:style => "float:left; position: relative; left: 1px;"}
+ %tr.selected
+ %th
+ %h1
+ today
+ %td.number
+ %h1.green
+ = total.fees_paid_today + total.principal_paid + total.interest_paid
+ %p collected
+ %td.number
+ %h1
+ = total.total_fees_due + total.principal_due + total.interest_due
+ %p due
+ %td.number
+ %h1.red
+ 0
+ %p
+ missed
+
+
+ .detail
+ #today
+ %table.new-report{:style => "margin: auto; width: 90%"}
+ %thead
+ %tr
+ %th.purple
+ - ["adv", "prin", "int", "fees","default","total"].each do |k|
+ %th
+ = k
+ %tr
+ %th
+ Paid
+ - t = 0
+ - keys = [:total_advance_paid, :principal_paid, :interest_paid, :fees_paid_today, :noop]
+ - keys.map do |k|
+ %td.green
+ - value = total.send(k) || 0
+ - t += value
+ = value.to_currency.split(".")[0]
+ %th
+ = t.to_currency.split(".")[0]
+ %tr
+ %th
+ Due
+ - t = 0
+ - keys = [:noop, :principal_due, :interest_due, :fees_due_today, :total_default]
+ - keys.map do |k|
+ %td.red
+ - value = total.send(k) || 0
+ - t += value
+ = value.to_currency.split(".")[0]
+ %th
+ = t.to_currency.split(".")[0]
+ %h1.green
+ = link_to "See a more detailed report", url(:controller => :browse, :action => :centers_paying_today)
+ #hbarchart{:style => "width: 92%; height: 30px; margin-left: 3%"}
+ #infovis{:style => "width: 90%; height: 400px; margin: auto"}
+
+
+
+ #staff
+ %h1 Staff Members
+
+ %table
+ %tr
+ %th
+
+
+
View
1 app/views/cachers/_loans.html.haml
@@ -4,7 +4,6 @@
- histories = LoanHistory.latest({:center_id => params[:center_id]}, @date)
- histories.each do |h|
- cls = h.status == :outstanding ? "" : "closed"
- - next if (params[:show_all].blank? and h.status != :outstanding)
%tr{:class => cls}
- keys.each do |at|
- val = h.send(at)
View
2 app/views/clients/_list.html.haml
@@ -42,8 +42,6 @@
%td &nbsp;
%td
= client.id
- - if center_leader and center_leader.client_id == client.id
- =image_tag("elements/tick.gif", :title => "Center leader", :height => "23px", :style => "float: right;")
- unless client.active
%i{:style => "background-color : #{client.active ? '#fff' : '#fdd'}; padding: 3px;" }
inactive
View
6 app/views/clients/index.html.haml
@@ -44,10 +44,6 @@
%tr.odd
%td Landmark
%td=@center.landmark
- %tr
- %td Center leader
- - leader = @center.leader
- %td=leader.client.name if leader
- if total_attendance_at_branch = Attendance.count("client.center_id" => @center.id) and total_attendance_at_branch > 0
%tr
%td
@@ -78,7 +74,7 @@
%li#map
Map
%div.tab
- =partial "clients/list", :center_leader => leader
+ =partial "clients/list", :center_leader => @clients.first
%p.menu-strip
= link_to 'Create new client', resource(@branch, @center, :clients, :new), :class => "add"
= link_to "Bulk entry of loans", url(:enter_loans, :action => :bulk_form, :query => @center), :class => "edit"
View
0 app/views/dashboard/today.html.haml → app/views/dashboard/old_today.html.haml
File renamed without changes.
View
2 config/router.rb
@@ -43,7 +43,7 @@
resources :targets, :id => %r(\d+)
resources :holidays
resources :fees
-# resources :verifications
+ resources :verifications
resources :ledger_entries
resources :loan_products
resources :users, :id => %r(\d+)
View
6 lib/functions.rb
@@ -210,7 +210,7 @@ def +(other)
(keys + other.keys).uniq.each do |k|
if has_key?(k) and other.has_key?(k)
if self[k].respond_to?(:+) and other[k].respond_to?(:+)
- rhash[k] = self[k] + other[k]
+ rhash[k] = self[k] + other[k] rescue self[k]
end
elsif other.has_key?(k)
rhash[k] = other[k]
@@ -318,6 +318,7 @@ def url(params)
def get_bulk_insert_sql(table_name, data)
+ t = Time.now
keys = data.first.keys
sql = "INSERT INTO #{table_name}(#{keys.join(',')} )
VALUES "
@@ -338,6 +339,7 @@ def get_bulk_insert_sql(table_name, data)
values << "(#{value.join(',')})"
end
sql += values.join(",") + ";"
-
+ Merb.logger.info "sql statement crafted in #{Time.now - t}"
+ sql
end
View
45 lib/tasks/demo.rake
@@ -0,0 +1,45 @@
+# Add the local gems dir if found within the app root; any dependencies loaded
+# hereafter will try to load from the local gems before loading system gems.
+if (local_gem_dir = File.join(File.dirname(__FILE__), '..', '..', 'gems')) && $BUNDLE.nil?
+ $BUNDLE = true; Gem.clear_paths; Gem.path.unshift(local_gem_dir)
+end
+
+require "merb-core"
+
+# this loads all plugins required in your init file so don't add them
+# here again, Merb will do it for you
+Merb.start_environment(:environment => ENV['MERB_ENV'] || 'development')
+
+
+namespace :mostfit do
+ namespace :demo do
+ desc "randomly pay centers"
+ task :randomly_pay_centers do
+ date = Date.today
+ centers = LoanHistory.all(:date => date).aggregate(:center_id)
+ s = StaffMember.all(:active => true).first
+ u = User.first
+ while true
+ id = centers[(rand * centers.count).to_i]
+ center = Center.get(id)
+ unless center
+ puts "no center with id #{id}...skipping"
+ next
+ else
+ branch = center.branch
+ lhs = LoanHistory.all(:center_id => id, :date => date).aggregate(:loan_id, :principal_due, :interest_due)
+ puts "doing center #{id}. #{lhs.count} loans: BRANCH #{branch.name}"
+ lhs.each_with_index do |lh, i|
+ t = Time.now
+ l = Loan.get(lh[0])
+ next unless l
+ amount = lh[1] + lh[2]
+ print "\t repaying loan id #{l.id} (#{i}/#{lhs.count}) with amount #{amount}..."
+ l.repay(amount, u, date, s, false, PRORATA_REPAYMENT_STYLE)
+ print "...done (#{(Time.now - t).round} secs)\n"
+ end
+ end
+ end
+ end
+ end
+end
View
2 lib/tasks/mock_data.rake
@@ -77,7 +77,7 @@ namespace :mostfit do
loan_ids.each_with_index do |loan_id, idx|
loan = Loan.get(loan_id)
next unless loan
- loan.update_history
+ loan.update_history_only
pdone = (idx + 1)/co.to_f
elapsed = (Time.now - t0).round
avg_s_per_loan = (elapsed.to_f/(idx + 1)).round(2)
View
141 lib/tasks/prepare_db.rake
@@ -14,58 +14,101 @@ namespace :mostfit do
namespace :db do
desc "populate the database using the csv's"
task :prepare do
+ # repository.adapter.execute(%Q{
+ # alter table loan_history modify actual_outstanding_total decimal(15,2) not null,
+ # modify scheduled_outstanding_total decimal(15,2) not null,
+ # modify actual_outstanding_principal decimal(15,2) not null,
+ # modify scheduled_outstanding_principal decimal(15,2) not null,
+ # modify scheduled_principal_due decimal(15,2) not null,
+ # modify scheduled_interest_due decimal(15,2) not null,
+ # modify principal_due decimal(15,2) not null,
+ # modify interest_due decimal(15,2) not null,
+ # modify principal_paid decimal(15,2) not null,
+ # modify interest_paid decimal(15,2) not null,
+ # modify total_interest_due decimal(15,2) not null,
+ # modify total_principal_due decimal(15,2) not null,
+ # modify total_principal_paid decimal(15,2) not null,
+ # modify total_interest_paid decimal(15,2) not null,
+ # modify advance_principal_paid decimal(15,2) not null,
+ # modify advance_interest_paid decimal(15,2) not null,
+ # modify advance_principal_adjusted decimal(15,2) not null,
+ # modify advance_interest_adjusted decimal(15,2) not null,
+ # modify principal_in_default decimal(15,2) not null,
+ # modify interest_in_default decimal(15,2) not null,
+ # modify total_fees_due decimal(15,2) not null,
+ # modify total_fees_paid decimal(15,2) not null,
+ # modify fees_due_today decimal(15,2) not null,
+ # modify composite_key decimal(10,4) not null;
+ # })
repository.adapter.execute(%Q{
- alter table loan_history modify actual_outstanding_total decimal(10,2) not null,
- modify scheduled_outstanding_total decimal(10,2) not null,
- modify actual_outstanding_principal decimal(10,2) not null,
- modify scheduled_outstanding_principal decimal(10,2) not null,
- modify scheduled_principal_due decimal(10,2) not null,
- modify scheduled_interest_due decimal(10,2) not null,
- modify principal_due decimal(10,2) not null,
- modify interest_due decimal(10,2) not null,
- modify principal_paid decimal(10,2) not null,
- modify interest_paid decimal(10,2) not null,
- modify total_interest_due decimal(10,2) not null,
- modify total_principal_due decimal(10,2) not null,
- modify total_principal_paid decimal(10,2) not null,
- modify total_interest_paid decimal(10,2) not null,
- modify advance_principal_paid decimal(10,2) not null,
- modify advance_interest_paid decimal(10,2) not null,
- modify advance_principal_adjusted decimal(10,2) not null,
- modify advance_interest_adjusted decimal(10,2) not null,
- modify principal_in_default decimal(10,2) not null,
- modify interest_in_default decimal(10,2) not null,
- modify total_fees_due decimal(10,2) not null,
- modify total_fees_paid decimal(10,2) not null,
- modify fees_due_today decimal(10,2) not null,
- modify composite_key decimal(10,4) not null;
- })
- repository.adapter.execute(%Q{
- alter table cachers modify actual_outstanding_total decimal(10,2) not null,
- modify scheduled_outstanding_total decimal(10,2) not null,
- modify actual_outstanding_principal decimal(10,2) not null,
- modify scheduled_outstanding_principal decimal(10,2) not null,
- modify scheduled_principal_due decimal(10,2) not null,
- modify scheduled_interest_due decimal(10,2) not null,
- modify principal_due decimal(10,2) not null,
- modify interest_due decimal(10,2) not null,
- modify principal_paid decimal(10,2) not null,
- modify interest_paid decimal(10,2) not null,
- modify total_interest_due decimal(10,2) not null,
- modify total_principal_due decimal(10,2) not null,
- modify total_principal_paid decimal(10,2) not null,
- modify total_interest_paid decimal(10,2) not null,
- modify advance_principal_paid decimal(10,2) not null,
- modify advance_interest_paid decimal(10,2) not null,
- modify advance_principal_adjusted decimal(10,2) not null,
- modify advance_interest_adjusted decimal(10,2) not null,
- modify principal_in_default decimal(10,2) not null,
- modify interest_in_default decimal(10,2) not null,
- modify total_fees_due decimal(10,2) not null,
- modify total_fees_paid decimal(10,2) not null,
- modify fees_due_today decimal(10,2) not null;
+ alter table cachers modify actual_outstanding_total decimal(15,2) not null,
+ modify scheduled_outstanding_total decimal(15,2) not null,
+ modify actual_outstanding_principal decimal(15,2) not null,
+ modify scheduled_outstanding_principal decimal(15,2) not null,
+ modify scheduled_principal_due decimal(15,2) not null,
+ modify scheduled_interest_due decimal(15,2) not null,
+ modify principal_due decimal(15,2) not null,
+ modify interest_due decimal(15,2) not null,
+ modify principal_paid decimal(15,2) not null,
+ modify interest_paid decimal(15,2) not null,
+ modify total_interest_due decimal(15,2) not null,
+ modify total_principal_due decimal(15,2) not null,
+ modify total_principal_paid decimal(15,2) not null,
+ modify total_interest_paid decimal(15,2) not null,
+ modify advance_principal_paid decimal(15,2) not null,
+ modify advance_interest_paid decimal(15,2) not null,
+ modify advance_principal_adjusted decimal(15,2) not null,
+ modify advance_interest_adjusted decimal(15,2) not null,
+ modify principal_in_default decimal(15,2) not null,
+ modify interest_in_default decimal(15,2) not null,
+ modify total_fees_due decimal(15,2) not null,
+ modify total_fees_paid decimal(15,2) not null,
+ modify fees_due_today decimal(15,2) not null;
})
end
+
+
+ desc "copies financial tables from new_layout"
+ task :copy_tables, :database do |task, args|
+ db = args[:database]
+ tables = %w{branches centers clients loans payments loan_history funders funding_lines users staff_members areas regions comments client_groups occupations applicable_fees repayment_styles loan_products insurance_policies fees}
+ tables.each do |t|
+ repository.adapter.execute("drop table if exists #{t}")
+ repository.adapter.execute("create table #{t} select * from #{db}.#{t}")
+ end
+ end
+
+ desc "increases ids by max(id) to easily copy back into legacy database"
+ task :up_ids do
+ max = {:branches => nil, :centers => nil, :clients => nil, :loans => nil, :payments => nil, :client_groups => nil}
+ # get the max id for each table
+ max.keys.each {|k| max[k] = repository.adapter.query("select max(id) from #{k.to_s}")[0]}
+ max.each{|k,v| puts "#{k}:#{v}"}
+
+ # update the ids
+ max.keys.each {|k| repository.adapter.execute("update #{k.to_s} set id = id + #{max[k]}")}
+
+ #update the children
+ update = {:centers => [:branch], :clients => [:center, :client_group], :client_groups => [:center],
+ :loans => [:client], :attendances => [:client, :center], :payments => [:loan, :client, :c_branch, :c_center],
+ :insurance_policies => [:loan, :client], :loan_history => [:loan, :client, :center, :branch]}
+ update.each do |k,v|
+ v.each do |f|
+ max_key = f.to_s.match(/^c_/) ? f.to_s.split("_")[1].pluralize.to_sym : f.to_s.pluralize.to_sym
+ repository.adapter.execute("update #{k.to_s} set #{f.to_s}_id = #{f.to_s}_id + #{max[max_key]}")
+ end
+ end
+ end
+
+ desc "copies the updated tables back into the original database"
+ task :copy_tables_back, :database do |task, args|
+ db = args[:database]
+ tables = %w{branches centers clients loans payments loan_history}
+ tables.each do |t|
+ repository.adapter.execute("insert into #{db}.#{t} select * from #{t}")
+ end
+ end
+
end
end
View
131 public/javascripts/old_treemap.js
@@ -0,0 +1,131 @@
+var labelType, useGradients, nativeTextSupport, animate;
+
+(function() {
+ var ua = navigator.userAgent,
+ iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
+ typeOfCanvas = typeof HTMLCanvasElement,
+ nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
+ textSupport = nativeCanvasSupport
+ && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
+ //I'm setting this based on the fact that ExCanvas provides text support for IE
+ //and that as of today iPhone/iPad current text support is lame
+ labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
+ nativeTextSupport = labelType == 'Native';
+ useGradients = nativeCanvasSupport;
+ animate = !(iStuff || !nativeCanvasSupport);
+})();
+
+var Log = {
+ elem: false,
+ write: function(text){
+ if (!this.elem)
+ this.elem = document.getElementById('log');
+ this.elem.innerHTML = text;
+ this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
+ }
+};
+
+
+
+
+function init_treemap(data,date){
+ //init TreeMap
+ x = 1;
+ if (!(typeof(tm) == 'undefined')) {
+ tm.loadJSON(data);
+ tm.refresh();
+ }
+ else {
+ tm = new $jit.TM.Squarified({
+ //where to inject the visualization
+ injectInto: 'infovis',
+ //parent box title heights
+ titleHeight: 15,
+ //enable animations
+ animate: true,
+ //box offsets
+ offset: 1,
+ //Attach left and right click events
+ Events: {
+ enable: true,
+ onClick: function(node) {
+ if(node) get_charts('date=' + date_string + '&branch_id=' + node.data.branch_id);
+ },
+ onRightClick: function() {
+ get_charts('date=' + date_string);
+ }
+ },
+ duration: 1000,
+ //Enable tips
+ Tips: {
+ enable: true,
+ //add positioning offsets
+ offsetX: 20,
+ offsetY: 20,
+ //implement the onShow method to
+ //add content to the tooltip when a node
+ //is hovered
+ onShow: function(tip, node, isLeaf, domElement) {
+ var html = "<div class=\"tip-title\">" + node.name
+ + "</div><div class=\"tip-text\">";
+ var data = node.data;
+ html += "branch: " + data.branch_name + "<br>";
+ html += "balance outstanding:" + data.amounts.actual_outstanding_principal + "<br>";
+ html += "principal paid:" + data.amounts.principal_paid + "<br>";
+ html += "principal due:" + data.amounts.principal_due + "<br>";
+ html += "interest paid:" + data.amounts.interest_paid + "<br>";
+ html += "interest due:" + data.amounts.interest_due + "<br>";
+ html += "amount in default" + (-(data.amounts.interest_in_default + data.amounts.principal_in_default)) + "<br>";
+ tip.innerHTML = html;
+ }
+ },
+ //Add the name of the node in the correponding label
+ //This method is called once, on label creation.
+ onCreateLabel: function(domElement, node){
+ domElement.innerHTML = node.name;
+ var style = domElement.style;
+ style.display = '';
+ style.border = '1px solid transparent';
+ domElement.onmouseover = function() {
+ style.border = '1px solid #9FD4FF';
+ };
+ domElement.onmouseout = function() {
+ style.border = '1px solid transparent';
+ };
+ }
+ });
+
+ tm.loadJSON(data);
+ tm.refresh();
+
+
+ }
+ //end
+ //add events to radio buttons
+ var sq = $jit.id('r-sq'),
+ st = $jit.id('r-st'),
+ sd = $jit.id('r-sd');
+ var util = $jit.util;
+ util.addEvent(sq, 'change', function() {
+ if(!sq.checked) return;
+ util.extend(tm, new $jit.Layouts.TM.Squarified);
+ tm.refresh();
+ });
+ util.addEvent(st, 'change', function() {
+ if(!st.checked) return;
+ util.extend(tm, new $jit.Layouts.TM.Strip);
+ tm.layout.orientation = "v";
+ tm.refresh();
+ });
+ util.addEvent(sd, 'change', function() {
+ if(!sd.checked) return;
+ util.extend(tm, new $jit.Layouts.TM.SliceAndDice);
+ tm.layout.orientation = "v";
+ tm.refresh();
+ });
+ //add event to the back button
+ var back = $jit.id('back');
+ $jit.util.addEvent(back, 'click', function() {
+ tm.out();
+ });
+}
View
121 public/javascripts/treemap.js.new
@@ -0,0 +1,121 @@
+var labelType, useGradients, nativeTextSupport, animate;
+
+(function() {
+ var ua = navigator.userAgent,
+ iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
+ typeOfCanvas = typeof HTMLCanvasElement,
+ nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
+ textSupport = nativeCanvasSupport
+ && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
+ //I'm setting this based on the fact that ExCanvas provides text support for IE
+ //and that as of today iPhone/iPad current text support is lame
+ labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
+ nativeTextSupport = labelType == 'Native';
+ useGradients = nativeCanvasSupport;
+ animate = !(iStuff || !nativeCanvasSupport);
+})();
+
+var Log = {
+ elem: false,
+ write: function(text){
+ if (!this.elem)
+ this.elem = document.getElementById('log');
+ this.elem.innerHTML = text;
+ this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
+ }
+};
+
+
+
+
+function init_treemap(data){
+ //init TreeMap
+ tm = new $jit.TM.Squarified({
+ //where to inject the visualization
+ injectInto: 'infovis',
+ //parent box title heights
+ titleHeight: 15,
+ //enable animations
+ animate: false,
+ //box offsets
+ offset: 1,
+ //Attach left and right click events
+ Events: {
+ enable: true,
+ onClick: function(node) {
+ if(node) tm.enter(node);
+ },
+ onRightClick: function() {
+ tm.out();
+ }
+ },
+ duration: 1000,
+ //Enable tips
+ Tips: {
+ enable: true,
+ //add positioning offsets
+ offsetX: 20,
+ offsetY: 20,
+ //implement the onShow method to
+ //add content to the tooltip when a node
+ //is hovered
+ onShow: function(tip, node, isLeaf, domElement) {
+ var html = "<div class=\"tip-title\">" + node.name
+ + "</div><div class=\"tip-text\">";
+ var data = node.data;
+ if(data.playcount) {
+ html += "play count: " + data.playcount;
+ }
+ if(data.image) {
+ html += "<img src=\""+ data.image +"\" class=\"album\" />";
+ }
+ tip.innerHTML = html;
+ }
+ },
+ //Add the name of the node in the correponding label
+ //This method is called once, on label creation.
+ onCreateLabel: function(domElement, node){
+ domElement.innerHTML = node.name;
+ var style = domElement.style;
+ style.display = '';
+ style.border = '1px solid transparent';
+ domElement.onmouseover = function() {
+ style.border = '1px solid #9FD4FF';
+ };
+ domElement.onmouseout = function() {
+ style.border = '1px solid transparent';
+ };
+ }
+ });
+
+ tm.loadJSON(data);
+ tm.refresh();
+ //end
+ //add events to radio buttons
+ var sq = $jit.id('r-sq'),
+ st = $jit.id('r-st'),
+ sd = $jit.id('r-sd');
+ var util = $jit.util;
+ util.addEvent(sq, 'change', function() {
+ if(!sq.checked) return;
+ util.extend(tm, new $jit.Layouts.TM.Squarified);
+ tm.refresh();
+ });
+ util.addEvent(st, 'change', function() {
+ if(!st.checked) return;
+ util.extend(tm, new $jit.Layouts.TM.Strip);
+ tm.layout.orientation = "v";
+ tm.refresh();
+ });
+ util.addEvent(sd, 'change', function() {
+ if(!sd.checked) return;
+ util.extend(tm, new $jit.Layouts.TM.SliceAndDice);
+ tm.layout.orientation = "v";
+ tm.refresh();
+ });
+ //add event to the back button
+ var back = $jit.id('back');
+ $jit.util.addEvent(back, 'click', function() {
+ tm.out();
+ });
+}
View
2 slices/maintainer/lib/maintainer.rb
@@ -49,7 +49,7 @@ def self.init
# Activation hook - runs after AfterAppLoads BootLoader
def self.activate
- User.create(:login => "maintainer", :password => "password", :password_confirmation => "password", :role => :maintainer) if User.all(:role => :maintainer).length == 0
+ User.create(:login => "maintainer", :password => "password", :password_confirmation => "password", :role => :maintainer) if User.all(:role => :maintainer).length == 0 rescue false
DM_REPO.scope { Maintainer::DeploymentItem.auto_upgrade! }
DM_REPO.scope { Maintainer::HistoryItem.auto_upgrade! }
DM_REPO.scope { Maintainer::DeploymentItem.create_from_last_commit if Maintainer::DeploymentItem.all.empty? }

0 comments on commit 3da5465

Please sign in to comment.
Something went wrong with that request. Please try again.