Search in sources :

Example 1 with Context

use of org.apache.metron.stellar.dsl.Context in project metron by apache.

the class BinFunctionsTest method run.

public static Object run(String rule, Map<String, Object> variables) {
    Context context = Context.EMPTY_CONTEXT();
    StellarProcessor processor = new StellarProcessor();
    Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
    return processor.parse(rule, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), context);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) StellarFunctions(org.apache.metron.stellar.dsl.StellarFunctions) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) Test(org.junit.Test) Assert(org.junit.Assert) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 2 with Context

use of org.apache.metron.stellar.dsl.Context in project metron by apache.

the class MedianAbsoluteDeviationTest method run.

public static Object run(String rule, Map<String, Object> variables) {
    Context context = Context.EMPTY_CONTEXT();
    StellarProcessor processor = new StellarProcessor();
    Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
    return processor.parse(rule, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), context);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) TDistribution(org.apache.commons.math3.distribution.TDistribution) ImmutableMap(com.google.common.collect.ImmutableMap) SerDeUtils(org.apache.metron.common.utils.SerDeUtils) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) Test(org.junit.Test) GaussianRandomGenerator(org.apache.commons.math3.random.GaussianRandomGenerator) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) ArrayList(java.util.ArrayList) List(java.util.List) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) Map(java.util.Map) StellarFunctions(org.apache.metron.stellar.dsl.StellarFunctions) Assert(org.junit.Assert) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 3 with Context

use of org.apache.metron.stellar.dsl.Context in project metron by apache.

the class ConfigurationsUtils method setupStellarStatically.

public static void setupStellarStatically(CuratorFramework client, Optional<String> globalConfig) {
    /*
      In order to validate stellar functions, the function resolver must be initialized.  Otherwise,
      those utilities that require validation cannot validate the stellar expressions necessarily.
    */
    Context.Builder builder = new Context.Builder().with(Context.Capabilities.ZOOKEEPER_CLIENT, () -> client);
    if (globalConfig.isPresent()) {
        builder = builder.with(Context.Capabilities.GLOBAL_CONFIG, () -> GLOBAL.deserialize(globalConfig.get())).with(Context.Capabilities.STELLAR_CONFIG, () -> GLOBAL.deserialize(globalConfig.get()));
    } else {
        builder = builder.with(Context.Capabilities.STELLAR_CONFIG, () -> new HashMap<>());
    }
    Context stellarContext = builder.build();
    StellarFunctions.FUNCTION_RESOLVER().initialize(stellarContext);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) HashMap(java.util.HashMap)

Example 4 with Context

use of org.apache.metron.stellar.dsl.Context in project metron by apache.

the class GetProfileTest method testWithConfigAndOneGroup.

/**
 * Values should be retrievable that have been stored within a 'group', with
 * configuration different than current global config.
 * This time put the config_override case before the non-override case.
 */
@Test
public void testWithConfigAndOneGroup() {
    final int periodsPerHour = 4;
    final int expectedValue = 2302;
    final int hours = 2;
    final long startTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(hours);
    final List<Object> group = Arrays.asList("weekends");
    // setup - write some measurements to be read later
    final int count = hours * periodsPerHour;
    ProfileMeasurement m = new ProfileMeasurement().withProfileName("profile1").withEntity("entity1").withPeriod(startTime, periodDuration, periodUnits);
    profileWriter.write(m, count, group, val -> expectedValue);
    // create a variable that contains the groups to use
    state.put("groups", group);
    // now change the executor configuration
    Context context2 = setup2();
    // validate it is changed in significant way
    @SuppressWarnings("unchecked") Map<String, Object> global = (Map<String, Object>) context2.getCapability(Context.Capabilities.GLOBAL_CONFIG).get();
    Assert.assertEquals(global.get(PROFILER_PERIOD.getKey()), Long.toString(periodDuration2));
    Assert.assertNotEquals(periodDuration, periodDuration2);
    // execute - read the profile values - with config_override.
    // first two override values are strings, third is deliberately a number.
    String overrides = "{'profiler.client.period.duration' : '" + periodDuration + "', " + "'profiler.client.period.duration.units' : '" + periodUnits.toString() + "', " + "'profiler.client.salt.divisor' : " + saltDivisor + " }";
    String expr = "PROFILE_GET('profile1', 'entity1'" + ", PROFILE_FIXED(4, 'HOURS', " + overrides + "), ['weekends'], " + overrides + ")";
    @SuppressWarnings("unchecked") List<Integer> result = run(expr, List.class);
    // validate - expect to read all values from the past 4 hours
    Assert.assertEquals(count, result.size());
    // execute - read the profile values - with (wrong) default global config values.
    // No error message at this time, but returns empty results list, because
    // row keys are not correctly calculated.
    expr = "PROFILE_GET('profile1', 'entity1', PROFILE_FIXED(4, 'HOURS'), ['weekends'])";
    result = run(expr, List.class);
    // validate - expect to fail to read any values
    Assert.assertEquals(0, result.size());
}
Also used : Context(org.apache.metron.stellar.dsl.Context) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with Context

