Skip to content

Commit fd12b57

Browse files
authored
Dataframe parsing bug fixes (#16)
* fix: getField * Fix bug which skips the first column regardless of if it is a timestamp * date(timestamp + "Z") was breaking timestamps. Use whatever O2 gives us, unless it's microseconds which needs converting to MS. * Only add Time column if it's part of the request [breaking for queries relying on this implicit field, but deduplicating for the queries that manually request it including example requests] * fix: getGraphDataFrame * [bugfix] No longer skip first requested column * [bugfix] Remove type: number for non-time fields which breaks string responses * Detect timestamp column in request * fix: Add getColumnsFromQuery quote stripping [big] The dataframe parser would fail to assign fields wrapped in quotes i.e. select 1 as "server" * change: Utilize datasource defined timestamp column automatically in data frame parser * Implement #14 fix in #15 to pass tests
1 parent b8983b2 commit fd12b57

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"eslint-plugin-deprecation": "^2.0.0"
5858
},
5959
"engines": {
60-
"node": ">=16"
60+
"node": ">=18"
6161
},
6262
"dependencies": {
6363
"@emotion/css": "^11.1.3",

src/components/QueryEditor.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
3232
setIsLoading(isLoading.slice(1));
3333
};
3434

35-
const isInDashboard = useMemo(() => app === 'panel-editor', [app]);
36-
37-
const getTimeStampColumnName = () => {
38-
return datasource.instanceSettings?.jsonData?.timestamp_column || '_timestamp';
39-
};
40-
4135
useEffect(() => {
4236
startLoading();
4337
getOrganizations({ url: datasource.url, page_num: 0, page_size: 1000, sort_by: 'id' })

src/datasource.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,20 @@ export class DataSource
7272

7373
// As we don't show histogram for sql mode in explore
7474
if (options.app === 'explore' && target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME) && target.sqlMode) {
75-
return getGraphDataFrame([], target, options.app);
75+
return getGraphDataFrame([], target, options.app, this.timestampColumn);
7676
}
7777

7878
this.cachedQuery.requestQuery = JSON.stringify(reqData);
7979
this.cachedQuery.isFetching = true;
8080
return this.doRequest(target, reqData)
8181
.then((response) => {
8282
if (options.app === 'panel-editor' || options.app === 'dashboard') {
83-
return getGraphDataFrame(response.hits, target, options.app);
83+
return getGraphDataFrame(response.hits, target, options.app, this.timestampColumn);
8484
}
8585

8686
const logsDataFrame = getLogsDataFrame(response.hits, target, this.streamFields, this.timestampColumn);
8787

88-
const graphDataFrame = getGraphDataFrame(response?.aggs?.histogram || [], target, options.app);
88+
const graphDataFrame = getGraphDataFrame(response?.aggs?.histogram || [], target, options.app, this.timestampColumn);
8989

9090
this.cachedQuery.promise?.resolve({ graph: graphDataFrame, logs: logsDataFrame });
9191

src/features/log/queryResponseBuilder.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ export const getLogsDataFrame = (
3636
return logsData;
3737
};
3838

39-
export const getGraphDataFrame = (data: any, target: MyQuery, app: string) => {
39+
export const getGraphDataFrame = (
40+
data: any,
41+
target: MyQuery,
42+
app: string,
43+
timestampColumn = '_timestamp'
44+
) => {
4045
const graphData = getDefaultDataFrame(target.refId, 'graph');
4146

4247
let fields = ['zo_sql_key', 'zo_sql_num'];
@@ -49,39 +54,52 @@ export const getGraphDataFrame = (data: any, target: MyQuery, app: string) => {
4954
}
5055
}
5156

52-
graphData.addField({
53-
config: {
54-
filterable: true,
55-
},
56-
name: 'Time',
57-
type: FieldType.time,
58-
});
59-
60-
for (let i = 1; i < fields.length; i++) {
61-
graphData.addField({
62-
name: fields[i],
63-
type: FieldType.number,
64-
});
57+
for (let i = 0; i < fields.length; i++) {
58+
if (fields[i] === timestampColumn) {
59+
graphData.addField({
60+
config: {
61+
filterable: true,
62+
},
63+
name: 'Time',
64+
type: FieldType.time,
65+
});
66+
} else {
67+
graphData.addField({
68+
name: fields[i],
69+
});
70+
}
6571
}
6672

6773
if (!data.length) {
6874
return graphData;
6975
}
7076

7177
data.forEach((log: any) => {
72-
graphData.add(getField(log, fields));
78+
graphData.add(getField(log, fields, timestampColumn));
7379
});
7480

7581
return graphData;
7682
};
7783

78-
const getField = (log: any, columns: any) => {
79-
let field: any = {
80-
Time: new Date(log[columns[0]] + 'Z').getTime(),
81-
};
82-
83-
for (let i = 1; i < columns.length; i++) {
84-
field[columns[i]] = log[columns[i]];
84+
const getField = (log: any, columns: any, timestampColumn: string) => {
85+
let field: any = {};
86+
87+
for (let i = 0; i < columns.length; i++) {
88+
let col_name = columns[i];
89+
let col_value = log[col_name]
90+
if (col_name === timestampColumn) {
91+
// We have to convert microseconds if we receive them
92+
// 500 billion / year 17814 is probably a good threshold for milliseconds
93+
if (col_value > 500_000_000_000) {
94+
col_value = convertTimeToMs(col_value);
95+
field["Time"] = col_value;
96+
} else {
97+
// Convert any other date fmt
98+
field["Time"] = new Date(col_value).getTime();
99+
}
100+
} else {
101+
field[col_name] = log[col_name];
102+
}
85103
}
86104

87105
return field;
@@ -127,7 +145,9 @@ const getColumnsFromQuery = (query: string) => {
127145

128146
// If alias exists, use that, otherwise use column name
129147
if (aliasMatch) {
130-
columnNames.push(aliasMatch[1]);
148+
// SQL alias may have quotes, strip those.
149+
let stripped = aliasMatch[1].replace(/^['"]|['"]$/g, '');
150+
columnNames.push(stripped);
131151
} else {
132152
columnNames.push(column);
133153
}

0 commit comments

Comments
 (0)