use of org.apache.plc4x.java.scraper.exception.ScraperConfigurationException in project plc4x by apache.
the class TriggerConfiguration method createConfiguration.
/**
* creates the TriggerConfiguration for a given ScrapeJob from triggerConfig-String
* @param jobTriggerStrategy config-string from file
* @param triggeredScrapeJob job belonging to the config
* @return created TriggerConfiguration
* @throws ScraperConfigurationException when something goes wrong
*/
public static TriggerConfiguration createConfiguration(String jobTriggerStrategy, TriggeredScrapeJobImpl triggeredScrapeJob) throws ScraperConfigurationException {
Matcher matcher = TRIGGER_STRATEGY_PATTERN.matcher(jobTriggerStrategy);
if (matcher.matches()) {
String triggerStrategy = matcher.group("strategy");
String scheduledMs = matcher.group("scheduledInterval");
if (logger.isDebugEnabled()) {
logger.debug("Strategy: {}, scheduled ms: {}", triggerStrategy, scheduledMs);
}
String triggerVar = matcher.group("triggerVar");
String comparatorString = matcher.group("comp");
String comparatorVariable = matcher.group("compVar");
switch(triggerStrategy) {
case TRIGGER:
if (triggerVar == null || comparatorString == null || comparatorVariable == null) {
throw new ScraperConfigurationException("TRIGGER_VAR trigger strategy needs the trigger-condition - information missing! given configString: " + jobTriggerStrategy);
}
List<TriggerElement> triggerElements = new ArrayList<>();
// TODO Change this (probably only 1 source to get the connection directly)
String connectionString = triggeredScrapeJob.getSourceConnections().get(triggeredScrapeJob.getSourceConnections().keySet().iterator().next());
TriggerElement triggerElement = new TriggerElement(comparatorString, null, comparatorVariable, triggerVar, triggerStrategy, connectionString);
triggerElement.setTriggerJob(triggeredScrapeJob.getJobName());
triggerElements.add(triggerElement);
String concatConn = matcher.group("concatConn");
String triggerVar2 = matcher.group("triggerVar2");
String comparatorString2 = matcher.group("comp2");
String comparatorVariable2 = matcher.group("compVar2");
if (triggerVar2 != null && comparatorString2 != null && comparatorVariable2 != null && concatConn != null) {
TriggerElement triggerElement2 = new TriggerElement(comparatorString2, concatConn, comparatorVariable2, triggerVar2, triggerStrategy, connectionString);
triggerElement2.setTriggerJob(triggeredScrapeJob.getJobName());
triggerElements.add(triggerElement2);
}
// ToDo add clever Strategy to concat more than two conditions if needed
return new TriggerConfiguration(TriggerType.TRIGGER_VAR, scheduledMs, triggerElements, triggeredScrapeJob);
case SCHEDULED:
if (triggerVar != null || comparatorString != null || comparatorVariable != null) {
throw new ScraperConfigurationException("SCHEDULED trigger strategy must only be used with scheduled interval - nothing more! given configString: " + jobTriggerStrategy);
}
return new TriggerConfiguration(TriggerType.SCHEDULED, scheduledMs);
default:
throw new ScraperConfigurationException("Unknown Trigger Strategy " + triggerStrategy);
}
}
throw new ScraperConfigurationException("Invalid trigger strategy string description: " + jobTriggerStrategy);
}
use of org.apache.plc4x.java.scraper.exception.ScraperConfigurationException in project plc4x by apache.
the class TriggerConfigurationTest method testValidFieldQueryParsing.
@ParameterizedTest
@MethodSource("validTriggerPattern")
void testValidFieldQueryParsing(String triggerConfig, TriggerConfiguration.TriggerType triggerType, long scrapeInterval, TriggerConfiguration.Comparator comparator1, Object refValue1, Boolean previousMode1, TriggerConfiguration.Comparator comparator2, Object refValue2, Boolean previousMode2, TriggerConfiguration.ConcatType concatType) {
TriggeredScrapeJobImpl triggeredScrapeJob = Mockito.mock(TriggeredScrapeJobImpl.class);
TriggerConfiguration triggerConfiguration = null;
try {
triggerConfiguration = TriggerConfiguration.createConfiguration(triggerConfig, triggeredScrapeJob);
} catch (ScraperConfigurationException e) {
// should not happen
}
assertThat(triggerConfiguration, notNullValue());
assertThat(triggerConfiguration.getScrapeInterval(), equalTo(scrapeInterval));
assertThat(triggerConfiguration.getTriggerType(), equalTo(triggerType));
if (!triggerConfiguration.getTriggerElementList().isEmpty()) {
assertThat(triggerConfiguration.getTriggerElementList().get(0).getComparatorType(), equalTo(comparator1));
assertThat(triggerConfiguration.getTriggerElementList().get(0).getCompareValue(), equalTo(refValue1));
assertThat(triggerConfiguration.getTriggerElementList().get(0).getPreviousMode(), equalTo(previousMode1));
assertThat(triggerConfiguration.getTriggerElementList().get(0).getConcatType(), nullValue());
if (triggerConfiguration.getTriggerElementList().size() > 1) {
assertThat(triggerConfiguration.getTriggerElementList().get(1).getComparatorType(), equalTo(comparator2));
assertThat(triggerConfiguration.getTriggerElementList().get(1).getCompareValue(), equalTo(refValue2));
assertThat(triggerConfiguration.getTriggerElementList().get(1).getPreviousMode(), equalTo(previousMode2));
assertThat(triggerConfiguration.getTriggerElementList().get(1).getConcatType(), equalTo(concatType));
}
}
}
use of org.apache.plc4x.java.scraper.exception.ScraperConfigurationException in project plc4x by apache.
the class ScraperConfigurationTriggeredImpl method getJobs.
public static List<ScrapeJob> getJobs(List<JobConfigurationImpl> jobConfigurations, Map<String, String> sources) throws ScraperConfigurationException {
List<ScrapeJob> scrapeJobs = new ArrayList<>();
for (JobConfiguration jobConfiguration : jobConfigurations) {
if (jobConfiguration.getTriggerConfig() != null) {
logger.info("Assuming job as triggered job because triggerConfig has been set");
scrapeJobs.add(new TriggeredScrapeJobImpl(jobConfiguration.getName(), jobConfiguration.getTriggerConfig(), getSourcesForAliases(jobConfiguration.getSources(), sources), jobConfiguration.getFields()));
} else {
if (jobConfiguration.getScrapeRate() != null) {
logger.info("Assuming job as classic job because triggerConfig has NOT been set but scrapeRate has.");
scrapeJobs.add(new ScrapeJobImpl(jobConfiguration.getName(), jobConfiguration.getScrapeRate(), getSourcesForAliases(jobConfiguration.getSources(), sources), jobConfiguration.getFields()));
} else {
logger.info("Job has lack of trigger/scheduled config");
throw new ScraperConfigurationException(String.format("Job %s was intended to be o triggered annotation, but no triggerConfig-Field could be found. Canceling!", jobConfiguration.getName()));
}
}
}
return scrapeJobs;
}
Aggregations