def filter_by_age(self, source='creation_date', direction=None, timestring=None, unit=None, unit_count=None, epoch=None, exclude=False): """ Remove snapshots from `snapshots` by relative age calculations. :arg source: Source of snapshot age. Can be 'name', or 'creation_date'. :arg direction: Time to filter, either ``older`` or ``younger`` :arg timestring: An strftime string to match the datestamp in an snapshot name. Only used for snapshot filtering by ``name``. :arg unit: One of ``seconds``, ``minutes``, ``hours``, ``days``, ``weeks``, ``months``, or ``years``. :arg unit_count: The number of ``unit`` (s). ``unit_count`` * ``unit`` will be calculated out to the relative number of seconds. :arg epoch: An epoch timestamp used in conjunction with ``unit`` and ``unit_count`` to establish a point of reference for calculations. If not provided, the current time will be used. :arg exclude: If `exclude` is `True`, this filter will remove matching snapshots from `snapshots`. If `exclude` is `False`, then only matching snapshots will be kept in `snapshots`. Default is `False` """ self.loggit.debug('Starting filter_by_age') # Get timestamp point of reference, por por = utils.get_point_of_reference(unit, unit_count, epoch) self.loggit.debug('Point of Reference: {0}'.format(por)) if not direction: raise exceptions.MissingArgument( 'Must provide a value for "direction"') if direction not in ['older', 'younger']: raise ValueError( 'Invalid value for "direction": {0}'.format(direction)) self._calculate_ages(source=source, timestring=timestring) for snapshot in self.working_list(): if not self.snapshot_info[snapshot][self.age_keyfield]: self.loggit.debug('Removing snapshot {0} for having no age') self.snapshots.remove(snapshot) continue msg = ('Snapshot "{0}" age ({1}), direction: "{2}", point of ' 'reference, ({3})'.format( snapshot, utils.fix_epoch( self.snapshot_info[snapshot][self.age_keyfield]), direction, por)) # Because time adds to epoch, smaller numbers are actually older # timestamps. snapshot_age = utils.fix_epoch( self.snapshot_info[snapshot][self.age_keyfield]) if direction == 'older': agetest = snapshot_age < por else: # 'younger' agetest = snapshot_age > por self.__excludify(agetest, exclude, snapshot, msg)
def filter_by_age(self, source='creation_date', direction=None, timestring=None, unit=None, unit_count=None, epoch=None, exclude=False ): """ Remove snapshots from `snapshots` by relative age calculations. :arg source: Source of snapshot age. Can be 'name', or 'creation_date'. :arg direction: Time to filter, either ``older`` or ``younger`` :arg timestring: An strftime string to match the datestamp in an snapshot name. Only used for snapshot filtering by ``name``. :arg unit: One of ``seconds``, ``minutes``, ``hours``, ``days``, ``weeks``, ``months``, or ``years``. :arg unit_count: The number of ``unit`` (s). ``unit_count`` * ``unit`` will be calculated out to the relative number of seconds. :arg epoch: An epoch timestamp used in conjunction with ``unit`` and ``unit_count`` to establish a point of reference for calculations. If not provided, the current time will be used. :arg exclude: If `exclude` is `True`, this filter will remove matching snapshots from `snapshots`. If `exclude` is `False`, then only matching snapshots will be kept in `snapshots`. Default is `False` """ self.loggit.debug('Starting filter_by_age') # Get timestamp point of reference, PoR PoR = utils.get_point_of_reference(unit, unit_count, epoch) self.loggit.debug('Point of Reference: {0}'.format(PoR)) if not direction: raise exceptions.MissingArgument('Must provide a value for "direction"') if direction not in ['older', 'younger']: raise ValueError( 'Invalid value for "direction": {0}'.format(direction) ) self._calculate_ages(source=source, timestring=timestring) for snapshot in self.working_list(): if not self.snapshot_info[snapshot][self.age_keyfield]: self.loggit.debug('Removing snapshot {0} for having no age') self.snapshots.remove(snapshot) continue msg = ( 'Snapshot "{0}" age ({1}), direction: "{2}", point of ' 'reference, ({3})'.format( snapshot, utils.fix_epoch(self.snapshot_info[snapshot][self.age_keyfield]), direction, PoR ) ) # Because time adds to epoch, smaller numbers are actually older # timestamps. snapshot_age = utils.fix_epoch( self.snapshot_info[snapshot][self.age_keyfield]) if direction == 'older': agetest = snapshot_age < PoR else: # 'younger' agetest = snapshot_age > PoR self.__excludify(agetest, exclude, snapshot, msg)
def most_recent(self): """ Return the most recent snapshot based on `start_time_in_millis`. """ self.empty_list_check() most_recent_time = 0 most_recent_snap = '' for snapshot in self.snapshots: snaptime = utils.fix_epoch( self.snapshot_info[snapshot]['start_time_in_millis']) if snaptime > most_recent_time: most_recent_snap = snapshot most_recent_time = snaptime return most_recent_snap
def filter_period( self, period_type='relative', source='name', range_from=None, range_to=None, date_from=None, date_to=None, date_from_format=None, date_to_format=None, timestring=None, unit=None, week_starts_on='sunday', epoch=None, exclude=False, ): """ Match `snapshots` with ages within a given period. :arg period_type: Can be either ``absolute`` or ``relative``. Default is ``relative``. ``date_from`` and ``date_to`` are required when using ``period_type='absolute'`. ``range_from`` and ``range_to`` are required with ``period_type='relative'`. :arg source: Source of snapshot age. Can be 'name', or 'creation_date'. :arg range_from: How many ``unit`` (s) in the past/future is the origin? :arg range_to: How many ``unit`` (s) in the past/future is the end point? :arg date_from: The simplified date for the start of the range :arg date_to: The simplified date for the end of the range. If this value is the same as ``date_from``, the full value of ``unit`` will be extrapolated for the range. For example, if ``unit`` is ``months``, and ``date_from`` and ``date_to`` are both ``2017.01``, then the entire month of January 2017 will be the absolute date range. :arg date_from_format: The strftime string used to parse ``date_from`` :arg date_to_format: The strftime string used to parse ``date_to`` :arg timestring: An strftime string to match the datestamp in an snapshot name. Only used for snapshot filtering by ``name``. :arg unit: One of ``hours``, ``days``, ``weeks``, ``months``, or ``years``. :arg week_starts_on: Either ``sunday`` or ``monday``. Default is ``sunday`` :arg epoch: An epoch timestamp used to establish a point of reference for calculations. If not provided, the current time will be used. :arg exclude: If `exclude` is `True`, this filter will remove matching indices from `indices`. If `exclude` is `False`, then only matching indices will be kept in `indices`. Default is `False` """ self.loggit.debug('Filtering snapshots by period') if period_type not in ['absolute', 'relative']: raise ValueError( 'Unacceptable value: {0} -- "period_type" must be either ' '"absolute" or "relative".'.format(period_type) ) if period_type == 'relative': func = utils.date_range args = [unit, range_from, range_to, epoch] kwgs = { 'week_starts_on': week_starts_on } if type(range_from) != type(int()) or type(range_to) != type(int()): raise exceptions.ConfigurationError( '"range_from" and "range_to" must be integer values') else: func = utils.absolute_date_range args = [unit, date_from, date_to] kwgs = { 'date_from_format': date_from_format, 'date_to_format': date_to_format } for reqd in [date_from, date_to, date_from_format, date_to_format]: if not reqd: raise exceptions.ConfigurationError( 'Must provide "date_from", "date_to", ' '"date_from_format", and "date_to_format" with ' 'absolute period_type' ) try: start, end = func(*args, **kwgs) except Exception as e: utils.report_failure(e) self._calculate_ages(source=source, timestring=timestring) for snapshot in self.working_list(): if not self.snapshot_info[snapshot][self.age_keyfield]: self.loggit.debug('Removing snapshot {0} for having no age') self.snapshots.remove(snapshot) continue age = utils.fix_epoch(self.snapshot_info[snapshot][self.age_keyfield]) msg = ( 'Snapshot "{0}" age ({1}), period start: "{2}", period ' 'end, ({3})'.format( snapshot, age, start, end ) ) # Because time adds to epoch, smaller numbers are actually older # timestamps. inrange = ((age >= start) and (age <= end)) self.__excludify(inrange, exclude, snapshot, msg)