Skip to content

Commit

Permalink
[ryan] save summaries only
Browse files Browse the repository at this point in the history
  • Loading branch information
perf committed Aug 15, 2012
1 parent 331b31b commit 1569e29
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GIT
PATH
remote: .
specs:
active_metric (2.0.0)
active_metric (2.0.1)
bson_ext (= 1.4.0)
mongo (= 1.4.0)
rails (~> 3.1)
Expand Down
33 changes: 19 additions & 14 deletions lib/active_metric/behavior/graph_calculation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def has_graph_data
true
end

def update_graph_model
remove_last_sample_from_cache

def update_graph_model(remaining_interval_samples)
pop_necessary_samples(remaining_interval_samples)
remaining_interval_samples.each do |sample|
sample.stats.each do|stat|
series = graph_view_model.series_for(stat.access_name.to_s)
Expand All @@ -32,6 +31,23 @@ def update_graph_model
self.save!
end

def pop_necessary_samples(remaining_interval_samples)
data_to_pop = calculate_data_to_pop(remaining_interval_samples)
pop_from_series(data_to_pop) if data_to_pop > 0
end

def calculate_data_to_pop(remaining_interval_samples)
incoming_index = remaining_interval_samples.first.sample_index
next_index = graph_view_model.size
next_index - incoming_index
end

def pop_from_series(data_to_pop)
graph_view_model.series_data.each do |series|
series.pop_data(data_to_pop)
end
end

def initialize_graph_view_model
if self.class.sample_type
axises_defined = self.class.sample_type.axises_defined
Expand All @@ -43,17 +59,6 @@ def initialize_graph_view_model
GraphViewModel.create_from_meta_data(axises_defined, stats_defined, name: name)
end

def remove_last_sample_from_cache
graph_view_model.series_data.each do |series|
series.pop_data
end
end

def remaining_interval_samples
sample_skip = graph_view_model.size
interval_samples[sample_skip..-1]
end

def time(sample_time)
((sample_time - start_time)).to_i
end
Expand Down
4 changes: 2 additions & 2 deletions lib/active_metric/point_series_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def push_data(value)
push_all(:data, [value])
end

def pop_data
pop(:data,1)
def pop_data(data_to_pop = 1)
pop(:data,data_to_pop)
end

end
Expand Down
9 changes: 4 additions & 5 deletions lib/active_metric/sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ class Sample
field :timestamp, :type => Integer
field :measurement_count, :type => Integer, :default => 0
field :sum, :type => Integer, :default => 0
field :sample_index, :type => Integer

index(:timestamp => -1)
index({:samplable_id => 1}, {:background => true})

def initialize(attr = nil, options = nil, measurement = nil)
def initialize(attr = nil, options = nil, measurement = nil, index = 0)
@seed_measurement = measurement
@latest_measurement = nil
super(attr, options)
self.sample_index = index
if stats.empty?
self.stats = self.class.stats_defined.map(&:create_stat)
end
Expand Down Expand Up @@ -111,7 +110,7 @@ def update_stats(measurement)
end

def new_sample
self.class.new({:samplable => self.samplable, :interval => interval},{},@latest_measurement)
self.class.new({:samplable => self.samplable, :interval => interval},{},@latest_measurement, sample_index + 1)
end

def self.stats_defined
Expand Down
51 changes: 22 additions & 29 deletions lib/active_metric/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ class Subject
include GraphCalculation

belongs_to :report, :class_name => "ActiveMetric::Report", :polymorphic => true
has_many :samples, :class_name => "ActiveMetric::Sample", :as => :samplable, :dependent => :destroy

field :name, :type => String

index({:report_id => -1},{:background => true})
Expand All @@ -25,7 +23,7 @@ def value_from_summary(method)
end

def summary
@summary ||= samples.where(:interval => nil).first ||
@summary ||= ActiveMetric::Sample.where(:samplable => to_param, :interval => nil).first ||
self.class.summary_type.create(:samplable => self,
:interval => nil)
end
Expand All @@ -42,20 +40,16 @@ def ensure_standard_deviator_for(property)
standard_deviators[property] ||= StandardDeviator.new(property)
end

def interval_samples
samples.reject{|s| s.interval.nil?}.sort_by(&:timestamp)
end

