ConfigEditor.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React, { PureComponent } from 'react';
  2. import {
  3. DataSourcePluginOptionsEditorProps,
  4. updateDatasourcePluginJsonDataOption,
  5. onUpdateDatasourceJsonDataOptionSelect,
  6. onUpdateDatasourceJsonDataOptionChecked,
  7. } from '@grafana/data';
  8. import { Alert, DataSourceHttpSettings, InlineFormLabel, LegacyForms } from '@grafana/ui';
  9. import store from 'app/core/store';
  10. import { GraphiteOptions, GraphiteType } from '../types';
  11. import { DEFAULT_GRAPHITE_VERSION, GRAPHITE_VERSIONS } from '../versions';
  12. import { MappingsConfiguration } from './MappingsConfiguration';
  13. import { fromString, toString } from './parseLokiLabelMappings';
  14. const { Select, Switch } = LegacyForms;
  15. export const SHOW_MAPPINGS_HELP_KEY = 'grafana.datasources.graphite.config.showMappingsHelp';
  16. const graphiteVersions = GRAPHITE_VERSIONS.map((version) => ({ label: `${version}.x`, value: version }));
  17. const graphiteTypes = Object.entries(GraphiteType).map(([label, value]) => ({
  18. label,
  19. value,
  20. }));
  21. export type Props = DataSourcePluginOptionsEditorProps<GraphiteOptions>;
  22. type State = {
  23. showMappingsHelp: boolean;
  24. };
  25. export class ConfigEditor extends PureComponent<Props, State> {
  26. constructor(props: Props) {
  27. super(props);
  28. this.state = {
  29. showMappingsHelp: store.getObject(SHOW_MAPPINGS_HELP_KEY, true),
  30. };
  31. }
  32. renderTypeHelp = () => {
  33. return (
  34. <p>
  35. There are different types of Graphite compatible backends. Here you can specify the type you are using. If you
  36. are using{' '}
  37. <a href="https://github.com/grafana/metrictank" className="pointer" target="_blank" rel="noreferrer">
  38. Metrictank
  39. </a>{' '}
  40. then select that here. This will enable Metrictank specific features like query processing meta data. Metrictank
  41. is a multi-tenant timeseries engine for Graphite and friends.
  42. </p>
  43. );
  44. };
  45. componentDidMount() {
  46. updateDatasourcePluginJsonDataOption(this.props, 'graphiteVersion', this.currentGraphiteVersion);
  47. }
  48. render() {
  49. const { options, onOptionsChange } = this.props;
  50. const currentVersion = graphiteVersions.find((item) => item.value === this.currentGraphiteVersion);
  51. return (
  52. <>
  53. {options.access === 'direct' && (
  54. <Alert title="Deprecation Notice" severity="warning">
  55. This data source uses browser access mode. This mode is deprecated and will be removed in the future. Please
  56. use server access mode instead.
  57. </Alert>
  58. )}
  59. <DataSourceHttpSettings
  60. defaultUrl="http://localhost:8080"
  61. dataSourceConfig={options}
  62. onChange={onOptionsChange}
  63. />
  64. <h3 className="page-heading">Graphite details</h3>
  65. <div className="gf-form-group">
  66. <div className="gf-form-inline">
  67. <div className="gf-form">
  68. <InlineFormLabel tooltip="This option controls what functions are available in the Graphite query editor.">
  69. Version
  70. </InlineFormLabel>
  71. <Select
  72. aria-label="Graphite version"
  73. value={currentVersion}
  74. options={graphiteVersions}
  75. width={8}
  76. onChange={onUpdateDatasourceJsonDataOptionSelect(this.props, 'graphiteVersion')}
  77. />
  78. </div>
  79. </div>
  80. <div className="gf-form-inline">
  81. <div className="gf-form">
  82. <InlineFormLabel tooltip={this.renderTypeHelp}>Type</InlineFormLabel>
  83. <Select
  84. aria-label="Graphite backend type"
  85. options={graphiteTypes}
  86. value={graphiteTypes.find((type) => type.value === options.jsonData.graphiteType)}
  87. width={8}
  88. onChange={onUpdateDatasourceJsonDataOptionSelect(this.props, 'graphiteType')}
  89. />
  90. </div>
  91. </div>
  92. {options.jsonData.graphiteType === GraphiteType.Metrictank && (
  93. <div className="gf-form-inline">
  94. <div className="gf-form">
  95. <Switch
  96. label="Rollup indicator"
  97. labelClass={'width-10'}
  98. tooltip="Shows up as an info icon in panel headers when data is aggregated"
  99. checked={!!options.jsonData.rollupIndicatorEnabled}
  100. onChange={onUpdateDatasourceJsonDataOptionChecked(this.props, 'rollupIndicatorEnabled')}
  101. />
  102. </div>
  103. </div>
  104. )}
  105. </div>
  106. <MappingsConfiguration
  107. mappings={(options.jsonData.importConfiguration?.loki?.mappings || []).map(toString)}
  108. showHelp={this.state.showMappingsHelp}
  109. onDismiss={() => {
  110. this.setState({ showMappingsHelp: false });
  111. store.setObject(SHOW_MAPPINGS_HELP_KEY, false);
  112. }}
  113. onRestoreHelp={() => {
  114. this.setState({ showMappingsHelp: true });
  115. store.setObject(SHOW_MAPPINGS_HELP_KEY, true);
  116. }}
  117. onChange={(mappings) => {
  118. onOptionsChange({
  119. ...options,
  120. jsonData: {
  121. ...options.jsonData,
  122. importConfiguration: {
  123. ...options.jsonData.importConfiguration,
  124. loki: {
  125. mappings: mappings.map(fromString),
  126. },
  127. },
  128. },
  129. });
  130. }}
  131. />
  132. </>
  133. );
  134. }
  135. private get currentGraphiteVersion() {
  136. return this.props.options.jsonData.graphiteVersion || DEFAULT_GRAPHITE_VERSION;
  137. }
  138. }