lib/ruote/log/storage_history.rb in ruote-2.1.11 vs lib/ruote/log/storage_history.rb in ruote-2.2.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2010, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -30,15 +30,27 @@ # # Warning : don't use this history implementation when the storage is # HashStorage. It will fill up your memory... Keeping history for a # transient ruote is a bit overkill (IMHO). # + # == using the StorageHistory + # + # engine.add_service( + # 'history', 'ruote/log/storage_history', 'Ruote::StorageHistory') + # + # # ... + # + # process_history = engine.history.by_wfid(wfid0) + # + # Note that, by default, the history is an in-memory history (and it is + # useless when there are multiple workers). + # class StorageHistory DATE_REGEX = /!(\d{4}-\d{2}-\d{2})!/ - def initialize (context, options={}) + def initialize(context, options={}) @context = context @options = options if @context.worker @@ -48,12 +60,27 @@ @context.storage.add_type('history') @context.worker.subscribe(:all, self) end end - def by_process (wfid) + # Returns all the wfids for which there are history items (msgs) stored. + # + def wfids + wfids = @context.storage.ids('history').collect { |id| + id.split('!').last + }.uniq.sort + + wfids.delete('no_wfid') + + wfids + end + + # Returns all the msgs for a given wfid (process instance id). + # + def by_process(wfid) + @context.storage.get_many('history', wfid) end alias :by_wfid :by_process # Returns an array [ most recent date, oldest date ] (Time instances). @@ -71,11 +98,16 @@ last = Time.parse("#{lm} 00:00:00 UTC") + 24 * 3600 [ first, last ] end - def by_date (date) + # Returns all the history events for a given day. + # + # Takes as argument whatever is a datetime when turned to a string and + # parsed. + # + def by_date(date) date = Time.parse(date.to_s).strftime('%Y-%m-%d') @context.storage.get_many('history', /!#{date}!/) end @@ -95,10 +127,10 @@ end # This is the method called by the workqueue. Incoming engine events # are 'processed' here. # - def notify (msg) + def notify(msg) msg = msg.dup # a shallow copy is sufficient si = if fei = msg['fei']