Search in sources :

Example 1 with ScraperConfigurationException

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);
}
Also used : Matcher(java.util.regex.Matcher) ScraperConfigurationException(org.apache.plc4x.java.scraper.exception.ScraperConfigurationException) ArrayList(java.util.ArrayList)

Example 2 with ScraperConfigurationException

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));
        }
    }
}
Also used : ScraperConfigurationException(org.apache.plc4x.java.scraper.exception.ScraperConfigurationException) TriggeredScrapeJobImpl(org.apache.plc4x.java.scraper.triggeredscraper.TriggeredScrapeJobImpl) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with ScraperConfigurationException

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;
}
Also used : ScrapeJob(org.apache.plc4x.java.scraper.ScrapeJob) TriggeredScrapeJobImpl(org.apache.plc4x.java.scraper.triggeredscraper.TriggeredScrapeJobImpl) ScrapeJobImpl(org.apache.plc4x.java.scraper.ScrapeJobImpl) ScraperConfigurationException(org.apache.plc4x.java.scraper.exception.ScraperConfigurationException) ArrayList(java.util.ArrayList) TriggeredScrapeJobImpl(org.apache.plc4x.java.scraper.triggeredscraper.TriggeredScrapeJobImpl) JobConfiguration(org.apache.plc4x.java.scraper.config.JobConfiguration)

Aggregations

ScraperConfigurationException (org.apache.plc4x.java.scraper.exception.ScraperConfigurationException)3 ArrayList (java.util.ArrayList)2 TriggeredScrapeJobImpl (org.apache.plc4x.java.scraper.triggeredscraper.TriggeredScrapeJobImpl)2 Matcher (java.util.regex.Matcher)1 ScrapeJob (org.apache.plc4x.java.scraper.ScrapeJob)1 ScrapeJobImpl (org.apache.plc4x.java.scraper.ScrapeJobImpl)1 JobConfiguration (org.apache.plc4x.java.scraper.config.JobConfiguration)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1