Skip to content

Commit

Permalink
Add exporter drawer when clicking export all Grafana managed alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
soniaAguilarPeiron committed Sep 13, 2023
1 parent 268e6aa commit b7f910a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
17 changes: 9 additions & 8 deletions public/app/features/alerting/unified/MoreActionsRuleButtons.tsx
@@ -1,18 +1,20 @@
import React from 'react';
import { useLocation } from 'react-router-dom';
import { useToggle } from 'react-use';

import { urlUtil } from '@grafana/data';
import { Button, Dropdown, Icon, LinkButton, Menu, MenuItem } from '@grafana/ui';

import { logInfo, LogMessages } from './Analytics';
import { GrafanaRulesExporter } from './components/export/GrafanaRulesExporter';
import { useRulesAccess } from './utils/accessControlHooks';
import { createUrl } from './utils/url';

interface Props {}
interface Props { }

export function MoreActionsRuleButtons({}: Props) {
export function MoreActionsRuleButtons({ }: Props) {
const { canCreateGrafanaRules, canCreateCloudRules, canReadProvisioning } = useRulesAccess();
const location = useLocation();
const [showExportDrawer, toggleShowExportDrawer] = useToggle(false);
const newMenu = (
<Menu>
{(canCreateGrafanaRules || canCreateCloudRules) && (
Expand All @@ -25,12 +27,8 @@ export function MoreActionsRuleButtons({}: Props) {
)}
{canReadProvisioning && (
<MenuItem
url={createUrl('/api/v1/provisioning/alert-rules/export', {
download: 'true',
format: 'yaml',
})}
onClick={toggleShowExportDrawer}
label="Export all Grafana-managed rules"
target="_blank"
/>
)}
</Menu>
Expand All @@ -54,6 +52,9 @@ export function MoreActionsRuleButtons({}: Props) {
<Icon name="angle-down" />
</Button>
</Dropdown>
{showExportDrawer && (
<GrafanaRulesExporter onClose={toggleShowExportDrawer} />
)}
</>
);
}
11 changes: 9 additions & 2 deletions public/app/features/alerting/unified/api/alertRuleApi.ts
Expand Up @@ -8,7 +8,7 @@ import {
Labels,
PromRulesResponse,
RulerRuleGroupDTO,
RulerRulesConfigDTO,
RulerRulesConfigDTO
} from 'app/types/unified-alerting-dto';

import { ExportFormats } from '../components/export/providers';
Expand All @@ -22,7 +22,7 @@ import {
FetchPromRulesFilter,
groupRulesByFileName,
paramsWithMatcherAndState,
prepareRulesFilterQueryParams,
prepareRulesFilterQueryParams
} from './prometheus';
import { FetchRulerRulesFilter, rulerUrlBuilder } from './ruler';

Expand Down Expand Up @@ -192,6 +192,13 @@ export const alertRuleApi = alertingApi.injectEndpoints({
responseType: 'text',
}),
}),
exportRules: build.query<string, { format: ExportFormats }>({
query: ({ format }) => ({
url: `/api/v1/provisioning/alert-rules/export`,
params: { format: format },
responseType: 'text',
}),
}),
exportReceiver: build.query<string, { receiverName: string; decrypt: string; format: ExportFormats }>({
query: ({ receiverName, decrypt, format }) => ({
url: `/api/v1/provisioning/contact-points/export/`,
Expand Down
Expand Up @@ -15,7 +15,7 @@ interface GrafanaReceiversExportPreviewProps {
}

const GrafanaReceiversExportPreview = ({ decrypt, exportFormat, onClose }: GrafanaReceiversExportPreviewProps) => {
const { currentData: receiverDefinition = '', isFetching } = alertRuleApi.useExportReceiversQuery({
const { currentData: receiversDefinition = '', isFetching } = alertRuleApi.useExportReceiversQuery({
decrypt: decrypt,
format: exportFormat,
});
Expand All @@ -29,7 +29,7 @@ const GrafanaReceiversExportPreview = ({ decrypt, exportFormat, onClose }: Grafa
return (
<FileExportPreview
format={exportFormat}
textDefinition={receiverDefinition}
textDefinition={receiversDefinition}
downloadFileName={downloadFileName}
onClose={onClose}
/>
Expand Down
@@ -0,0 +1,54 @@
import React, { useState } from 'react';

import { LoadingPlaceholder } from '@grafana/ui';

import { alertRuleApi } from '../../api/alertRuleApi';

import { FileExportPreview } from './FileExportPreview';
import { GrafanaExportDrawer } from './GrafanaExportDrawer';
import { allGrafanaExportProviders, ExportFormats } from './providers';

interface GrafanaRulesExporterProps {
onClose: () => void;
}

export function GrafanaRulesExporter({ onClose }: GrafanaRulesExporterProps) {
const [activeTab, setActiveTab] = useState<ExportFormats>('yaml');

return (
<GrafanaExportDrawer
activeTab={activeTab}
onTabChange={setActiveTab}
onClose={onClose}
formatProviders={Object.values(allGrafanaExportProviders)}
>
<GrafanaRulesExportPreview exportFormat={activeTab} onClose={onClose} />
</GrafanaExportDrawer>
);
}

interface GrafanaRulesExportPreviewProps {
exportFormat: ExportFormats;
onClose: () => void;
}

function GrafanaRulesExportPreview({ exportFormat, onClose }: GrafanaRulesExportPreviewProps) {
const { currentData: rulesDefinition = '', isFetching } = alertRuleApi.useExportRulesQuery({
format: exportFormat,
});

const downloadFileName = `alert-rules-${new Date().getTime()}`;

if (isFetching) {
return <LoadingPlaceholder text="Loading...." />;
}

return (
<FileExportPreview
format={exportFormat}
textDefinition={rulesDefinition}
downloadFileName={downloadFileName}
onClose={onClose}
/>
);
}

0 comments on commit b7f910a

Please sign in to comment.