Search in sources :

Example 46 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project product-iots by wso2.

the class APIUtil method getAllEventsForDevice.

/**
 * Get sensor data for a device.
 *
 * @param tableName    table name of the data source
 * @param query        query for the table
 * @param sortByFields list of fields to sort the data by
 * @return List of SensorRecords that is sorted by the fields provided
 * @throws AnalyticsException
 */
public static List<SensorRecord> getAllEventsForDevice(String tableName, String query, List<SortByField> sortByFields) throws AnalyticsException {
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI();
    int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query);
    if (eventCount == 0) {
        return null;
    }
    List<SearchResultEntry> resultEntries = analyticsDataAPI.search(tenantId, tableName, query, 0, eventCount, sortByFields);
    List<String> recordIds = getRecordIds(resultEntries);
    AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds);
    Map<String, SensorRecord> sensorDatas = createSensorData(AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response));
    List<SensorRecord> sortedSensorData = getSortedSensorData(sensorDatas, resultEntries);
    return sortedSensorData;
}
Also used : SensorRecord(org.wso2.iot.sampledevice.api.dto.SensorRecord) AnalyticsDataResponse(org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse) AnalyticsDataAPI(org.wso2.carbon.analytics.api.AnalyticsDataAPI) SearchResultEntry(org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry)

Example 47 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project product-iots by wso2.

the class DeviceTypeUtils method setupDeviceManagementSchema.

public static void setupDeviceManagementSchema() throws DeviceMgtPluginException {
    try {
        Context ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup(DeviceTypeConstants.DATA_SOURCE_NAME);
        DeviceSchemaInitializer initializer = new DeviceSchemaInitializer(dataSource);
        log.info("Initializing device management repository database schema");
        initializer.createRegistryDatabase();
    } catch (NamingException e) {
        log.error("Error while looking up the data source: " + DeviceTypeConstants.DATA_SOURCE_NAME);
    } catch (Exception e) {
        throw new DeviceMgtPluginException("Error occurred while initializing Iot Device " + "Management database schema", e);
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Example 48 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project carbon-apimgt by wso2.

the class PolicyExportManager method createArchiveFromPolicies.

/**
 * Create Archive from policies.
 *
 * @param sourceDir   Source directory containing execution plans
 * @param archiveDir  Archive containing directory
 * @param archiveName name of the archive
 * @return String object of the file path
 * @throws APIMgtDAOException if creating archive failed
 */
private String createArchiveFromPolicies(String sourceDir, String archiveDir, String archiveName) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Creating archive for execution plans.");
    }
    try {
        APIFileUtils.archiveDirectory(sourceDir, archiveDir, archiveName);
    } catch (APIMgtDAOException e) {
        String errorMessage = "Error occurred creating archive :" + archiveName + " at " + archiveDir;
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e);
    }
    String archivePath = archiveDir + File.separator + archiveName + ZIP;
    log.info("Created archive for execution plans: " + archivePath);
    return archivePath;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException)

Example 49 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project carbon-apimgt by wso2.

the class FileBasedApplicationImportExportManager method createArchiveFromExportedAppArtifacts.

/**
 * Creates an archive of the contained application details.
 *
 * @param sourceDirectory Directory which contains source file
 * @param archiveLocation Directory to generate the zip archive
 * @param archiveName     Name of the zip archive
 * @return path to the created archive file
 * @throws APIMgtEntityImportExportException
 */
public String createArchiveFromExportedAppArtifacts(String sourceDirectory, String archiveLocation, String archiveName) throws APIMgtEntityImportExportException {
    String archivedFilePath = null;
    try {
        APIFileUtils.archiveDirectory(sourceDirectory, archiveLocation, archiveName);
    } catch (APIManagementException e) {
        // cleanup the archive root directory
        try {
            FileUtils.deleteDirectory(new File(path));
        } catch (IOException e1) {
            log.warn("Unable to remove directory " + path);
        }
        String errorMsg = "Error while archiving directory " + sourceDirectory;
        throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.APPLICATION_EXPORT_ERROR);
    }
    archivedFilePath = archiveLocation + File.separator + archiveName + ".zip";
    return archivedFilePath;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IOException(java.io.IOException) File(java.io.File) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 50 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultSiddhiAppforAppPolicy.

public static String createDefaultSiddhiAppforAppPolicy() {
    ApplicationPolicy policy = createDefaultApplicationPolicy();
    RequestCountLimit limit = (RequestCountLimit) createDefaultApplicationPolicy().getDefaultQuotaPolicy().getLimit();
    String siddhiApp = "@App:name('application_" + policy.getPolicyName() + "')\n" + "@App:description('ExecutionPlan for app_" + policy.getPolicyName() + "')\n" + "@source(type='inMemory', topic='apim', @map(type='passThrough'))\n" + "define stream RequestStream (messageID string, appKey string, appTier string, " + "subscriptionKey string," + " apiKey string, apiTier string, subscriptionTier string, resourceKey string," + " resourceTier string," + " userId string,  apiContext string, apiVersion string, appTenant string, apiTenant string," + " appId " + "string, apiName string, propertiesMap string);\n" + "@sink(type='jms', @map(type='text'),\n" + "factory.initial='org.wso2.andes.jndi.PropertiesFileInitialContextFactory'," + " provider.url='tcp://localhost:5672', destination='TEST.FOO', connection.factory." + "type='topic',\n" + "connection.factory.jndi.name='TopicConnectionFactory')\n" + "define stream GlobalThrottleStream (throttleKey string, isThrottled bool" + ", expiryTimeStamp long);\n" + "FROM RequestStream\n" + "SELECT messageID, (appTier == '" + policy.getPolicyName() + "') AS isEligible, appKey AS throttleKey, " + "propertiesMap\n" + "INSERT INTO EligibilityStream;\n" + "FROM EligibilityStream[isEligible==true]#throttler:timeBatch(" + policy.getDefaultQuotaPolicy().getLimit().getUnitTime() + " " + policy.getDefaultQuotaPolicy().getLimit().getTimeUnit() + ", 0)\n" + "select throttleKey, (count(messageID) >= " + limit.getRequestCount() + ")" + " as isThrottled, expiryTimeStamp group by throttleKey\n" + "INSERT ALL EVENTS into ResultStream;\n" + "from ResultStream#throttler:emitOnStateChange(throttleKey, isThrottled)\n" + "select *\n" + "insert into GlobalThrottleStream;\n";
    return siddhiApp;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)

Aggregations

Test (org.testng.annotations.Test)22 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 ArrayList (java.util.ArrayList)11 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 OMElement (org.apache.axiom.om.OMElement)8 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)8 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)8 Event (org.wso2.siddhi.core.event.Event)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Compiler (org.wso2.ballerinalang.compiler.Compiler)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)6