public static GenericJsonModel toGenericJsonModel(List<MetricsPacket> metricsPackets, Node node) {
        Map<ServiceId, List<MetricsPacket>> packetsByService = metricsPackets.stream()
                .collect(Collectors.groupingBy(MetricsPacket::service, LinkedHashMap::new, toList()));

        var jsonModel = new GenericJsonModel();
        if (node != null) {
            jsonModel.hostname = node.hostname;
            jsonModel.role = node.role;
        }

        var genericServices = new ArrayList<GenericService>();
        packetsByService.forEach((serviceId, packets) -> {
            var genericMetricsList = packets.stream()
                    .filter(packet -> ! (packet.metrics().isEmpty() && packet.dimensions().isEmpty()))
                    .map(packet -> new GenericMetrics(packet.metrics(), packet.dimensions()))
                    .toList();
            var genericService = packets.stream().findFirst()
                    .map(firstPacket -> new GenericService(serviceId.id,
                                                           firstPacket.timestamp(),
                                                           StatusCode.values()[firstPacket.statusCode()],
                                                           firstPacket.statusMessage(),
                                                           genericMetricsList))
                    .get();
            if (VESPA_NODE_SERVICE_ID.equals(serviceId)) {
                jsonModel.node = new GenericNode(genericService.timeAsInstant(), genericService.metrics);
            } else {
                genericServices.add(genericService);

            }
        });

        jsonModel.services = genericServices;
        return jsonModel;
    }
Utilities for converting between metrics packets and the generic json format.

@author gjoranv
metrics-proxy/src/main/java/ai/vespa/metricsproxy/metric/model/json/GenericJsonUtil.java