def cdx_sort_closest(closest, cdx_iter, limit=10): """ sort CDXCaptureResult by closest to timestamp. """ closest_cdx = [] closest_keys = [] closest_sec = timestamp_to_sec(closest) for cdx in cdx_iter: sec = timestamp_to_sec(cdx[TIMESTAMP]) key = abs(closest_sec - sec) # create tuple to sort by key #bisect.insort(closest_cdx, (key, cdx)) i = bisect.bisect_right(closest_keys, key) closest_keys.insert(i, key) closest_cdx.insert(i, cdx) if len(closest_cdx) == limit: # assuming cdx in ascending order and keys have started increasing if key > closest_keys[-1]: break if len(closest_cdx) > limit: closest_cdx.pop() for cdx in closest_cdx: yield cdx
def make_head_insert(rule, cdx): params['wombat_ts'] = cdx['timestamp'] if include_ts else '' params['wombat_sec'] = timestamp_to_sec(cdx['timestamp']) params['is_live'] = cdx.get('is_live') if self.banner_view: banner_html = self.banner_view.render_to_string(env, cdx=cdx, **params) params['banner_html'] = banner_html return self.render_to_string(env, cdx=cdx, **params)
def make_head_insert(rule, cdx): params['wombat_ts'] = cdx['timestamp'] if include_ts else '' params['wombat_sec'] = timestamp_to_sec(cdx['timestamp']) params['is_live'] = 'true' if cdx.get('is_live') else 'false' if self.banner_view: banner_html = self.banner_view.render_to_string(env, cdx=cdx, **params) params['banner_html'] = banner_html return self.render_to_string(env, cdx=cdx, **params)
def format_ts(value, format_='%a, %b %d %Y %H:%M:%S'): """Formats the supplied timestamp using format_ :param str value: The timestamp to be formatted :param str format_: The format string :return: The correctly formatted timestamp as determined by format_ :rtype: str """ if format_ == '%s': return timestamp_to_sec(value) else: value = timestamp_to_datetime(value) return value.strftime(format_)
def format_ts(value, format_='%a, %b %d %Y %H:%M:%S'): if format_ == '%s': return timestamp_to_sec(value) else: value = timestamp_to_datetime(value) return value.strftime(format_)