CacheSettingsForm.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React from 'react';
  2. import { Field, Switch, Input, Button, HorizontalGroup, Tooltip, Icon, Label } from '@grafana/ui';
  3. import { CacheSettingsDisable } from './CacheSettingsDisable';
  4. import { Props } from './DataSourceCache';
  5. interface CacheSettingsProps {
  6. loading: boolean;
  7. setUseDefaultTTL: (useDefaultTTL: boolean) => void;
  8. setTtlQueriesMs: (ttl: number) => void;
  9. setTtlResourcesMs: (ttl: number) => void;
  10. }
  11. export const CacheSettingsForm = (props: Props & CacheSettingsProps) => {
  12. const {
  13. updateDataSourceCache,
  14. pageId,
  15. useDefaultTTL,
  16. setUseDefaultTTL,
  17. defaultTTLMs,
  18. ttlQueriesMs,
  19. setTtlQueriesMs,
  20. ttlResourcesMs,
  21. setTtlResourcesMs,
  22. loading,
  23. enabled,
  24. dataSourceID,
  25. dataSourceUID,
  26. } = props;
  27. return (
  28. <div>
  29. <Field
  30. description={`Enable this to use the default TTL set in Grafana's configuration (${defaultTTLMs} ms)`}
  31. label="Use default TTL"
  32. disabled={loading}
  33. >
  34. <Switch
  35. value={useDefaultTTL}
  36. onChange={() => {
  37. setUseDefaultTTL(!useDefaultTTL);
  38. }}
  39. />
  40. </Field>
  41. <Field
  42. description="The time-to-live for a query cache item in milliseconds. Example: 5000"
  43. label="Query TTL"
  44. disabled={loading || useDefaultTTL}
  45. >
  46. <Input
  47. type="number"
  48. className="max-width-10"
  49. placeholder="1000"
  50. min={0}
  51. value={(useDefaultTTL ? defaultTTLMs : ttlQueriesMs) || 0}
  52. onChange={(el) => {
  53. setTtlQueriesMs(el.currentTarget.valueAsNumber);
  54. }}
  55. />
  56. </Field>
  57. <Field
  58. description="The time-to-live for resources cache items in milliseconds. Example: 5000"
  59. label={
  60. <Label>
  61. <span>Resource TTL</span>
  62. <Tooltip
  63. content={
  64. <div>
  65. Resources are dynamic values that Grafana data source plugins retrieve from data sources for use in
  66. the query editor. Examples are Splunk namespaces, Prometheus labels, and CloudWatch metric names.
  67. Since these values update less frequently, you might prefer a longer cache TTL for Resources than
  68. queries. See the <a href="https://grafana.com/docs/grafana/latest/enterprise/query-caching/">docs</a>{' '}
  69. for more info.
  70. </div>
  71. }
  72. >
  73. <Icon name="question-circle" />
  74. </Tooltip>
  75. </Label>
  76. }
  77. disabled={loading || useDefaultTTL}
  78. >
  79. <Input
  80. type="number"
  81. className="max-width-10"
  82. placeholder="1000"
  83. min={0}
  84. value={(useDefaultTTL ? defaultTTLMs : ttlResourcesMs) || 0}
  85. onChange={(el) => {
  86. setTtlResourcesMs(el.currentTarget.valueAsNumber);
  87. }}
  88. />
  89. </Field>
  90. <HorizontalGroup spacing="md" align="flex-start" justify="flex-start">
  91. <CacheSettingsDisable {...props} />
  92. <Button
  93. disabled={loading}
  94. onClick={() =>
  95. updateDataSourceCache(pageId, {
  96. dataSourceID,
  97. dataSourceUID,
  98. enabled,
  99. defaultTTLMs,
  100. ttlQueriesMs,
  101. ttlResourcesMs,
  102. useDefaultTTL,
  103. })
  104. }
  105. >
  106. Save
  107. </Button>
  108. </HorizontalGroup>
  109. </div>
  110. );
  111. };