Search in sources :

Example 61 with Group

use of org.wso2.charon.core.objects.Group in project siddhi by wso2.

the class SimpleQueryTestCase method testCreatingFilterQuery.

// from StockStream[ 7+9.5>price AND 100>=volume]
// insert into OutStockStream symbol, avg(price) as avgPrice
// group by symbol
// having avgPrice>50
@Test
public void testCreatingFilterQuery() {
    Query query = Query.query();
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.compare(Expression.value(100), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("volume")))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.insertInto("OutStockStream");
    SiddhiApp.siddhiApp("test").addQuery(query);
}
Also used : Query(org.wso2.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 62 with Group

use of org.wso2.charon.core.objects.Group in project siddhi by wso2.

the class SimpleQueryTestCase method test3.

@Test
public void test3() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from AllStockQuotes#window.time(10 min)\n" + "select symbol as symbol, price, avg(price) as averagePrice \n" + "group by symbol \n" + "having ( price > ( averagePrice*1.02) ) or ( averagePrice > price ) " + "insert into FastMovingStockQuotes \n;");
    Query api = Query.query().from(InputStream.stream("AllStockQuotes").window("time", Expression.Time.minute(10))).select(Selector.selector().select("symbol", Expression.variable("symbol")).select(Expression.variable("price")).select("averagePrice", Expression.function("avg", Expression.variable("price"))).groupBy(Expression.variable("symbol")).having(Expression.or(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression.multiply(Expression.variable("averagePrice"), Expression.value(1.02))), Expression.compare(Expression.variable("averagePrice"), Compare.Operator.GREATER_THAN, Expression.variable("price"))))).insertInto("FastMovingStockQuotes");
    AssertJUnit.assertEquals(api, query);
}
Also used : Query(org.wso2.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 63 with Group

use of org.wso2.charon.core.objects.Group in project siddhi by wso2.

the class PartitionQueryTestCase method testCreatingFilterQuery.

// from StockStream[ 7+9.5>price AND 100>=volume]
// insert into OutStockStream symbol, avg(price) as avgPrice
// group by symbol
// having avgPrice>50
@Test
public void testCreatingFilterQuery() {
    Partition partition = Partition.partition().with("StockStream", Expression.variable("symbol")).with("StockStream1", Expression.variable("symbol")).with("StockStream2", Partition.range("LessValue", Expression.compare(Expression.value(7), Compare.Operator.GREATER_THAN, Expression.variable("price"))), Partition.range("HighValue", Expression.compare(Expression.value(9.5), Compare.Operator.LESS_THAN, Expression.variable("price1"))));
    Query query = Query.query();
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.compare(Expression.value(100), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("volume")))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.insertIntoInner("OutStockStream");
    partition.addQuery(query);
}
Also used : Partition(org.wso2.siddhi.query.api.execution.partition.Partition) Query(org.wso2.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 64 with Group

use of org.wso2.charon.core.objects.Group in project siddhi by wso2.

the class PatternQueryTestCase method testPatternQuery2.

// from every (e1=Stream1[price >= 30]) -> e2=Stream1[ price >= 20] -> e3=Stream2[ price >= e1.price]
// select e1.symbol, avg(e2.price ) as avgPrice
// group by e1.symbol
// having avgPrice>50;
// insert into OutputStream
@Test
public void testPatternQuery2() {
    Query query = Query.query();
    query.from(InputStream.patternStream(State.next(State.every(State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(30))))), State.next(State.stream(InputStream.stream("e2", "Stream1").filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(20)))), State.stream(InputStream.stream("e3", "Stream2").filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("price").ofStream("e1"))))))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol").ofStream("e1")).select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2"))).groupBy(Expression.variable("symbol").ofStream("e1")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN, Expression.value(50))));
    query.insertInto("OutputStream");
}
Also used : Query(org.wso2.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 65 with Group

use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.

the class AnalyticsDAOImpl method getApplicationCount.

/**
 * @see AnalyticsDAO#getApplicationCount(Instant, Instant, String)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<ApplicationCount> getApplicationCount(Instant fromTimestamp, Instant toTimestamp, String createdBy) throws APIMgtDAOException {
    final String query;
    if (StringUtils.isNotEmpty(createdBy)) {
        query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_APPLICATION " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "AND CREATED_BY = ?" + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
    } else {
        query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_APPLICATION " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
    }
    List<ApplicationCount> applicationCountList = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setTimestamp(1, Timestamp.from(fromTimestamp));
        statement.setTimestamp(2, Timestamp.from(toTimestamp));
        if (StringUtils.isNotEmpty(createdBy)) {
            statement.setString(3, createdBy);
        }
        log.debug("Executing query: {} ", query);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            long count = 0;
            while (rs.next()) {
                ApplicationCount applicationCount = new ApplicationCount();
                count += rs.getLong("count");
                applicationCount.setTimestamp(rs.getTimestamp("time").getTime());
                applicationCount.setCount(count);
                applicationCountList.add(applicationCount);
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error while creating database connection/prepared-statement";
        throw new APIMgtDAOException(errorMsg, e);
    }
    return applicationCountList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ApplicationCount(org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

Test (org.testng.annotations.Test)155 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)99 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)94 Event (org.wso2.siddhi.core.event.Event)89 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)80 Group (org.wso2.charon3.core.objects.Group)57 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)53 CharonException (org.wso2.charon3.core.exceptions.CharonException)43 HashMap (java.util.HashMap)38 Connection (java.sql.Connection)34 SQLException (java.sql.SQLException)34 ArrayList (java.util.ArrayList)34 IdentitySCIMException (org.wso2.carbon.identity.scim2.common.exceptions.IdentitySCIMException)34 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)33 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)32 PreparedStatement (java.sql.PreparedStatement)29 ResultSet (java.sql.ResultSet)29 Operation (io.swagger.v3.oas.annotations.Operation)27 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)27 Response (javax.ws.rs.core.Response)27