CDB-1535 Store cache hits on Geocoding model

pull/300/merge
David Arango 11 years ago
parent d0c22a6dc7
commit 2378926f6a

@ -67,6 +67,7 @@ class Geocoding < Sequel::Model
sleep(2)
end until ['completed', 'cancelled', 'failed'].include? state
return false if state == 'cancelled'
self.update(cache_hits: table_geocoder.cache.hits)
table_geocoder.process_results
create_automatic_geocoding if automatic_geocoding_id.blank?
self.update(state: 'finished')

@ -413,7 +413,7 @@ $$
date_to = (options[:to] ? options[:to].to_date : Date.today)
date_from = (options[:from] ? options[:from].to_date : self.last_billing_cycle)
Geocoding.where('user_id = ? AND created_at >= ? and created_at <= ?', self.id, date_from, date_to + 1.days)
.sum(:processed_rows).to_i
.sum("processed_rows + cache_hits".lit).to_i
end # get_geocoding_calls
# Legacy stats fetching

@ -1,6 +1,6 @@
Sequel.migration do
up do
add_column :geocodings, :cache_hits, :integer
add_column :geocodings, :cache_hits, :integer, default: 0
end
down do

@ -6,7 +6,7 @@ module CartoDB
BATCH_SIZE = 10
attr_reader :connection, :working_dir, :table_name,
attr_reader :connection, :working_dir, :table_name, :hits,
:max_rows, :sql_api, :formatter, :cache_results
def initialize(arguments)
@ -37,6 +37,7 @@ module CartoDB
end while rows.size >= BATCH_SIZE
create_temp_table
load_results_to_temp_table
@hits = connection.fetch("SELECT count(*) FROM #{temp_table_name}").first[:count].to_i
copy_results_to_table
rescue => e
drop_temp_table

@ -191,7 +191,8 @@ describe CartoDB::TableGeocoder do
end
t.process_results
t.geocoder.status.should eq 'completed'
t.geocoder.processed_rows.should eq 3
t.geocoder.processed_rows.to_i.should eq 3
t.cache.hits.should eq 34
@db.fetch("select count(*) from #{@table_name} where the_geom is null").first[:count].should eq 3
@db.fetch("select count(*) from #{@table_name} where cartodb_georef_status is false").first[:count].should eq 3
end

@ -57,9 +57,10 @@ describe Geocoding do
end
describe '#run!' do
it 'updates total_rows, processed_rows and state' do
it 'updates total_rows, processed_rows, cache_hits and state' do
geocoding = Geocoding.create(user: @user, table: @table, formatter: 'b')
geocoding.table_geocoder.stubs(:run).returns true
geocoding.table_geocoder.stubs(:cache).returns OpenStruct.new(hits: 5)
geocoding.table_geocoder.stubs(:process_results).returns true
CartoDB::Geocoder.any_instance.stubs(:status).returns 'completed'
CartoDB::Geocoder.any_instance.stubs(:update_status).returns true
@ -70,6 +71,7 @@ describe Geocoding do
geocoding.total_rows.should eq 20
geocoding.processed_rows.should eq 10
geocoding.state.should eq 'finished'
geocoding.cache_hits.should eq 5
end
it 'marks the geocoding as failed if the geocoding job fails' do

@ -218,7 +218,7 @@ describe User do
delete_user_data @user
@user.stubs(:last_billing_cycle).returns(Date.today)
FactoryGirl.create(:geocoding, user: @user, created_at: Time.now, processed_rows: 1)
FactoryGirl.create(:geocoding, user: @user, created_at: Time.now - 5.days, processed_rows: 2)
FactoryGirl.create(:geocoding, user: @user, created_at: Time.now - 5.days, processed_rows: 1, cache_hits: 1)
end
it "should return the sum of geocoded rows for the current billing period" do

Loading…
Cancel
Save