public AggregationPipeline aggregationPipeline() { return ds().createAggregation(modelType()); }
public List<KVStore> distinct(MorphiaQuery q, String field, String... fields) { List<Group> id = C.listOf(fields).prepend(field).map(new $.Transformer<String, Group>() { @Override public Group transform(String s) { return Group.grouping(s); } }); AggregationPipeline pipeline = ds().createAggregation(modelType()); if (null != q) { pipeline.match(q.morphiaQuery()); } Iterator<DistinctResult> result = pipeline.group(id).out(DistinctResult.class); List<KVStore> retList = C.newList(); while (result.hasNext()) { DistinctResult dr = result.next(); retList.add(new KVStore(dr._id)); } return retList; }
public List<Metric> findByMetricByAvg(String id, String source, String metric, Periods period) { final Datastore datastore = this.datastore(); Query<LogTrace> query = prepareRange(datastore.createQuery(LogTrace.class), period); query.field(source).equal(id); final AggregationPipeline aggregation = datastore.createAggregation(LogTrace.class) .match(query) .group(source, Group.grouping("metric", Group.last(source)), Group.grouping("value", Accumulator.accumulator("$avg", metric))); final Iterator<Metric> aggregate = aggregation.aggregate(Metric.class); List<Metric> list = new ArrayList<>(); aggregate.forEachRemaining(list::add); return list; }
public List<Metric> findByMetricBySum(String id, String source, String metric, Periods period) { final Datastore datastore = this.datastore(); Query<LogTrace> query = prepareRange(datastore.createQuery(LogTrace.class), period); query.field(source).equal(id); final AggregationPipeline aggregation = datastore.createAggregation(LogTrace.class) .match(query) .group(metric, Group.grouping("metric", Group.last(metric)), Group.grouping("value", Accumulator.accumulator("$sum", 1))); final Iterator<Metric> aggregate = aggregation.aggregate(Metric.class); List<Metric> list = new ArrayList<>(); aggregate.forEachRemaining(list::add); return list; }
/** * Creates a descending list of metrics for a specified period of time. * * @param id Trace field wanted * @param size max number of elements to return * @param period period of time wanted * @return List of metrics */ public List<Metric> findByTop(String id, int size, Periods period) { final Datastore datastore = this.datastore(); Query<LogTrace> query = prepareRange(datastore.createQuery(LogTrace.class), period); query.field(id).notEqual(null); final AggregationPipeline pipeline = datastore.createAggregation(LogTrace.class) .match(query) .group(id, Group.grouping("metric", Group.last(id)), Group.grouping("value", Accumulator.accumulator("$sum", 1))) .sort(Sort.descending("value")) .limit(size); final Iterator<Metric> aggregate = pipeline.aggregate(Metric.class); List<Metric> list = new ArrayList<>(); aggregate.forEachRemaining(list::add); return list; }