LokiQueryBuilderOptions.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React from 'react';
  2. import { CoreApp, SelectableValue } from '@grafana/data';
  3. import { EditorRow, EditorField } from '@grafana/experimental';
  4. import { reportInteraction } from '@grafana/runtime';
  5. import { RadioButtonGroup, Select } from '@grafana/ui';
  6. import { AutoSizeInput } from 'app/plugins/datasource/prometheus/querybuilder/shared/AutoSizeInput';
  7. import { QueryOptionGroup } from 'app/plugins/datasource/prometheus/querybuilder/shared/QueryOptionGroup';
  8. import { preprocessMaxLines, queryTypeOptions, RESOLUTION_OPTIONS } from '../../components/LokiOptionFields';
  9. import { isMetricsQuery } from '../../datasource';
  10. import { LokiQuery, LokiQueryType } from '../../types';
  11. export interface Props {
  12. query: LokiQuery;
  13. onChange: (update: LokiQuery) => void;
  14. onRunQuery: () => void;
  15. app?: CoreApp;
  16. }
  17. export const LokiQueryBuilderOptions = React.memo<Props>(({ app, query, onChange, onRunQuery }) => {
  18. const onQueryTypeChange = (value: LokiQueryType) => {
  19. onChange({ ...query, queryType: value });
  20. onRunQuery();
  21. };
  22. const onResolutionChange = (option: SelectableValue<number>) => {
  23. reportInteraction('grafana_loki_resolution_clicked', {
  24. app,
  25. resolution: option.value,
  26. });
  27. onChange({ ...query, resolution: option.value });
  28. onRunQuery();
  29. };
  30. const onLegendFormatChanged = (evt: React.FormEvent<HTMLInputElement>) => {
  31. onChange({ ...query, legendFormat: evt.currentTarget.value });
  32. onRunQuery();
  33. };
  34. function onMaxLinesChange(e: React.SyntheticEvent<HTMLInputElement>) {
  35. const newMaxLines = preprocessMaxLines(e.currentTarget.value);
  36. if (query.maxLines !== newMaxLines) {
  37. onChange({ ...query, maxLines: newMaxLines });
  38. onRunQuery();
  39. }
  40. }
  41. let queryType = query.queryType ?? (query.instant ? LokiQueryType.Instant : LokiQueryType.Range);
  42. let showMaxLines = !isMetricsQuery(query.expr);
  43. return (
  44. <EditorRow>
  45. <QueryOptionGroup title="Options" collapsedInfo={getCollapsedInfo(query, queryType, showMaxLines)}>
  46. <EditorField
  47. label="Legend"
  48. tooltip="Series name override or template. Ex. {{hostname}} will be replaced with label value for hostname."
  49. >
  50. <AutoSizeInput
  51. placeholder="{{label}}"
  52. id="loki-query-editor-legend-format"
  53. type="string"
  54. minWidth={14}
  55. defaultValue={query.legendFormat}
  56. onCommitChange={onLegendFormatChanged}
  57. />
  58. </EditorField>
  59. <EditorField label="Type">
  60. <RadioButtonGroup options={queryTypeOptions} value={queryType} onChange={onQueryTypeChange} />
  61. </EditorField>
  62. {showMaxLines && (
  63. <EditorField label="Line limit" tooltip="Upper limit for number of log lines returned by query.">
  64. <AutoSizeInput
  65. className="width-4"
  66. placeholder="auto"
  67. type="number"
  68. min={0}
  69. defaultValue={query.maxLines?.toString() ?? ''}
  70. onCommitChange={onMaxLinesChange}
  71. />
  72. </EditorField>
  73. )}
  74. <EditorField label="Resolution">
  75. <Select
  76. isSearchable={false}
  77. onChange={onResolutionChange}
  78. options={RESOLUTION_OPTIONS}
  79. value={query.resolution || 1}
  80. aria-label="Select resolution"
  81. />
  82. </EditorField>
  83. </QueryOptionGroup>
  84. </EditorRow>
  85. );
  86. });
  87. function getCollapsedInfo(query: LokiQuery, queryType: LokiQueryType, showMaxLines: boolean): string[] {
  88. const queryTypeLabel = queryTypeOptions.find((x) => x.value === queryType);
  89. const resolutionLabel = RESOLUTION_OPTIONS.find((x) => x.value === (query.resolution ?? 1));
  90. const items: string[] = [];
  91. if (query.legendFormat) {
  92. items.push(`Legend: ${query.legendFormat}`);
  93. }
  94. if (query.resolution) {
  95. items.push(`Resolution: ${resolutionLabel?.label}`);
  96. }
  97. items.push(`Type: ${queryTypeLabel?.label}`);
  98. if (showMaxLines && query.maxLines) {
  99. items.push(`Line limit: ${query.maxLines}`);
  100. }
  101. return items;
  102. }
  103. LokiQueryBuilderOptions.displayName = 'LokiQueryBuilderOptions';