use of org.apache.metron.stellar.dsl.Context in project metron by apache.

the class StellarAdapter method enrich.

@Override
public JSONObject enrich(CacheKey value) {
    Context stellarContext = (Context) value.getConfig().getConfiguration().get(STELLAR_CONTEXT_CONF);
    ConfigHandler handler = getHandler.apply(value.getConfig());
    Map<String, Object> globalConfig = value.getConfig().getConfiguration();
    Map<String, Object> sensorConfig = value.getConfig().getEnrichment().getConfig();
    if (handler == null) {
        _LOG.trace("Stellar ConfigHandler is null.");
        return new JSONObject();
    }
    Long slowLogThreshold = null;
    if (_PERF_LOG.isDebugEnabled()) {
        slowLogThreshold = ConversionUtils.convert(globalConfig.getOrDefault(STELLAR_SLOW_LOG, STELLAR_SLOW_LOG_DEFAULT), Long.class);
    }
    // Ensure that you clone the message, because process will modify the message.  If the message object is modified
    // then cache misses will happen because the cache will be modified.
    Map<String, Object> message = new HashMap<>(value.getValue(Map.class));
    VariableResolver resolver = new MapVariableResolver(message, sensorConfig, globalConfig);
    StellarProcessor processor = new StellarProcessor();
    JSONObject enriched = process(message, handler, value.getField(), slowLogThreshold, processor, resolver, stellarContext);
    _LOG.trace("Stellar Enrichment Success: {}", enriched);
    return enriched;
}
Also used : Context(org.apache.metron.stellar.dsl.Context) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) JSONObject(org.json.simple.JSONObject) VariableResolver(org.apache.metron.stellar.dsl.VariableResolver) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) HashMap(java.util.HashMap) Map(java.util.Map) ConfigHandler(org.apache.metron.common.configuration.enrichment.handler.ConfigHandler)

Aggregations

Context (org.apache.metron.stellar.dsl.Context)27 Test (org.junit.Test)15 HashMap (java.util.HashMap)14 Map (java.util.Map)12 StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)9 List (java.util.List)8 ArrayList (java.util.ArrayList)6 DefaultVariableResolver (org.apache.metron.stellar.dsl.DefaultVariableResolver)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 StellarFunctions (org.apache.metron.stellar.dsl.StellarFunctions)5 Assert (org.junit.Assert)5 ImmutableList (com.google.common.collect.ImmutableList)3 MapVariableResolver (org.apache.metron.stellar.dsl.MapVariableResolver)3 VariableResolver (org.apache.metron.stellar.dsl.VariableResolver)3 Before (org.junit.Before)3 Collectors (java.util.stream.Collectors)2 StellarPredicateProcessor (org.apache.metron.stellar.common.StellarPredicateProcessor)2 ConversionUtils (org.apache.metron.stellar.common.utils.ConversionUtils)2 ParseException (org.apache.metron.stellar.dsl.ParseException)2 FunctionResolver (org.apache.metron.stellar.dsl.functions.resolver.FunctionResolver)2