Skip to content

Commit

Permalink
Data type float into string field type conflicting (#144)
Browse files Browse the repository at this point in the history
<!-- Thanks for the contribution, this is awesome. -->

# PR Details
## Description
There are a few more fields that were stored as string, but the incoming
message is of type float.

<!--- Describe your changes in detail -->

![image](https://user-images.githubusercontent.com/62157949/235765847-eb3fe9f9-1bda-44b7-b632-f5b3012bd9c2.png)

## Related Issue
#138
#145

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## How Has This Been Tested?
NA
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [x] Defect fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that cause existing functionality
to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [ ] I have added any new packages to the sonar-scanner.properties file
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
[CDA Telematics Contributing
Guide](https://github.com/usdot-fhwa-stol/cda-telematics/blob/main/Contributing.md)
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
  • Loading branch information
adev4a authored May 2, 2023
2 parents c55f61e + a71d39b commit 52e3338
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ else if (value.toString().matches("[-+]?\\d*\\.?\\d+")) {
else if (value.toString().matches("^[+-]?\\d+(?:\\.\\d*(?:[eE][+-]?\\d+)?)?$")) {
pairs += key + "=" + 0.0;
}
//Leave NaN value as float (Assuming influx treats NaN value as float type) and do not convert to string
else if (value.toString().toLowerCase().strip().equals("nan")){
pairs += key + "=" + value;
}
// If none of the above Regex matches, considering it as string
else {
pairs += key + "=\"" + value.toString().replaceAll("\\s", "") + "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ CLOUD_UNIT_ID_LIST=cloud_id
INFLUX_ORG=my-org
INFLUX_ORG_ID=<INFLUX_ORG_ID>
INFLUX_TOKEN=<INFLUXDB-TOKEN>
#Edge case for hostBSMId and TCR/TCM ID where the Ids can be all digits or alpha characters
TO_STR_VALUES=hostBSMId,TrafficControlRequest.reqid,tcmV01.reqid
#Edge case for hostBSMId, sender_bsm_id, core_data.id and TCR/TCM ID where the Ids can be all digits or alpha characters
TO_STR_VALUES=hostBSMId,TrafficControlRequest.reqid,tcmV01.reqid,m_header.sender_bsm_id,core_data.id
# Connection timeout to influx bucket. Unit: milliseconds
INFLUX_CONNECT_TIMEOUT=1000
# Timeout while writing data to influx. Unit: milliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ public void convertJson2KeyValuePairs() {
String tcm_k_v_pairs = converter.convertJson2KeyValuePairs(tcm_json_str, to_str_values);
expected = "payload.TrafficControlMessage.tcmV01.id=\"007d1d1c5ea3f134ab2e9d868a033372\",payload.TrafficControlMessage.tcmV01.params.vclasses.seven-or-more-axle-multi-trailer-truck=\"None\",payload.TrafficControlMessage.tcmV01.reqid=\"4947918446524142\",payload.TrafficControlMessage.tcmV01.geometry.reflat=389549140,payload.TrafficControlMessage.tcmV01.params.vclasses.light-truck-van=\"None\",payload.TrafficControlMessage.tcmV01.params.vclasses.four-or-more-axle-single-unit-truck=\"None\",payload.TrafficControlMessage.tcmV01.geometry.refwidth=405,payload.TrafficControlMessage.tcmV01.params.vclasses.bus=\"None\",payload.TrafficControlMessage.tcmV01.geometry.heading=3312,payload.TrafficControlMessage.tcmV01.params.regulatory.true=\"None\",payload.TrafficControlMessage.tcmV01.params.vclasses.four-or-fewer-axle-single-trailer-truck=\"None\",payload.TrafficControlMessage.tcmV01.params.vclasses.motorcycle=\"None\",payload.TrafficControlMessage.tcmV01.geometry.reftime=28004757,payload.TrafficControlMessage.tcmV01.params.vclasses.six-axle-multi-trailer-truck=\"None\",payload.TrafficControlMessage.tcmV01.params.vclasses.three-axle-single-unit-truck=\"None\",payload.TrafficControlMessage.tcmV01.params.vclasses.passenger-car=\"None\",payload.TrafficControlMessage.tcmV01.geometry.reflon=-771490031,payload.TrafficControlMessage.tcmV01.msgtot=1,payload.TrafficControlMessage.tcmV01.params.detail.maxspeed=45,payload.TrafficControlMessage.tcmV01.params.schedule.start=28004757,payload.TrafficControlMessage.tcmV01.params.schedule.dow=1111111,payload.TrafficControlMessage.tcmV01.geometry.datum=\"WGS84\",payload.TrafficControlMessage.tcmV01.params.vclasses.two-axle-six-tire-single-unit-truck=\"None\",payload.TrafficControlMessage.tcmV01.msgnum=1,payload.TrafficControlMessage.tcmV01.reqseq=0,payload.TrafficControlMessage.tcmV01.params.schedule.end=153722867280912,payload.TrafficControlMessage.tcmV01.params.vclasses.micromobile=\"None\",payload.TrafficControlMessage.tcmV01.params.vclasses.six-or-more-axle-single-trailer-truck=\"None\",payload.TrafficControlMessage.tcmV01.updated=0,payload.TrafficControlMessage.tcmV01.params.vclasses.five-or-fewer-axle-multi-trailer-truck=\"None\",payload.TrafficControlMessage.tcmV01.params.vclasses.five-axle-single-trailer-truck=\"None\",payload.TrafficControlMessage.tcmV01.geometry.proj=\"epsg:3785\",payload.TrafficControlMessage.tcmV01.geometry.refelv=0";
assertEquals(expected, tcm_k_v_pairs);

//Message that has NaN value
String nan_json_str = "{\"metadata.unit_name\":\"BlackPacifica\",\"metadata.event_name\":\"UC3\",\"metadata.location\":\"TFHRC\",\"metadata.unit_type\":\"Platform\",\"metadata.unit_id\":\"DOT-508\",\"metadata.testing_type\":\"Integration\",\"payload.core_data.sec_mark\":\"40328\",\"payload.core_data.heading\":\"6160\",\"payload.core_data.brakes.scs\":\"0\",\"payload.core_data.brakes.abs\":\"NaN\",\"payload.core_data.brakes.aux_brakes\":\"0\",\"payload.core_data.brakes.brake_boost\":\"0\",\"payload.core_data.brakes.traction\":\"0\",\"payload.core_data.accel_set.vert\":\"-127\",\"payload.core_data.accel_set.lat\":\"2\",\"payload.core_data.accel_set.long\":\"-79\",\"payload.core_data.accel_set.yaw\":\"12\",\"payload.core_data.accuracy.orientation\":\"65535\",\"payload.core_data.accuracy.semi_minor\":\"255\",\"payload.core_data.accuracy.semi_major\":\"255\",\"payload.core_data.long\":\"-77.1476267\",\"payload.core_data.speed\":\"282\",\"payload.core_data.transmission\":\"9.890579531202093e-05\",\"payload.core_data.size.length\":\"500\",\"payload.core_data.size.width\":\"200\",\"payload.core_data.elev\":\"384\",\"payload.core_data.angle\":\"127\",\"payload.core_data.id\":\"11111111\",\"payload.core_data.lat\":\"38.9549740\",\"payload.core_data.msg_count\":\"70\"}";
String nan_k_v_pairs = converter.convertJson2KeyValuePairs(nan_json_str, to_str_values);
expected = "metadata.unit_id=\"DOT-508\",payload.core_data.accuracy.semi_major=255,payload.core_data.brakes.abs=NaN,payload.core_data.transmission=0.0,payload.core_data.brakes.brake_boost=0,payload.core_data.accel_set.long=-79,payload.core_data.accuracy.semi_minor=255,metadata.unit_type=\"Platform\",payload.core_data.brakes.scs=0,payload.core_data.brakes.traction=0,payload.core_data.lat=38.9549740,payload.core_data.heading=6160,payload.core_data.accel_set.yaw=12,payload.core_data.size.length=500,payload.core_data.size.width=200,metadata.testing_type=\"Integration\",payload.core_data.speed=282,payload.core_data.angle=127,metadata.event_name=\"UC3\",payload.core_data.long=-77.1476267,payload.core_data.brakes.aux_brakes=0,payload.core_data.msg_count=70,payload.core_data.accel_set.lat=2,metadata.location=\"TFHRC\",payload.core_data.sec_mark=40328,payload.core_data.id=11111111,metadata.unit_name=\"BlackPacifica\",payload.core_data.accel_set.vert=-127,payload.core_data.elev=384,payload.core_data.accuracy.orientation=65535";
assertEquals(expected, nan_k_v_pairs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,16 @@ public void flattenJson() {
} catch (ParseException e) {
e.printStackTrace();
}

String nan_json_metadata_str = "{\"unit_name\":\"BlackPacifica\",\"event_name\":\"UC3\",\"location\":\"TFHRC\",\"unit_type\":\"Platform\",\"unit_id\":\"DOT-508\",\"testing_type\":\"Integration\"}";
String nan_BSM_json_payload_str = "{\"core_data\":{\"accel_set\":{\"lat\":\"2\",\"long\":\"-79\",\"vert\":\"-127\",\"yaw\":\"12\"},\"accuracy\":{\"orientation\":\"65535\",\"semi_major\":\"255\",\"semi_minor\":\"255\"},\"angle\":\"127\",\"brakes\":{\"abs\":\"NaN\",\"aux_brakes\":\"0\",\"brake_boost\":\"0\",\"scs\":\"0\",\"traction\":\"0\"},\"elev\":\"384\",\"heading\":\"6160\",\"id\":\"11111111\",\"lat\":\"38.9549740\",\"long\":\"-77.1476267\",\"msg_count\":\"70\",\"sec_mark\":\"40328\",\"size\":{\"length\":\"500\",\"width\":\"200\"},\"speed\":\"282\",\"transmission\":\"0\"}}";
String nan_json_str = "{ \"metadata\":" + nan_json_metadata_str + ",\"payload\":" + nan_BSM_json_payload_str + "}";
String nan_flattened_json_str = helper.flattenJsonStr(nan_json_str);
try {
JSONObject flattened_json = (JSONObject) parser.parse(nan_flattened_json_str);
assertEquals("NaN", flattened_json.get("payload.core_data.brakes.abs"));
} catch (ParseException e) {
e.printStackTrace();
}
}
}

0 comments on commit 52e3338

Please sign in to comment.