def __getitem__(self, index): if index == 0: self._zero_value["key"] = self._key return self._zero_value else: if self._accuracy >= self._batch_duration: logging.warning( "Current accuracy {} is more or equal batch duration {}. You can get incorrect " "results of analysis in this case ".format( self._accuracy, self._batch_duration)) nano_timestamp, nano_delta, nano_accuracy = nanotime.timestamp( time()), nanotime.seconds( index * self._batch_duration), nanotime.seconds( self._accuracy) begin_time_interval = nano_timestamp - nano_delta - nano_accuracy end_time_interval = nano_timestamp - nano_delta + nano_accuracy influx_key = None if self._key: influx_key = dict( map(lambda x, y: (x, y), self._key_fields_name, self._key)) historical_values = self._historical_data_repository_singleton.read( self._measurement, begin_time_interval.nanoseconds(), end_time_interval.nanoseconds(), influx_key) if historical_values: historical_values = sorted(historical_values, key=lambda point: point["time"], reverse=True)[0] historical_values["key"] = self._key return historical_values else: return {}
def test_construction(self): def eq(time1, time2): self.assertEqual(time1, time2) self.assertEqual(int(time1), time2._ns) self.assertEqual(time1._ns, int(time1)) self.assertEqual(time1._ns, int(time2)) self.assertEqual(time1._ns, time2._ns) self.assertEqual(time1.nanoseconds(), time2.nanoseconds()) self.assertEqual(time1.microseconds(), time2.microseconds()) self.assertEqual(time1.milliseconds(), time2.milliseconds()) self.assertEqual(time1.seconds(), time2.seconds()) self.assertEqual(time1.minutes(), time2.minutes()) self.assertEqual(time1.hours(), time2.hours()) self.assertEqual(time1.days(), time2.days()) self.assertEqual(time1.timestamp(), time2.timestamp()) self.assertEqual(time1.datetime(), time2.datetime()) self.assertEqual(time1.unixtime(), time2.unixtime()) #self.assertEqual(time1, time1.unixtime()) #self.assertEqual(time1, time2.unixtime()) #self.assertEqual(time1, time1.timestamp()) #self.assertEqual(time1, time2.timestamp()) #self.assertEqual(time2, time1.unixtime()) #self.assertEqual(time2, time2.unixtime()) #self.assertEqual(time2, time1.timestamp()) #self.assertEqual(time2, time2.timestamp()) def close(x, y, epsilon=1e-6): return abs(x - y) < epsilon for i in range(0, 50): # basic eq(nanotime.seconds(1), nanotime.nanoseconds(1e9)) eq(nanotime.seconds(1), nanotime.microseconds(1e6)) eq(nanotime.seconds(1), nanotime.milliseconds(1e3)) eq(nanotime.seconds(1), nanotime.seconds(1)) eq(nanotime.seconds(1), nanotime.minutes(1.0/60)) eq(nanotime.seconds(1), nanotime.hours(1.0/3600)) eq(nanotime.seconds(1), nanotime.days(1.0/(3600 * 24))) nt1 = nanotime.now() self.assertTrue(nanotime.now() > nt1) # timestamp ts1 = time.time() ts2 = nanotime.timestamp(ts1).timestamp() eq(nanotime.timestamp(ts1), nanotime.timestamp(ts1)) self.assertTrue(close(ts2, ts1)) self.assertEqual(nanotime.timestamp(ts1), ts1) # datetime dt1 = datetime.datetime.now() dt2 = nanotime.datetime(dt1).datetime() eq(nanotime.datetime(dt1), nanotime.datetime(dt1)) self.assertTrue(close(dt1, dt2, datetime.timedelta(microseconds=1)))
def analysis_record(input_tuple_value, data_structure, time_delta, accuracy, rule, alert_sender_singleton, historical_data_repository_singleton, measurement, key_fields_name=None, key=None): """ Checks data for deviations :param key: This is the key to the data aggregation :param input_tuple_value: input data to check :return: """ nano_timestamp, nano_delta, nano_accuracy = nanotime.timestamp(time()), nanotime.seconds( time_delta), nanotime.seconds(accuracy) begin_time_interval = nano_timestamp - nano_delta - nano_accuracy end_time_interval = nano_timestamp - nano_delta + nano_accuracy # create a dictionary with a key equal to the name of field from rule and a value equal to index in the # input tuple nfti (name field to index) name_to_index = dict(map(lambda x: (x, data_structure[x]), rule.keys())) # call for real historical data influx_key = None if key: influx_key = dict(map(lambda x, y: (x, y), key_fields_name, key)) historical_values = historical_data_repository_singleton.read(measurement, begin_time_interval.nanoseconds(), end_time_interval.nanoseconds(), influx_key) if historical_values: historical_values = sorted(historical_values, key=lambda point: point["time"]) # we analyse only first value from data, because next values will analysed in next moments for field in rule.keys(): lower_bound = historical_values[0][field] * (1 - float(rule[field]) / 100) upper_bound = historical_values[0][field] * (1 + float(rule[field]) / 100) current_value = input_tuple_value[name_to_index[field]] if (current_value < lower_bound) or (current_value > upper_bound): alert_sender_singleton.send_message(timestamp=time(), param={"key": key, "field": field, "lower_bound": lower_bound, "upper_bound": upper_bound, "value": current_value})
def test_other(self): test = self.subtest_attribute(TimeAttribute) test(5, nanotime.nanotime(5)) test(5.2, nanotime.nanotime(5.2)) self.assertRaises(TypeError, test, self) self.assertRaises(TypeError, test, '5') self.assertRaises(TypeError, test, '5a') test(nanotime.seconds(1000)) test(None) test = self.subtest_attribute(DateTimeAttribute) self.assertRaises(TypeError, test, 5) self.assertRaises(TypeError, test, 5.2) self.assertRaises(TypeError, test, self) self.assertRaises(ValueError, test, '5') self.assertRaises(ValueError, test, '5a') test(datetime.datetime.now()) test(None) d = datetime.datetime(2011, 7, 12, 13, 19, 29, 151455) test(str(d), d) d = datetime.datetime(2011, 7, 12, 13, 19, 29, 0) test(str(d), d) test = self.subtest_attribute(ListAttribute) self.assertRaises(TypeError, test, 5, ['5']) self.assertRaises(TypeError, test, 5.2, ['5.2']) self.assertRaises(TypeError, test, self, [str(self)]) test('5', ['5']) test('5a', ['5', 'a']) test([]) test(['fdgfds', 'gfdsgfds', 'gfdsgfds', 'gfdsgfds']) test([4214, 321, 43, 21], ['4214', '321', '43', '21']) test(xrange(0, 10), map(str, range(0, 10))) test(None) test = self.subtest_attribute(DictAttribute) self.assertRaises(TypeError, test, 5) self.assertRaises(TypeError, test, 5.2) self.assertRaises(TypeError, test, self) self.assertRaises(TypeError, test, '5') self.assertRaises(TypeError, test, '5a') test({}) test({'a':'b'}) test({'1213':3214}, {'1213':'3214'}) test({1213:3214}, {'1213':'3214'}) test(None)
def test_other(self): test = self.subtest_attribute(TimeAttribute) test(5, nanotime.nanotime(5)) test(5.2, nanotime.nanotime(5.2)) self.assertRaises(TypeError, test, self) self.assertRaises(TypeError, test, "5") self.assertRaises(TypeError, test, "5a") test(nanotime.seconds(1000)) test(None) test = self.subtest_attribute(DateTimeAttribute) self.assertRaises(TypeError, test, 5) self.assertRaises(TypeError, test, 5.2) self.assertRaises(TypeError, test, self) self.assertRaises(ValueError, test, "5") self.assertRaises(ValueError, test, "5a") test(datetime.datetime.now()) test(None) d = datetime.datetime(2011, 7, 12, 13, 19, 29, 151455) test(str(d), d) d = datetime.datetime(2011, 7, 12, 13, 19, 29, 0) test(str(d), d) test = self.subtest_attribute(ListAttribute) self.assertRaises(TypeError, test, 5, ["5"]) self.assertRaises(TypeError, test, 5.2, ["5.2"]) self.assertRaises(TypeError, test, self, [str(self)]) test("5", ["5"]) test("5a", ["5", "a"]) test([]) test(["fdgfds", "gfdsgfds", "gfdsgfds", "gfdsgfds"]) test([4214, 321, 43, 21], ["4214", "321", "43", "21"]) test(xrange(0, 10), map(str, range(0, 10))) test(None) test = self.subtest_attribute(DictAttribute) self.assertRaises(TypeError, test, 5) self.assertRaises(TypeError, test, 5.2) self.assertRaises(TypeError, test, self) self.assertRaises(TypeError, test, "5") self.assertRaises(TypeError, test, "5a") test({}) test({"a": "b"}) test({"1213": 3214}, {"1213": "3214"}) test({1213: 3214}, {"1213": "3214"}) test(None)
def __subtest_arithmetic(self, start, extra): eq = self.assertEqual start = float(start) extra = float(extra) t1 = nanotime.seconds(start) eq(nanotime.seconds(start + extra), t1 + nanotime.nanotime(extra * 1e9)) eq(nanotime.seconds(start + extra), t1 + nanotime.seconds(extra)) eq(nanotime.seconds(start - extra), t1 - nanotime.nanotime(extra * 1e9)) eq(nanotime.seconds(start - extra), t1 - nanotime.seconds(extra)) eq(nanotime.seconds(start * extra), t1 * nanotime.nanotime(extra)) eq(nanotime.seconds(start * extra), t1 * nanotime.nanoseconds(extra)) eq(nanotime.seconds(start / extra), t1 / nanotime.nanotime(extra)) eq(nanotime.seconds(start / extra), t1 / nanotime.nanoseconds(extra)) self.assertTrue(nanotime.seconds(start + extra) > t1) self.assertTrue(nanotime.seconds(start - extra) < t1) t2 = nanotime.seconds(start + extra) self.assertTrue(t2 > nanotime.nanotime(0)) self.assertTrue(nanotime.nanotime(0) < t2)