async def setUp(self): self.monitor = Monitor("monitor@localhost", "passw0rd", "data0") self.engine = create_engine("sqlite:///database.sql") self.breaker = CircuitBreaker() self.cache_to_save = 10 self.connection = Connection(self.engine, self.cache_to_save, 100000, self.breaker) self.analyser_predictor = HelperAnalyserPredictor(self.connection) self.sink = HelperSink(self.breaker) self.sink.external.reset_mock() self.analyser = Analyser("analyser@localhost", "passw0rd", self.analyser_predictor, self.connection, ["monitor@localhost"]) self.executor = Executor("executor@localhost", "passw0rd", self.sink) self.planner_predictor = HelperPlannerPredictor(self.connection) self.planner = Planner("planner@localhost", "passw0rd", self.planner_predictor, ["executor@localhost"]) self.monitor.start() self.executor.start() await asyncio.sleep(2) self.analyser.start() self.planner.start() await asyncio.sleep(2)
async def setUp(self): self.monitor = Monitor("monitor@localhost", "passw0rd", "data0") self.engine = create_engine("sqlite:///database.sql") self.breaker = CircuitBreaker() self.connection = Connection(self.engine, 10, 10000, self.breaker) self.predictor = HelperAnalyserPredictor(self.connection) self.analyser = Analyser("analyser@localhost", "passw0rd", self.predictor, self.connection, ["monitor@localhost"]) self.monitor.start() await asyncio.sleep(2) self.analyser.start() await asyncio.sleep(1)
class TestMAPEIntegration(TestCase): async def setUp(self): self.monitor = Monitor("monitor@localhost", "passw0rd", "data0") self.engine = create_engine("sqlite:///database.sql") self.breaker = CircuitBreaker() self.cache_to_save = 10 self.connection = Connection(self.engine, self.cache_to_save, 100000, self.breaker) self.analyser_predictor = HelperAnalyserPredictor(self.connection) self.sink = HelperSink(self.breaker) self.sink.external.reset_mock() self.analyser = Analyser("analyser@localhost", "passw0rd", self.analyser_predictor, self.connection, ["monitor@localhost"]) self.executor = Executor("executor@localhost", "passw0rd", self.sink) self.planner_predictor = HelperPlannerPredictor(self.connection) self.planner = Planner("planner@localhost", "passw0rd", self.planner_predictor, ["executor@localhost"]) self.monitor.start() self.executor.start() await asyncio.sleep(2) self.analyser.start() self.planner.start() await asyncio.sleep(2) def tearDown(self): self.monitor.stop() self.analyser.stop() self.planner.stop() self.executor.stop() self.breaker.close() os.unlink("database.sql") @patch("driftage.planner.behaviour.predict.datetime") async def test_should_execute_monitored_data(self, mock_dt): now = datetime.utcnow() mock_dt.utcnow.return_value = now for i in range(self.cache_to_save): self.monitor({"my data": i}) await asyncio.sleep(1) self.sink.external.assert_called_once_with({ 'timestamp': now.timestamp(), 'identifier': 'data0', 'predicted': True })
def setUp(self): self.predictor = Mock() self.predictor.retrain_period = 3 self.connection = Mock() self.monitors = ["m1", "m2", "m3"] self.analyser = Analyser("user_test", "pass_test", self.predictor, self.connection, self.monitors) self.analyser.presence = Mock()
class TestMonitorAnalyseIntegration(TestCase): async def setUp(self): self.monitor = Monitor("monitor@localhost", "passw0rd", "data0") self.engine = create_engine("sqlite:///database.sql") self.breaker = CircuitBreaker() self.connection = Connection(self.engine, 10, 10000, self.breaker) self.predictor = HelperAnalyserPredictor(self.connection) self.analyser = Analyser("analyser@localhost", "passw0rd", self.predictor, self.connection, ["monitor@localhost"]) self.monitor.start() await asyncio.sleep(2) self.analyser.start() await asyncio.sleep(1) def tearDown(self): self.monitor.stop() self.analyser.stop() self.breaker.close() os.unlink("database.sql") async def test_should_analyse_monitored_data(self): dt_from = datetime.utcnow() for i in range(10): self.monitor({"my data": i}) dt_to = datetime.utcnow() time.sleep(1) df = await self.connection.get_between( table.c.driftage_datetime_monitored, dt_from, dt_to) self.assertEqual(len(df.index), 10) async def test_should_not_save_monitored_data_less_than_bulk(self): dt_from = datetime.utcnow() for i in range(9): self.monitor({"my data": i}) dt_to = datetime.utcnow() time.sleep(1) df = await self.connection.get_between( table.c.driftage_datetime_monitored, dt_from, dt_to) self.assertEqual(len(df.index), 0)
def main(predictor: ADWINPredictor): engine = create_engine(os.environ["KB_CONNECTION_STRING"]) connection = Connection(engine, bulk_size=1000, bulk_time=1) name = os.environ["ANALYSER_NAME"] with open(f"/tmp/{name}_results.csv", "w") as f: # nosec predictor = predictor(connection, f) analyser = Analyser( # nosec f"{name}@localhost", os.environ["ANALYSER_PASSWORD"], predictor, connection, [f"monitor_{name.split('_')[1]}@localhost"]) logger.info("Waiting Ejabberd...") time.sleep(30) logger.info("Starting analyser...") analyser.start() logger.info("Analyser alive") while True: try: time.sleep(1) except KeyboardInterrupt: break analyser.stop() logger.info("Analyser stopped")
detector = self.detectors.get(X.identifier, ADWIN(self.delta)) self.detectors[X.identifier] = detector detector.add_element(X.data["sensor"]) return detector.detected_change() engine = create_engine(os.environ["KB_CONNECTION_STRING"]) connection = Connection(engine, bulk_size=1000, bulk_time=1) predictor = ADWINPredictor(connection) my_number = os.environ["MY_NUMBER"] analyser = Analyser( # nosec f"analyser_{my_number}@localhost", os.environ["ANALYSER_PASSWORD"], predictor, connection, [f"monitor_{my_number}@localhost"]) logger.info("Waiting Ejabberd...") time.sleep(30) logger.info("Starting analyser...") analyser.start() logger.info("Analyser alive") while True: try: time.sleep(1) except KeyboardInterrupt: break analyser.stop() logger.info("Analyser stopped")