use of org.apache.metron.profiler.ProfilePeriod in project metron by apache.
the class HBaseProfilerClient method fetch.
/**
* Fetch the values stored in a profile based on a set of timestamps.
*
* @param clazz The type of values stored by the profile.
* @param profile The name of the profile.
* @param entity The name of the entity.
* @param groups The groups used to sort the profile data.
* @param periods The set of profile measurement periods
* @param defaultValue The default value to specify. If empty, the result will be sparse.
* @return A list of values.
*/
@Override
public <T> List<T> fetch(Class<T> clazz, String profile, String entity, List<Object> groups, Iterable<ProfilePeriod> periods, Optional<T> defaultValue) {
byte[] columnFamily = Bytes.toBytes(columnBuilder.getColumnFamily());
byte[] columnQualifier = columnBuilder.getColumnQualifier("value");
// find all the row keys that satisfy this fetch
List<byte[]> keysToFetch = rowKeyBuilder.rowKeys(profile, entity, groups, periods);
// create a Get for each of the row keys
List<Get> gets = keysToFetch.stream().map(k -> new Get(k).addColumn(columnFamily, columnQualifier)).collect(Collectors.toList());
// get the 'gets'
return get(gets, columnQualifier, columnFamily, clazz, defaultValue);
}
use of org.apache.metron.profiler.ProfilePeriod in project metron by apache.
the class VerboseProfile method apply.
@Override
public Object apply(List<Object> args, Context context) throws ParseException {
// required arguments
String profile = getArg(0, String.class, args);
String entity = getArg(1, String.class, args);
List<ProfilePeriod> periods = getArg(2, List.class, args);
// optional 'groups' argument
List<Object> groups = new ArrayList<>();
if (args.size() >= 4) {
groups = getArg(3, List.class, args);
}
// get globals from the context
Map<String, Object> globals = (Map<String, Object>) context.getCapability(GLOBAL_CONFIG).orElse(Collections.emptyMap());
// lazily create the profiler client, if needed
if (client == null) {
RowKeyBuilder rowKeyBuilder = getRowKeyBuilder(globals);
ColumnBuilder columnBuilder = getColumnBuilder(globals);
TableProvider provider = getTableProvider(globals);
long periodDuration = getPeriodDurationInMillis(globals);
client = new HBaseProfilerClient(provider, rowKeyBuilder, columnBuilder, periodDuration, getTableName(globals), HBaseConfiguration.create());
}
// is there a default value?
Optional<Object> defaultValue = Optional.empty();
if (globals != null) {
defaultValue = Optional.ofNullable(PROFILER_DEFAULT_VALUE.get(globals));
}
List<ProfileMeasurement> measurements = client.fetch(Object.class, profile, entity, groups, periods, defaultValue);
// render a view of each profile measurement
List<Object> results = new ArrayList<>();
for (ProfileMeasurement measurement : measurements) {
results.add(render(measurement));
}
return results;
}
use of org.apache.metron.profiler.ProfilePeriod in project metron by apache.
the class WindowLookbackTest method testSpecifyingOnlySelector.
@Test
public void testSpecifyingOnlySelector() {
String stellarStatement = "PROFILE_WINDOW('1 hour')";
Map<String, Object> variables = new HashMap<>();
StellarProcessor stellar = new StellarProcessor();
List<ProfilePeriod> periods = (List<ProfilePeriod>) stellar.parse(stellarStatement, new DefaultVariableResolver(k -> variables.get(k), k -> variables.containsKey(k)), resolver, context);
assertEquals(TimeUnit.HOURS.toMillis(1) / getDurationMs(), periods.size());
}
use of org.apache.metron.profiler.ProfilePeriod in project metron by apache.
the class WindowLookbackTest method test.
public State test(String windowSelector, Date now, Optional<Map<String, Object>> config, Assertions... assertions) {
List<Range<Long>> windowIntervals = WindowProcessor.process(windowSelector).toIntervals(now.getTime());
String stellarStatement = "PROFILE_WINDOW('" + windowSelector + "', now" + (config.isPresent() ? ", config" : "") + ")";
Map<String, Object> variables = new HashMap<>();
variables.put("now", now.getTime());
if (config.isPresent()) {
variables.put("config", config.get());
}
StellarProcessor stellar = new StellarProcessor();
List<ProfilePeriod> periods = (List<ProfilePeriod>) stellar.parse(stellarStatement, new DefaultVariableResolver(k -> variables.get(k), k -> variables.containsKey(k)), resolver, context);
State state = new State(windowIntervals, periods);
for (Assertions assertion : assertions) {
assertTrue(assertion.test(state), assertion.name());
}
return state;
}
use of org.apache.metron.profiler.ProfilePeriod in project metron by apache.
the class SaltyRowKeyBuilderTest method testRowKeys.
/**
* `rowKeys` should return all of the row keys needed to retrieve the profile values over a given time horizon.
*/
@Test
public void testRowKeys() {
int hoursAgo = 1;
// setup
List<Object> groups = Collections.emptyList();
rowKeyBuilder = new SaltyRowKeyBuilder(saltDivisor, periodDuration, periodUnits);
// a dummy profile measurement
long now = System.currentTimeMillis();
long oldest = now - TimeUnit.HOURS.toMillis(hoursAgo);
ProfileMeasurement m = new ProfileMeasurement().withProfileName("profile").withEntity("entity").withPeriod(oldest, periodDuration, periodUnits).withProfileValue(22);
// generate a list of expected keys
List<byte[]> expectedKeys = new ArrayList<>();
for (int i = 0; i < (hoursAgo * 4) + 1; i++) {
// generate the expected key
byte[] rk = rowKeyBuilder.rowKey(m);
expectedKeys.add(rk);
// advance to the next period
ProfilePeriod next = m.getPeriod().next();
m = new ProfileMeasurement().withProfileName("profile").withEntity("entity").withPeriod(next.getStartTimeMillis(), periodDuration, periodUnits);
}
// execute
List<byte[]> actualKeys = rowKeyBuilder.rowKeys(measurement.getProfileName(), measurement.getEntity(), groups, oldest, now);
// validate - expectedKeys == actualKeys
for (int i = 0; i < actualKeys.size(); i++) {
byte[] actual = actualKeys.get(i);
byte[] expected = expectedKeys.get(i);
assertThat(actual, equalTo(expected));
}
}
Aggregations