# Tai Sakuma <sakuma@fnal.gov>
import multiprocessing
import time

from ProgressReport import ProgressReport

##____________________________________________________________________________||
class ProgressReporter(object):
    def __init__(self, queue, pernevents = 1000):
        self.queue = queue
        self.pernevents = pernevents
        self.lastReportTime = time.time()
        self.interval = 0.02 # [second]

    def report(self, event, component):
        if not self.needToReport(event, component): return
        done = event.iEvent + 1
        report = ProgressReport(name = component.name, done = done, total = event.nEvents)
        self.queue.put(report)
        self.lastReportTime = time.time()

    def needToReport(self, event, component):
        iEvent = event.iEvent + 1 # add 1 because event.iEvent starts from 0
        if time.time() - self.lastReportTime > self.interval: return True
        if iEvent % self.pernevents == 0: return True
        if iEvent == event.nEvents: return True
        return False

##____________________________________________________________________________||

@@ -334,50 +334,8 @@
 nts%0A
-        self.lastReportTime = time.time()%0A
     
@@ -369,16 +369,41 @@
 %5Bsecond%5D
+%0A        self._readTime()
 %0A%0A    de
@@ -666,31 +666,14 @@
 elf.
-lastReportTime = time.t
+_readT
 ime(
@@ -813,13 +813,14 @@
  if 
-time.
+self._
 time
@@ -833,22 +833,16 @@
 elf.last
-Report
 Time %3E s
@@ -961,32 +961,32 @@
 ts: return True%0A
-
         return F
@@ -991,16 +991,111 @@
  False%0A%0A
+    def _time(self): return time.time()%0A    def _readTime(self): self.lastTime = self._time()%0A%0A
 ##______

add new mothods to ProgressReporter to improve testability