Ejemplo n.º 1
0
class BasicTests(unittest.TestCase):
    def setUp(self):
        # Creating SiddhiManager
        self.siddhiManager = SiddhiManager()
        self.siddhiApp = "" + "define stream cseEventStream (symbol string, price float, volume long); " \
                         + "" + "@info(name = 'query1') " + "from cseEventStream[volume < 150] " \
                         + "select symbol,price " + "insert into outputStream ;"
        # Generating runtime
        # print(self.siddhiApp)
        self.siddhiAppRuntime = self.siddhiManager.createSiddhiAppRuntime(
            self.siddhiApp)

    def test_input_handler(self):
        logging.info("Test1: Test Input Handler")
        # Retrieving input handler to push events into Siddhi
        inputHandler = self.siddhiAppRuntime.getInputHandler("cseEventStream")
        # Starting event processing
        self.siddhiAppRuntime.start()

        # Sending events to Siddhi
        inputHandler.send(["IBM", 700.0, LongType(100)])
        inputHandler.send(["WSO2", 60.5, LongType(200)])
        inputHandler.send(["GOOG", 50, LongType(30)])
        inputHandler.send(["IBM", 76.6, LongType(400)])

    def test_siddhi_app_runtime_callback(self):
        logging.info("Test2: Test Siddhi App Runtime Callback")
        # Adding callback to retrieve output events from query

        global hitCount
        hitCount = 2

        class ConcreteQueryCallback(QueryCallback):
            def receive(self, timestamp, inEvents, outEvents):
                PrintEvent(timestamp, inEvents, outEvents)
                global hitCount
                hitCount -= 1

        self.siddhiAppRuntime.addCallback("query1", ConcreteQueryCallback())

        # Retrieving input handler to push events into Siddhi
        inputHandler = self.siddhiAppRuntime.getInputHandler("cseEventStream")
        # Starting event processing
        self.siddhiAppRuntime.start()

        # Sending events to Siddhi
        inputHandler.send(["IBM", 700.0, LongType(100)])
        inputHandler.send(["WSO2", 60.5, LongType(200)])
        inputHandler.send(["GOOG", 50, LongType(30)])
        inputHandler.send(["IBM", 76.6, LongType(400)])

        sleep(0.5)
        self.assertEqual(hitCount, 0)

    def tearDown(self):
        # shutting down the runtime
        self.siddhiAppRuntime.shutdown()

        # shutting down Siddhi
        self.siddhiManager.shutdown()
Ejemplo n.º 2
0
def run(args):
    logger.info('Bootstrapping:')
    siddhi_manager = SiddhiManager()
    logger.info('Manager up...')
    runtime = siddhi_manager.createSiddhiAppRuntime(SIDDHI_APP)
    logger.info('Runtime up...')

    # Add listener to capture output events
    class LogQueryCallbackImpl(QueryCallback):
        def receive(self, timestamp, inEvents, outEvents):
            #PrintEvent(timestamp, inEvents, outEvents)
            log_filenames = [event.getData(1) for event in inEvents]
            logger.info('%sCompressing log file %s%s%s', Fore.WHITE,
                        Fore.GREEN, ', '.join(log_filenames), Style.RESET_ALL)

    class BucketQueryCallbackImpl(QueryCallback):
        def receive(self, timestamp, inEvents, outEvents):
            #PrintEvent(timestamp, inEvents, outEvents)
            log_filenames = [event.getData(1) for event in inEvents]
            logger.info('%sCompressing log file %s%s%s', Fore.WHITE,
                        Fore.GREEN, ', '.join(log_filenames), Style.RESET_ALL)

    class ObjectQueryCallbackImpl(QueryCallback):
        def receive(self, timestamp, inEvents, outEvents):
            #PrintEvent(timestamp, inEvents, outEvents)
            for event in inEvents:
                logger.info('%sCompressing log file %s%s/%s%s',
                            Fore.WHITE, Fore.GREEN, event.getData(1),
                            event.getData(2), Style.RESET_ALL)
                async_task = s3_bucket_tasks.compress_log.delay(
                    event.getData(1), event.getData(2))
                logger.info('%sAsync task: %s%r%s', Fore.WHITE, Fore.YELLOW,
                            async_task, Style.RESET_ALL)

    runtime.addCallback(LOG_QUERY_NAME, LogQueryCallbackImpl())
    runtime.addCallback(BUCKET_QUERY_NAME, BucketQueryCallbackImpl())
    runtime.addCallback(OBJECT_QUERY_NAME, ObjectQueryCallbackImpl())

    # Retrieving input handler to push events into Siddhi
    input_handler = runtime.getInputHandler(INPUT_STREAM_NAME)

    # Starting event processing
    logger.info('Starting runtime...')
    runtime.start()

    try:
        monitor = S3Monitor()
        monitor.monitor_buckets(input_handler)
        # dummy_log_events(input_handler)
        logger.info('Waiting for any residual events')
        time.sleep(10)
    except Exception as e:  # pylint: disable=broad-except
        logger.error('UNEXPECTED ERROR: %s', e)
        raise
    finally:
        logger.info('Shutting down...')
        siddhi_manager.shutdown()
        logger.info('Goodbye')
