thingspeak/app/models/channel.rb

495 lines
16 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: channels
#
2014-05-15 22:42:47 +02:00
# id :integer not null, primary key
# user_id :integer
# name :string(255)
# description :string(255)
# latitude :decimal(15, 10)
# longitude :decimal(15, 10)
# field1 :string(255)
# field2 :string(255)
# field3 :string(255)
# field4 :string(255)
# field5 :string(255)
# field6 :string(255)
# field7 :string(255)
# field8 :string(255)
# scale1 :integer
# scale2 :integer
# scale3 :integer
# scale4 :integer
# scale5 :integer
# scale6 :integer
# scale7 :integer
# scale8 :integer
# created_at :datetime
# updated_at :datetime
# elevation :string(255)
# last_entry_id :integer
# public_flag :boolean default(FALSE)
# options1 :string(255)
# options2 :string(255)
# options3 :string(255)
# options4 :string(255)
# options5 :string(255)
# options6 :string(255)
# options7 :string(255)
# options8 :string(255)
# social :boolean default(FALSE)
# slug :string(255)
# status :string(255)
# url :string(255)
# video_id :string(255)
# video_type :string(255)
# clearing :boolean default(FALSE), not null
# ranking :integer
# user_agent :string(255)
# realtime_io_serial_number :string(36)
2014-06-30 19:16:22 +02:00
# metadata :text
#
2011-03-27 22:56:15 +02:00
class Channel < ActiveRecord::Base
include KeyUtilities
belongs_to :user
has_many :feeds
has_many :daily_feeds
has_many :api_keys, :dependent => :destroy
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
has_many :comments, :dependent => :destroy
has_many :windows, :dependent => :destroy, :autosave => true
accepts_nested_attributes_for :tags
self.include_root_in_json = true
attr_readonly :created_at
after_commit :set_default_name
before_destroy :delete_feeds
2014-07-31 21:35:21 +02:00
validates :video_type, :presence => true, :if => lambda{ |channel| channel.video_id.present? }
scope :public_viewable, lambda { where("public_flag = true AND social != true") }
scope :not_social, lambda { where("social != true") }
scope :is_public, lambda { where("public_flag = true") }
scope :active, lambda { where("channels.last_entry_id > 1 and channels.updated_at > ?", DateTime.now.utc - 7.day) }
scope :being_cleared, lambda { where("clearing = true") }
scope :by_array, lambda {|ids| { :conditions => ["id in (?)", ids.uniq] } }
scope :with_tag, lambda {|name| joins(:tags).where("tags.name = ?", name) }
# pagination variables
cattr_reader :per_page
@@per_page = 15
2014-03-14 00:16:35 +01:00
# how often the channel is updated
def update_rate
last_feeds = self.feeds.order('entry_id desc').limit(2)
rate = (last_feeds.first.created_at - last_feeds.last.created_at) if last_feeds.length == 2
return rate
end
2014-02-28 19:57:58 +01:00
# write key for a channel
def write_api_key
self.api_keys.where(:write_flag => true).first.api_key
end
# select options
2014-06-30 19:16:22 +02:00
def select_options(options = nil)
only = [:name, :created_at, :updated_at, :id, :last_entry_id]
2014-07-31 21:35:21 +02:00
only += [:description] if self.description.present?
2014-06-30 19:16:22 +02:00
only += [:metadata] if options.present? && options[:metadata] == 'true'
2014-07-31 21:35:21 +02:00
only += [:latitude] if self.latitude.present?
only += [:longitude] if self.longitude.present?
only += [:elevation] if self.elevation.present?
only += [:field1] if self.field1.present?
only += [:field2] if self.field2.present?
only += [:field3] if self.field3.present?
only += [:field4] if self.field4.present?
only += [:field5] if self.field5.present?
only += [:field6] if self.field6.present?
only += [:field7] if self.field7.present?
only += [:field8] if self.field8.present?
# return a hash
return { :only => only }
end
# adds a feed to the channel
def add_status_feed(status)
# update the entry_id for the channel
entry_id = self.next_entry_id
self.last_entry_id = entry_id
self.save
# create the new feed with the correct status and entry_id
self.feeds.create(:status => status, :entry_id => entry_id)
end
# get next last_entry_id for a channel
def next_entry_id
self.last_entry_id.nil? ? 1 : self.last_entry_id + 1
end
# for internal admin use, shows the ids of a channel per month (useful as a proxy for growth)
def show_growth
output = []
date = self.feeds.order("entry_id asc").first.created_at
# while the date is in the past
while (date < Time.now)
# get a feed on that day
feed = self.feeds.where("created_at > ?", date).where("created_at < ?", date + 1.day).first
# output the date and feed id
output << "#{date.strftime('%Y-%m-%d')},#{feed.id}" if feed.present?
# set the date 1 month further
date = date + 1.month
end
# show the output
puts output.join("\n")
end
# paginated hash for json and xml output
# channels input must be paginated
def self.paginated_hash(channels)
{
pagination:
{
current_page: channels.current_page,
per_page: channels.per_page,
total_entries: channels.total_entries,
},
channels: channels.as_json(Channel.public_options)
}
end
# for to_json or to_xml, return only the public attributes
def self.public_options
{
:root => false,
:only => [:id, :name, :description, :latitude, :longitude, :last_entry_id, :elevation, :created_at, :ranking],
:methods => :username,
:include => { :tags => {:only => [:id, :name]}}
}
end
# used when creating a channel
def self.private_options
{
:root => false,
2014-06-30 19:16:22 +02:00
:only => [:id, :name, :description, :metadata, :latitude, :longitude, :last_entry_id, :elevation, :created_at, :ranking],
:methods => :username,
:include => {
:tags => {:only => [:id, :name]},
:api_keys => {:only => [:api_key, :write_flag]}
}
}
end
# login name of the user who created the channel
def username; self.user.try(:login); end
# custom as_json method to allow: root => false
def as_json(options = nil)
root = include_root_in_json
root = options[:root] if options.try(:key?, :root)
if root
root = self.class.model_name.element if root == true
{ root => serializable_hash(options) }
else
serializable_hash(options)
end
end
2014-07-31 21:35:21 +02:00
# private windows
def private_windows(show_flag = false)
show_flag = (show_flag.to_s == 'true') ? true : false
return self.windows.where("private_flag = true AND show_flag = ?", show_flag)
end
2014-07-31 21:35:21 +02:00
# public windows
def public_windows(show_flag = false)
show_flag = (show_flag.to_s == 'true') ? true : false
return self.windows.where("private_flag = false AND show_flag = ?", show_flag)
end
2014-07-31 21:35:21 +02:00
# check if the channel is public
def public?; self.public_flag; end
2014-07-31 21:35:21 +02:00
# check if the video has changed
2014-08-01 21:23:28 +02:00
def video_changed?; video_id_changed? || video_type_changed?; end
2014-07-31 21:35:21 +02:00
# check if the location has changed
2014-08-01 21:23:28 +02:00
def location_changed?; latitude_changed? || longitude_changed?; end
2014-07-31 21:35:21 +02:00
# check if the any of the fields have changed
def fields_changed?
field1_changed? || field2_changed? || field3_changed? || field4_changed? ||
field5_changed? || field6_changed? || field7_changed? || field8_changed?
end
2014-08-02 20:36:38 +02:00
# update the chart windows
def update_chart_windows
# for each field
self.fields.each do |field_name|
# if the field exists, update the private and public chart window
if self.send("#{field_name}").present?
update_chart_window(field_name, true)
update_chart_window(field_name, false)
end
end
2014-08-02 20:36:38 +02:00
# remove chart windows for fields that don't exist
chart_windows = windows.where(window_type: 'chart')
chart_windows.each do |chart_window|
chart_window.destroy if self.send("field#{chart_window.content_id}").blank?
end
end
2014-08-03 01:51:52 +02:00
# update the status window
def update_status_window(private_flag)
window = windows.where(:window_type => 'status', :private_flag => private_flag).first
status_html = "<iframe class=\"statusIFrame\" width=\"450\" height=\"260\" frameborder=\"0\" src=\"/channels/#{self.id}/status/recent\"></iframe>"
# if no status window, create one
if window.blank?
window = Window.new(window_type: 'status', position: 1, col: 1, title: 'window_status', private_flag: private_flag)
end
2014-08-03 01:51:52 +02:00
# set html and add the window
window.html = status_html
2014-08-03 01:51:52 +02:00
self.windows.push(window)
end
2014-08-03 01:51:52 +02:00
# update the video window
def update_video_window(private_flag)
window = windows.where(:window_type => 'video', :private_flag => private_flag).first
2014-08-03 01:51:52 +02:00
# if the video fields are both present
if video_id.present? && video_type.present?
youtube_html = "<iframe class=\"youtube-player\" type=\"text/html\" width=\"452\" height=\"260\" src=\"https://www.youtube.com/embed/#{video_id}?wmode=transparent\" frameborder=\"0\" wmode=\"Opaque\" ></iframe>"
vimeo_html = "<iframe class=\"vimeo-player\" type=\"text/html\" width=\"452\" height=\"260\" src=\"http://player.vimeo.com/video/#{video_id}\" frameborder=\"0\"></iframe>"
2014-08-03 01:51:52 +02:00
# if no video window, create one
if window.blank?
window = Window.new(window_type: 'video', position: 1, col: 1, title: 'window_channel_video', private_flag: private_flag)
end
2014-08-03 01:51:52 +02:00
# add the html and save the window
window.html = youtube_html if video_type == 'youtube'
window.html = vimeo_html if video_type == 'vimeo'
2014-08-03 01:51:52 +02:00
self.windows.push(window)
# else delete the window
else
2014-08-03 01:51:52 +02:00
window.delete if window.present?
end
end
2014-08-03 01:51:52 +02:00
# update the location window
def update_location_window(private_flag)
window = windows.where(:window_type => 'location', :private_flag => private_flag).first
# if the latitude and longitude are present
if latitude.present? && longitude.present?
maps_html = "<iframe width=\"450\" height=\"260\" frameborder=\"0\" scrolling=\"no\" " +
"src=\"/channels/#{id}/maps/channel_show?width=450&height=260\"></iframe>"
2014-08-03 01:51:52 +02:00
# if no location window, create one
if window.blank?
window = Window.new(window_type: 'location', position: 0, col: 1, title: 'window_map', private_flag: private_flag)
end
2014-08-03 01:51:52 +02:00
# add the html and save the window
window.html = maps_html
self.windows.push(window)
# else delete the window
else
2014-08-03 01:51:52 +02:00
window.delete if window.present?
end
end
# get recent status messages from channel
def recent_statuses
self.feeds.select('status, created_at, entry_id').order('created_at DESC').limit(30).collect {|f| f unless f.status.blank? }.compact
end
2014-08-03 01:51:52 +02:00
# get the latest feed using the channel's last_entry_id
def latest_feed
self.feeds.where(:entry_id => self.last_entry_id).first
end
def delete_feeds
# if a small number of feeds or redis is not present
if self.feeds.count < 1000 || REDIS_ENABLED == false
Feed.delete_all(["channel_id = ?", self.id])
DailyFeed.delete_all(["channel_id = ?", self.id])
begin
self.update_attribute(:last_entry_id, nil)
rescue Exception => e
end
# else delete via background resque job
else
self.update_attribute(:clearing, true)
Resque.enqueue(ClearChannelJob, self.id)
end
end
# true if channel is active
def active?
return (last_entry_id and updated_at and last_entry_id > 1 and updated_at > DateTime.now.utc - 1.days)
end
def list_tags
(self.tags.collect { |t| t.name }).join(', ')
end
def save_tags(tags)
# for each tag
tags.split(',').each do |name|
tag = Tag.find_by_name(name.strip)
# save if new tag
if tag.nil?
tag = Tag.new
tag.name = name.strip
tag.save
end
2014-08-02 00:07:55 +02:00
tagging = Tagging.where(tag_id: tag.id, channel_id: self.id).first
# save if new tagging
if tagging.nil?
tagging = Tagging.new
tagging.channel_id = self.id
tagging.tag_id = tag.id
tagging.save
end
end
# delete any tags that were removed
self.remove_tags(tags)
end
# if tags don't exist anymore, remove them
def remove_tags(tags)
tag_array = tags.split(',')
# remove white space
tag_array = tag_array.collect {|t| t.strip }
# get all taggings for this channel
2014-08-02 00:07:55 +02:00
taggings = Tagging.where(channel_id: self.id).includes(:tag)
# check for existence
taggings.each do |tagging|
# if tagging is not in list
if !tag_array.include?(tagging.tag.name)
# delete tagging
tagging.delete
end
end
end
2014-08-03 01:51:52 +02:00
# add a write api key to the channel
def add_write_api_key
write_key = self.api_keys.new
write_key.user = self.user
write_key.write_flag = true
write_key.api_key = generate_api_key
write_key.save
end
2014-08-03 01:51:52 +02:00
# runs after a feed is posted
def queue_react
self.reacts.on_insertion.each do |react|
begin
Resque.enqueue(ReactJob, react.id)
rescue Exception => e
end
end
end
def field_label(field_number)
self.attributes["field#{field_number}"]
end
2014-08-03 01:51:52 +02:00
# get the valid fields as an array, for example: ["field1", "field2", "field3", "field5"]
def fields
fields = attribute_names.reject { |x|
!(x.index('field') && self[x] && !self[x].empty?)
}
end
2014-08-01 21:23:28 +02:00
# set the ranking correctly for the channel
def set_ranking
new_ranking = 0
new_ranking += 15 if name.present?
new_ranking += 20 if description.present?
new_ranking += 15 if latitude.present? && longitude.present?
new_ranking += 15 if url.present?
new_ranking += 15 if video_id.present? && video_type.present?
new_ranking += 20 if tags.present?
2014-08-01 21:23:28 +02:00
# update the ranking if it has changed
update_attribute(:ranking, new_ranking) if self.ranking != new_ranking
return new_ranking
end
2014-08-02 20:36:38 +02:00
# set the windows for the channel
def set_windows(new_channel = false)
# check for video window
if video_changed?
2014-08-03 01:51:52 +02:00
update_video_window(true)
update_video_window(false)
end
2014-08-03 01:51:52 +02:00
# check for location window
if location_changed?
2014-08-03 01:51:52 +02:00
update_location_window(true)
update_location_window(false)
end
2014-08-03 01:51:52 +02:00
# add the status window no matter what, and only display it if it has values
update_status_window(true)
update_status_window(false)
2014-08-02 20:36:38 +02:00
# update chart windows if this is a new channel or the fields have changed
update_chart_windows if new_channel || fields_changed?
end
private
2011-03-27 22:56:15 +02:00
2014-08-02 20:36:38 +02:00
# set the chart window; field_name should be a string like 'field4'
def update_chart_window(field_name, private_flag)
field_number = field_name.last.to_i
2011-03-27 22:56:15 +02:00
2014-08-02 20:36:38 +02:00
# get the chart window
window = self.windows.where(window_type: 'chart', content_id: field_number, private_flag: private_flag).first
# if there is no chart window for this field, add a default one
if window.blank?
window = Window.new(window_type: 'chart', position: 0, col: 0, title: 'window_field_chart',
name: field_name, content_id: field_number, private_flag: private_flag)
end
2011-03-27 22:56:15 +02:00
2014-08-02 20:36:38 +02:00
# set the options if they don't already exist
window.options ||= "&results=60&dynamic=true"
# associate the window with the channel
self.windows.push window
# set the html
window.html = "<iframe id=\"iframe#{window.id}\" width=\"450\" height=\"260\" style=\"border: 1px solid #cccccc;\" src=\"/channels/#{self.id}/charts/#{field_number.to_s}?width=450&height=260::OPTIONS::\" ></iframe>"
2011-03-27 22:56:15 +02:00
2014-08-02 20:36:38 +02:00
# save the window, and raise an exception if it fails
if !window.save
raise "The Window could not be saved"
end
end
2011-03-27 22:56:15 +02:00
2014-08-01 21:23:28 +02:00
# set the default channel name
def set_default_name
update_attribute(:name, "#{I18n.t(:channel_default_name)} #{self.id}") if self.name.blank?
end
end
2011-03-27 22:56:15 +02:00