Created
May 7, 2010 11:05
-
-
Save maliqq/393286 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Timeline | |
| attr_reader :data | |
| def initialize(order) | |
| @order = order | |
| @dt = @order.track_from..@order.track_to | |
| end | |
| def create_reports! | |
| load_data(:channel_id => @order.channel_id, :video_id => @order.video_id) | |
| remove_isolated_sequences! | |
| remove_short_sequences! | |
| sequences.each { |sequence| | |
| @order.reports.create(:sequence_start => sequence.min, :sequence_end => sequence.max) | |
| } | |
| end | |
| def load_data(conditions) | |
| @data = Broadcast.find(:all, | |
| :select => "dt, MAX(percentage) AS percentage", | |
| :group => "dt", | |
| :order => "dt", | |
| :conditions => conditions.merge(:dt => @dt.min..@dt.max) | |
| ) | |
| @actual_range = @data.map(&:dt) | |
| @actual_range = @dt if @actual_range.empty? | |
| end | |
| def min; @actual_range.min; end | |
| def max; @actual_range.max; end | |
| MIN_GAP = 2 | |
| def remove_isolated_sequences! | |
| filt = [] | |
| @data.each_with_index do |cur, index| | |
| prv = @data[index - 1] || ({'dt' => min - 10}) | |
| nxt = @data[index + 1] || ({'dt' => max + 10}) | |
| # удаляем изолированные "всплески", шумы | |
| p1 = cur['dt'] - prv['dt'] | |
| p2 = nxt['dt'] - cur['dt'] | |
| if p1 >= MIN_GAP && p2 >= MIN_GAP | |
| next | |
| end | |
| filt.push(cur) | |
| end | |
| @data = filt | |
| end | |
| MIN_SEQ_SIZE = 3 | |
| def remove_short_sequences! | |
| current_seq = [] | |
| seq = [] | |
| @data.each_with_index do |cur, index| | |
| prv = @data[index - 1] | |
| if cur['dt'] - prv['dt'] >= MIN_GAP # конец секции | |
| if current_seq.size <= MIN_SEQ_SIZE | |
| seq += current_seq | |
| end | |
| current_seq = [] | |
| end | |
| current_seq.push(index) | |
| end | |
| filt = [] | |
| @data.each_with_index do |cur, index| | |
| next if seq.include?(index) | |
| filt.push(cur) | |
| end | |
| @data = filt | |
| end | |
| MIN_SEQ_DISTANCE = 60 | |
| MIN_SEQ_DURATION = 10 | |
| def sequences | |
| unless defined?(@sequences) | |
| @sequences = [] | |
| current_seq = [] | |
| @data.each_with_index { |cur, index| | |
| prv = @data[index - 1] | |
| if cur['dt'] - prv['dt'] >= MIN_SEQ_DISTANCE | |
| @sequences.push current_seq if current_seq.size >= MIN_SEQ_DURATION | |
| current_seq = [] | |
| end | |
| current_seq.push cur['dt'] | |
| } | |
| @sequences.push current_seq if current_seq.size >= MIN_SEQ_DURATION | |
| end | |
| @sequences | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment