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]
def __init__(self,pid,pathname,length=None,checksum=None,fix_time=None,create_time=None,mod_time=None,checksum_type='md5',fix=True): """all timestamps should be datetimes with timezone""" self.pid = pid self.pathname = pathname self.filename = os.path.basename(pathname) self.length = length self.checksum = checksum self.checksum_type = checksum_type self.fix_time = datetime2utcdatetime(fix_time) self.create_time = datetime2utcdatetime(create_time) self.mod_time = datetime2utcdatetime(mod_time) if fix: self.fix()