|
@@ -10,9 +10,11 @@ import org.jetlinks.community.device.service.LocalDeviceInstanceService;
|
|
import org.jetlinks.community.device.timeseries.DeviceTimeSeriesMetric;
|
|
import org.jetlinks.community.device.timeseries.DeviceTimeSeriesMetric;
|
|
import org.jetlinks.community.gateway.MessageGateway;
|
|
import org.jetlinks.community.gateway.MessageGateway;
|
|
import org.jetlinks.community.gateway.TopicMessage;
|
|
import org.jetlinks.community.gateway.TopicMessage;
|
|
|
|
+import org.jetlinks.community.gateway.annotation.Subscribe;
|
|
import org.jetlinks.community.micrometer.MeterRegistryManager;
|
|
import org.jetlinks.community.micrometer.MeterRegistryManager;
|
|
import org.jetlinks.community.timeseries.TimeSeriesManager;
|
|
import org.jetlinks.community.timeseries.TimeSeriesManager;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
@@ -22,6 +24,19 @@ import java.util.function.Function;
|
|
@Component
|
|
@Component
|
|
public class DeviceStatusMeasurementProvider extends StaticMeasurementProvider {
|
|
public class DeviceStatusMeasurementProvider extends StaticMeasurementProvider {
|
|
|
|
|
|
|
|
+ private MeterRegistry registry;
|
|
|
|
+
|
|
|
|
+ Map<String, LongAdder> productCounts = new ConcurrentHashMap<>();
|
|
|
|
+
|
|
|
|
+ Function<String, LongAdder> counterAdder = productId ->
|
|
|
|
+ productCounts.computeIfAbsent(productId, __id -> {
|
|
|
|
+ LongAdder adder = new LongAdder();
|
|
|
|
+ Gauge.builder("online-count", adder, LongAdder::sum)
|
|
|
|
+ .tag("productId", __id)
|
|
|
|
+ .register(registry);
|
|
|
|
+ return adder;
|
|
|
|
+ });
|
|
|
|
+
|
|
public DeviceStatusMeasurementProvider(MeterRegistryManager registryManager,
|
|
public DeviceStatusMeasurementProvider(MeterRegistryManager registryManager,
|
|
LocalDeviceInstanceService instanceService,
|
|
LocalDeviceInstanceService instanceService,
|
|
TimeSeriesManager timeSeriesManager,
|
|
TimeSeriesManager timeSeriesManager,
|
|
@@ -32,37 +47,30 @@ public class DeviceStatusMeasurementProvider extends StaticMeasurementProvider {
|
|
|
|
|
|
addMeasurement(new DeviceStatusRecordMeasurement(instanceService, timeSeriesManager));
|
|
addMeasurement(new DeviceStatusRecordMeasurement(instanceService, timeSeriesManager));
|
|
|
|
|
|
- MeterRegistry registry = registryManager.getMeterRegister(DeviceTimeSeriesMetric.deviceMetrics().getId(),
|
|
|
|
|
|
+ registry = registryManager.getMeterRegister(DeviceTimeSeriesMetric.deviceMetrics().getId(),
|
|
"target", "msgType", "productId");
|
|
"target", "msgType", "productId");
|
|
- Map<String, LongAdder> productCounts = new ConcurrentHashMap<>();
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- Function<String, LongAdder> counterAdder = productId ->
|
|
|
|
- productCounts.computeIfAbsent(productId, __id -> {
|
|
|
|
- LongAdder adder = new LongAdder();
|
|
|
|
- Gauge.builder("online-count", adder, LongAdder::sum)
|
|
|
|
- .tag("productId", __id)
|
|
|
|
- .register(registry);
|
|
|
|
- return adder;
|
|
|
|
- });
|
|
|
|
|
|
+ @Subscribe("/device/*/online")
|
|
|
|
+ public Mono<Void> incrementOnline(TopicMessage msg){
|
|
|
|
+ return Mono.fromRunnable(()->{
|
|
|
|
+ String productId = parseProductId(msg);
|
|
|
|
+ counterAdder.apply(productId).increment();
|
|
|
|
+ registry
|
|
|
|
+ .counter("online", "productId", productId)
|
|
|
|
+ .increment();
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
|
|
- //上线
|
|
|
|
- messageGateway.subscribe("/device/*/online")
|
|
|
|
- .map(this::parseProductId)
|
|
|
|
- .subscribe(productId -> {
|
|
|
|
- counterAdder.apply(productId).increment();
|
|
|
|
- registry
|
|
|
|
- .counter("online", "productId", productId)
|
|
|
|
- .increment();
|
|
|
|
- });
|
|
|
|
- //下线
|
|
|
|
- messageGateway.subscribe("/device/*/offline")
|
|
|
|
- .map(this::parseProductId)
|
|
|
|
- .subscribe(productId -> {
|
|
|
|
- counterAdder.apply(productId).decrement();
|
|
|
|
- registry
|
|
|
|
- .counter("offline", "productId", productId)
|
|
|
|
- .increment();
|
|
|
|
- });
|
|
|
|
|
|
+ @Subscribe("/device/*/offline")
|
|
|
|
+ public Mono<Void> incrementOffline(TopicMessage msg){
|
|
|
|
+ return Mono.fromRunnable(()->{
|
|
|
|
+ String productId = parseProductId(msg);
|
|
|
|
+ counterAdder.apply(productId).increment();
|
|
|
|
+ registry
|
|
|
|
+ .counter("offline", "productId", productId)
|
|
|
|
+ .increment();
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
private String parseProductId(TopicMessage msg) {
|
|
private String parseProductId(TopicMessage msg) {
|