event.rb 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  2. #
  3. # Copyright (c) 2016, Electric Power Research Institute (EPRI)
  4. # All rights reserved.
  5. #
  6. # OpenADR ("this software") is licensed under BSD 3-Clause license.
  7. #
  8. # Redistribution and use in source and binary forms, with or without modification,
  9. # are permitted provided that the following conditions are met:
  10. #
  11. # * Redistributions of source code must retain the above copyright notice, this
  12. # list of conditions and the following disclaimer.
  13. #
  14. # * Redistributions in binary form must reproduce the above copyright notice,
  15. # this list of conditions and the following disclaimer in the documentation
  16. # and/or other materials provided with the distribution.
  17. #
  18. # * Neither the name of EPRI nor the names of its contributors may
  19. # be used to endorse or promote products derived from this software without
  20. # specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. # OF SUCH DAMAGE.
  32. #
  33. # This EPRI software incorporates work covered by the following copyright and permission
  34. # notices. You may not use these works except in compliance with their respective
  35. # licenses, which are provided below.
  36. #
  37. # These works are provided by the copyright holders and contributors "as is" and any express or
  38. # implied warranties, including, but not limited to, the implied warranties of merchantability
  39. # and fitness for a particular purpose are disclaimed.
  40. #
  41. #########################################################################################
  42. # MIT Licensed Libraries
  43. #########################################################################################
  44. #
  45. # * actionmailer 3.2.12 (http://www.rubyonrails.org) - Email composition, delivery, and receiving framework (part of Rails).
  46. # * actionpack 3.2.12 (http://www.rubyonrails.org) - Web-flow and rendering framework putting the VC in MVC (part of Rails).
  47. # * activemodel 3.2.12 (http://www.rubyonrails.org) - A toolkit for building modeling frameworks (part of Rails).
  48. # * activerecord 3.2.12 (http://www.rubyonrails.org) - Object-relational mapper framework (part of Rails).
  49. # * activeresource 3.2.12 (http://www.rubyonrails.org) - REST modeling framework (part of Rails).
  50. # * activesupport 3.2.12 (http://www.rubyonrails.org) - A toolkit of support libraries and Ruby core extensions extracted from the Rails framework.
  51. # * arel 3.0.2 (http://github.com/rails/arel) - Arel is a SQL AST manager for Ruby
  52. # * bootstrap-sass 3.1.1.0 (https://github.com/twbs/bootstrap-sass) - Twitter's Bootstrap, converted to Sass and ready to drop into Rails or Compass
  53. # * builder 3.0.4 (http://onestepback.org) - Builders for MarkUp.
  54. # * bundler 1.12.5 (http://bundler.io) - The best way to manage your application's dependencies
  55. # * capybara 2.4.4 (http://github.com/jnicklas/capybara) - Capybara aims to simplify the process of integration testing Rack applications, such as Rails, Sinatra or Merb
  56. # * coffee-rails 3.2.2 () - Coffee Script adapter for the Rails asset pipeline.
  57. # * coffee-script-source 1.6.3 (http://jashkenas.github.com/coffee-script/) - The CoffeeScript Compiler
  58. # * docile 1.1.5 (https://ms-ati.github.io/docile/) - Docile keeps your Ruby DSLs tame and well-behaved
  59. # * edn 1.0.0 () - 'edn implements a reader for Extensible Data Notation by Rich Hickey.'
  60. # * erubis 2.7.0 (http://www.kuwata-lab.com/erubis/) - a fast and extensible eRuby implementation which supports multi-language
  61. # * execjs 1.4.0 (https://github.com/sstephenson/execjs) - Run JavaScript code from Ruby
  62. # * factory_girl 4.5.0 (https://github.com/thoughtbot/factory_girl) - factory_girl provides a framework and DSL for defining and using model instance factories.
  63. # * factory_girl_rails 4.5.0 (http://github.com/thoughtbot/factory_girl_rails) - factory_girl_rails provides integration between factory_girl and rails 3
  64. # * gem-licenses 0.1.2 (http://github.com/dblock/gem-licenses) - List all gem licenses.
  65. # * hike 1.2.3 (http://github.com/sstephenson/hike) - Find files in a set of paths
  66. # * i18n 0.6.5 (http://github.com/svenfuchs/i18n) - New wave Internationalization support for Ruby
  67. # * jdbc-postgresql 9.2.1000 (https://github.com/rosenfeld/jdbc-postgresql) - PostgresSQL jdbc driver for JRuby
  68. # * journey 1.0.4 (http://github.com/rails/journey) - Journey is a router
  69. # * jquery-rails 3.0.4 (http://rubygems.org/gems/jquery-rails) - Use jQuery with Rails 3
  70. # * json-schema 2.6.2 (http://github.com/ruby-json-schema/json-schema/tree/master) - Ruby JSON Schema Validator
  71. # * mail 2.4.4 (http://github.com/mikel/mail) - Mail provides a nice Ruby DSL for making, sending and reading emails.
  72. # * metaclass 0.0.4 (http://github.com/floehopper/metaclass) - Adds a metaclass method to all Ruby objects
  73. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  74. # * mocha 1.1.0 (http://gofreerange.com/mocha/docs) - Mocking and stubbing library
  75. # * multi_json 1.7.9 (http://github.com/intridea/multi_json) - A common interface to multiple JSON libraries.
  76. # * nokogiri 1.6.5 (http://nokogiri.org) - Nokogiri (鋸) is an HTML, XML, SAX, and Reader parser
  77. # * polyglot 0.3.3 (http://github.com/cjheath/polyglot) - Augment 'require' to load non-Ruby file types
  78. # * rack-test 0.6.2 (http://github.com/brynary/rack-test) - Simple testing API built on Rack
  79. # * railties 3.2.12 (http://www.rubyonrails.org) - Tools for creating, working with, and running Rails applications.
  80. # * rake 10.1.0 (http://rake.rubyforge.org) - Ruby based make-like utility.
  81. # * rspec-core 2.14.3 (http://github.com/rspec/rspec-core) - rspec-core-2.14.3
  82. # * rspec-expectations 2.14.0 (http://github.com/rspec/rspec-expectations) - rspec-expectations-2.14.0
  83. # * rspec-mocks 2.14.1 (http://github.com/rspec/rspec-mocks) - rspec-mocks-2.14.1
  84. # * rspec-rails 2.14.0 (http://github.com/rspec/rspec-rails) - rspec-rails-2.14.0
  85. # * sass 3.2.9 (http://sass-lang.com/) - A powerful but elegant CSS compiler that makes CSS fun again.
  86. # * sass-rails 3.2.6 () - Sass adapter for the Rails asset pipeline.
  87. # * simplecov 0.9.0 (http://github.com/colszowka/simplecov) - Code coverage for Ruby 1.9+ with a powerful configuration library and automatic merging of coverage across test suites
  88. # * spork 1.0.0rc3 (http://github.com/sporkrb/spork) - spork
  89. # * therubyrhino 2.0.2 (http://github.com/cowboyd/therubyrhino) - Embed the Rhino JavaScript interpreter into JRuby
  90. # * thor 0.18.1 (http://whatisthor.com/) - A scripting framework that replaces rake, sake and rubigen
  91. # * tilt 1.4.1 (http://github.com/rtomayko/tilt/) - Generic interface to multiple Ruby template engines
  92. # * treetop 1.4.14 (https://github.com/cjheath/treetop) - A Ruby-based text parsing and interpretation DSL
  93. # * uglifier 2.1.2 (http://github.com/lautis/uglifier) - Ruby wrapper for UglifyJS JavaScript compressor
  94. # * xpath 2.0.0 (http://github.com/jnicklas/xpath) - Generate XPath expressions from Ruby
  95. # * blankslate 2.1.2.4 (http://github.com/masover/blankslate) - BlankSlate extracted from Builder.
  96. # * bourbon 3.1.8 (https://github.com/thoughtbot/bourbon) - Bourbon Sass Mixins using SCSS syntax.
  97. # * coffee-script 2.2.0 (http://github.com/josh/ruby-coffee-script) - Ruby CoffeeScript Compiler
  98. # * diff-lcs 1.2.4 (http://diff-lcs.rubyforge.org/) - Diff::LCS computes the difference between two Enumerable sequences using the McIlroy-Hunt longest common subsequence (LCS) algorithm
  99. # * jquery-ui-rails 4.0.3 (https://github.com/joliss/jquery-ui-rails) - jQuery UI packaged for the Rails asset pipeline
  100. # * parslet 1.4.0 (http://kschiess.github.com/parslet) - Parser construction library with great error reporting in Ruby.
  101. # * rack 1.4.5 (http://rack.github.com/) - a modular Ruby webserver interface
  102. # * rack-cache 1.2 (http://tomayko.com/src/rack-cache/) - HTTP Caching for Rack
  103. # * rack-ssl 1.3.3 (https://github.com/josh/rack-ssl) - Force SSL/TLS in your app.
  104. # * rails 3.2.12 (http://www.rubyonrails.org) - Full-stack web application framework.
  105. # * simplecov-html 0.8.0 (https://github.com/colszowka/simplecov-html) - Default HTML formatter for SimpleCov code coverage tool for ruby 1.9+
  106. # * tzinfo 0.3.37 (http://tzinfo.rubyforge.org/) - Daylight-savings aware timezone library
  107. # * warbler 1.4.0.beta1 (http://caldersphere.rubyforge.org/warbler) - Warbler chirpily constructs .war files of your Rails applications.
  108. #
  109. #########################################################################################
  110. # BSD Licensed Libraries
  111. #########################################################################################
  112. #
  113. # * activerecord-jdbc-adapter 1.2.9.1 (https://github.com/jruby/activerecord-jdbc-adapter) - Copyright (c) 2006-2012 Nick Sieger <nick@nicksieger.com>, Copyright (c) 2006-2008 Ola Bini <ola.bini@gmail.com>
  114. # * jdbc-postgres 9.2.1004 (https://github.com/jruby/activerecord-jdbc-adapter) - Copyright (c) 1997-2011, PostgreSQL Global Development Group
  115. # * d3js 3.5.16 (https://d3js.org/) Copyright (c) 2015 Mike Bostock
  116. #
  117. #########################################################################################
  118. # Ruby Licensed Libraries
  119. #########################################################################################
  120. #
  121. # * json 1.8.0 (http://json-jruby.rubyforge.org/) - JSON implementation for JRuby
  122. # * rubyzip 0.9.9 (http://github.com/aussiegeek/rubyzip) - rubyzip is a ruby module for reading and writing zip files
  123. # * httpclient 2.3.4.1 (http://github.com/nahi/httpclient) - gives something like the functionality of libwww-perl (LWP) in Ruby
  124. # * test-unit 2.5.5 (http://test-unit.rubyforge.org/) - test-unit - Improved version of Test::Unit bundled in Ruby 1.8.x.
  125. #
  126. #########################################################################################
  127. # Public domain - creative commons Licensed Libraries
  128. #########################################################################################
  129. #
  130. # * torquebox 3.1.2 (http://torquebox.org/) - TorqueBox Gem
  131. # * torquebox-cache 3.1.2 (http://torquebox.org/) - TorqueBox Cache Gem
  132. # * torquebox-configure 3.1.2 (http://torquebox.org/) - TorqueBox Configure Gem
  133. # * torquebox-core 3.1.2 (http://torquebox.org/) - TorqueBox Core Gem
  134. # * torquebox-messaging 3.1.2 (http://torquebox.org/) - TorqueBox Messaging Client
  135. # * torquebox-naming 3.1.2 (http://torquebox.org/) - TorqueBox Naming Client
  136. # * torquebox-rake-support 3.1.2 (http://torquebox.org/) - TorqueBox Rake Support
  137. # * torquebox-security 3.1.2 (http://torquebox.org/) - TorqueBox Security Gem
  138. # * torquebox-server 3.1.2 (http://torquebox.org/) - TorqueBox Server Gem
  139. # * torquebox-stomp 3.1.2 (http://torquebox.org/) - TorqueBox STOMP Support
  140. # * torquebox-transactions 3.1.2 (http://torquebox.org/) - TorqueBox Transactions Gem
  141. # * torquebox-web 3.1.2 (http://torquebox.org/) - TorqueBox Web Gem
  142. #
  143. #########################################################################################
  144. # Apache Licensed Libraries
  145. #########################################################################################
  146. #
  147. # * addressable 2.3.8 (https://github.com/sporkmonger/addressable) - URI Implementation
  148. # * bcrypt-ruby 3.0.1 (http://bcrypt-ruby.rubyforge.org) - OpenBSD's bcrypt() password hashing algorithm.
  149. # * database_cleaner 1.4.0 (http://github.com/bmabey/database_cleaner) - Strategies for cleaning databases. Can be used to ensure a clean state for testing.
  150. # * annotate 2.5.0 (http://github.com/ctran/annotate_models) - Annotates Rails Models, routes, fixtures, and others based on the database schema.
  151. # * nvd3 1.8.4 (http://nvd3.org/) Copeyright (c) 2014 Novus Partners - chart library based on d3js
  152. # * smack 3.3.1 (https://www.igniterealtime.org/projects/smack/) - XMPP library
  153. #
  154. #########################################################################################
  155. # LGPL
  156. #########################################################################################
  157. #
  158. # * jruby-1.7.4
  159. # * jruby-jars 1.7.4 (http://github.com/jruby/jruby/tree/master/gem/jruby-jars) - The core JRuby code and the JRuby stdlib as jar
  160. # ** JRuby is tri-licensed GPL, LGPL, and EPL.
  161. #
  162. #########################################################################################
  163. # MPL Licensed Libraries
  164. #########################################################################################
  165. #
  166. # * therubyrhino_jar 1.7.4 (http://github.com/cowboyd/therubyrhino) - Rhino's jars packed for therubyrhino
  167. #
  168. #########################################################################################
  169. # Artistic 2.0
  170. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  171. #
  172. #########################################################################################
  173. #
  174. #########################################################################################
  175. # GPL-2
  176. #########################################################################################
  177. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  178. #
  179. #########################################################################################
  180. # No License Given
  181. #########################################################################################
  182. #
  183. # * spork-testunit 0.0.8 (http://github.com/timcharper/spork-testunit) - spork-testunit
  184. # * sprockets 2.2.2 (http://getsprockets.org/) - Rack-based asset packaging system
  185. #
  186. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  187. # == Schema Information
  188. #
  189. # Table name: events
  190. #
  191. # id :integer not null, primary key
  192. # event_id :string(255)
  193. # modification_number :integer
  194. # priority :integer
  195. # market_context_id :integer
  196. # event_status_id :integer
  197. # test_event :string(255)
  198. # vtn_comment :string(255)
  199. # dtstart :datetime
  200. # duration :integer
  201. # tolerance :integer default(0)
  202. # ei_notification :integer
  203. # ei_rampup :integer default(0)
  204. # ei_recovery :integer default(0)
  205. # created_at :datetime not null
  206. # updated_at :datetime not null
  207. # response_required_type_id :integer
  208. # template :boolean
  209. # schedule_id :integer
  210. # published :boolean
  211. # payload :text
  212. # account_id :integer
  213. # include_subscribed_vens_only :boolean
  214. # include_party_id :boolean
  215. # time_zone :string(255) default("UTC")
  216. # event_interface_name :string(255)
  217. #
  218. # dtstart_str is a "shadow" attribute of dtstart
  219. # without this attribute, default code will assign a string from the user directly to the datetime
  220. # object. if the string is invalid, dtstart will be null and there's no way to detect if the user
  221. # intended to set the value. using the shadow attribute, we tell when the user attempted to set
  222. # the value and catch invalid values in the model. alternatively, this could be handled in the
  223. # controller, but that requires injecting code in every action where the date could be invovled
  224. # this method handles them all in one spot
  225. class Event < ActiveRecord::Base
  226. attr_accessible :duration, :ei_notification, :ei_rampup, :ei_recovery, :event_id,
  227. :event_status_id, :market_context_id, :priority, :test_event, :tolerance, :vtn_comment,
  228. :response_required_type_id, :account_id, :include_subscribed_vens_only, :include_party_id,
  229. :event_interface_name, :time_zone, :dtstart_str#, :dtstart
  230. attr_writer :signal_name_id, :signal_type_id, :payload_value
  231. scope :in_datetime_range, ->(first_datetime, last_datetime) { where("dtstart >= ? AND dtstart < ?", first_datetime, last_datetime) }
  232. scope :order_by_dtstart, -> { order('dtstart ASC') }
  233. scope :order_by_duration, -> { order('duration DESC') }
  234. scope :active, -> { where("event_status_id not in (?)", EventStatus.inactive.pluck(:id)) }
  235. scope :active24, -> { where("event_status_id not in (?) or (event_status_id in (?) and events.updated_at < ?)", EventStatus.inactive.pluck(:id), EventStatus.inactive.pluck(:id), Time.now + 24.hours) }
  236. belongs_to :market_context
  237. belongs_to :event_status
  238. belongs_to :response_required_type
  239. belongs_to :schedule
  240. belongs_to :account
  241. # creating/removing these associations will add/remove VENs from this event
  242. # the callbacks ensure that VENs that are added/removed from the event receive a distributeEvent message
  243. # HACK: the after_add callback is getting called before the record is saved
  244. # added event_target to handle callbacks when associations are added
  245. # has_many :event_groups, :dependent => :destroy, before_add: :before_target_change, after_add: :after_target_change, before_remove: :before_target_change, after_remove: :after_target_change
  246. has_many :event_groups, :dependent => :destroy, before_remove: :before_target_change, after_remove: :after_target_change
  247. has_many :event_resources, :dependent => :destroy, before_remove: :before_target_change, after_remove: :after_target_change
  248. has_many :event_vens, :dependent => :destroy, before_remove: :before_target_change, after_remove: :after_target_change
  249. has_many :event_parties, :dependent => :destroy, before_remove: :before_target_change, after_remove: :after_target_change
  250. has_many :event_responses, :dependent => :destroy, order: "created_at DESC"
  251. has_many :event_opts, :dependent => :destroy, order: "created_at DESC"
  252. #####################################################################
  253. # event targets
  254. has_many :event_targets
  255. has_many :targets, through: :event_targets
  256. # Returns collection of an instance's targeted VENs (no duplicates for multi-targeted VENs)
  257. has_many :vens, through: :targets, uniq: true
  258. has_many :groups, through: :event_groups
  259. has_many :resource_types, through: :event_resources
  260. has_many :target_vens, through: :event_vens, source: :ven
  261. has_many :market_context_subscriptions, through: :event_parties
  262. #####################################################################
  263. has_many :event_signals, :dependent => :destroy
  264. has_many :schedules, dependent: :destroy
  265. #####################################################################
  266. # ven associations: which vens are tied to this event
  267. # all of these return a list of vens
  268. has_many :ven_events, through: :event_vens, source: :ven
  269. # groups
  270. has_many :group_members, through: :groups
  271. # has_many :account, through: :group_members
  272. has_many :ven_groups, through: :account, source: :vens
  273. # parties
  274. has_many :ven_market_context_subscriptions, through: :market_context_subscriptions, source: :ven
  275. # resources
  276. has_many :resources, through: :resource_types
  277. has_many :ven_resources, through: :resources, source: :ven
  278. #####################################################################
  279. # none of these are event fields but they're used to create a default
  280. # signal. There's probably a better way to do this
  281. attr_accessor :dtstart_str, :signal_type_id, :signal_name_id, :payload_value
  282. #####################################################################
  283. after_initialize do |event|
  284. # Set the value to return when `:dtstart_str` is called from outside this class
  285. # (prevents conflict when setting `:dtstart` based on the `:dtstart_str` passed in via params)
  286. event.dtstart_str = event.dtstart.in_time_zone(event.time_zone).strftime("%Y-%m-%d %-l:%M%P") unless event.dtstart.nil? || event.time_zone.nil?
  287. end
  288. #####################################################################
  289. validates :response_required_type_id, :presence => true
  290. validates :duration, :presence => true, :numericality => { greater_than_or_equal_to: 0, only_integer: true }
  291. validates :event_id, :presence => true, uniqueness: { case_sensitive: false }
  292. validates :market_context_id, :presence => true
  293. validates :priority, :presence => true
  294. validates :tolerance, numericality: { only_integer: true }
  295. validates :ei_notification, numericality: { only_integer: true }
  296. validates :ei_rampup, numericality: { only_integer: true }
  297. validates :ei_recovery, numericality: { only_integer: true }
  298. #####################################################################
  299. validates :time_zone,
  300. presence: true,
  301. inclusion: { in: ActiveSupport::TimeZone.zones_map }
  302. #####################################################################
  303. validates_each :dtstart_str do |record, atr, value|
  304. if (value && (DateTime.parse(value) rescue ArgumentError) == ArgumentError)
  305. record.errors.add(atr, 'must be a valid datetime')
  306. else
  307. if not value.nil?
  308. record.set_dtstart
  309. if record.overlaps
  310. # record.errors.add_to_base('Event overlaps with an existing event')
  311. record.errors.add(atr, 'overlaps with an existing event')
  312. record.errors.add(:duration, 'overlaps with an existing event')
  313. end
  314. end
  315. end
  316. end
  317. #####################################################################
  318. #before_save do |event|
  319. # event.modification_number = 0 #? event.modification_number += 1 : event.modification_number = 0
  320. #end
  321. #####################################################################
  322. before_validation(:on => :create) do
  323. self.event_status = EventStatus.find_by_name("far")
  324. self.modification_number = -1
  325. self.published = false
  326. true
  327. end
  328. #####################################################################
  329. #after_save do |event|
  330. # set the modification_number to 0 when first created
  331. # if event.modification_number == -1
  332. # event.update_column('modification_number', 0)
  333. # else
  334. # event.update_column('modification_number', event.modification_number + 1)
  335. # end
  336. # EventService.new.queue_distribute_event(event.vens)
  337. #end
  338. #####################################################################
  339. ## Target association callbacks
  340. ## creating/removing an association will add/remove VENS from this
  341. ## event. any change to the event list should trigger a distributeEvent
  342. ## message to the VENs. this is handled by queueing a message through EventService
  343. ## EventService.new.queue_distribute_event(@affected_vens)
  344. #####################################################################
  345. def before_target_change(assoc_target)
  346. # @ven_list_before_target_change = self.vens
  347. end
  348. #####################################################################
  349. # search for overlapping events
  350. def overlaps
  351. begin
  352. # candidate events are in the same market context and are not cancelled and are not a template
  353. where_clause = "market_context_id = #{self.market_context_id} and event_status_id != #{EventStatus.find_by_name('cancelled').id} and (template = false or template is null)"
  354. # and are not this event
  355. where_clause = where_clause + " and id != #{self.id}" if not self.id.nil?
  356. events = Event.where(where_clause)
  357. events.each do |event|
  358. if self.dtend > event.dtstart and self.dtend <= event.dtend
  359. return true
  360. end
  361. if event.dtend > self.dtstart and event.dtend <= self.dtend
  362. return true
  363. end
  364. end
  365. rescue
  366. end
  367. false
  368. end
  369. #####################################################################
  370. def set_dtstart
  371. # Convert dtstart_str and time_zone to appropriate datetime with zone (which DB will convert to UTC)
  372. Time.use_zone(self.time_zone) do
  373. self.dtstart = Time.zone.parse(dtstart_str)
  374. end
  375. end
  376. #####################################################################
  377. def dtend
  378. start_after = self.tolerance.nil? ? 0 : self.tolerance
  379. self.dtstart + (self.duration + start_after).minutes
  380. end
  381. #####################################################################
  382. def after_target_change(assoc_target)
  383. self.unpublish
  384. #self.reload
  385. #@ven_list_after_target_change = self.vens
  386. # subtraction gives the elements in the first array that are not in the second array
  387. #if @ven_list_before_target_change.count > @ven_list_after_target_change.count
  388. # @affected_vens = @ven_list_before_target_change - @ven_list_after_target_change
  389. #else
  390. # @affected_vens = @ven_list_after_target_change - @ven_list_before_target_change
  391. #end
  392. #EventService.new.queue_distribute_event(@affected_vens)
  393. end
  394. #####################################################################
  395. def market_context_color
  396. self.market_context.color if self.market_context
  397. end
  398. #####################################################################
  399. def self.fields
  400. EventFields.new.fields
  401. end
  402. #####################################################################
  403. def self.generate_ven_payloads(vens, push_payload=true)
  404. vens.each do |ven|
  405. ven.generate_distribute_event_payload_value # Method only sets value (does not save ven)
  406. ven.save
  407. end
  408. # notify all vens that the message has changed
  409. EventService.new.queue_distribute_event(vens) if push_payload
  410. end
  411. #####################################################################
  412. def publish(increment_modification_number=true, push_payload=true)
  413. self.reload
  414. if increment_modification_number or not self.published
  415. self.modification_number = self.modification_number + 1
  416. self.published = true
  417. self.save
  418. end
  419. # generate the event payload
  420. Event.generate_ven_payloads(self.vens, push_payload)
  421. end
  422. #####################################################################
  423. def unpublish
  424. # set publish == false
  425. # indicates some portion of the event has been modified
  426. self.update_column('published', false)
  427. end
  428. #####################################################################
  429. def set_defaults
  430. self.event_id = SecureRandom.hex(10)
  431. self.tolerance ||= 0
  432. self.ei_notification = 0
  433. self.ei_rampup = 0
  434. self.ei_recovery = 0
  435. self.priority ||= 0
  436. end
  437. #####################################################################
  438. def self.search(params, time_zone, account)
  439. collected_events = []
  440. Time.use_zone(time_zone) do
  441. if params[:first_date_string].present?
  442. first_date = params[:first_date_string].to_date
  443. else
  444. first_date = (Time.now - 1.day).to_date
  445. end
  446. if params[:last_date_string].present?
  447. last_date = params[:last_date_string].to_date
  448. else
  449. last_date = (Time.now + 2.days).to_date
  450. end
  451. if first_date > last_date
  452. # Swap values, so first_date < last_date
  453. first_date, last_date = last_date, first_date
  454. end
  455. range_start_datetime = first_date.beginning_of_day
  456. range_end_datetime = last_date.end_of_day
  457. collected_events = Event.where("dtstart >= ? AND dtstart < ?", range_start_datetime, range_end_datetime).order("dtstart desc")
  458. end
  459. # Searches from non-admin accounts should limit scope of search to that account's events
  460. unless account.present? && account.is_admin?
  461. end
  462. if params[:comment_string] && params[:comment_string].present?
  463. comment_string = params[:comment_string]
  464. events_by_comment_string = Event.where("vtn_comment ILIKE ?", "%#{ comment_string }%")
  465. if collected_events == []
  466. collected_events += events_by_comment_string
  467. else
  468. collected_events &= events_by_comment_string
  469. end
  470. return collected_events if collected_events == []
  471. end
  472. if params[:market_context_ids]
  473. market_context_ids = params[:market_context_ids].map(&:to_i)
  474. else
  475. market_context_ids = MarketContext.all.map(&:id)
  476. end
  477. if params[:status_ids]
  478. status_ids = params[:status_ids].map(&:to_i)
  479. else
  480. status_ids = EventStatus.all.map(&:id)
  481. end
  482. events_to_keep = []
  483. collected_events.each do |event|
  484. if market_context_ids.include?(event.market_context.id) &&
  485. status_ids.include?(event.event_status.id)
  486. events_to_keep.append(event)
  487. end
  488. end
  489. # Search results for non-admin accounts should be scoped to their own events
  490. if account.present? && account.is_admin?
  491. account_limited_events = Event.all
  492. else
  493. account_limited_events = account.events
  494. end
  495. account_limited_events & collected_events & events_to_keep
  496. end
  497. #####################################################################
  498. def event_non_groups
  499. Group.joins("left join event_groups on groups.id = event_groups.group_id and event_groups.event_id = #{self.id}").where("event_groups.id is null")
  500. end
  501. #####################################################################
  502. def event_non_resources
  503. ResourceType.joins("left join event_resources on resource_types.id = event_resources.resource_type_id and event_resources.event_id = #{self.id}").where("event_resources.id is null");
  504. end
  505. #####################################################################
  506. def event_non_vens
  507. Ven.joins("left join event_vens on vens.id = event_vens.ven_id and event_vens.event_id = #{self.id}").where("event_vens.id is null");
  508. end
  509. #####################################################################
  510. def event_non_parties
  511. MarketContextSubscription.joins("left join event_parties on market_context_subscriptions.id = event_parties.market_context_subscription_id and event_parties.event_id = #{self.id}").where("event_parties.id is null");
  512. end
  513. #####################################################################
  514. def event_non_targets
  515. Target.joins("left join event_targets on targets.id = event_targets.target_id and event_targets.event_id = #{self.id}").where("event_targets.id is null");
  516. end
  517. #####################################################################
  518. def cancel
  519. self.with_lock do
  520. return false if self.event_status.name == "completed"
  521. self.event_status = EventStatus.find_by_name("cancelled")
  522. self.save!
  523. self.unpublish
  524. end
  525. true
  526. end
  527. #####################################################################
  528. def last_event_opt(ven_id)
  529. event_opt = self.event_opts.where("ven_id = #{ven_id} and event_modification_number = #{self.modification_number}").order("created_at desc").first
  530. event_opt
  531. end
  532. #####################################################################
  533. def opt_state_name(ven_id)
  534. event_opt = last_event_opt(ven_id)
  535. event_opt.nil? ? '<no opt received>' : event_opt.opt_type.name
  536. end
  537. #####################################################################
  538. # conformance rule 206: if the last opt message was from optCreate,
  539. # any subsequent createdEvent messages MUST be ignored
  540. # return true if the last opt in/out was from a optCreate
  541. # used in the event_service.createEvent to determine if a message shold be ignored
  542. def last_opt_type_is_opt_create?(ven_id)
  543. event_opt = last_event_opt(ven_id)
  544. return false if event_opt.nil?
  545. return false if event_opt.is_create_opt.nil?
  546. event_opt.is_create_opt
  547. end
  548. #####################################################################
  549. def self.update_statuses
  550. events = Event.where("template <> true or template is null")
  551. affected_vens = []
  552. events.each do |event|
  553. begin
  554. if event.update_status
  555. affected_vens = (affected_vens + event.vens).uniq
  556. end
  557. rescue Exception => ex
  558. OadrLogger.instance.log_caught_exception(ex)
  559. end
  560. end
  561. return [events,affected_vens]
  562. end
  563. #####################################################################
  564. def update_status
  565. modified = false
  566. self.with_lock do
  567. status_name = self.event_status.name
  568. return false if status_name == "cancelled" # or self.published == false
  569. now = DateTime.now
  570. if self.dtstart > now
  571. if now + self.ei_rampup.to_i.minutes >= dtstart
  572. status_name = "near"
  573. else
  574. status_name = "far"
  575. end
  576. elsif self.dtstart <= now && (self.dtend > now || self.duration == 0)
  577. status_name = "active"
  578. else
  579. status_name = "completed"
  580. end
  581. if status_name != self.event_status.name
  582. OadrLogger.instance.log_info("event #{self.id} status change: #{self.event_status.name} to #{status_name}")
  583. # use update_column to avoid calling the after_save callback
  584. # plus, event_statuses function handles queuing the distributeEvent message
  585. event_status = EventStatus.find_by_name(status_name)
  586. # self.update_column('event_status_id', event_status.id)
  587. self.event_status = event_status
  588. self.save!
  589. modified = true
  590. end
  591. end
  592. # if the event_status was modified and it's been published, re-publish the event
  593. # we don't want to publish the event if it hasn't been published since that could
  594. # push changes to clients that aren't ready to be pushed
  595. if modified == true and self.published == true
  596. self.publish(false, false)
  597. end
  598. modified
  599. end
  600. #####################################################################
  601. # implements conformance statement 15: event sort order
  602. def self.compare(event1, event2)
  603. # active events occur first
  604. if event1.event_status.name == 'active' and event2.event_status.name != 'active'
  605. return -1
  606. end
  607. if event2.event_status.name == 'active' and event1.event_status.name != 'active'
  608. return 1
  609. end
  610. # completed events are last
  611. if event1.event_status.name == 'completed' and event2.event_status.name != 'completed'
  612. return 1
  613. end
  614. if event2.event_status.name == 'completed' and event1.event_status.name != 'completed'
  615. return -1
  616. end
  617. # between active events, the event with the lowest priority is first
  618. if event1.event_status.name == 'active' and event2.event_status.name == 'active'
  619. if event1.priority < event2.priority
  620. return -1
  621. end
  622. if event2.priority < event1.priority
  623. return 1
  624. end
  625. # events with the same priority order by start date
  626. if event1.dtstart <= event2.dtstart
  627. return -1
  628. end
  629. return 1
  630. end
  631. if event1.dtstart < event2.dtstart
  632. return -1
  633. end
  634. if event2.dtstart < event1.dtstart
  635. return 1
  636. end
  637. if event1.priority < event2.priority
  638. return -1
  639. end
  640. if event2.priority < event1.priority
  641. return 1
  642. end
  643. return 1
  644. end
  645. #####################################################################
  646. def self.deep_clone(event, schedule_id, start_time_str)
  647. new_event = Utility.clone_active_record(event, [:id, :modification_number, :dtstart, :created_at, :updated_at, :template, :schedule_id],
  648. modification_number: 0, template: false, event_id: SecureRandom.hex(10), schedule_id: schedule_id)
  649. # TODO: find a better way to handle virtual attributes
  650. # since this "virtual" attribute isn't in the attributes hash, the clone function
  651. # above can't copy this attribute
  652. # set it manually and dave the object
  653. new_event.dtstart_str = start_time_str
  654. new_event.save
  655. event.event_signals.each do |signal|
  656. new_signal = Utility.clone_active_record(signal, [:id], {event_id: new_event.id, signal_id: SecureRandom.hex(10)})
  657. signal.event_signal_intervals.each do |interval|
  658. new_interval = Utility.clone_active_record(interval, [:id], {event_signal_id: new_signal.id})
  659. end
  660. end
  661. Event.clone_target(event.event_groups, new_event.id)
  662. Event.clone_target(event.event_resources, new_event.id)
  663. Event.clone_target(event.event_vens, new_event.id)
  664. Event.clone_target(event.event_parties, new_event.id)
  665. return new_event
  666. end
  667. #####################################################################
  668. private
  669. def self.clone_target(targets, event_id)
  670. targets.each do |target|
  671. new_target = Utility.clone_active_record(target, [:id], {event_id: event_id})
  672. end
  673. end
  674. end