DataSourceCache.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React, { useEffect, useState } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { HorizontalGroup, useTheme2 } from '@grafana/ui';
  4. import PageHeader from 'app/core/components/PageHeader/PageHeader';
  5. import { UpgradeBox, UpgradeContent, UpgradeContentProps } from 'app/core/components/Upgrade/UpgradeBox';
  6. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  7. import { getNavModel } from 'app/core/selectors/navModel';
  8. import { highlightTrial } from 'app/features/admin/utils';
  9. import { loadDataSource, loadDataSourceMeta } from 'app/features/datasources/state/actions';
  10. import { getDataSourceLoadingNav } from 'app/features/datasources/state/navModel';
  11. import { EnterpriseStoreState } from '../types';
  12. import { CacheCTA } from './CacheCTA';
  13. import { CacheClean } from './CacheClean';
  14. import { CacheSettingsForm } from './CacheSettingsForm';
  15. import {
  16. loadDataSourceCache,
  17. enableDataSourceCache,
  18. disableDataSourceCache,
  19. updateDataSourceCache,
  20. cleanCache,
  21. } from './state/actions';
  22. interface RouteProps extends GrafanaRouteComponentProps<{ uid: string }> {}
  23. function mapStateToProps(state: EnterpriseStoreState, props: RouteProps) {
  24. const dataSourceUid = props.match.params.uid;
  25. const dataSourceLoadingNav = getDataSourceLoadingNav('cache');
  26. return {
  27. ...state.dataSourceCache,
  28. navModel: getNavModel(state.navIndex, `datasource-cache-${dataSourceUid}`, dataSourceLoadingNav),
  29. pageId: dataSourceUid,
  30. isDefault: state.dataSources.dataSource.isDefault,
  31. };
  32. }
  33. const mapDispatchToProps = {
  34. disableDataSourceCache,
  35. updateDataSourceCache,
  36. enableDataSourceCache,
  37. loadDataSourceCache,
  38. loadDataSource,
  39. cleanCache,
  40. loadDataSourceMeta,
  41. };
  42. export const connector = connect(mapStateToProps, mapDispatchToProps);
  43. export type Props = ConnectedProps<typeof connector>;
  44. export const Caching = (props: Props) => {
  45. const [enabled, setEnabled] = useState(props.enabled);
  46. const [loading, setLoading] = useState(true);
  47. const [useDefaultTTL, setUseDefaultTTL] = useState(props.useDefaultTTL);
  48. const [ttlQueriesMs, setTtlQueriesMs] = useState(props.ttlQueriesMs);
  49. const [ttlResourcesMs, setTtlResourcesMs] = useState(props.ttlResourcesMs);
  50. const { navModel, pageId, loadDataSource, loadDataSourceCache, loadDataSourceMeta, enableDataSourceCache } = props;
  51. useEffect(() => {
  52. setEnabled(props.enabled);
  53. setUseDefaultTTL(props.useDefaultTTL);
  54. setTtlQueriesMs(props.ttlQueriesMs);
  55. setTtlResourcesMs(props.ttlResourcesMs);
  56. setLoading(false);
  57. }, [props.useDefaultTTL, props.ttlQueriesMs, props.ttlResourcesMs, props.enabled]);
  58. useEffect(() => {
  59. loadDataSource(pageId).then(loadDataSourceMeta);
  60. loadDataSourceCache(pageId);
  61. }, [loadDataSourceCache, loadDataSource, loadDataSourceMeta, pageId]);
  62. const content = enabled ? (
  63. CacheSettingsForm({
  64. ...props,
  65. loading,
  66. useDefaultTTL,
  67. setUseDefaultTTL,
  68. ttlQueriesMs,
  69. setTtlQueriesMs,
  70. ttlResourcesMs,
  71. setTtlResourcesMs,
  72. })
  73. ) : highlightTrial() ? (
  74. <DataSourceCacheUpgradeContent
  75. action={{
  76. text: 'Enable caching',
  77. onClick: () => {
  78. enableDataSourceCache(pageId);
  79. },
  80. }}
  81. />
  82. ) : (
  83. CacheCTA(props)
  84. );
  85. return (
  86. <div>
  87. <PageHeader model={navModel} />
  88. <div className="page-container page-body">
  89. {highlightTrial() && (
  90. <UpgradeBox
  91. featureId={'query-caching'}
  92. eventVariant={'trial'}
  93. featureName={'query caching'}
  94. text="Enable query caching for free during your trial of Grafana Pro."
  95. />
  96. )}
  97. <div className="page-action-bar">
  98. {enabled && !highlightTrial() && <h3 className="page-sub-heading">Caching</h3>}
  99. <div className="page-action-bar__spacer" />
  100. <HorizontalGroup spacing="md" align="flex-end" justify="flex-end">
  101. {enabled && <CacheClean {...props} />}
  102. </HorizontalGroup>
  103. </div>
  104. {content}
  105. </div>
  106. </div>
  107. );
  108. };
  109. export interface DataSourceCacheUpgradeContentProps {
  110. action?: UpgradeContentProps['action'];
  111. }
  112. export const DataSourceCacheUpgradeContent = ({ action }: DataSourceCacheUpgradeContentProps) => {
  113. const theme = useTheme2();
  114. return (
  115. <UpgradeContent
  116. action={action}
  117. listItems={[
  118. 'Load dashboards in less than a second from the cache, even when they include big queries and lots of users are looking at once',
  119. 'Save money and avoid rate limiting by reducing the number of API calls you make to data sources like Splunk, CloudWatch and Github',
  120. 'Author dashboards more smoothly by caching the data used to construct queries, like fields in ServiceNow or metrics available from Datadog',
  121. ]}
  122. image={`query-caching-${theme.isLight ? 'light' : 'dark'}.png`}
  123. featureUrl={'https://grafana.com/docs/grafana/latest/enterprise/query-caching'}
  124. featureName={'query caching'}
  125. description={
  126. 'With query caching, you can load dashboards faster and reduce costly queries to data sources by temporarily storing query results in memory, Redis, or Memcached.'
  127. }
  128. />
  129. );
  130. };
  131. export default connector(Caching);