Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rules): add UI for displaying automated rules #378

Merged
merged 4 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions src/app/Rules/Rules.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import * as React from 'react';
import { Card, CardBody, EmptyState, EmptyStateIcon, Title } from '@patternfly/react-core';
import { SearchIcon } from '@patternfly/react-icons';
import { Table, TableBody, TableHeader, TableVariant, ICell, ISortBy, info, sortable } from '@patternfly/react-table';
import { ServiceContext } from '@app/Shared/Services/Services';
import { NotificationCategory } from '@app/Shared/Services/NotificationChannel.service';
import { useSubscriptions } from '@app/utils/useSubscriptions';
import { BreadcrumbPage } from '@app/BreadcrumbPage/BreadcrumbPage';
import { LoadingView } from '@app/LoadingView/LoadingView';

interface Rule {
name: string;
description: string;
matchExpression: string;
archivalPeriodSeconds: number;
preservedArchives: number;
maxAgeSeconds: number;
maxSizeBytes: number;
}

export const Rules = () => {
const context = React.useContext(ServiceContext);
const addSubscription = useSubscriptions();

const [isLoading, setIsLoading] = React.useState(false);
const [sortBy, setSortBy] = React.useState({} as ISortBy);
const [rules, setRules] = React.useState([] as Rule[]);

const tableColumns = [
{
title: 'Name',
transforms: [ sortable ],
},
{ title: 'Description', },
{
title: 'Match Expression',
transforms: [
info({
tooltip: 'A code-snippet expression which must evaluate to a boolean when applied to a given target. If the expression evaluates to true then the rule applies to that target.'
})
],
},
{
title: 'Archival Period',
transforms: [
info({
tooltip: 'Period in seconds. Cryostat will connect to matching targets at this interval and copy the relevant recording data into its archives.'
})
],
},
{
title: 'Preserved Archives',
transforms: [
info({
tooltip: 'The number of recording copies to be maintained in the Cryostat archives. Cryostat will continue retrieving further archived copies and trimming the oldest copies from the archive to maintain this limit.'
})
],
},
{
title: 'Maximum Age',
transforms: [
info({
tooltip: 'The maximum age in seconds for data kept in the JFR recordings started by this rule.'
})
],
},
{
title: 'Maximum Size',
transforms: [
info({
tooltip: 'The maximum size in bytes for JFR recordings started by this rule.'
})
andrewazores marked this conversation as resolved.
Show resolved Hide resolved
],
},
] as ICell[];

const refreshRules = React.useCallback(() => {
setIsLoading(true);
addSubscription(
context.api.doGet('rules', 'v2').subscribe((v: any) => {
setRules(v.data.result);
setIsLoading(false);
})
);
}, [setIsLoading, addSubscription, context, context.api, setRules]);

React.useEffect(() => {
refreshRules();
}, [context, context.api]);

React.useEffect(() => {
addSubscription(
context.notificationChannel.messages(NotificationCategory.RuleCreated)
.subscribe(v => setRules(old => old.concat(v.message)))
);
}, [addSubscription, context, context.notificationChannel, setRules]);

React.useEffect(() => {
addSubscription(
context.notificationChannel.messages(NotificationCategory.RuleDeleted)
.subscribe(v => setRules(old => old.filter(o => o.name != v.message.name)))
)
}, [addSubscription, context, context.notificationChannel, setRules]);

React.useEffect(() => {
if (!context.settings.autoRefreshEnabled()) {
return;
}
const id = window.setInterval(() => refreshRules(), context.settings.autoRefreshPeriod() * context.settings.autoRefreshUnits());
return () => window.clearInterval(id);
}, []);

const handleSort = (event, index, direction) => {
andrewazores marked this conversation as resolved.
Show resolved Hide resolved
setSortBy({ index, direction });
};

const displayRules = React.useMemo(
() => rules.map((r: Rule) => ([ r.name, r.description, r.matchExpression, r.archivalPeriodSeconds, r.preservedArchives, r.maxAgeSeconds, r.maxSizeBytes ])),
[rules]
);

const viewContent = () => {
if (rules.length === 0) {
return (<>
<EmptyState>
<EmptyStateIcon icon={SearchIcon}/>
<Title headingLevel="h4" size="lg">
No Automated Rules
</Title>
</EmptyState>
</>);
} else if (isLoading) {
return <LoadingView />;
} else {
return (<>
<Table aria-label="Automated Rules table"
variant={TableVariant.compact}
cells={tableColumns}
rows={displayRules}
sortBy={sortBy}
onSort={handleSort}
>
<TableHeader />
<TableBody />
</Table>
</>);
}
};

return (<>
<BreadcrumbPage pageTitle='Automated Rules' >
<Card>
<CardBody>
{viewContent()}
</CardBody>
</Card>
</BreadcrumbPage>
</>);

};
4 changes: 2 additions & 2 deletions src/app/Shared/Services/Api.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ export class ApiService {
return this.grafanaDashboardUrlSubject.asObservable();
}

doGet<T>(path: string): Observable<T> {
return this.sendRequest('v1', path, { method: 'GET' }).pipe(map(resp => resp.json()), concatMap(from), first());
doGet<T>(path: string, apiVersion: ApiVersion = 'v1'): Observable<T> {
return this.sendRequest(apiVersion, path, { method: 'GET' }).pipe(map(resp => resp.json()), concatMap(from), first());
}

downloadReport(recording: ArchivedRecording): void {
Expand Down
16 changes: 16 additions & 0 deletions src/app/Shared/Services/NotificationChannel.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export enum NotificationCategory {
ArchivedRecordingDeleted = 'ArchivedRecordingDeleted',
TemplateCreated = 'TemplateUploaded',
TemplateDeleted = 'TemplateDeleted',
RuleCreated = 'RuleCreated',
RuleDeleted = 'RuleDeleted',
}

export enum CloseStatus {
Expand Down Expand Up @@ -143,6 +145,20 @@ const messageKeys = new Map([
body: evt => `${evt.message.template.name} was deleted`
} as NotificationMessageMapper
],
[
NotificationCategory.RuleCreated, {
variant: AlertVariant.success,
title: 'Automated Rule Created',
body: evt => `${evt.message.name} was created`
} as NotificationMessageMapper
],
[
NotificationCategory.RuleDeleted, {
variant: AlertVariant.success,
title: 'Automated Rule Deleted',
body: evt => `${evt.message.name} was deleted`
} as NotificationMessageMapper
],
]);

interface NotificationMessageMapper {
Expand Down
9 changes: 9 additions & 0 deletions src/app/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { Events } from '@app/Events/Events';
import { Login } from '@app/Login/Login';
import { NotFound } from '@app/NotFound/NotFound';
import { Recordings } from '@app/Recordings/Recordings';
import { Rules } from '@app/Rules/Rules';
import { Settings } from '@app/Settings/Settings';
import { SecurityPanel } from '@app/SecurityPanel/SecurityPanel';
import { ServiceContext } from '@app/Shared/Services/Services';
Expand Down Expand Up @@ -87,6 +88,14 @@ const routes: IAppRoute[] = [
title: 'About',
navGroup: OVERVIEW,
},
{
component: Rules,
exact: true,
label: 'Automated Rules',
path: '/rules',
title: 'Automated Rules',
navGroup: CONSOLE,
},
{
component: Recordings,
exact: true,
Expand Down