Ejemplo n.º 3
0
 def setUp(self):
     # Creating SiddhiManager
     self.siddhiManager = SiddhiManager()
     self.siddhiApp = "" + "define stream cseEventStream (symbol string, price float, volume long); " \
                      + "" + "@info(name = 'query1') " + "from cseEventStream[volume < 150] " \
                      + "select symbol,price " + "insert into outputStream ;"
     # Generating runtime
     # print(self.siddhiApp)
     self.siddhiAppRuntime = self.siddhiManager.createSiddhiAppRuntime(
         self.siddhiApp)
Ejemplo n.º 4
0
    def testMathRandomFunctionWithSeed(self):
        logging.info("RandomFunctionExtension TestCase, with seed")

        # Creating SiddhiManager
        siddhiManager = SiddhiManager()

        # Creating Query
        streamDefinition = "define stream inputStream (symbol string, price long, volume long);"
        query = "@info(name = 'query1') from inputStream select symbol , math:rand(12) as randNumber " + \
                "insert into outputStream;"

        # Setting up Siddhi App
        siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(
            streamDefinition + query)

        # Setting up callback
        _self_shaddow = self

        class ConcreteQueryCallback(QueryCallback):
            def receive(self, timestamp, inEvents, outEvents):
                PrintEvent(timestamp, inEvents, outEvents)
                _self_shaddow.count.addAndGet(len(inEvents))
                _self_shaddow.eventArrived = True
                if len(inEvents) == 3:
                    randNumbers = [0, 0, 0]
                    randNumbers[0] = inEvents[0].getData(1)
                    randNumbers[1] = inEvents[1].getData(1)
                    randNumbers[2] = inEvents[2].getData(1)
                    isDuplicatePresent = False

                    logging.info(randNumbers[0] + ", " + randNumbers[1])

                    if randNumbers[0] == randNumbers[1] or randNumbers[0] == randNumbers[2] or randNumbers[1] == \
                            randNumbers[2]:
                        isDuplicatePresent = True

                    _self_shaddow.assertEquals(False, isDuplicatePresent)

        siddhiAppRuntime.addCallback("query1", ConcreteQueryCallback())

        # Retrieving input handler to push events into Siddhi
        inputHandler = siddhiAppRuntime.getInputHandler("inputStream")
        # Starting event processing
        siddhiAppRuntime.start()

        # Sending events to Siddhi
        inputHandler.send(["IBM", 700.0, LongType(100)])
        inputHandler.send(["WSO2", 60.5, LongType(200)])
        inputHandler.send(["XYZ", 60.5, LongType(200)])
        sleep(0.5)

        self.assertEqual(self.count.get(), 3)
        self.assertTrue(self.eventArrived)

        siddhiManager.shutdown()
