-
Notifications
You must be signed in to change notification settings - Fork 11.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logs: Add Download logs
button to log-browser
#55163
Merged
svennergr
merged 9 commits into
main
from
svennergr/50188-loki-datasource-make-the-downloads-button-more-prominent
Sep 29, 2022
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a07026
move download functions to utils
svennergr d096277
add downloads button
svennergr 3c58afe
removed unused export annotations
svennergr 1996f3a
changed button to `ToolbarButton`
svennergr fad6b17
added tests and restructured download methods
svennergr b0b07be
pin `blob-polyfill`
svennergr dd2d3f5
Merge branch 'main' into svennergr/50188-loki-datasource-make-the-dow…
svennergr 261c0a6
add missing import
svennergr 4a7b0a3
changed type
svennergr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,23 +1,19 @@ | ||
import { css } from '@emotion/css'; | ||
import { Trans, t } from '@lingui/macro'; | ||
import { saveAs } from 'file-saver'; | ||
import { t, Trans } from '@lingui/macro'; | ||
import React, { PureComponent } from 'react'; | ||
import AutoSizer from 'react-virtualized-auto-sizer'; | ||
|
||
import { | ||
applyFieldOverrides, | ||
applyRawFieldOverrides, | ||
CoreApp, | ||
CSVConfig, | ||
DataFrame, | ||
DataTransformerID, | ||
dateTimeFormat, | ||
dateTimeFormatISO, | ||
MutableDataFrame, | ||
SelectableValue, | ||
toCSV, | ||
transformDataFrame, | ||
TimeZone, | ||
CoreApp, | ||
transformDataFrame, | ||
} from '@grafana/data'; | ||
import { selectors } from '@grafana/e2e-selectors'; | ||
import { reportInteraction } from '@grafana/runtime'; | ||
|
@@ -32,6 +28,7 @@ import { transformToZipkin } from 'app/plugins/datasource/zipkin/utils/transform | |
|
||
import { InspectDataOptions } from './InspectDataOptions'; | ||
import { getPanelInspectorStyles } from './styles'; | ||
import { downloadAsJson, downloadDataFrameAsCsv, downloadLogsModelAsTxt } from './utils/download'; | ||
|
||
interface Props { | ||
isLoading: boolean; | ||
|
@@ -99,43 +96,19 @@ export class InspectDataTab extends PureComponent<Props, State> { | |
const { panel } = this.props; | ||
const { transformId } = this.state; | ||
|
||
const dataFrameCsv = toCSV([dataFrame], csvConfig); | ||
|
||
const blob = new Blob([String.fromCharCode(0xfeff), dataFrameCsv], { | ||
type: 'text/csv;charset=utf-8', | ||
}); | ||
const displayTitle = panel ? panel.getDisplayTitle() : 'Explore'; | ||
const transformation = transformId !== DataTransformerID.noop ? '-as-' + transformId.toLocaleLowerCase() : ''; | ||
const fileName = `${displayTitle}-data${transformation}-${dateTimeFormat(new Date())}.csv`; | ||
saveAs(blob, fileName); | ||
downloadDataFrameAsCsv(dataFrame, panel ? panel.getDisplayTitle() : 'Explore', csvConfig, transformId); | ||
}; | ||
|
||
exportLogsAsTxt = () => { | ||
const { data, panel, app } = this.props; | ||
|
||
reportInteraction('grafana_logs_download_logs_clicked', { | ||
app, | ||
format: 'logs', | ||
}); | ||
const logsModel = dataFrameToLogsModel(data || [], undefined); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great cleanup. |
||
let textToDownload = ''; | ||
|
||
logsModel.meta?.forEach((metaItem) => { | ||
const string = `${metaItem.label}: ${JSON.stringify(metaItem.value)}\n`; | ||
textToDownload = textToDownload + string; | ||
}); | ||
textToDownload = textToDownload + '\n\n'; | ||
|
||
logsModel.rows.forEach((row) => { | ||
const newRow = dateTimeFormatISO(row.timeEpochMs) + '\t' + row.entry + '\n'; | ||
textToDownload = textToDownload + newRow; | ||
}); | ||
|
||
const blob = new Blob([textToDownload], { | ||
type: 'text/plain;charset=utf-8', | ||
}); | ||
const displayTitle = panel ? panel.getDisplayTitle() : 'Explore'; | ||
const fileName = `${displayTitle}-logs-${dateTimeFormat(new Date())}.txt`; | ||
saveAs(blob, fileName); | ||
const logsModel = dataFrameToLogsModel(data || [], undefined); | ||
downloadLogsModelAsTxt(logsModel, panel ? panel.getDisplayTitle() : 'Explore'); | ||
}; | ||
|
||
exportTracesAsJson = () => { | ||
|
@@ -153,45 +126,31 @@ export class InspectDataTab extends PureComponent<Props, State> { | |
switch (df.meta?.custom?.traceFormat) { | ||
case 'jaeger': { | ||
let res = transformToJaeger(new MutableDataFrame(df)); | ||
this.saveTraceJson(res, panel); | ||
downloadAsJson(res, (panel ? panel.getDisplayTitle() : 'Explore') + '-traces'); | ||
break; | ||
} | ||
case 'zipkin': { | ||
let res = transformToZipkin(new MutableDataFrame(df)); | ||
this.saveTraceJson(res, panel); | ||
downloadAsJson(res, (panel ? panel.getDisplayTitle() : 'Explore') + '-traces'); | ||
break; | ||
} | ||
case 'otlp': | ||
default: { | ||
let res = transformToOTLP(new MutableDataFrame(df)); | ||
this.saveTraceJson(res, panel); | ||
downloadAsJson(res, (panel ? panel.getDisplayTitle() : 'Explore') + '-traces'); | ||
break; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
saveTraceJson = (json: any, panel?: PanelModel) => { | ||
const blob = new Blob([JSON.stringify(json)], { | ||
type: 'application/json', | ||
}); | ||
const displayTitle = panel ? panel.getDisplayTitle() : 'Explore'; | ||
const fileName = `${displayTitle}-traces-${dateTimeFormat(new Date())}.json`; | ||
saveAs(blob, fileName); | ||
}; | ||
|
||
exportServiceGraph = () => { | ||
const { data, panel } = this.props; | ||
if (!data) { | ||
return; | ||
} | ||
|
||
const blob = new Blob([JSON.stringify(data)], { | ||
type: 'application/json', | ||
}); | ||
const displayTitle = panel ? panel.getDisplayTitle() : 'Explore'; | ||
const fileName = `${displayTitle}-service-graph-${dateTimeFormat(new Date())}.json`; | ||
saveAs(blob, fileName); | ||
downloadAsJson(data, panel ? panel.getDisplayTitle() : 'Explore'); | ||
}; | ||
|
||
onDataFrameChange = (item: SelectableValue<DataTransformerID | number>) => { | ||
|
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,114 @@ | ||
import saveAs from 'file-saver'; | ||
|
||
import { | ||
dataFrameFromJSON, | ||
DataFrameJSON, | ||
dateTimeFormat, | ||
dateTimeFormatISO, | ||
FieldType, | ||
LogRowModel, | ||
LogsMetaKind, | ||
} from '@grafana/data'; | ||
|
||
import { downloadAsJson, downloadDataFrameAsCsv, downloadLogsModelAsTxt } from './download'; | ||
|
||
jest.mock('file-saver', () => jest.fn()); | ||
|
||
describe('inspector download', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.useFakeTimers({ now: new Date(1400000000000) }); | ||
}); | ||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
describe('downloadDataFrameAsCsv', () => { | ||
const json: DataFrameJSON = { | ||
schema: { | ||
fields: [ | ||
{ name: 'time', type: FieldType.time }, | ||
{ name: 'name', type: FieldType.string }, | ||
{ name: 'value', type: FieldType.number }, | ||
], | ||
}, | ||
data: { | ||
values: [[100], ['a'], [1]], | ||
}, | ||
}; | ||
|
||
it.each([[dataFrameFromJSON(json), 'test', '"time","name","value"\r\n100,a,1\r\n\r\n']])( | ||
'should, when logsModel is %s and title is %s, resolve in %s', | ||
async (dataFrame, title, expected) => { | ||
downloadDataFrameAsCsv(dataFrame, title); | ||
const call = (saveAs as unknown as jest.Mock).mock.calls[0]; | ||
const blob = call[0]; | ||
const filename = call[1]; | ||
const text = await blob.text(); | ||
|
||
expect(text).toEqual(expected); | ||
expect(filename).toEqual(`${title}-data-${dateTimeFormat(1400000000000)}.csv`); | ||
} | ||
); | ||
}); | ||
|
||
describe('downloadAsJson', () => { | ||
it.each([ | ||
['foo', 'test', '"foo"'], | ||
[1, 'test', '1'], | ||
[{ foo: 'bar' }, 'test', '{"foo":"bar"}'], | ||
])('should, when logsModel is %s and title is %s, resolve in %s', async (logsModel, title, expected) => { | ||
downloadAsJson(logsModel, title); | ||
const call = (saveAs as unknown as jest.Mock).mock.calls[0]; | ||
const blob = call[0]; | ||
const filename = call[1]; | ||
const text = await blob.text(); | ||
|
||
expect(text).toEqual(expected); | ||
expect(filename).toEqual(`${title}-${dateTimeFormat(1400000000000)}.json`); | ||
}); | ||
}); | ||
|
||
describe('downloadLogsModelAsTxt', () => { | ||
it.each([ | ||
[{ meta: [], rows: [] }, 'test', '\n\n'], | ||
[ | ||
{ meta: [{ label: 'testLabel', value: 'testValue', kind: LogsMetaKind.String }], rows: [] }, | ||
'test', | ||
'testLabel: "testValue"\n\n\n', | ||
], | ||
[{ meta: [{ label: 'testLabel', value: 1, kind: LogsMetaKind.Number }], rows: [] }, 'test', 'testLabel: 1\n\n\n'], | ||
[ | ||
{ | ||
meta: [ | ||
{ label: 'testLabel', value: 1, kind: LogsMetaKind.String }, | ||
{ label: 'secondTestLabel', value: 2, kind: LogsMetaKind.String }, | ||
], | ||
rows: [], | ||
}, | ||
'test', | ||
'testLabel: 1\nsecondTestLabel: 2\n\n\n', | ||
], | ||
[ | ||
{ | ||
meta: [ | ||
{ label: 'testLabel', value: 1, kind: LogsMetaKind.String }, | ||
{ label: 'secondTestLabel', value: 2, kind: LogsMetaKind.String }, | ||
], | ||
rows: [{ timeEpochMs: 100, entry: 'testEntry' } as unknown as LogRowModel], | ||
}, | ||
'test', | ||
`testLabel: 1\nsecondTestLabel: 2\n\n\n${dateTimeFormatISO(100)}\ttestEntry\n`, | ||
], | ||
])('should, when logsModel is %s and title is %s, resolve in %s', async (logsModel, title, expected) => { | ||
downloadLogsModelAsTxt(logsModel, title); | ||
const call = (saveAs as unknown as jest.Mock).mock.calls[0]; | ||
const blob = call[0]; | ||
const filename = call[1]; | ||
const text = await blob.text(); | ||
|
||
expect(text).toEqual(expected); | ||
expect(filename).toEqual(`${title}-logs-${dateTimeFormat(1400000000000)}.txt`); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added that in order to add methods to the
Blob
object, which seem to be missing on JSDom.Ref jsdom/jsdom#2555