def calculate(measurement)
summary.calculate(measurement)
returned_sample = current_sample.calculate(measurement)
update_subject_calculators(measurement)
update_current_sample(returned_sample) if is_new_sample?(returned_sample)
update_subject_calculators(measurement)
end

def update_current_sample(returned_sample)
@current_sample = returned_sample
self.complete
@current_sample = returned_sample
end

def is_new_sample?(returned_sample)
Expand All @@ -68,30 +62,29 @@ def update_subject_calculators(measurement)
end

def complete
self.summary.complete
self.current_sample.complete
self.update_graph_model
summary.complete
current_sample.complete
update_graph_model([current_sample])

self.save!
save!
end

def current_sample
@current_sample ||= interval_samples.last ||
self.class.sample_type.new(:samplable => self,
:interval => self.class.interval_length)
end

def headers_for_table
headers = []
summary.stats.each do |stat|
headers << stat.name
end
end

def graphable_stats
return {} unless samples.any?
samples.first.stat_meta_data
end
@current_sample ||= self.class.sample_type.new(:samplable => self,
:interval => self.class.interval_length)
end

#def headers_for_table
# headers = []
# summary.stats.each do |stat|
# headers << stat.name
# end
#end
#
#def graphable_stats
# return {} unless samples.any?
# samples.first.stat_meta_data
#end

def self.sample_type
nil
Expand Down
4 changes: 2 additions & 2 deletions test/behavior_tests/graph_calculation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class GraphCalculationTest < ActiveSupport::TestCase
subject = TestSubject.create :report => report

10.times do |value|
#value += 10
subject.calculate TestMeasurement.new(:value => value, :timestamp => value)
end

Expand Down Expand Up @@ -39,8 +40,7 @@ class GraphCalculationTest < ActiveSupport::TestCase
assert_equal first_stat_name, gvm.series_data.map(&:label).sort.first
end


test "update series data recalculates last sample" do
test "update series data recalculates last sample if on same index" do
report = Report.create
subject = TestSubject.create :report => report

Expand Down
26 changes: 15 additions & 11 deletions test/graph_view_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,21 @@ class GraphViewModelTest < ActiveSupport::TestCase
end


test "can retrieve partial array" do

graph_view_model = GraphViewModel.create()
graph_view_model.series_data << generate_series_data(4)

partial_graph = GraphViewModel.where(_id: graph_view_model.id).
slice("series_data.data" => [2,MONGO_MAX_LIMIT]).first

assert_equal [[2,2],[3,3]], partial_graph.series_data.first.data
end

#test "can retrieve partial array" do
# subject = Subject.create
# graph_view_model = subject.graph_view_model
# graph_view_model.series_data << generate_series_data(4)
# graph_view_model.series_data << generate_series_data(4, [[5,5],[6,6],[7,7],[8,8]])
#
#
# partial_graph = subject.graph_view_model_starting_at(2)
#
# assert_equal [[2,2],[3,3]], partial_graph.series_data.first.data
# assert_equal [[7,7],[8,8]], partial_graph.series_data.second.data
#
# graph_view_model.series_data[0].push_data([9,9])
# graph_view_model.series_data[1].push_data([10,10])
#end

def generate_series_data(x_count, label = "label")
data = []
Expand Down
11 changes: 0 additions & 11 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ class IntegrationTest < ActiveSupport::TestCase
assert_close_to 2.87, subject.summary.standard_deviation_value.value
end

test "interval samples should order before skip" do
report = Report.create
subject = TestSubject.create :report => report
50.times do |value|
subject.calculate TestMeasurement.new(:value => value, :timestamp => value)
end
subject.complete
assert_equal 47, subject.interval_samples[9].timestamp
end

test "summary should update when samples changes" do
report = Report.create
subject = TestSubject.create :report => report
Expand All @@ -95,7 +85,6 @@ class IntegrationTest < ActiveSupport::TestCase
subject.calculate TestMeasurement.new(:value => 6, :timestamp => 6)

assert_equal 4.0, subject.summary.eightieth_value.value

end

test "subjects should delegate to summaries" do
Expand Down

0 comments on commit 1569e29

Please sign in to comment.