예제 #1
0
파일: feed.py 프로젝트: joefutrelle/oii
 def latest(self,n=25,timestamp=None):
     """most recent n bins from the given timestamp (defaults to now)"""
     if timestamp is None:
         timestamp = utcdtnow()
     return self._ts_query(end_time=timestamp).\
         order_by(desc(Bin.sample_time)).\
         limit(n)
예제 #2
0
파일: orm.py 프로젝트: joefutrelle/oii
 def changed(self, event, state=RUNNING, message=HEARTBEAT, ts=None, ttl=None):
     """call to record the event of a product state change"""
     if event is not None:
         self.event = event
     if state is not None:
         self.state = state
     if message is not None:
         self.message = message
     if ts is None:
         ts = utcdtnow()
     self.ts = ts
     # expiration semantics:
     # if ttl is never set, never expire
     # if ttl is set to FOREVER, no longer expire
     # if ttl is set to an integer, expire in that many seconds
     # if ttl is None, make no change to expiration time or ttl
     if ttl is not None:
         ttl = int(ttl)
         if ttl==FOREVER:
             self.ttl = None
         else:
             self.ttl = ttl
     if self.ttl is not None:
         self.expires = self.ts + timedelta(seconds=self.ttl)
     else:
         self.expires = None
예제 #3
0
파일: feed.py 프로젝트: joefutrelle/oii
 def elapsed(self,timestamp=None):
     """time elapsed since latest bin at the given time (default now) (utc datetime).
     returns a timedelta"""
     if timestamp is None:
         timestamp = utcdtnow()
     latest = self.latest(1,timestamp)[0]
     return timestamp - latest.sample_time
예제 #4
0
파일: feed.py 프로젝트: LouisK130/oii
 def nearest(self, n=1, timestamp=None):
     """nearest bin. timestamp must be a utc datetime, defaults to now"""
     if timestamp is None:
         timestamp = utcdtnow()
     n_before = self._ts_query(end_time=timestamp).order_by(desc(Bin.sample_time)).limit(n).all()
     min_date = datetime2utcdatetime(n_before[-1].sample_time)
     max_date = timestamp + (timestamp - min_date)
     n_after = self._ts_query(start_time=timestamp, end_time=max_date).order_by(Bin.sample_time).limit(n).all()
     cand = list(n_before) + list(n_after)
     return sorted(cand, key=lambda b: timestamp - datetime2utcdatetime(b.sample_time))[:n]
예제 #5
0
파일: app.py 프로젝트: LouisK130/oii
def ts_metric(ts_label, callback, start=None, end=None, s=None):
    if s is None:
        s = 86400
    if end is None:
        end = utcdtnow()
    if start is None:
        start = end - timedelta(seconds=s)
    with Feed(session, ts_label) as feed:
        result = []
        for b in feed.time_range(start, end):
            r = canonicalize_bin(ts_label, b)
            r.update(callback(b))
            result.append(r)
    return Response(json.dumps(result), mimetype=MIME_JSON)
예제 #6
0
파일: orm.py 프로젝트: joefutrelle/oii
 def expire(self, state=RUNNING, new_state=WAITING, event='expired', message=None, **kw):
     """allow products to expire whose most recent event is older
     than their TTL allows"""
     if state == new_state:
         raise ValueError('state and new_state are both %s' % state)
     now = utcdtnow()
     n = 0
     for p in self.session.query(Product).\
         filter(and_(Product.ttl.isnot(None),Product.ttl!=FOREVER)).\
         filter(Product.expires.isnot(None)).\
         filter(now > Product.expires).\
         with_lockmode('update'):
         p.changed('expired', new_state)
         n += 1
     self.session.commit()
     return n
예제 #7
0
파일: orm.py 프로젝트: joefutrelle/oii
 def older_than(self, ago):
     """find all products whose events occurred longer than ago ago.
     ago must be a datetime.timedelta"""
     now = utcdtnow()
     return self.session.query(Product).\
         filter(Product.ts < now - ago)