refactor: remove telemetry field api from history app
This commit is contained in:
@@ -24,19 +24,12 @@ public final class EventHistoryEnvelopeIngestor implements EnvelopeBatchIngestor
|
||||
private static final Logger log = LoggerFactory.getLogger(EventHistoryEnvelopeIngestor.class);
|
||||
|
||||
private final TdengineHistoryWriter tdengineWriter;
|
||||
private final boolean telemetryFieldsEnabled;
|
||||
|
||||
public EventHistoryEnvelopeIngestor(TdengineHistoryWriter tdengineWriter) {
|
||||
this(tdengineWriter, false);
|
||||
}
|
||||
|
||||
public EventHistoryEnvelopeIngestor(TdengineHistoryWriter tdengineWriter,
|
||||
boolean telemetryFieldsEnabled) {
|
||||
if (tdengineWriter == null) {
|
||||
throw new IllegalArgumentException("tdengineWriter must not be null");
|
||||
}
|
||||
this.tdengineWriter = tdengineWriter;
|
||||
this.telemetryFieldsEnabled = telemetryFieldsEnabled;
|
||||
}
|
||||
|
||||
public void ingest(byte[] kafkaValue) throws IOException {
|
||||
@@ -129,13 +122,5 @@ public final class EventHistoryEnvelopeIngestor implements EnvelopeBatchIngestor
|
||||
if (!locations.isEmpty()) {
|
||||
tdengineWriter.appendLocations(locations);
|
||||
}
|
||||
if (telemetryFieldsEnabled) {
|
||||
var telemetryFields = envelopes.stream()
|
||||
.flatMap(envelope -> TdengineEnvelopeRows.telemetryFields(envelope).stream())
|
||||
.toList();
|
||||
if (!telemetryFields.isEmpty()) {
|
||||
tdengineWriter.appendTelemetryFields(telemetryFields);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,11 @@ package com.lingniu.ingest.eventhistory;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineHistoryReader;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineLocationRow;
|
||||
import com.lingniu.ingest.tdenginehistory.TdenginePage;
|
||||
import com.lingniu.ingest.tdenginehistory.TdenginePageCursor;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineQueryOrder;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineRawFrameQuery;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineRawFrameRow;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineTelemetryFieldRow;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -39,8 +37,6 @@ public final class RawFrameHistoryController {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {};
|
||||
private static final int LOCATION_ENRICH_LIMIT = 1;
|
||||
private static final int TELEMETRY_ENRICH_LIMIT = 500;
|
||||
private final TdengineHistoryReader reader;
|
||||
|
||||
public RawFrameHistoryController(TdengineHistoryReader reader) {
|
||||
@@ -76,8 +72,6 @@ public final class RawFrameHistoryController {
|
||||
@RequestParam(defaultValue = "DESC") TdengineQueryOrder order,
|
||||
@Parameter(description = "返回原始帧数量上限,最大 1000。", example = "100")
|
||||
@RequestParam(defaultValue = "100") int limit,
|
||||
@Parameter(description = "是否按 rawUri 补充 location/telemetry 解析字段。默认 false,避免 raw 列表产生额外 TDengine 查询。", example = "false")
|
||||
@RequestParam(defaultValue = "false") boolean includeParsedFields,
|
||||
@Parameter(description = "上一页返回的 nextCursor.ts。", example = "2026-06-29T05:00:01Z")
|
||||
@RequestParam(required = false) String cursorTs,
|
||||
@Parameter(description = "上一页返回的 nextCursor.id。", example = "frame-xxx")
|
||||
@@ -101,7 +95,7 @@ public final class RawFrameHistoryController {
|
||||
order,
|
||||
limit,
|
||||
cursor(cursorTs, cursorId)));
|
||||
return RawFramePageResponse.from(reader, page, includeParsedFields);
|
||||
return RawFramePageResponse.from(page);
|
||||
}
|
||||
|
||||
private static Integer parseMessageId(String messageId) {
|
||||
@@ -152,15 +146,13 @@ public final class RawFrameHistoryController {
|
||||
List<RawFrameResponse> items,
|
||||
CursorResponse nextCursor) {
|
||||
|
||||
private static RawFramePageResponse from(TdengineHistoryReader reader,
|
||||
TdenginePage<TdengineRawFrameRow> page,
|
||||
boolean includeParsedFields) throws IOException {
|
||||
private static RawFramePageResponse from(TdenginePage<TdengineRawFrameRow> page) {
|
||||
CursorResponse next = page.nextCursor()
|
||||
.map(cursor -> new CursorResponse(cursor.ts().toString(), cursor.tieBreaker()))
|
||||
.orElse(null);
|
||||
List<RawFrameResponse> items = new java.util.ArrayList<>(page.items().size());
|
||||
for (TdengineRawFrameRow row : page.items()) {
|
||||
items.add(RawFrameResponse.from(reader, row, includeParsedFields));
|
||||
items.add(RawFrameResponse.from(row));
|
||||
}
|
||||
return new RawFramePageResponse(
|
||||
items,
|
||||
@@ -193,9 +185,7 @@ public final class RawFrameHistoryController {
|
||||
String vin,
|
||||
String phone) {
|
||||
|
||||
private static RawFrameResponse from(TdengineHistoryReader reader,
|
||||
TdengineRawFrameRow row,
|
||||
boolean includeParsedFields) throws IOException {
|
||||
private static RawFrameResponse from(TdengineRawFrameRow row) {
|
||||
Map<String, Object> metadata = metadata(row.metadataJson());
|
||||
return new RawFrameResponse(
|
||||
row.ts().toString(),
|
||||
@@ -213,7 +203,7 @@ public final class RawFrameHistoryController {
|
||||
row.metadataJson(),
|
||||
row.parsedJson(),
|
||||
metadata,
|
||||
parsedFields(reader, row, metadata, parsed(row.parsedJson()), includeParsedFields),
|
||||
parsedFields(metadata, parsed(row.parsedJson())),
|
||||
row.protocol(),
|
||||
row.vehicleKey(),
|
||||
row.vin(),
|
||||
@@ -231,11 +221,8 @@ public final class RawFrameHistoryController {
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, Object> parsedFields(TdengineHistoryReader reader,
|
||||
TdengineRawFrameRow row,
|
||||
Map<String, Object> metadata,
|
||||
Map<String, Object> parsed,
|
||||
boolean includeParsedFields) throws IOException {
|
||||
private static Map<String, Object> parsedFields(Map<String, Object> metadata,
|
||||
Map<String, Object> parsed) {
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
if (parsed.isEmpty()) {
|
||||
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
|
||||
@@ -252,21 +239,6 @@ public final class RawFrameHistoryController {
|
||||
} else {
|
||||
out.putAll(parsed);
|
||||
}
|
||||
if (!includeParsedFields) {
|
||||
return immutableMap(out);
|
||||
}
|
||||
for (TdengineLocationRow location : reader.queryLocationsByRawUri(
|
||||
row.protocol(), row.rawUri(), LOCATION_ENRICH_LIMIT)) {
|
||||
addLocation(out, location);
|
||||
}
|
||||
for (TdengineTelemetryFieldRow field : reader.queryTelemetryFieldsByRawUri(
|
||||
row.protocol(), row.rawUri(), TELEMETRY_ENRICH_LIMIT)) {
|
||||
out.put(field.fieldKey(), typedValue(field));
|
||||
out.put(field.fieldKey() + "._type", field.valueType());
|
||||
if (field.unit() != null && !field.unit().isBlank()) {
|
||||
out.put(field.fieldKey() + "._unit", field.unit());
|
||||
}
|
||||
}
|
||||
return immutableMap(out);
|
||||
}
|
||||
|
||||
@@ -281,29 +253,6 @@ public final class RawFrameHistoryController {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addLocation(Map<String, Object> out, TdengineLocationRow row) {
|
||||
out.put("location.longitude", row.longitude());
|
||||
out.put("location.latitude", row.latitude());
|
||||
out.put("location.altitudeM", row.altitudeM());
|
||||
out.put("location.speedKmh", row.speedKmh());
|
||||
out.put("location.directionDeg", row.directionDeg());
|
||||
out.put("location.alarmFlag", row.alarmFlag());
|
||||
out.put("location.statusFlag", row.statusFlag());
|
||||
if (row.totalMileageKm() != null) {
|
||||
out.put("location.totalMileageKm", row.totalMileageKm());
|
||||
}
|
||||
}
|
||||
|
||||
private static Object typedValue(TdengineTelemetryFieldRow field) {
|
||||
if (field.valueDouble() != null) {
|
||||
return field.valueDouble();
|
||||
}
|
||||
if (field.valueLong() != null) {
|
||||
return field.valueLong();
|
||||
}
|
||||
return field.valueText();
|
||||
}
|
||||
|
||||
private static Map<String, Object> immutableMap(Map<String, Object> values) {
|
||||
return Collections.unmodifiableMap(new LinkedHashMap<>(values));
|
||||
}
|
||||
|
||||
@@ -1,213 +0,0 @@
|
||||
package com.lingniu.ingest.eventhistory;
|
||||
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineHistoryReader;
|
||||
import com.lingniu.ingest.tdenginehistory.TdenginePage;
|
||||
import com.lingniu.ingest.tdenginehistory.TdenginePageCursor;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineQueryOrder;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineTelemetryFieldQuery;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineTelemetryFieldRow;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@ConditionalOnProperty(
|
||||
name = {
|
||||
"lingniu.ingest.event-history.enabled",
|
||||
"lingniu.ingest.tdengine-history.telemetry-fields-enabled"
|
||||
},
|
||||
havingValue = "true")
|
||||
@ConditionalOnBean(TdengineHistoryReader.class)
|
||||
@RequestMapping("/api/event-history/telemetry")
|
||||
@Tag(name = "telemetry-field-history-controller", description = "遥测字段历史分页查询接口。")
|
||||
public final class TelemetryFieldHistoryController {
|
||||
|
||||
private static final Map<String, String> JT808_FIELD_ALIASES = Map.of(
|
||||
"location.longitude", "longitude",
|
||||
"location.latitude", "latitude",
|
||||
"location.altitude_m", "altitude_m",
|
||||
"location.speed_kmh", "speed_kmh",
|
||||
"location.direction_deg", "direction_deg",
|
||||
"location.alarm_flag", "location_alarm_flag",
|
||||
"location.status_flag", "location_status_raw");
|
||||
|
||||
private final TdengineHistoryReader reader;
|
||||
|
||||
public TelemetryFieldHistoryController(TdengineHistoryReader reader) {
|
||||
if (reader == null) {
|
||||
throw new IllegalArgumentException("reader must not be null");
|
||||
}
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
@GetMapping("/fields")
|
||||
@Operation(
|
||||
summary = "查询单车单字段历史值",
|
||||
description = "按协议、车辆标识、字段 key 和时间范围查询 TDengine telemetry_fields。分页使用 cursorTs + cursorId,不使用 offset。")
|
||||
public TelemetryFieldPageResponse fields(
|
||||
@Parameter(description = "协议名,例如 GB32960 或 JT808。", required = true, example = "GB32960")
|
||||
@RequestParam String protocol,
|
||||
@Parameter(description = "字段 key,例如 FUEL_CELL_V2016.hydrogenComsuxxx。", required = true)
|
||||
@RequestParam String fieldKey,
|
||||
@Parameter(description = "内部车辆键。传入后优先使用。", example = "LNVFC000000000001")
|
||||
@RequestParam(required = false) String vehicleKey,
|
||||
@Parameter(description = "VIN。GB32960 常用该字段作为车辆键。", example = "LNVFC000000000001")
|
||||
@RequestParam(required = false) String vin,
|
||||
@Parameter(description = "JT808 终端手机号或终端标识;JT808 会自动映射为 jt808:<phone>。", example = "g7gps")
|
||||
@RequestParam(required = false) String phone,
|
||||
@Parameter(description = "开始时间,支持日期或精确到秒的时间;无时区时按东八区解析。", example = "2026-06-29T13:00:00")
|
||||
@RequestParam String dateFrom,
|
||||
@Parameter(description = "结束时间,支持日期或精确到秒的时间;无时区时按东八区解析。", example = "2026-06-29T13:10:00")
|
||||
@RequestParam String dateTo,
|
||||
@Parameter(description = "排序方向。", example = "DESC")
|
||||
@RequestParam(defaultValue = "DESC") TdengineQueryOrder order,
|
||||
@Parameter(description = "返回值数量上限,最大 1000。", example = "100")
|
||||
@RequestParam(defaultValue = "100") int limit,
|
||||
@Parameter(description = "上一页返回的 nextCursor.ts。", example = "2026-06-29T05:00:01Z")
|
||||
@RequestParam(required = false) String cursorTs,
|
||||
@Parameter(description = "上一页返回的 nextCursor.id。", example = "event-xxx#0")
|
||||
@RequestParam(required = false) String cursorId) throws IOException {
|
||||
String normalizedProtocol = require(protocol, "protocol is required for telemetry field query").toUpperCase(Locale.ROOT);
|
||||
String normalizedFieldKey = normalizeFieldKey(
|
||||
normalizedProtocol,
|
||||
require(fieldKey, "fieldKey is required for telemetry field query"));
|
||||
QueryTimeRange range = QueryTimeRange.parse(dateFrom, dateTo);
|
||||
TdenginePage<TdengineTelemetryFieldRow> page = reader.queryTelemetryFields(new TdengineTelemetryFieldQuery(
|
||||
normalizedProtocol,
|
||||
resolveVehicleKey(normalizedProtocol, vehicleKey, vin, phone),
|
||||
normalizedFieldKey,
|
||||
range.eventTimeFrom(),
|
||||
range.eventTimeTo().plusMillis(1),
|
||||
order,
|
||||
limit,
|
||||
cursor(cursorTs, cursorId)));
|
||||
return TelemetryFieldPageResponse.from(page);
|
||||
}
|
||||
|
||||
private static String normalizeFieldKey(String protocol, String fieldKey) {
|
||||
if (!"JT808".equals(protocol)) {
|
||||
return fieldKey;
|
||||
}
|
||||
return JT808_FIELD_ALIASES.getOrDefault(fieldKey, fieldKey);
|
||||
}
|
||||
|
||||
private static String resolveVehicleKey(String protocol, String vehicleKey, String vin, String phone) {
|
||||
String explicitVehicleKey = trimToNull(vehicleKey);
|
||||
if (explicitVehicleKey != null) {
|
||||
return explicitVehicleKey;
|
||||
}
|
||||
String vinValue = trimToNull(vin);
|
||||
if (vinValue != null) {
|
||||
return vinValue;
|
||||
}
|
||||
String phoneValue = trimToNull(phone);
|
||||
if (phoneValue != null) {
|
||||
if ("JT808".equals(protocol) && !phoneValue.startsWith("jt808:")) {
|
||||
return "jt808:" + phoneValue;
|
||||
}
|
||||
return phoneValue;
|
||||
}
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "vehicleKey, vin or phone is required");
|
||||
}
|
||||
|
||||
private static String require(String value, String message) {
|
||||
String trimmed = trimToNull(value);
|
||||
if (trimmed == null) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, message);
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
private static String trimToNull(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
private static TdenginePageCursor cursor(String cursorTs, String cursorId) {
|
||||
boolean hasTs = cursorTs != null && !cursorTs.isBlank();
|
||||
boolean hasId = cursorId != null && !cursorId.isBlank();
|
||||
if (!hasTs && !hasId) {
|
||||
return null;
|
||||
}
|
||||
if (!hasTs || !hasId) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "cursorTs and cursorId must be provided together");
|
||||
}
|
||||
return new TdenginePageCursor(Instant.parse(cursorTs.trim()), cursorId.trim());
|
||||
}
|
||||
|
||||
public record TelemetryFieldPageResponse(
|
||||
List<TelemetryFieldResponse> items,
|
||||
CursorResponse nextCursor) {
|
||||
|
||||
private static TelemetryFieldPageResponse from(TdenginePage<TdengineTelemetryFieldRow> page) {
|
||||
CursorResponse next = page.nextCursor()
|
||||
.map(cursor -> new CursorResponse(cursor.ts().toString(), cursor.tieBreaker()))
|
||||
.orElse(null);
|
||||
return new TelemetryFieldPageResponse(
|
||||
page.items().stream().map(TelemetryFieldResponse::from).toList(),
|
||||
next);
|
||||
}
|
||||
}
|
||||
|
||||
public record CursorResponse(String ts, String id) {
|
||||
}
|
||||
|
||||
public record TelemetryFieldResponse(
|
||||
String eventTime,
|
||||
String receivedAt,
|
||||
String factId,
|
||||
String frameId,
|
||||
String fieldKey,
|
||||
String valueType,
|
||||
String valueText,
|
||||
Double valueDouble,
|
||||
Long valueLong,
|
||||
String unit,
|
||||
String quality,
|
||||
String sourcePath,
|
||||
String rawUri,
|
||||
String metadataJson,
|
||||
String protocol,
|
||||
String vehicleKey,
|
||||
String vin,
|
||||
String phone) {
|
||||
|
||||
private static TelemetryFieldResponse from(TdengineTelemetryFieldRow row) {
|
||||
return new TelemetryFieldResponse(
|
||||
row.ts().toString(),
|
||||
row.receivedAt().toString(),
|
||||
row.factId(),
|
||||
row.frameId(),
|
||||
row.fieldKey(),
|
||||
row.valueType(),
|
||||
row.valueText(),
|
||||
row.valueDouble(),
|
||||
row.valueLong(),
|
||||
row.unit(),
|
||||
row.quality(),
|
||||
row.sourcePath(),
|
||||
row.rawUri(),
|
||||
row.metadataJson(),
|
||||
row.protocol(),
|
||||
row.vehicleKey(),
|
||||
row.vin(),
|
||||
row.phone());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import com.lingniu.ingest.eventhistory.Jt808LocationHistoryController;
|
||||
import com.lingniu.ingest.eventhistory.Jt808RawFrameHistoryController;
|
||||
import com.lingniu.ingest.eventhistory.LocationHistoryController;
|
||||
import com.lingniu.ingest.eventhistory.RawFrameHistoryController;
|
||||
import com.lingniu.ingest.eventhistory.TelemetryFieldHistoryController;
|
||||
import com.lingniu.ingest.protocol.gb32960.codec.Gb32960MessageDecoder;
|
||||
import com.lingniu.ingest.protocol.gb32960.config.Gb32960AutoConfiguration;
|
||||
import com.lingniu.ingest.sink.kafka.KafkaSinkAutoConfiguration;
|
||||
@@ -53,10 +52,8 @@ public class EventHistoryAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnBean(TdengineHistoryWriter.class)
|
||||
@ConditionalOnMissingBean
|
||||
public EventHistoryEnvelopeIngestor tdengineEventHistoryEnvelopeIngestor(TdengineHistoryWriter writer,
|
||||
@Value("${lingniu.ingest.tdengine-history.telemetry-fields-enabled:false}")
|
||||
boolean telemetryFieldsEnabled) {
|
||||
return new EventHistoryEnvelopeIngestor(writer, telemetryFieldsEnabled);
|
||||
public EventHistoryEnvelopeIngestor tdengineEventHistoryEnvelopeIngestor(TdengineHistoryWriter writer) {
|
||||
return new EventHistoryEnvelopeIngestor(writer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -130,14 +127,6 @@ public class EventHistoryAutoConfiguration {
|
||||
return new Jt808RawFrameHistoryController(reader);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(TdengineHistoryReader.class)
|
||||
@ConditionalOnProperty(prefix = "lingniu.ingest.tdengine-history", name = "telemetry-fields-enabled", havingValue = "true")
|
||||
@ConditionalOnMissingBean
|
||||
public TelemetryFieldHistoryController telemetryFieldHistoryController(TdengineHistoryReader reader) {
|
||||
return new TelemetryFieldHistoryController(reader);
|
||||
}
|
||||
|
||||
static Path archiveRoot(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return Path.of(System.getProperty("java.io.tmpdir"), "lingniu-archive");
|
||||
|
||||
Reference in New Issue
Block a user