CloudWatchLink.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { Component } from 'react';
  2. import { PanelData } from '@grafana/data';
  3. import { Icon } from '@grafana/ui';
  4. import { encodeUrl, AwsUrl } from '../aws_url';
  5. import { CloudWatchDatasource } from '../datasource';
  6. import { CloudWatchLogsQuery } from '../types';
  7. interface Props {
  8. query: CloudWatchLogsQuery;
  9. panelData?: PanelData;
  10. datasource: CloudWatchDatasource;
  11. }
  12. interface State {
  13. href: string;
  14. }
  15. export default class CloudWatchLink extends Component<Props, State> {
  16. state: State = { href: '' };
  17. async componentDidUpdate(prevProps: Props) {
  18. const { panelData: panelDataNew } = this.props;
  19. const { panelData: panelDataOld } = prevProps;
  20. if (panelDataOld !== panelDataNew && panelDataNew?.request) {
  21. const href = this.getExternalLink();
  22. this.setState({ href });
  23. }
  24. }
  25. getExternalLink(): string {
  26. const { query, panelData, datasource } = this.props;
  27. const range = panelData?.request?.range;
  28. if (!range) {
  29. return '';
  30. }
  31. const start = range.from.toISOString();
  32. const end = range.to.toISOString();
  33. const urlProps: AwsUrl = {
  34. end,
  35. start,
  36. timeType: 'ABSOLUTE',
  37. tz: 'UTC',
  38. editorString: query.expression ?? '',
  39. isLiveTail: false,
  40. source: query.logGroupNames ?? [],
  41. };
  42. return encodeUrl(urlProps, datasource.getActualRegion(query.region));
  43. }
  44. render() {
  45. const { href } = this.state;
  46. return (
  47. <a href={href} target="_blank" rel="noopener noreferrer">
  48. <Icon name="share-alt" /> CloudWatch Logs Insights
  49. </a>
  50. );
  51. }
  52. }