Exemplo n.º 1
0
    def test_9_making_ts_stream_prediction_no_group(self):
        print(f"\nExecuting {self._testMethodName}")
        from mindsdb_streams import RedisStream
        PREDICTOR_NAME = TS_PREDICTOR + "_no_group"
        STREAM_IN = STREAM_IN_TS + 'no_group'
        STREAM_OUT = STREAM_OUT_TS + 'no_group'

        self.train_ts_predictor(TS_DS_NAME, PREDICTOR_NAME, with_gb=False)

        url = f'{HTTP_API_ROOT}/streams/ts_stream_{STREAM_SUFFIX}_no_group'
        res = requests.put(url,
                           json={
                               "predictor": PREDICTOR_NAME,
                               "stream_in": STREAM_IN,
                               "stream_out": STREAM_OUT,
                               "integration": INTEGRATION_NAME,
                           })

        self.assertEqual(res.status_code, 200)
        stream_in = RedisStream(STREAM_IN, CONNECTION_PARAMS)
        stream_out = RedisStream(STREAM_OUT, CONNECTION_PARAMS)

        for x in range(210, 221):
            stream_in.write({'x1': x, 'x2': 2 * x, 'order': x, 'y': 3 * x})
            time.sleep(5)

        self.assertEqual(len(list(stream_out.read())), 2)
Exemplo n.º 2
0
    def _make_stream(self, s: db.Stream):
        if s.learning_params and s.learning_threshold:
            learning_params = json.loads(s.learning_params) if isinstance(
                s.learning_params, str) else s.learning_params
            return StreamLearningController(
                s.name,
                s.predictor,
                learning_params,
                s.learning_threshold,
                stream_in=RedisStream(s.stream_in, self.connection_info),
                stream_out=RedisStream(s.stream_out, self.connection_info),
                in_thread=True)

        return StreamController(
            s.name,
            s.predictor,
            stream_in=RedisStream(s.stream_in, self.connection_info),
            stream_out=RedisStream(s.stream_out, self.connection_info),
            stream_anomaly=RedisStream(s.anomaly_stream, self.connection_info)
            if s.anomaly_stream is not None else None,
            in_thread=True)
Exemplo n.º 3
0
    def test_3_making_stream_prediction(self):
        print(f"\nExecuting {self._testMethodName}")
        from mindsdb_streams import RedisStream

        stream_in = RedisStream(STREAM_IN, CONNECTION_PARAMS)
        stream_out = RedisStream(STREAM_OUT, CONNECTION_PARAMS)

        for x in range(1, 3):
            stream_in.write({'x1': x, 'x2': 2 * x})
        time.sleep(10)

        self.assertEqual(len(list(stream_out.read())), 2)
Exemplo n.º 4
0
    def test_length(self):
        print(f"\nExecuting {self._testMethodName}")
        from mindsdb_streams import RedisStream

        stream = RedisStream(f'test_stream_length_{STREAM_SUFFIX}',
                             CONNECTION_PARAMS)

        self.assertEqual(len(list(stream.read())), 0)
        time.sleep(5)

        stream.write({'0': 0})
        time.sleep(5)

        self.assertEqual(len(list(stream.read())), 1)

        stream.write({'0': 0})
        stream.write({'0': 0})
        time.sleep(5)

        self.assertEqual(len(list(stream.read())), 2)
        self.assertEqual(len(list(stream.read())), 0)
Exemplo n.º 5
0
    def __init__(self, config, name, db_info):
        self.connection_info = db_info['connection']

        # Back compatibility with initial API version
        self.control_stream = db_info.get('control_stream') or db_info.get(
            'stream') or None
        if 'advanced' in db_info:
            self.connection_info['advanced'] = db_info['advanced']

        StreamIntegration.__init__(
            self,
            config,
            name,
            control_stream=RedisStream(self.control_stream,
                                       self.connection_info)
            if self.control_stream else None)
Exemplo n.º 6
0
    def test_5_making_ts_stream_prediction(self):
        print(f"\nExecuting {self._testMethodName}")
        from mindsdb_streams import RedisStream
        stream_in = RedisStream(STREAM_IN_TS, CONNECTION_PARAMS)
        stream_out = RedisStream(STREAM_OUT_TS, CONNECTION_PARAMS)

        for x in range(230, 241):
            stream_in.write({
                'x1': x,
                'x2': 2 * x,
                'order': x,
                'group': "A",
                'y': 3 * x
            })
            time.sleep(0.01)
        time.sleep(10)

        self.assertEqual(len(list(stream_out.read())), 2)
Exemplo n.º 7
0
    def test_8_test_online_learning(self):
        print(f"\nExecuting {self._testMethodName}")
        from mindsdb_streams import RedisStream
        control_stream = RedisStream(CONTROL_STREAM, CONNECTION_PARAMS)
        stream_in = RedisStream(STREAM_IN_OL, CONNECTION_PARAMS)
        stream_out = RedisStream(STREAM_OUT_OL, CONNECTION_PARAMS)
        PREDICTOR_NAME = "ONLINE_LEARNING"

        control_stream.write({
            'action': 'create',
            'name': f'{self._testMethodName}_{STREAM_SUFFIX}',
            'predictor': PREDICTOR_NAME,
            'learning_params': {
                "to_predict": "y",
                'kwargs': {
                    'time_aim': 3
                }
            },
            'learning_threshold': 10,
            'stream_in': STREAM_IN_OL,
            'stream_out': STREAM_OUT_OL,
        })

        for x in range(1, 101):
            stream_in.write({'x1': x, 'x2': 2 * x, 'y': 3 * x})

        start_time = time.time()
        while (time.time() - start_time) < 300:
            time.sleep(5)
            res = list(stream_out.read())
            if res and res[0]['status'] == 'success':
                break
        else:
            raise Exception('Create predictor timeout')

        url = f'{HTTP_API_ROOT}/predictors/{PREDICTOR_NAME}'
        res = requests.get(url)
        self.assertEqual(
            res.status_code, 200,
            f"expected to get {PREDICTOR_NAME} info, but have {res.text}")
Exemplo n.º 8
0
    def test_6_create_stream_redis_native_api(self):
        print(f"\nExecuting {self._testMethodName}")
        from mindsdb_streams import RedisStream
        control_stream = RedisStream(CONTROL_STREAM, CONNECTION_PARAMS)
        control_stream.write({
            'action': 'create',
            'name': f'{self._testMethodName}_{STREAM_SUFFIX}',
            'predictor': DEFAULT_PREDICTOR,
            'stream_in': STREAM_IN_NATIVE,
            'stream_out': STREAM_OUT_NATIVE,
        })

        time.sleep(5)

        stream_in = RedisStream(STREAM_IN_NATIVE, CONNECTION_PARAMS)
        stream_out = RedisStream(STREAM_OUT_NATIVE, CONNECTION_PARAMS)

        for x in range(1, 3):
            stream_in.write({'x1': x, 'x2': 2 * x})
            time.sleep(5)
        time.sleep(30)

        self.assertEqual(len(list(stream_out.read())), 2)