Skip to content

Commit 6e0701f

Browse files
authored
fix the issue with logs and table visualzation in dashboards (#19)
* fix the issue with logs and table visualzation in dashboards * add space
1 parent 85446a1 commit 6e0701f

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/components/QueryEditor.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
1717
const [orgOptions, setOrgOptions]: any = useState([]);
1818
const [isMounted, setIsMounted]: any = useState(false);
1919
const [isLoading, setIsLoading]: any = useState([]);
20+
const [displayOptions, setDisplayOptions]: any = useState([]);
21+
2022

2123
const isInDashboard = useMemo(() => app === 'panel-editor', [app]);
2224

@@ -32,6 +34,14 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
3234
setIsLoading(isLoading.slice(1));
3335
};
3436

37+
useEffect(() => {
38+
setDisplayOptions([
39+
{ label: 'Auto', value: 'auto' },
40+
{ label: 'Force Graph', value: 'graph' },
41+
{ label: 'Force Logs', value: 'logs' },
42+
]);
43+
}, []);
44+
3545
useEffect(() => {
3646
startLoading();
3747
getOrganizations({ url: datasource.url, page_num: 0, page_size: 1000, sort_by: 'id' })
@@ -66,6 +76,7 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
6676
stream: streams[0].name,
6777
organization: orgs.data[0].name,
6878
sqlMode: isInDashboard ? true : false,
79+
displayMode: query.displayMode ?? 'auto'
6980
});
7081
} else if (isInDashboard && query.organization && query.stream && query.query) {
7182
updateQuery();
@@ -137,6 +148,11 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
137148
});
138149
};
139150

151+
const onDisplayModeChange = (displayMode: any) => {
152+
const newMode = displayMode.value as MyQuery['displayMode'];
153+
onChange({ ...query, displayMode: newMode });
154+
};
155+
140156
const onChangeQuery = ({ value, sqlMode }: { value: string; sqlMode: boolean }) => {
141157
onChange({ ...query, query: value, sqlMode: sqlMode });
142158
};
@@ -266,6 +282,18 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, app, data
266282
<Switch data-testid="query-editor-sql-mode-switch" value={!!query.sqlMode} onChange={toggleSqlMode} />
267283
</div>
268284
)}
285+
{/* Display Mode selector */}
286+
<div className={css`display: flex; align-items: center; padding-bottom: 0.5rem;`}>
287+
<InlineLabel className={css`width: fit-content;`} transparent>
288+
Display Mode
289+
</InlineLabel>
290+
<Select
291+
options={displayOptions}
292+
value={displayOptions.find((o: any) => o.value === query.displayMode) || displayOptions[0]}
293+
onChange={onDisplayModeChange}
294+
width={20}
295+
/>
296+
</div>
269297
{query.stream && (
270298
<ZincEditor
271299
key={generateEditorId}

src/datasource.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,23 @@ export class DataSource
5757
});
5858
}
5959
const reqData = buildQuery(target, timestamps, this.streamFields, options.app, this.timestampColumn);
60-
if (JSON.stringify(reqData) === this.cachedQuery.requestQuery) {
60+
const cacheKey = JSON.stringify({
61+
reqData,
62+
displayMode: target.displayMode ?? 'auto',
63+
});
64+
65+
if (cacheKey === this.cachedQuery.requestQuery) {
6166
return this.cachedQuery.data
6267
?.then((res) => {
68+
const mode = target.displayMode || 'auto';
6369
if (target?.refId?.includes(REF_ID_STARTER_LOG_VOLUME)) {
6470
return res.graph;
6571
}
72+
if (options.app === 'panel-editor' || options.app === 'dashboard') {
73+
if(mode === 'graph' || mode === 'auto') {
74+
return res.graph;
75+
}
76+
}
6677
return res.logs;
6778
})
6879
.finally(() => {
@@ -75,12 +86,16 @@ export class DataSource
7586
return getGraphDataFrame([], target, options.app, this.timestampColumn);
7687
}
7788

78-
this.cachedQuery.requestQuery = JSON.stringify(reqData);
89+
this.cachedQuery.requestQuery = cacheKey;
7990
this.cachedQuery.isFetching = true;
8091
return this.doRequest(target, reqData)
8192
.then((response) => {
8293
if (options.app === 'panel-editor' || options.app === 'dashboard') {
83-
return getGraphDataFrame(response.hits, target, options.app, this.timestampColumn);
94+
const mode = target.displayMode || 'auto';
95+
const logsDf = getLogsDataFrame(response.hits, target, this.streamFields, this.timestampColumn);
96+
const graphDf = getGraphDataFrame(response.hits, target, options.app, this.timestampColumn);
97+
this.cachedQuery.promise?.resolve({ graph: graphDf, logs: logsDf });
98+
return mode === 'logs' ? logsDf : graphDf;
8499
}
85100

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

src/features/log/queryResponseBuilder.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { FieldType, MutableDataFrame, PreferredVisualisationType } from '@grafan
22
import { MyQuery } from '../../types';
33
import { convertTimeToMs, getFieldType } from '../../utils/zincutils';
44

5+
const isTimeField = (name: string, timestampColumn: string): boolean =>
6+
name === timestampColumn || name.startsWith('x_axis');
7+
58
export const getLogsDataFrame = (
69
data: any,
710
target: MyQuery,
@@ -55,7 +58,7 @@ export const getGraphDataFrame = (
5558
}
5659

5760
for (let i = 0; i < fields.length; i++) {
58-
if (fields[i] === timestampColumn) {
61+
if (isTimeField(fields[i], timestampColumn)) {
5962
graphData.addField({
6063
config: {
6164
filterable: true,
@@ -87,7 +90,7 @@ const getField = (log: any, columns: any, timestampColumn: string) => {
8790
for (let i = 0; i < columns.length; i++) {
8891
let col_name = columns[i];
8992
let col_value = log[col_name]
90-
if (col_name === timestampColumn) {
93+
if (isTimeField(col_name, timestampColumn)) {
9194
// We have to convert microseconds if we receive them
9295
// 500 billion / year 17814 is probably a good threshold for milliseconds
9396
if (col_value > 500_000_000_000) {

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export interface MyQuery extends DataQuery {
1212
rows: number;
1313
};
1414
streamFields: any[];
15+
displayMode?: 'auto' | 'graph' | 'logs';
1516
}
1617

1718
export const DEFAULT_QUERY: Partial<MyQuery> = {
1819
constant: 6.5,
20+
displayMode: 'auto',
1921
};
2022

2123
/**

0 commit comments

Comments
 (0)