package org.kepennar.aproc.metrics;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.reverseOrder;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Future;
import java.util.function.Function;

import javax.inject.Inject;

import org.kepennar.aproc.metrics.model.Metric;
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

@Service
public class MetricsService {
	private final MetricRepository repo;
	
	private Function<org.springframework.boot.actuate.metrics.Metric<?>, Metric> toMetric = m -> {
		String name = m.getName().substring(15);
		return new Metric(name, m.getValue().longValue());
	};
	private Comparator<Long> reverse = reverseOrder();
	
	@Inject
	public MetricsService(MetricRepository repo) {
		this.repo = repo;
	}
	
	@Async
	public Future<List<Metric>> getCounters() {
		List<Metric> metrics = newArrayList(repo.findAll())
			.stream().filter(m-> {
				return m.getName().matches("counter.status\\..*");
			}).map(toMetric)
			.sorted(comparing(m -> m.getValue(), reverse))
		.collect(toList());
		return new AsyncResult<>(metrics);
	}
	
	@Async
	public Future<List<Metric>> getGauges() {
		 		
		 List<Metric> metrics = newArrayList(repo.findAll())
				.stream().filter(m-> {
					return m.getName().matches("gauge.response\\..*");
				})
				.map(toMetric)				
				.sorted(comparing(m -> m.getValue(), reverse))
			.collect(toList());
		 return new AsyncResult<>(metrics);
	}
}

@@ -240,37 +240,8 @@
 t;%0A%0A
-import java.util.Comparator;%0A
 impo
@@ -931,60 +931,8 @@
 %09%7D;%0A
-%09private Comparator%3CLong%3E reverse = reverseOrder();%0A
 %09%0A%09@
@@ -1070,116 +1070,51 @@
 %7B%0A%09%09
-List%3CMetric%3E metrics = newArrayList(repo.findAll())%0A%09%09%09.stream().filter(m-%3E %7B%0A%09%09%09%09return m.getName().m
+return new AsyncResult%3C%3E(getMetricsM
 atch
-es
+ing
 (%22co
@@ -1136,101 +1136,66 @@
 .*%22)
+)
 ;%0A%09
-%09%09%7D).map(toMetric)%0A%09%09%09.sorted(comparing(m -%3E m.getValue(), reverse))%0A%09%09.collect(toList());
+%7D%0A%09%0A%09@Async%0A%09public Future%3CList%3CMetric%3E%3E getGauges() %7B
 %0A%09%09r
@@ -1222,46 +1222,66 @@
 t%3C%3E(
-m
+getM
 etrics
-);%0A%09%7D%0A%09%0A%09@Async%0A%09public Future%3C
+Matching(%22gauge.response%5C%5C..*%22));%0A%09%7D%0A%09%0A%09private 
 List
@@ -1292,55 +1292,52 @@
 ric%3E
-%3E
  get
-Gauges() %7B%0A%09%09 %09%09%0A%09%09 List%3CMetric%3E metrics =
+MetricsMatching(String regex) %7B%0A%09%09return
  new
@@ -1384,16 +1384,17 @@
 filter(m
+ 
 -%3E %7B%0A%09%09%09
@@ -1422,37 +1422,21 @@
 matches(
-%22gauge.response%5C%5C..*%22
+regex
 );%0A%09%09%09%09%7D
@@ -1508,16 +1508,23 @@
  reverse
+Order()
 ))%0A%09%09%09.c
@@ -1545,46 +1545,8 @@
 ));%0A
-%09%09 return new AsyncResult%3C%3E(metrics);%0A
 %09%7D%0A%7D

remove duplication