def testIndexPatternInString(self): index_info = 'some-index-%{+YYYY.MM.DD}' expected = 'some-index-2015.12.29,some-index-2015.12.30,some-index-2015.12.31,some-index-2016.01.01' built_indexes = tattle.get_indexes(index_info, datemath('2016-01-01||-3d'), datemath('2016-01-01')) self.assertEqual(built_indexes, expected)
def testFromDictDefaultPatternAndDay(self): index_info = {'name': 'some-index-'} expected = 'some-index-2015.12.29,some-index-2015.12.30,some-index-2015.12.31,some-index-2016.01.01' built_indexes = tattle.get_indexes(index_info, datemath('2016-01-01||-3d'), datemath('2016-01-01')) self.assertEqual(built_indexes, expected)
def duration(self, key='matches'): if not self.data: return datemath('now') else: d = getattr(self, key) d = sorted(d, key=itemgetter(self.timestamp)) return datemath(d[-1]) - datemath(d[0])
def testStarIndexNames(self): index_info = 'some-index-*' expected = 'some-index-2015.12.29,some-index-2015.12.30,some-index-2015.12.31,some-index-2016.01.01' built_indexes = tattle.get_indexes(index_info, datemath('2016-01-01||-3d'), datemath('2016-01-01')) self.assertEqual(built_indexes, expected)
def build_indexes(self, index, **kwargs): args = {} for k, v in kwargs.items(): args[k] = v return tattle.get_indexes(index, datemath(kwargs.get('start', 'now-1h')), datemath(kwargs.get('end', 'now')))
def testFromDictOne(self): index_info = { 'pattern': 'YYYY.MM.DD', 'interval': 'day', 'name': 'some-index-' } expected = 'some-index-2015.12.29,some-index-2015.12.30,some-index-2015.12.31,some-index-2016.01.01' built_indexes = tattle.get_indexes(index_info, datemath('2016-01-01||-3d'), datemath('2016-01-01')) self.assertEqual(built_indexes, expected)
def testIndexPatternInStringWithInterval(self): index_info = 'some-index-%{+YYYY.MM.DD.HH}:hour' expected = 'some-index-2015.12.29.00,some-index-2015.12.29.01,some-index-2015.12.29.02,some-index-2015.12.29.03,some-index-2015.12.29.04,some-index-2015.12.29.05,some-index-2015.12.29.06,some-index-2015.12.29.07,some-index-2015.12.29.08,some-index-2015.12.29.09,some-index-2015.12.29.10,some-index-2015.12.29.11,some-index-2015.12.29.12,some-index-2015.12.29.13,some-index-2015.12.29.14,some-index-2015.12.29.15,some-index-2015.12.29.16,some-index-2015.12.29.17,some-index-2015.12.29.18,some-index-2015.12.29.19,some-index-2015.12.29.20,some-index-2015.12.29.21,some-index-2015.12.29.22,some-index-2015.12.29.23,some-index-2015.12.30.00,some-index-2015.12.30.01,some-index-2015.12.30.02,some-index-2015.12.30.03,some-index-2015.12.30.04,some-index-2015.12.30.05,some-index-2015.12.30.06,some-index-2015.12.30.07,some-index-2015.12.30.08,some-index-2015.12.30.09,some-index-2015.12.30.10,some-index-2015.12.30.11,some-index-2015.12.30.12,some-index-2015.12.30.13,some-index-2015.12.30.14,some-index-2015.12.30.15,some-index-2015.12.30.16,some-index-2015.12.30.17,some-index-2015.12.30.18,some-index-2015.12.30.19,some-index-2015.12.30.20,some-index-2015.12.30.21,some-index-2015.12.30.22,some-index-2015.12.30.23,some-index-2015.12.31.00,some-index-2015.12.31.01,some-index-2015.12.31.02,some-index-2015.12.31.03,some-index-2015.12.31.04,some-index-2015.12.31.05,some-index-2015.12.31.06,some-index-2015.12.31.07,some-index-2015.12.31.08,some-index-2015.12.31.09,some-index-2015.12.31.10,some-index-2015.12.31.11,some-index-2015.12.31.12,some-index-2015.12.31.13,some-index-2015.12.31.14,some-index-2015.12.31.15,some-index-2015.12.31.16,some-index-2015.12.31.17,some-index-2015.12.31.18,some-index-2015.12.31.19,some-index-2015.12.31.20,some-index-2015.12.31.21,some-index-2015.12.31.22,some-index-2015.12.31.23,some-index-2016.01.01.00' built_indexes = tattle.get_indexes(index_info, datemath('2016-01-01||-3d'), datemath('2016-01-01')) self.assertEqual(built_indexes, expected) index_info = 'some-index-%{+YYYY.MM.DD.HH}:day' expected = 'some-index-2015.12.29.00,some-index-2015.12.30.00,some-index-2015.12.31.00,some-index-2016.01.01.00' built_indexes = tattle.get_indexes(index_info, datemath('2016-01-01||-3d'), datemath('2016-01-01')) self.assertEqual(built_indexes, expected)
def convert_kibana_time(time_string, current_time, round_direction='down'): """Convert Kibana/ES date math into Python datetimes :param time_string: Time-string following :param current_time: Reference point for date math :return: Datetime object based on ``time_string`` math :Examples: >>> now = datetime(2020, 5, 12, 15, 0, 0) >>> convert_kibana_time("now-3m", now) datetime.datetime(2020, 5, 12, 14, 57, tzinfo=tzutc()) """ if isinstance(current_time, datetime): current_time = arrow.get(current_time) result = arrow.get(datemath.datemath(time_string, now=current_time)) if isinstance(time_string, str) and round_direction == 'up': #Kibana treats time rounding different than elasticsearch if time_string.endswith("/s"): result = result.shift(seconds=1, microseconds=-1) elif time_string.endswith("/m"): result = result.shift(minutes=1, microseconds=-1) elif time_string.endswith("/h"): result = result.shift(hours=1, microseconds=-1) elif time_string.endswith("/d"): result = result.shift(days=1, microseconds=-1) elif time_string.endswith("/w"): result = result.shift(weeks=1, microseconds=-1) elif time_string.endswith("/M"): result = result.shift(months=1, microseconds=-1) elif time_string.endswith("/y"): result = result.shift(years=1, microseconds=-1) return result.datetime
def get_time_range(self, timespan): (tz, ) = list( filter(lambda _tz: re.match(fr'.*\b{_tz}\b.*', timespan), all_timezones)) or ['UTC'] timespan = re.sub(fr'[ ;/\-\s,]*{tz}[ ;/\-\s,]*', ' ', timespan) now = re.search(self.NOW_REGEX, timespan) timespan = re.sub(self.NOW_REGEX, '', timespan).strip() src_timestamp = None if now and now[1]: src_timestamp = datemath(now[1]) _time_a, _time_b = re.split(re.compile(r'\s+-\s+'), timespan) from_to = [ datemath(_time_a.strip(), tz=tz, now=src_timestamp), datemath(_time_b.strip(), tz=tz, now=src_timestamp), ] from_to.sort() return from_to
def cron_check(cronstr, now=datemath('now'), itertype='next'): try: entry = CronTab(cronstr) except Exception as e: raise CronException("Unable to parse cron expression, reason: %s" % e) # returns how many seconds are left until/from the {itertype} ( next or previous ) run #return entry.next(now) return getattr(entry, itertype)(now)
def test_cron(cronstr, now=datemath('now')): try: entry = CronTab(cronstr) except Exception as e: raise CronException("Unable to parse cron expression, reason: %s" % e) localtime = now return (entry.matchers.minute(localtime.minute, localtime) and entry.matchers.hour(localtime.hour, localtime) and entry.matchers.day(localtime.day, localtime) and entry.matchers.month(localtime.month, localtime) and entry.matchers.weekday(localtime.isoweekday() % 7, localtime))
def __init__(self, **kwargs): self.alert = None self.es_results = None self.results = None self.matches = None self.intentions = None self.results_hits = None self.results_aggs = None self.results_total = None self.timeframe = datemath('now-1m') self.timestamp = '@timestamp' self.data = (self.matches, self.es_results, self.alert) for k, v in kwargs.items(): setattr(self, k, v) self.results_hits = self.get_results_hits() self.results_aggs = self.get_results_aggs() self.results_total = self.get_results_total()
def testShouldCronCheck(self): # True Things tests = [ ( '2016-01-01T00:00:00', '0 0 * * *', True ), ( '2016-01-01T00:00:55', '0 0 * * *', True ), ( '2016-01-01T00:10:00', '0 0 * * *', False ), ( '2016-01-01T01:20:00', '* * * * fri', True), ( '2016-01-01T01:20:00', '* 2-4 * * mon-thu', False), ( '2017-01-01T00:00:00', '* * * * sat-sun', True), ( '2018-08-22T12:13:22', '00-59 12-14 * * mon-fri', True) ] for test in tests: timestr, cronstr, boolean = test now = datemath(timestr) cron_check = tattle.check_cron_schedule(cronstr, now) assert cron_check == boolean
def curate_snapshots(): ''' curates snapshots as defined by their retention time in config.yaml ''' for instance in conf['instances']: env.region = instance['region'] env.account = conf['accounts'][instance['account']] conn = get_conn() reservations = conn.get_all_instances(filters={'tag:Name': instance['name']}) res_instances = [i for r in reservations for i in r.instances] for i in res_instances: volumes = conn.get_all_volumes(filters={'attachment.instance_id': i.id}) for volume in volumes: snapshots = conn.get_all_snapshots(filters={'volume-id': volume.id, 'status': 'completed'}) for snapshot in snapshots: retention_time = datemath('now-%s' % instance.get('retention', '7d')) snapshot_create_time = dateutil.parser.parse(snapshot.start_time) # remove snapshots if they are over their retention period if snapshot_create_time < retention_time: try: conn.delete_snapshot(snapshot.id) logit('info', 'EBS snapshot %s/%s deleted for %s/%s, because it was older than %s old' % ( snapshot.id, volume.id, instance['region'], instance['name'], retention_time)) except Exception as e: error_msg = 'Unable to delete EBS snapshot: reson: %s, id: %s, volume: %s, host: %s, region: %s' % ( e, snapshot.id, volume.id, instance['name'], instance['region'] ) logit('error', error_msg)
def testParse(self): # Baisc dates self.assertEqual( dm('2016.01.02').format(iso8601), '2016-01-02T00:00:00-00:00') self.assertEqual( dm('2016-01-02').format(iso8601), '2016-01-02T00:00:00-00:00') self.assertEqual( dm('2016-01-02 01:00:00').format(iso8601), '2016-01-02T01:00:00-00:00') # Rounding Tests self.assertEqual( dm('2016-01-01||/d').format('YYYY-MM-DDTHH:mm:ssZZ'), '2016-01-01T00:00:00-00:00') self.assertEqual( dm('2014-11-18||/y').format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-01-01T00:00:00-00:00') self.assertEqual( dm('2016-01-01 14:00:00||/w').format('YYYY-MM-DDTHH:mm:ssZZ'), '2015-12-28T00:00:00-00:00') self.assertEqual( dm('2014-11||/M').format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-11-01T00:00:00-00:00') self.assertEqual( dm('2016-01-02||/M+1h+1m').format(iso8601), '2016-01-01T01:01:00-00:00') self.assertEqual( dm('2016-01-02||/d+1h').format(iso8601), '2016-01-02T01:00:00-00:00') self.assertEqual( dm('2016-01-02T14:02:00||/h').format(iso8601), '2016-01-02T14:00:00-00:00') self.assertEqual( dm('2016-01-02T14:02:00||/H').format(iso8601), '2016-01-02T14:00:00-00:00') # Rounding Up Tests self.assertEqual( dm('2016-01-01||/d', roundDown=False).format('YYYY-MM-DDTHH:mm:ssZZ'), '2016-01-01T23:59:59-00:00') self.assertEqual( dm('2014-11-18||/y', roundDown=False).format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-12-31T23:59:59-00:00') # Timezone Tests self.assertEqual( dm('now', tz='US/Pacific').format(iso8601), arrow.utcnow().to('US/Pacific').format(iso8601)) self.assertEqual( dm('2017-09-22 10:20:00', tz='US/Pacific').datetime, pydatetime(2017, 9, 22, 10, 20, 00, tzinfo=tz.gettz('US/Pacific'))) self.assertEqual(dm('2016-01-01', tz='UTC'), arrow.get('2016-01-01').to('UTC')) self.assertEqual(dm('2016-01-01', tz='US/Eastern'), pydatetime(2016, 1, 1, tzinfo=tz.gettz('US/Eastern'))) self.assertEqual( datemath('2016-01-01T01:00:00', tz='US/Central'), pydatetime(2016, 1, 1, 1, 0, 0, tzinfo=tz.gettz('US/Central'))) # relitive formats # addition self.assertEqual( dm('+1s').format(iso8601), arrow.utcnow().replace(seconds=+1).format(iso8601)) self.assertEqual( dm('+1m').format(iso8601), arrow.utcnow().replace(minutes=+1).format(iso8601)) self.assertEqual( dm('+1h').format(iso8601), arrow.utcnow().replace(hours=+1).format(iso8601)) self.assertEqual( dm('+1d').format(iso8601), arrow.utcnow().replace(days=+1).format(iso8601)) self.assertEqual( dm('+1w').format(iso8601), arrow.utcnow().replace(weeks=+1).format(iso8601)) self.assertEqual( dm('+1M').format(iso8601), arrow.utcnow().replace(months=+1).format(iso8601)) self.assertEqual( dm('+1Y').format(iso8601), arrow.utcnow().replace(years=+1).format(iso8601)) self.assertEqual( dm('+1y').format(iso8601), arrow.utcnow().replace(years=+1).format(iso8601)) # subtraction self.assertEqual( dm('-1s').format(iso8601), arrow.utcnow().replace(seconds=-1).format(iso8601)) self.assertEqual( dm('-1m').format(iso8601), arrow.utcnow().replace(minutes=-1).format(iso8601)) self.assertEqual( dm('-1h').format(iso8601), arrow.utcnow().replace(hours=-1).format(iso8601)) self.assertEqual( dm('-1d').format(iso8601), arrow.utcnow().replace(days=-1).format(iso8601)) self.assertEqual( dm('-1w').format(iso8601), arrow.utcnow().replace(weeks=-1).format(iso8601)) self.assertEqual( dm('-1M').format(iso8601), arrow.utcnow().replace(months=-1).format(iso8601)) self.assertEqual( dm('-1Y').format(iso8601), arrow.utcnow().replace(years=-1).format(iso8601)) self.assertEqual( dm('-1y').format(iso8601), arrow.utcnow().replace(years=-1).format(iso8601)) # rounding self.assertEqual( dm('/s').format(iso8601), arrow.utcnow().floor('second').format(iso8601)) self.assertEqual( dm('/m').format(iso8601), arrow.utcnow().floor('minute').format(iso8601)) self.assertEqual( dm('/h').format(iso8601), arrow.utcnow().floor('hour').format(iso8601)) self.assertEqual( dm('/d').format(iso8601), arrow.utcnow().floor('day').format(iso8601)) self.assertEqual( dm('/w').format(iso8601), arrow.utcnow().floor('week').format(iso8601)) self.assertEqual( dm('/M').format(iso8601), arrow.utcnow().floor('month').format(iso8601)) self.assertEqual( dm('/Y').format(iso8601), arrow.utcnow().floor('year').format(iso8601)) self.assertEqual( dm('/y').format(iso8601), arrow.utcnow().floor('year').format(iso8601)) # rounding up self.assertEqual( dm('/s', roundDown=False).format(iso8601), arrow.utcnow().ceil('second').format(iso8601)) self.assertEqual( dm('/m', roundDown=False).format(iso8601), arrow.utcnow().ceil('minute').format(iso8601)) self.assertEqual( dm('/h', roundDown=False).format(iso8601), arrow.utcnow().ceil('hour').format(iso8601)) self.assertEqual( dm('/d', roundDown=False).format(iso8601), arrow.utcnow().ceil('day').format(iso8601)) self.assertEqual( dm('/w', roundDown=False).format(iso8601), arrow.utcnow().ceil('week').format(iso8601)) self.assertEqual( dm('/M', roundDown=False).format(iso8601), arrow.utcnow().ceil('month').format(iso8601)) self.assertEqual( dm('/Y', roundDown=False).format(iso8601), arrow.utcnow().ceil('year').format(iso8601)) self.assertEqual( dm('/y', roundDown=False).format(iso8601), arrow.utcnow().ceil('year').format(iso8601)) # roundDown option tests self.assertEqual( dm('2016-01-01T14:00:00||/d').format(iso8601), '2016-01-01T00:00:00-00:00') self.assertEqual( dm('2016-01-01T14:00:00||/d', roundDown=False).format(iso8601), '2016-01-01T23:59:59-00:00') # complicated date math self.assertEqual( dm('now/d-1h').format(iso8601), arrow.utcnow().floor('day').replace(hours=-1).format(iso8601)) self.assertEqual( dm('+1h').format(iso8601), arrow.utcnow().replace(hours=+1).format(iso8601)) self.assertEqual( dm('/M+2d').format(iso8601), arrow.utcnow().floor('month').replace(days=+2).format(iso8601)) self.assertEqual( dm('now/w+2d-2h').format(iso8601), arrow.utcnow().floor('week').replace(days=+2, hours=-2).format(iso8601)) self.assertEqual( dm('now/M+1w-2h+10s').format(iso8601), arrow.utcnow().floor('month').replace(weeks=+1, hours=-2, seconds=+10).format(iso8601)) self.assertEqual( dm('now-1d/d').format(iso8601), arrow.utcnow().replace(days=-1).floor('day').format(iso8601)) self.assertEqual( dm('now+1d/d').format(iso8601), arrow.utcnow().replace(days=1).floor('day').format(iso8601)) self.assertEqual( dm('now-10d/d').format(iso8601), arrow.utcnow().replace(days=-10).floor('day').format(iso8601)) self.assertEqual( dm('now+10d/d').format(iso8601), arrow.utcnow().replace(days=10).floor('day').format(iso8601)) # future self.assertEqual( dm('+1s').format(iso8601), arrow.utcnow().replace(seconds=+1).format(iso8601)) self.assertEqual( dm('+1s+2m+3h').format(iso8601), arrow.utcnow().replace(seconds=+1, minutes=+2, hours=+3).format(iso8601)) self.assertEqual( dm('+1m').format(iso8601), arrow.utcnow().replace(minutes=+1).format(iso8601)) self.assertEqual( dm('+1m+5h').format(iso8601), arrow.utcnow().replace(minutes=+1, hours=+5).format(iso8601)) self.assertEqual( dm('/d+1m+5h').format(iso8601), arrow.utcnow().floor('day').replace(minutes=+1, hours=+5).format(iso8601)) self.assertEqual( dm('+1h').format(iso8601), arrow.utcnow().replace(hours=+1).format(iso8601)) self.assertEqual( dm('+1w').format(iso8601), arrow.utcnow().replace(weeks=+1).format(iso8601)) self.assertEqual( dm('+1w+12d').format(iso8601), arrow.utcnow().replace(weeks=+1, days=+12).format(iso8601)) self.assertEqual( dm('+2y').format(iso8601), arrow.utcnow().replace(years=+2).format(iso8601)) self.assertEqual( dm('+2y+22d+4h').format(iso8601), arrow.utcnow().replace(years=+2, days=+22, hours=+4).format(iso8601)) # past self.assertEqual( dm('-3w').format(iso8601), arrow.utcnow().replace(weeks=-3).format(iso8601)) self.assertEqual( dm('-3W').format(iso8601), arrow.utcnow().replace(weeks=-3).format(iso8601)) self.assertEqual( dm('-3w-2d-6h').format(iso8601), arrow.utcnow().replace(weeks=-3, days=-2, hours=-6).format(iso8601)) self.assertEqual( dm('-3w-2d-22h-36s').format(iso8601), arrow.utcnow().replace(weeks=-3, days=-2, hours=-22, seconds=-36).format(iso8601)) self.assertEqual( dm('-6y-3w-2d-22h-36s').format(iso8601), arrow.utcnow().replace(years=-6, weeks=-3, days=-2, hours=-22, seconds=-36).format(iso8601)) import datetime delta = datetime.timedelta(seconds=1) # datetime objects self.assertAlmostEqual(dm('now').datetime, arrow.utcnow().datetime, delta=delta) self.assertAlmostEqual(dm('now+1d').datetime, arrow.utcnow().replace(days=+1).datetime, delta=delta) self.assertAlmostEqual(dm('/w').datetime, arrow.utcnow().floor('week').datetime, delta=delta) # Floats self.assertEqual( dm('now-2.5h').format(iso8601), arrow.utcnow().replace(hours=-2.5).format(iso8601)) self.assertEqual( dm('now-2.5d').format(iso8601), arrow.utcnow().replace(days=-2.5).format(iso8601))
def check_cron_schedule(cronstr, now=datemath('now')): return test_cron(cronstr, now)
def testParse(self): # Baisc dates self.assertEqual( dm('2016.01.02').format(iso8601), '2016-01-02T00:00:00+00:00') self.assertEqual( dm('2016-01-02').format(iso8601), '2016-01-02T00:00:00+00:00') self.assertEqual( dm('2016-01-02 01:00:00').format(iso8601), '2016-01-02T01:00:00+00:00') # Rounding Tests self.assertEqual( dm('2016-01-01||/d').format('YYYY-MM-DDTHH:mm:ssZZ'), '2016-01-01T00:00:00+00:00') self.assertEqual( dm('2014-11-18||/y').format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-01-01T00:00:00+00:00') self.assertEqual( dm('2016-01-01 14:00:00||/w').format('YYYY-MM-DDTHH:mm:ssZZ'), '2015-12-28T00:00:00+00:00') self.assertEqual( dm('2014-11||/M').format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-11-01T00:00:00+00:00') self.assertEqual( dm('2016-01-02||/M+1h+1m').format(iso8601), '2016-01-01T01:01:00+00:00') self.assertEqual( dm('2016-01-02||/d+1h').format(iso8601), '2016-01-02T01:00:00+00:00') self.assertEqual( dm('2016-01-02T14:02:00||/h').format(iso8601), '2016-01-02T14:00:00+00:00') self.assertEqual( dm('2016-01-02T14:02:00||/H').format(iso8601), '2016-01-02T14:00:00+00:00') # Rounding Up Tests self.assertEqual( dm('2016-01-01||/d', roundDown=False).format('YYYY-MM-DDTHH:mm:ssZZ'), '2016-01-01T23:59:59+00:00') self.assertEqual( dm('2014-11-18||/y', roundDown=False).format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-12-31T23:59:59+00:00') # Timezone Tests self.assertEqual( dm('now', tz='US/Pacific').format(iso8601), arrow.utcnow().to('US/Pacific').format(iso8601)) self.assertEqual( dm('2017-09-22 10:20:00', tz='US/Pacific').datetime, pydatetime(2017, 9, 22, 10, 20, 00, tzinfo=tz.gettz('US/Pacific'))) self.assertEqual(dm('2016-01-01', tz='UTC'), arrow.get('2016-01-01').to('UTC')) self.assertEqual(dm('2016-01-01', tz='US/Eastern'), pydatetime(2016, 1, 1, tzinfo=tz.gettz('US/Eastern'))) self.assertEqual( datemath('2016-01-01T01:00:00', tz='US/Central'), pydatetime(2016, 1, 1, 1, 0, 0, tzinfo=tz.gettz('US/Central'))) self.assertEqual( datemath('2016-01-01T02:00:00', tz='US/Eastern'), pydatetime(2016, 1, 1, 2, tzinfo=tz.gettz('US/Eastern'))) # relitive formats # addition self.assertEqual( dm('+1s').format(iso8601), arrow.utcnow().shift(seconds=+1).format(iso8601)) self.assertEqual( dm('+1m').format(iso8601), arrow.utcnow().shift(minutes=+1).format(iso8601)) self.assertEqual( dm('+1h').format(iso8601), arrow.utcnow().shift(hours=+1).format(iso8601)) self.assertEqual( dm('+1d').format(iso8601), arrow.utcnow().shift(days=+1).format(iso8601)) self.assertEqual( dm('+1w').format(iso8601), arrow.utcnow().shift(weeks=+1).format(iso8601)) self.assertEqual( dm('+1M').format(iso8601), arrow.utcnow().shift(months=+1).format(iso8601)) self.assertEqual( dm('+1Y').format(iso8601), arrow.utcnow().shift(years=+1).format(iso8601)) self.assertEqual( dm('+1y').format(iso8601), arrow.utcnow().shift(years=+1).format(iso8601)) # subtraction self.assertEqual( dm('-1s').format(iso8601), arrow.utcnow().shift(seconds=-1).format(iso8601)) self.assertEqual( dm('-1m').format(iso8601), arrow.utcnow().shift(minutes=-1).format(iso8601)) self.assertEqual( dm('-1h').format(iso8601), arrow.utcnow().shift(hours=-1).format(iso8601)) self.assertEqual( dm('-1d').format(iso8601), arrow.utcnow().shift(days=-1).format(iso8601)) self.assertEqual( dm('-1w').format(iso8601), arrow.utcnow().shift(weeks=-1).format(iso8601)) self.assertEqual( dm('-1M').format(iso8601), arrow.utcnow().shift(months=-1).format(iso8601)) self.assertEqual( dm('-1Y').format(iso8601), arrow.utcnow().shift(years=-1).format(iso8601)) self.assertEqual( dm('-1y').format(iso8601), arrow.utcnow().shift(years=-1).format(iso8601)) # rounding self.assertEqual( dm('/s').format(iso8601), arrow.utcnow().floor('second').format(iso8601)) self.assertEqual( dm('/m').format(iso8601), arrow.utcnow().floor('minute').format(iso8601)) self.assertEqual( dm('/h').format(iso8601), arrow.utcnow().floor('hour').format(iso8601)) self.assertEqual( dm('/d').format(iso8601), arrow.utcnow().floor('day').format(iso8601)) self.assertEqual( dm('/w').format(iso8601), arrow.utcnow().floor('week').format(iso8601)) self.assertEqual( dm('/M').format(iso8601), arrow.utcnow().floor('month').format(iso8601)) self.assertEqual( dm('/Y').format(iso8601), arrow.utcnow().floor('year').format(iso8601)) self.assertEqual( dm('/y').format(iso8601), arrow.utcnow().floor('year').format(iso8601)) # rounding up self.assertEqual( dm('/s', roundDown=False).format(iso8601), arrow.utcnow().ceil('second').format(iso8601)) self.assertEqual( dm('/m', roundDown=False).format(iso8601), arrow.utcnow().ceil('minute').format(iso8601)) self.assertEqual( dm('/h', roundDown=False).format(iso8601), arrow.utcnow().ceil('hour').format(iso8601)) self.assertEqual( dm('/d', roundDown=False).format(iso8601), arrow.utcnow().ceil('day').format(iso8601)) self.assertEqual( dm('/w', roundDown=False).format(iso8601), arrow.utcnow().ceil('week').format(iso8601)) self.assertEqual( dm('/M', roundDown=False).format(iso8601), arrow.utcnow().ceil('month').format(iso8601)) self.assertEqual( dm('/Y', roundDown=False).format(iso8601), arrow.utcnow().ceil('year').format(iso8601)) self.assertEqual( dm('/y', roundDown=False).format(iso8601), arrow.utcnow().ceil('year').format(iso8601)) # roundDown option tests self.assertEqual( dm('2016-01-01T14:00:00||/d').format(iso8601), '2016-01-01T00:00:00+00:00') self.assertEqual( dm('2016-01-01T14:00:00||/d', roundDown=False).format(iso8601), '2016-01-01T23:59:59+00:00') # complicated date math self.assertEqual( dm('now/d-1h').format(iso8601), arrow.utcnow().floor('day').shift(hours=-1).format(iso8601)) self.assertEqual( dm('+1h').format(iso8601), arrow.utcnow().shift(hours=+1).format(iso8601)) self.assertEqual( dm('/M+2d').format(iso8601), arrow.utcnow().floor('month').shift(days=+2).format(iso8601)) self.assertEqual( dm('now/w+2d-2h').format(iso8601), arrow.utcnow().floor('week').shift(days=+2, hours=-2).format(iso8601)) self.assertEqual( dm('now/M+1w-2h+10s').format(iso8601), arrow.utcnow().floor('month').shift(weeks=+1, hours=-2, seconds=+10).format(iso8601)) self.assertEqual( dm('now-1d/d').format(iso8601), arrow.utcnow().shift(days=-1).floor('day').format(iso8601)) self.assertEqual( dm('now+1d/d').format(iso8601), arrow.utcnow().shift(days=1).floor('day').format(iso8601)) self.assertEqual( dm('now-10d/d').format(iso8601), arrow.utcnow().shift(days=-10).floor('day').format(iso8601)) self.assertEqual( dm('now+10d/d').format(iso8601), arrow.utcnow().shift(days=10).floor('day').format(iso8601)) self.assertEqual( dm('now-29d/d').format(iso8601), arrow.utcnow().shift(days=-29).floor('day').format(iso8601)) # future self.assertEqual( dm('+1s').format(iso8601), arrow.utcnow().shift(seconds=+1).format(iso8601)) self.assertEqual( dm('+1s+2m+3h').format(iso8601), arrow.utcnow().shift(seconds=+1, minutes=+2, hours=+3).format(iso8601)) self.assertEqual( dm('+1m').format(iso8601), arrow.utcnow().shift(minutes=+1).format(iso8601)) self.assertEqual( dm('+1m+5h').format(iso8601), arrow.utcnow().shift(minutes=+1, hours=+5).format(iso8601)) self.assertEqual( dm('/d+1m+5h').format(iso8601), arrow.utcnow().floor('day').shift(minutes=+1, hours=+5).format(iso8601)) self.assertEqual( dm('+1h').format(iso8601), arrow.utcnow().shift(hours=+1).format(iso8601)) self.assertEqual( dm('+1w').format(iso8601), arrow.utcnow().shift(weeks=+1).format(iso8601)) self.assertEqual( dm('+1w+12d').format(iso8601), arrow.utcnow().shift(weeks=+1, days=+12).format(iso8601)) self.assertEqual( dm('+2y').format(iso8601), arrow.utcnow().shift(years=+2).format(iso8601)) self.assertEqual( dm('+2y+22d+4h').format(iso8601), arrow.utcnow().shift(years=+2, days=+22, hours=+4).format(iso8601)) # past self.assertEqual( dm('-3w').format(iso8601), arrow.utcnow().shift(weeks=-3).format(iso8601)) self.assertEqual( dm('-3W').format(iso8601), arrow.utcnow().shift(weeks=-3).format(iso8601)) self.assertEqual( dm('-3w-2d-6h').format(iso8601), arrow.utcnow().shift(weeks=-3, days=-2, hours=-6).format(iso8601)) self.assertEqual( dm('-3w-2d-22h-36s').format(iso8601), arrow.utcnow().shift(weeks=-3, days=-2, hours=-22, seconds=-36).format(iso8601)) self.assertEqual( dm('-6y-3w-2d-22h-36s').format(iso8601), arrow.utcnow().shift(years=-6, weeks=-3, days=-2, hours=-22, seconds=-36).format(iso8601)) import datetime delta = datetime.timedelta(seconds=1) # datetime objects self.assertAlmostEqual(dm('now').datetime, arrow.utcnow().datetime, delta=delta) self.assertAlmostEqual(dm('now+1d').datetime, arrow.utcnow().shift(days=+1).datetime, delta=delta) self.assertAlmostEqual(dm('/w').datetime, arrow.utcnow().floor('week').datetime, delta=delta) # Floats self.assertEqual( dm('now-2.5h').format(iso8601), arrow.utcnow().shift(hours=-2.5).format(iso8601)) self.assertEqual( dm('now-2.5d').format(iso8601), arrow.utcnow().shift(days=-2.5).format(iso8601)) # Test epoch timestamps self.assertEqual( dm('1451610061').format(iso8601), '2016-01-01T01:01:01+00:00') self.assertEqual( dm(1367900664).format(iso8601), '2013-05-07T04:24:24+00:00') self.assertRaises(DateMathException, dm, '1451610061000') try: dm(1451610061000) except DateMathException as e: self.assertTrue( 'Unable to parse epoch timestamps in millis' in str(e)) # Catch invalid timeunits self.assertRaises(DateMathException, dm, '+1,') self.assertRaises(DateMathException, dm, '+1.') self.assertRaises(DateMathException, dm, '+1ö') self.assertRaises(DateMathException, dm, '+1ä') self.assertRaises(DateMathException, dm, '+1ü') self.assertRaises(DateMathException, dm, '+1ß') try: dm('+1,') except DateMathException as e: self.assertTrue('is not a valid timeunit' in str(e))
def build_indexes(self, index, **kwargs): args = {} for k,v in kwargs.items(): args[k] = v return tattle.get_indexes(index, datemath(kwargs.get('start', 'now-1h')), datemath(kwargs.get('end', 'now')))