Skip to content

Commit 7966d67

Browse files
committed
jsontrace: add events path to sorting job
Refactor sorting job, previous implementation was using heuristics to skip to the events. Now everything apart from the events is extracted and treated as metadata. New tests were added, json trace test files were generated using OpenAI gpt-5. Signed-off-by: Arnaud Fiorini <[email protected]>
1 parent 7951df7 commit 7966d67

File tree

10 files changed

+332
-100
lines changed

10 files changed

+332
-100
lines changed

jsontrace/org.eclipse.tracecompass.jsontrace.core.tests/src/org/eclipse/tracecompass/jsontrace/core/tests/JsonTraceTest.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@
2626
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
2727
import org.junit.Test;
2828

29+
import com.google.gson.Gson;
30+
import com.google.gson.JsonElement;
31+
2932
/**
3033
* Test generic Json trace
3134
*
3235
* @author Simon Delisle
3336
*/
3437
public class JsonTraceTest {
3538

39+
private final Gson gson = new Gson();
40+
3641
/**
3742
* Test the unsorted json trace
3843
*
@@ -45,7 +50,7 @@ public void testSortedTrace() throws TmfTraceException {
4550
long nbEvents = 5;
4651
ITmfTimestamp startTime = TmfTimestamp.fromNanos(1);
4752
ITmfTimestamp endTime = TmfTimestamp.fromNanos(5);
48-
testJsonTrace(path, nbEvents, startTime, endTime);
53+
testJsonTrace(path, nbEvents, startTime, endTime, "{\"events\":[]}");
4954
}
5055

5156
/**
@@ -60,10 +65,59 @@ public void testUnsortedTrace() throws TmfTraceException {
6065
long nbEvents = 5;
6166
ITmfTimestamp startTime = TmfTimestamp.fromNanos(1);
6267
ITmfTimestamp endTime = TmfTimestamp.fromNanos(5);
63-
testJsonTrace(path, nbEvents, startTime, endTime);
68+
testJsonTrace(path, nbEvents, startTime, endTime, "{\"events\":[]}");
6469
}
6570

66-
private static void testJsonTrace(String path, long expectedNbEvents, ITmfTimestamp startTime, ITmfTimestamp endTime)
71+
/**
72+
* Test the json trace with metadata
73+
*
74+
* @throws TmfTraceException
75+
* If there is a problem while initializing the trace
76+
*/
77+
@Test
78+
public void testMetadataBeginTrace() throws TmfTraceException {
79+
String path = "traces/traceMetadataBegin.json"; //$NON-NLS-1$
80+
long nbEvents = 6;
81+
ITmfTimestamp startTime = TmfTimestamp.fromNanos(1730000000000L);
82+
ITmfTimestamp endTime = TmfTimestamp.fromNanos(1730000005000L);
83+
testJsonTrace(path, nbEvents, startTime, endTime,
84+
"{\"events\":[], \"metadata\":{\"source\":\"sensor-A\",\"version\":1,\"generatedAt\":\"2025-10-23T12:00:00Z\"}}");
85+
}
86+
87+
/**
88+
* Test the json trace with metadata
89+
*
90+
* @throws TmfTraceException
91+
* If there is a problem while initializing the trace
92+
*/
93+
@Test
94+
public void testMetadataEndTrace() throws TmfTraceException {
95+
String path = "traces/traceMetadataEnd.json"; //$NON-NLS-1$
96+
long nbEvents = 5;
97+
ITmfTimestamp startTime = TmfTimestamp.fromNanos(1730000000000L);
98+
ITmfTimestamp endTime = TmfTimestamp.fromNanos(1730000004000L);
99+
testJsonTrace(path, nbEvents, startTime, endTime,
100+
"{\"events\":[], \"metadata\":{\"source\":\"sensor-A\",\"version\":1,\"generatedAt\":\"2025-10-23T12:00:00Z\"}}");
101+
}
102+
103+
/**
104+
* Test the json trace with metadata
105+
*
106+
* @throws TmfTraceException
107+
* If there is a problem while initializing the trace
108+
*/
109+
@Test
110+
public void testMetadataBeginEndTrace() throws TmfTraceException {
111+
String path = "traces/traceMetadataBeginEnd.json"; //$NON-NLS-1$
112+
long nbEvents = 5;
113+
ITmfTimestamp startTime = TmfTimestamp.fromNanos(1730000000000L);
114+
ITmfTimestamp endTime = TmfTimestamp.fromNanos(1730000004000L);
115+
testJsonTrace(path, nbEvents, startTime, endTime,
116+
"{\"events\":[], \"metadataStart\":{\"source\":\"sensor-A\",\"version\":1,\"generatedAt\":\"2025-10-23T12:00:00Z\"},"
117+
+ "\"metadataEnd\":{\"checksum\":\"abc123\",\"recordCount\":5,\"processedAt\":\"2025-10-23T12:05:00Z\"}}");
118+
}
119+
120+
private void testJsonTrace(String path, long expectedNbEvents, ITmfTimestamp startTime, ITmfTimestamp endTime, String metadata)
67121
throws TmfTraceException {
68122
ITmfTrace trace = new JsonStubTrace();
69123
try {
@@ -88,9 +142,11 @@ private static void testJsonTrace(String path, long expectedNbEvents, ITmfTimest
88142
assertEquals(expectedNbEvents, trace.getNbEvents());
89143
assertEquals(startTime.toNanos(), trace.getStartTime().toNanos());
90144
assertEquals(endTime.toNanos(), trace.getEndTime().toNanos());
145+
JsonElement expectedElement = gson.fromJson(metadata, JsonElement.class);
146+
JsonElement actualElement = gson.fromJson(((JsonStubTrace) trace).fMetadata, JsonElement.class);
147+
assertEquals(expectedElement, actualElement);
91148
} finally {
92149
trace.dispose();
93150
}
94151
}
95-
96152
}

jsontrace/org.eclipse.tracecompass.jsontrace.core.tests/src/org/eclipse/tracecompass/jsontrace/core/tests/stub/JsonStubTrace.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public class JsonStubTrace extends JsonTrace {
4848

4949
private static final String TIMESTAMP_KEY = "timestamp"; //$NON-NLS-1$
5050
private Gson GSON = new Gson();
51+
/**
52+
* metadata string
53+
*/
54+
public String fMetadata;
5155

5256
@Override
5357
public void initTrace(IResource resource, String path, Class<? extends ITmfEvent> type) throws TmfTraceException {
@@ -80,11 +84,10 @@ public void initTrace(IResource resource, String path, Class<? extends ITmfEvent
8084

8185
@Override
8286
public IStatus validate(IProject project, String path) {
83-
// Slow, but meh, it's a unit test and this is more readable.
84-
if (path.matches("traces/.*sortedTrace.json")) { //$NON-NLS-1$
87+
if (path.matches("traces/*.json")) { //$NON-NLS-1$
8588
return new TraceValidationStatus(MAX_CONFIDENCE, "json.trace.stub"); //$NON-NLS-1$
8689
}
87-
return Status.CANCEL_STATUS;
90+
return Status.error("Test trace was not validated");
8891
}
8992

9093
@Override

jsontrace/org.eclipse.tracecompass.jsontrace.core.tests/src/org/eclipse/tracecompass/jsontrace/core/tests/stub/JsonStubTraceSortingJob.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package org.eclipse.tracecompass.jsontrace.core.tests.stub;
1313

1414
import java.io.IOException;
15+
import java.util.List;
1516

1617
import org.eclipse.tracecompass.internal.jsontrace.core.job.SortingJob;
1718
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
@@ -32,12 +33,16 @@ public class JsonStubTraceSortingJob extends SortingJob {
3233
* Trace path
3334
*/
3435
public JsonStubTraceSortingJob(ITmfTrace trace, String path) {
35-
super(trace, path, "\"timestamp\":", 1); //$NON-NLS-1$
36+
super(trace, path, "\"timestamp\":", List.of("events")); //$NON-NLS-1$
3637
}
3738

3839
@Override
3940
protected void processMetadata(ITmfTrace trace, String dir) throws IOException {
4041
// No metadata to process
4142
}
4243

44+
@Override
45+
protected void processMetadataJson(ITmfTrace trace, String metadata) {
46+
((JsonStubTrace) trace).fMetadata = metadata;
47+
}
4348
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"metadata": {
3+
"source": "sensor-A",
4+
"version": 1,
5+
"generatedAt": "2025-10-23T12:00:00Z"
6+
},
7+
"events": [
8+
{"timestamp": 1730000000000, "eventContent": {"description": "Event 0"}},
9+
{"timestamp": 1730000001000, "eventContent": {"description": "Event 1"}},
10+
{"timestamp": 1730000002000, "eventContent": {"description": "Event 2"}},
11+
{"timestamp": 1730000003000, "eventContent": {"description": "Event 3"}},
12+
{"timestamp": 1730000004000, "eventContent": {"description": "Event 4"}},
13+
{"timestamp": 1730000005000, "eventContent": {"description": "Event 5"}}
14+
]
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"metadataStart": {
3+
"source": "sensor-A",
4+
"version": 1,
5+
"generatedAt": "2025-10-23T12:00:00Z"
6+
},
7+
"events": [
8+
{"timestamp": 1730000000000, "eventContent": {"description": "Event 0"}},
9+
{"timestamp": 1730000001000, "eventContent": {"description": "Event 1"}},
10+
{"timestamp": 1730000002000, "eventContent": {"description": "Event 2"}},
11+
{"timestamp": 1730000003000, "eventContent": {"description": "Event 3"}},
12+
{"timestamp": 1730000004000, "eventContent": {"description": "Event 4"}}
13+
],
14+
"metadataEnd": {
15+
"checksum": "abc123",
16+
"recordCount": 5,
17+
"processedAt": "2025-10-23T12:05:00Z"
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"events": [
3+
{"timestamp": 1730000000000, "eventContent": {"description": "Event 0"}},
4+
{"timestamp": 1730000001000, "eventContent": {"description": "Event 1"}},
5+
{"timestamp": 1730000002000, "eventContent": {"description": "Event 2"}},
6+
{"timestamp": 1730000003000, "eventContent": {"description": "Event 3"}},
7+
{"timestamp": 1730000004000, "eventContent": {"description": "Event 4"}}
8+
],
9+
"metadata": {
10+
"source": "sensor-A",
11+
"version": 1,
12+
"generatedAt": "2025-10-23T12:00:00Z"
13+
}
14+
}

jsontrace/org.eclipse.tracecompass.jsontrace.core/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ Require-Bundle: org.eclipse.tracecompass.common.core,
1616
Export-Package: org.eclipse.tracecompass.internal.jsontrace.core;x-internal:=true,
1717
org.eclipse.tracecompass.internal.jsontrace.core.job;x-friends:="org.eclipse.tracecompass.jsontrace.core.tests",
1818
org.eclipse.tracecompass.internal.provisional.jsontrace.core.trace;x-friends:="org.eclipse.tracecompass.jsontrace.core.tests"
19-
Import-Package: org.eclipse.tracecompass.traceeventlogger
19+
Import-Package: com.fasterxml.jackson.core,
20+
org.eclipse.tracecompass.traceeventlogger
2021
Bundle-Activator: org.eclipse.tracecompass.internal.jsontrace.core.Activator

0 commit comments

Comments
 (0)