Ejemplo n.º 5
0
    def testStringContainsFunction(self):
        logging.info("ContainsFunctionExtensionTestCase TestCase")

        # Creating SiddhiManager
        siddhiManager = SiddhiManager()

        # Creating Query
        streamDefinition = "define stream inputStream (symbol string, price long, volume long);"
        query = "@info(name = 'query1') " + \
                "from inputStream " + \
                "select symbol , str:contains(symbol, 'WSO2') as isContains " + \
                "insert into outputStream;"

        # Setting up Siddhi App
        siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(
            streamDefinition + query)

        # Setting up callback
        _self_shaddow = self

        class ConcreteQueryCallback(QueryCallback):
            def receive(self, timestamp, inEvents, outEvents):
                PrintEvent(timestamp, inEvents, outEvents)
                for inEvent in inEvents:
                    _self_shaddow.count.addAndGet(1)
                    if _self_shaddow.count.get() == 1:
                        _self_shaddow.assertEqual(False, inEvent.getData(1))

                    if _self_shaddow.count.get() == 2:
                        _self_shaddow.assertEqual(True, inEvent.getData(1))

                    if _self_shaddow.count.get() == 3:
                        _self_shaddow.assertEqual(True, inEvent.getData(1))

                _self_shaddow.eventArrived = True

        siddhiAppRuntime.addCallback("query1", ConcreteQueryCallback())

        # Retrieving input handler to push events into Siddhi
        inputHandler = siddhiAppRuntime.getInputHandler("inputStream")
        # Starting event processing
        siddhiAppRuntime.start()

        # Sending events to Siddhi
        inputHandler.send(["IBM", 700.0, LongType(100)])
        inputHandler.send(["WSO2", 60.5, LongType(200)])
        inputHandler.send(
            ["One of the best middleware is from WSO2.", 60.5,
             LongType(200)])
        sleep(0.5)

        self.assertEqual(self.count.get(), 3)
        self.assertTrue(self.eventArrived)

        siddhiManager.shutdown()
Ejemplo n.º 6
0
    def testStringRegexpFunction(self):
        logging.info("RegexpFunctionExtensionTestCase TestCase")

        # Creating SiddhiManager
        siddhiManager = SiddhiManager()

        # Creating Query
        streamDefinition = "define stream inputStream (symbol string, price long, regex string);"
        query = "@info(name = 'query1') from inputStream select symbol , " + \
                "str:regexp(symbol, regex) as beginsWithWSO2 " + \
                "insert into outputStream"

        # Setting up Siddhi App
        siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(
            streamDefinition + query)

        # Setting up callback
        _self_shaddow = self

        class ConcreteQueryCallback(QueryCallback):
            def receive(self, timestamp, inEvents, outEvents):
                PrintEvent(timestamp, inEvents, outEvents)
                for inEvent in inEvents:
                    _self_shaddow.count.addAndGet(1)
                    if _self_shaddow.count.get() == 1:
                        _self_shaddow.assertEqual(False, inEvent.getData(1))

                    if _self_shaddow.count.get() == 2:
                        _self_shaddow.assertEqual(True, inEvent.getData(1))

                    if _self_shaddow.count.get() == 3:
                        _self_shaddow.assertEqual(False, inEvent.getData(1))

                _self_shaddow.eventArrived = True

        siddhiAppRuntime.addCallback("query1", ConcreteQueryCallback())

        # Retrieving input handler to push events into Siddhi
        inputHandler = siddhiAppRuntime.getInputHandler("inputStream")
        # Starting event processing
        siddhiAppRuntime.start()

        # Sending events to Siddhi
        inputHandler.send(["hello hi hello", 700.0, "^WSO2(.*)"])
        inputHandler.send(["WSO2 abcdh", 60.5, "WSO(.*h)"])
        inputHandler.send(["aaWSO2 hi hello", 60.5, "^WSO2(.*)"])
        sleep(0.5)

        self.assertEqual(self.count.get(), 3)
        self.assertTrue(self.eventArrived)

        siddhiManager.shutdown()