Class: BuildTool::History::CommandLog
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- BuildTool::History::CommandLog
- Includes:
- StateHelper
- Defined in:
- lib/build-tool/model/command_log.rb
Overview
Represents a log entry for a command.
Constant Summary
Constants included from StateHelper
StateHelper::CANCELED_BY_USER, StateHelper::FINISHED_SUCCESSFUL, StateHelper::FINISHED_WITH_ERRORS, StateHelper::STARTED
Class Method Summary collapse
-
.last(count = 50) ⇒ Array
Get the last :count issued commands.
-
.last_by_module(modname, count = 3) ⇒ Object
Get the last :count command entries containing module :modname.
-
.last_success_by_module(modname) ⇒ Object
Return the last entry with the given state.
-
.most_recent(offset = nil) ⇒ Object
Loads the most recents entry from the database.
-
.older_than ⇒ Object
Get all commands older than :days.
Instance Method Summary collapse
-
#before_update ⇒ Object
Sequel Hook.
- #duration ⇒ Object
-
#finished(state) ⇒ Object
Call this if the command is finished.
-
#started ⇒ Object
Call this if the command is started.
Methods included from StateHelper
included, #state_char, #state_str
Class Method Details
.last(count = 50) ⇒ Array
Get the last :count issued commands
73 74 75 |
# File 'lib/build-tool/model/command_log.rb', line 73 def last( count = 50 ) order( 'id DESC' ).limit(count).all end |
.last_by_module(modname, count = 3) ⇒ Object
Get the last :count command entries containing module :modname.
78 79 80 |
# File 'lib/build-tool/model/command_log.rb', line 78 def last_by_module( modname, count = 3 ) where( 'id in ( select distinct command_log_id from module_logs where module = ? )', modname ).order( 'id DESC' ).first( count ); end |
.last_success_by_module(modname) ⇒ Object
Return the last entry with the given state.
83 84 85 86 87 88 |
# File 'lib/build-tool/model/command_log.rb', line 83 def last_success_by_module( modname ) where( "command_logs.id not in ( select command_log_id from module_logs where module_logs.module = ? and module_logs.state != #{StateHelper::FINISHED_SUCCESSFUL})", modname ). joins( "INNER JOIN module_logs ON module_logs.command_log_id = command_logs.id and module_logs.module = \"#{modname}\"" ). order( 'id DESC' ). first() end |
.most_recent(offset = nil) ⇒ Object
Loads the most recents entry from the database
61 62 63 64 65 66 67 |
# File 'lib/build-tool/model/command_log.rb', line 61 def most_recent( offset = nil ) if offset.nil? return where( "id = (SELECT MAX(id) FROM #{table_name})" ).first else return order( 'id DESC' ).limit( 1 ).offset( offset ).first end end |
.older_than ⇒ Object
Get all commands older than :days
92 93 94 |
# File 'lib/build-tool/model/command_log.rb', line 92 def older_than where( 'finished_at < ?', Date.today - 10 ) end |
Instance Method Details
#before_update ⇒ Object
Sequel Hook
Make sure a finished command has one of the accepted finished states.
45 46 47 48 49 50 |
# File 'lib/build-tool/model/command_log.rb', line 45 def before_update super if !self.finished_at.nil? raise StandardError, "Wrong state for finished Command" if ! [ FINISHED_SUCCESSFUL, FINISHED_WITH_ERRORS, CANCELED_BY_USER ].include? self.state end end |
#duration ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/build-tool/model/command_log.rb', line 18 def duration if self.finished_at dur = self.finished_at - self.started_at "%02d:%02d" % [ dur.to_i / 60, (dur% 60 ).to_i ] else "-----" end end |
#finished(state) ⇒ Object
Call this if the command is finished. [:finished] will be set to Time.now,
- :state
-
to the given value and the object is saved.
29 30 31 32 33 |
# File 'lib/build-tool/model/command_log.rb', line 29 def finished( state ) self.finished_at = Time.now self.state = state save! end |
#started ⇒ Object
Call this if the command is started. [:started] will be set to Time.now, [:state] to STARTED and the object is saved.
37 38 39 40 |
# File 'lib/build-tool/model/command_log.rb', line 37 def started self.started_at = Time.now save! end |