-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from quickwit-oss/ddelemeny/split-datasource-p…
…rocess-response Extract datasource query processing
- Loading branch information
Showing
5 changed files
with
104 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { DataFrame, DataLink, DataQueryRequest, DataQueryResponse, FieldType } from "@grafana/data"; | ||
import { getDataSourceSrv } from "@grafana/runtime"; | ||
import { QuickwitDataSource } from 'datasource'; | ||
import { DataLinkConfig, ElasticsearchQuery } from "../types"; | ||
|
||
export function getQueryResponseProcessor(datasource: QuickwitDataSource, request: DataQueryRequest<ElasticsearchQuery>) { | ||
return { | ||
processResponse: (response: DataQueryResponse) => { | ||
response.data.forEach((dataFrame) => { | ||
const metrics = request.targets[0].metrics; | ||
if (metrics && metrics[0].type === 'logs') { | ||
processLogsDataFrame(datasource, dataFrame); | ||
} | ||
}); | ||
return response; | ||
} | ||
}; | ||
} | ||
function getCustomFieldName(fieldname: string) { return `$qw_${fieldname}`; } | ||
export function processLogsDataFrame(datasource: QuickwitDataSource, dataFrame: DataFrame) { | ||
// Ignore log volume dataframe, no need to add links or a displayed message field. | ||
if (!dataFrame.refId || dataFrame.refId.startsWith('log-volume')) { | ||
return; | ||
} | ||
if (datasource.logMessageField) { | ||
const messageFields = datasource.logMessageField.split(','); | ||
let field_idx_list = []; | ||
for (const messageField of messageFields) { | ||
const field_idx = dataFrame.fields.findIndex((field) => field.name === messageField); | ||
if (field_idx !== -1) { | ||
field_idx_list.push(field_idx); | ||
} | ||
} | ||
const displayedMessages = Array(dataFrame.length); | ||
for (let idx = 0; idx < dataFrame.length; idx++) { | ||
let displayedMessage = ""; | ||
// If we have only one field, we assume the field name is obvious for the user and we don't need to show it. | ||
if (field_idx_list.length === 1) { | ||
displayedMessage = `${dataFrame.fields[field_idx_list[0]].values[idx]}`; | ||
} else { | ||
for (const field_idx of field_idx_list) { | ||
displayedMessage += ` ${dataFrame.fields[field_idx].name}=${dataFrame.fields[field_idx].values[idx]}`; | ||
} | ||
} | ||
displayedMessages[idx] = displayedMessage.trim(); | ||
} | ||
|
||
const newField = { | ||
name: getCustomFieldName('message'), | ||
type: FieldType.string, | ||
config: {}, | ||
values: displayedMessages, | ||
}; | ||
const [timestamp, ...rest] = dataFrame.fields; | ||
dataFrame.fields = [timestamp, newField, ...rest]; | ||
} | ||
|
||
if (!datasource.dataLinks.length) { | ||
return; | ||
} | ||
|
||
for (const field of dataFrame.fields) { | ||
const linksToApply = datasource.dataLinks.filter((dataLink) => dataLink.field === field.name); | ||
|
||
if (linksToApply.length === 0) { | ||
continue; | ||
} | ||
|
||
field.config = field.config || {}; | ||
field.config.links = [...(field.config.links || [], linksToApply.map(generateDataLink))]; | ||
} | ||
} | ||
function generateDataLink(linkConfig: DataLinkConfig): DataLink { | ||
const dataSourceSrv = getDataSourceSrv(); | ||
|
||
if (linkConfig.datasourceUid) { | ||
const dsSettings = dataSourceSrv.getInstanceSettings(linkConfig.datasourceUid); | ||
|
||
return { | ||
title: linkConfig.urlDisplayLabel || '', | ||
url: '', | ||
internal: { | ||
query: { query: linkConfig.url }, | ||
datasourceUid: linkConfig.datasourceUid, | ||
datasourceName: dsSettings?.name ?? 'Data source not found', | ||
}, | ||
}; | ||
} else { | ||
return { | ||
title: linkConfig.urlDisplayLabel || '', | ||
url: linkConfig.url, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters