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

count aggregate function #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/helpers/feed_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,73 @@ def feeds_into_medians(feeds, params)

return timeslices
end

# slice feed into sums
def feeds_into_counts(feeds, params)
# convert timescale (minutes) into seconds
seconds = params[:count].to_i * 60
# get floored time ranges
start_time = get_floored_time(feeds.first.created_at, seconds)
end_time = get_floored_time(feeds.last.created_at, seconds)

# create empty array with appropriate size
timeslices = Array.new((((end_time - start_time) / seconds).abs).floor)

# create a blank clone of the first feed so that we only get the necessary attributes
empty_feed = create_empty_clone(feeds.first)

# add feeds to array
feeds.each do |f|
i = ((f.created_at - start_time) / seconds).floor
f.created_at = start_time + i * seconds
# create multidimensional array
timeslices[i] = [] if timeslices[i].nil?
timeslices[i].push(f)
end

# keep track of whether numbers use commas as decimals
comma_flag = false

# fill in array
timeslices.each_index do |i|
# insert empty values
if timeslices[i].nil?
current_feed = empty_feed.dup
current_feed.created_at = (start_time + (i * seconds))
timeslices[i] = current_feed
# else sum the inner array
else
sum_feed = empty_feed.dup
sum_feed.created_at = timeslices[i].first.created_at
# for each feed
timeslices[i].each do |f|
# for each attribute, add to sum_feed so that we have the total
sum_feed.attribute_names.each do |attr|
# only add non-null integer fields
if attr.index('field') and !f[attr].nil? and is_a_number?(f[attr])

# set comma_flag once if we find a number with a comma
comma_flag = true if !comma_flag and f[attr].to_s.index(',')

# set initial data
if sum_feed[attr].nil?
sum_feed[attr] = 0
# add data
elsif f[attr]
sum_feed[attr] = sum_feed[attr] + 1
end

end
end
end

# set to the summed feed
timeslices[i] = object_sum(sum_feed, comma_flag, params[:round])
end
end

return timeslices
end

# checks for valid timescale
def timeparam_valid?(timeparam)
Expand Down