2014-02-17 18:05:39 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: feeds
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# channel_id :integer
|
|
|
|
# field1 :string(255)
|
|
|
|
# field2 :string(255)
|
|
|
|
# field3 :string(255)
|
|
|
|
# field4 :string(255)
|
|
|
|
# field5 :string(255)
|
|
|
|
# field6 :string(255)
|
|
|
|
# field7 :string(255)
|
|
|
|
# field8 :string(255)
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# entry_id :integer
|
|
|
|
# status :string(255)
|
|
|
|
# latitude :decimal(15, 10)
|
|
|
|
# longitude :decimal(15, 10)
|
|
|
|
# elevation :string(255)
|
|
|
|
# location :string(255)
|
|
|
|
#
|
|
|
|
|
2011-03-27 22:56:15 +02:00
|
|
|
class Feed < ActiveRecord::Base
|
2014-02-17 18:05:39 +01:00
|
|
|
extend FeedHelper
|
|
|
|
belongs_to :channel
|
|
|
|
|
2012-02-10 00:42:16 +01:00
|
|
|
self.include_root_in_json = false
|
2014-02-17 18:05:39 +01:00
|
|
|
|
2012-02-10 00:42:16 +01:00
|
|
|
attr_readonly :created_at
|
2011-03-27 22:56:15 +02:00
|
|
|
|
2014-04-04 05:08:00 +02:00
|
|
|
# delete feeds in batches
|
|
|
|
def self.delete_in_batches(channel_id)
|
|
|
|
channel = Channel.find(channel_id)
|
|
|
|
|
|
|
|
# while there are still feeds left
|
|
|
|
while channel.feeds.count > 0
|
|
|
|
# create the sql query to delete 1000 feeds from the channel
|
|
|
|
sql = "DELETE FROM feeds WHERE channel_id=#{channel_id} LIMIT 1000"
|
|
|
|
# execute the sql query
|
2014-10-06 17:42:02 +02:00
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
2014-04-04 05:08:00 +02:00
|
|
|
# wait a bit before the next delete
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-28 19:57:58 +01:00
|
|
|
# for to_xml, return only the public attributes
|
|
|
|
def self.public_options
|
|
|
|
{
|
2015-03-23 15:37:17 +01:00
|
|
|
:except => [:id, :updated_at, :location]
|
2014-02-28 19:57:58 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-02-17 18:05:39 +01:00
|
|
|
# only output these fields for feed
|
|
|
|
def self.select_options(channel, params)
|
|
|
|
only = [:created_at]
|
|
|
|
only += [:entry_id] unless timeparam_valid?(params[:timescale]) or timeparam_valid?(params[:average]) or timeparam_valid?(params[:median]) or timeparam_valid?(params[:sum])
|
|
|
|
only += [:field1] unless channel.field1.blank? or (params[:field_id] and !params[:field_id].index('1'))
|
|
|
|
only += [:field2] unless channel.field2.blank? or (params[:field_id] and !params[:field_id].index('2'))
|
|
|
|
only += [:field3] unless channel.field3.blank? or (params[:field_id] and !params[:field_id].index('3'))
|
|
|
|
only += [:field4] unless channel.field4.blank? or (params[:field_id] and !params[:field_id].index('4'))
|
|
|
|
only += [:field5] unless channel.field5.blank? or (params[:field_id] and !params[:field_id].index('5'))
|
|
|
|
only += [:field6] unless channel.field6.blank? or (params[:field_id] and !params[:field_id].index('6'))
|
|
|
|
only += [:field7] unless channel.field7.blank? or (params[:field_id] and !params[:field_id].index('7'))
|
|
|
|
only += [:field8] unless channel.field8.blank? or (params[:field_id] and !params[:field_id].index('8'))
|
2011-03-27 22:56:15 +02:00
|
|
|
|
2014-02-17 18:05:39 +01:00
|
|
|
# add geolocation data if necessary
|
|
|
|
if params[:location] and params[:location].upcase == 'TRUE'
|
|
|
|
only += [:latitude]
|
|
|
|
only += [:longitude]
|
|
|
|
only += [:elevation]
|
|
|
|
end
|
2011-03-27 22:56:15 +02:00
|
|
|
|
2014-02-17 18:05:39 +01:00
|
|
|
# add status if necessary
|
|
|
|
only += [:status] if params[:status] and params[:status].upcase == 'TRUE'
|
|
|
|
return only
|
|
|
|
end
|
2011-03-27 22:56:15 +02:00
|
|
|
|
2014-02-17 18:05:39 +01:00
|
|
|
# outputs feed info correctly, used by daily_feeds
|
|
|
|
def self.normalize_feeds(daily_feeds)
|
|
|
|
output = []
|
|
|
|
hash = {}
|
2011-03-27 22:56:15 +02:00
|
|
|
|
2014-02-17 18:05:39 +01:00
|
|
|
# for each daily feed
|
|
|
|
daily_feeds.each do |daily_feed|
|
|
|
|
# check if the feed already exists
|
|
|
|
existing_feed = hash[daily_feed['date']]
|
|
|
|
|
|
|
|
# skip blank feeds
|
|
|
|
next if daily_feed['date'].blank?
|
|
|
|
|
|
|
|
# if the feed exists
|
|
|
|
if existing_feed.present?
|
|
|
|
# add the new field
|
|
|
|
existing_feed["field#{daily_feed['field']}"] = daily_feed['result']
|
|
|
|
# else add a new feed
|
|
|
|
else
|
|
|
|
new_feed = Feed.new(:created_at => daily_feed['date'])
|
|
|
|
# set the field attribute correctly
|
|
|
|
new_feed["field#{daily_feed['field']}"] = daily_feed['result']
|
|
|
|
# add the feed
|
|
|
|
hash[daily_feed['date']] = new_feed
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# turn the hash into an array
|
|
|
|
output = hash.values
|
|
|
|
|
|
|
|
# sort by date
|
|
|
|
return output
|
|
|
|
end
|
|
|
|
|
|
|
|
# custom json output
|
|
|
|
def as_json(options = {})
|
2014-07-24 18:26:57 +02:00
|
|
|
super(Feed.public_options.merge(options))
|
2014-02-17 18:05:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# check if a field value is a number
|
|
|
|
# usage: Feed.numeric?(field_value)
|
|
|
|
def self.numeric?(object)
|
|
|
|
true if Float(object) rescue false
|
|
|
|
end
|
|
|
|
|
|
|
|
def field(number)
|
|
|
|
self.attributes["field#{number.to_i}"]
|
|
|
|
end
|
|
|
|
|
|
|
|
# make sure any selected fields are greater than a minimum
|
|
|
|
def greater_than?(minimum)
|
|
|
|
output = true
|
|
|
|
self.attributes.each do |attribute|
|
|
|
|
# if this attribute is a numeric field with a value
|
|
|
|
if attribute[0].to_s.index('field') == 0 && attribute[1].present? && Feed.numeric?(attribute[1])
|
|
|
|
output = false if attribute[1].to_f < minimum.to_f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return output
|
|
|
|
end
|
|
|
|
|
|
|
|
# make sure any selected fields are less than a minimum
|
|
|
|
def less_than?(maximum)
|
|
|
|
output = true
|
|
|
|
self.attributes.each do |attribute|
|
|
|
|
# if this attribute is a numeric field with a value
|
|
|
|
if attribute[0].to_s.index('field') == 0 && attribute[1].present? && Feed.numeric?(attribute[1])
|
|
|
|
output = false if attribute[1].to_f > maximum.to_f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return output
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|