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;
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations