refactor(app): redesign dashboard-item component [EE-3634] (#7175)

pull/7204/head
Chaim Lev-Ari 2022-07-06 11:23:53 +03:00 committed by GitHub
parent a66fd78dc1
commit 8bf1c91bc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 236 additions and 248 deletions

View File

@ -89,6 +89,14 @@ pr-icon {
stroke: var(--white-color);
}
.icon-badge {
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
padding: 1.5%;
}
.icon-nested-gray {
height: 30px;
width: 30px;

View File

@ -32,12 +32,14 @@
}
a {
color: var(--text-link-color);
color: inherit;
cursor: pointer;
}
a:hover,
a:focus {
color: var(--text-link-hover-color);
color: inherit;
text-decoration: none;
}
.input-group-addon {

View File

@ -19,7 +19,7 @@ jest.mock('@uirouter/react', () => ({
test('dashboard items should render correctly', async () => {
const { getByLabelText } = await renderComponent();
const subscriptionsItem = getByLabelText('Subscriptions');
const subscriptionsItem = getByLabelText('Subscription');
expect(subscriptionsItem).toBeVisible();
const subscriptionElements = within(subscriptionsItem);
@ -31,7 +31,7 @@ test('dashboard items should render correctly', async () => {
'Subscriptions'
);
const resourceGroupsItem = getByLabelText('Resource groups');
const resourceGroupsItem = getByLabelText('Resource group');
expect(resourceGroupsItem).toBeVisible();
const resourceGroupElements = within(resourceGroupsItem);
@ -47,20 +47,20 @@ test('dashboard items should render correctly', async () => {
test('when there are no subscriptions, should show 0 subscriptions and 0 resource groups', async () => {
const { getByLabelText } = await renderComponent();
const subscriptionElements = within(getByLabelText('Subscriptions'));
const subscriptionElements = within(getByLabelText('Subscription'));
expect(subscriptionElements.getByLabelText('value')).toHaveTextContent('0');
const resourceGroupElements = within(getByLabelText('Resource groups'));
const resourceGroupElements = within(getByLabelText('Resource group'));
expect(resourceGroupElements.getByLabelText('value')).toHaveTextContent('0');
});
test('when there is subscription & resource group data, should display these', async () => {
const { getByLabelText } = await renderComponent(1, { 'subscription-1': 2 });
const subscriptionElements = within(getByLabelText('Subscriptions'));
const subscriptionElements = within(getByLabelText('Subscription'));
expect(subscriptionElements.getByLabelText('value')).toHaveTextContent('1');
const resourceGroupElements = within(getByLabelText('Resource groups'));
const resourceGroupElements = within(getByLabelText('Resource group'));
expect(resourceGroupElements.getByLabelText('value')).toHaveTextContent('2');
});
@ -70,7 +70,7 @@ test('should correctly show total number of resource groups across multiple subs
'subscription-2': 3,
});
const resourceGroupElements = within(getByLabelText('Resource groups'));
const resourceGroupElements = within(getByLabelText('Resource group'));
expect(resourceGroupElements.getByLabelText('value')).toHaveTextContent('5');
});
@ -81,8 +81,8 @@ test('when only subscriptions fail to load, dont show the dashboard', async () =
500,
200
);
expect(queryByLabelText('Subscriptions')).not.toBeInTheDocument();
expect(queryByLabelText('Resource groups')).not.toBeInTheDocument();
expect(queryByLabelText('Subscription')).not.toBeInTheDocument();
expect(queryByLabelText('Resource group')).not.toBeInTheDocument();
});
test('when only resource groups fail to load, still show the subscriptions', async () => {
@ -92,8 +92,8 @@ test('when only resource groups fail to load, still show the subscriptions', asy
200,
500
);
expect(queryByLabelText('Subscriptions')).toBeInTheDocument();
expect(queryByLabelText('Resource groups')).not.toBeInTheDocument();
expect(queryByLabelText('Subscription')).toBeInTheDocument();
expect(queryByLabelText('Resource group')).not.toBeInTheDocument();
});
async function renderComponent(

View File

@ -7,6 +7,7 @@ import { r2a } from '@/react-tools/react2angular';
import { DashboardItem } from '@@/DashboardItem';
import { PageHeader } from '@@/PageHeader';
import { DashboardGrid } from '@@/DashboardItem/DashboardGrid';
import { useResourceGroups, useSubscriptions } from '../queries';
@ -53,22 +54,24 @@ export function DashboardView() {
<>
<PageHeader title="Home" breadcrumbs={[{ label: 'Dashboard' }]} />
{!subscriptionsQuery.isError && (
<div className="row">
<DashboardItem
value={subscriptionsCount as number}
icon="fa fa-th-list"
type="Subscriptions"
/>
{!resourceGroupsQuery.isError && (
<div className="mx-4">
{subscriptionsQuery.data && (
<DashboardGrid>
<DashboardItem
value={resourceGroupsCount as number}
value={subscriptionsCount as number}
icon="fa fa-th-list"
type="Resource groups"
type="Subscription"
/>
)}
</div>
)}
{!resourceGroupsQuery.isError && !resourceGroupsQuery.isLoading && (
<DashboardItem
value={resourceGroupsCount}
icon="fa fa-th-list"
type="Resource group"
/>
)}
</DashboardGrid>
)}
</div>
</>
);
}

View File

@ -235,49 +235,6 @@ angular
return runningTasks;
};
})
.filter('runningcontainers', function () {
'use strict';
return function runningContainersFilter(containers) {
return containers.filter(function (container) {
return container.State === 'running';
}).length;
};
})
.filter('stoppedcontainers', function () {
'use strict';
return function stoppedContainersFilter(containers) {
return containers.filter(function (container) {
return container.State === 'exited';
}).length;
};
})
.filter('healthycontainers', function () {
'use strict';
return function healthyContainersFilter(containers) {
return containers.filter(function (container) {
return container.Status === 'healthy';
}).length;
};
})
.filter('unhealthycontainers', function () {
'use strict';
return function unhealthyContainersFilter(containers) {
return containers.filter(function (container) {
return container.Status === 'unhealthy';
}).length;
};
})
.filter('imagestotalsize', function () {
'use strict';
return function (images) {
var totalImageSize = 0;
for (var i = 0; i < images.length; i++) {
var item = images[i];
totalImageSize += item.VirtualSize;
}
return totalImageSize;
};
})
.filter('tasknodename', function () {
'use strict';
return function (nodeId, nodes) {

View File

@ -28,136 +28,74 @@
</span>
</information-panel>
<div class="row" ng-if="(!applicationState.endpoint.mode.agentProxy || applicationState.endpoint.mode.provider !== 'DOCKER_SWARM_MODE') && info && endpoint">
<div class="col-sm-12">
<rd-widget>
<rd-widget-header icon="fa-tachometer-alt" title-text="Environment info"></rd-widget-header>
<rd-widget-body classes="no-padding">
<table class="table">
<tbody>
<tr>
<td>Environment</td>
<td>
{{ endpoint.Name }}
<span class="small text-muted space-left">
<i class="fa fa-microchip"></i> {{ endpoint.Snapshots[0].TotalCPU }} <i class="fa fa-memory space-left"></i> {{ endpoint.Snapshots[0].TotalMemory | humansize }}
</span>
<span class="small text-muted">
- {{ info.Swarm && info.Swarm.NodeID !== '' ? 'Swarm' : 'Standalone' }} {{ info.ServerVersion }}
<span ng-if="endpoint.Type === 2">+ <i class="fa fa-bolt" aria-hidden="true"></i> Agent</span></span
>
</td>
</tr>
<tr ng-if="showEnvUrl">
<td>URL</td>
<td>{{ endpoint.URL | stripprotocol }}</td>
</tr>
<tr>
<td>Tags</td>
<td>{{ endpointTags }}</td>
</tr>
<tr ng-if="applicationState.endpoint.mode.provider === 'DOCKER_SWARM_MODE' && applicationState.endpoint.mode.role === 'MANAGER'">
<td colspan="2">
<div class="btn-group" role="group" aria-label="...">
<a ui-sref="docker.swarm.visualizer"><i class="fa fa-object-group space-right" aria-hidden="true"></i>Go to cluster visualizer</a>
</div>
</td>
</tr>
</tbody>
</table>
</rd-widget-body>
</rd-widget>
<div ng-if="info">
<div class="row" ng-if="(!applicationState.endpoint.mode.agentProxy || applicationState.endpoint.mode.provider !== 'DOCKER_SWARM_MODE') && info && endpoint">
<div class="col-sm-12">
<rd-widget>
<rd-widget-header icon="fa-tachometer-alt" title-text="Environment info"></rd-widget-header>
<rd-widget-body classes="no-padding">
<table class="table">
<tbody>
<tr>
<td>Environment</td>
<td>
{{ endpoint.Name }}
<span class="small text-muted space-left">
<i class="fa fa-microchip"></i> {{ endpoint.Snapshots[0].TotalCPU }} <i class="fa fa-memory space-left"></i> {{ endpoint.Snapshots[0].TotalMemory | humansize }}
</span>
<span class="small text-muted">
- {{ info.Swarm && info.Swarm.NodeID !== '' ? 'Swarm' : 'Standalone' }} {{ info.ServerVersion }}
<span ng-if="endpoint.Type === 2">+ <i class="fa fa-bolt" aria-hidden="true"></i> Agent</span></span
>
</td>
</tr>
<tr ng-if="showEnvUrl">
<td>URL</td>
<td>{{ endpoint.URL | stripprotocol }}</td>
</tr>
<tr>
<td>Tags</td>
<td>{{ endpointTags }}</td>
</tr>
<tr ng-if="applicationState.endpoint.mode.provider === 'DOCKER_SWARM_MODE' && applicationState.endpoint.mode.role === 'MANAGER'">
<td colspan="2">
<div class="btn-group" role="group" aria-label="...">
<a ui-sref="docker.swarm.visualizer"><i class="fa fa-object-group space-right" aria-hidden="true"></i>Go to cluster visualizer</a>
</div>
</td>
</tr>
</tbody>
</table>
</rd-widget-body>
</rd-widget>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-6" ng-if="showStacks">
<a ui-sref="docker.stacks">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-th-list"></i>
</div>
<div class="title">{{ stackCount }}</div>
<div class="comment">{{ stackCount === 1 ? 'Stack' : 'Stacks' }}</div>
</rd-widget-body>
</rd-widget>
<div class="dashboard-grid mx-4">
<a ui-sref="docker.stacks" ng-if="showStacks">
<dashboard-item icon="'layers'" feather-icon="true" type="'Stack'" value="stackCount"></dashboard-item>
</a>
</div>
<div class="col-xs-12 col-md-6" ng-if="applicationState.endpoint.mode.provider === 'DOCKER_SWARM_MODE' && applicationState.endpoint.mode.role === 'MANAGER'">
<a ui-sref="docker.services">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-list-alt"></i>
</div>
<div class="title">{{ serviceCount }}</div>
<div class="comment">{{ serviceCount === 1 ? 'Service' : 'Services' }}</div>
</rd-widget-body>
</rd-widget>
<div ng-if="applicationState.endpoint.mode.provider === 'DOCKER_SWARM_MODE' && applicationState.endpoint.mode.role === 'MANAGER'">
<a ui-sref="docker.services">
<dashboard-item icon="'fa-list-alt'" type="'Service'" value="serviceCount"></dashboard-item>
</a>
</div>
<a ng-if="containers" ui-sref="docker.containers">
<dashboard-item icon="'fa-cubes'" type="'Container'" value="containers.length" children="containerStatusComponent"></dashboard-item>
</a>
</div>
<div class="col-xs-12 col-md-6" ng-if="containers">
<a ui-sref="docker.containers">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-cubes"></i>
</div>
<div class="pull-right" style="padding-left: 5px">
<div><i class="fa fa-power-off space-right green-icon"></i>{{ containers | runningcontainers }} running</div>
<div><i class="fa fa-power-off space-right red-icon"></i>{{ containers | stoppedcontainers }} stopped</div>
</div>
<div class="pull-right" style="padding-right: 5px">
<div><i class="fa fa-heartbeat space-right green-icon"></i>{{ containers | healthycontainers }} healthy</div>
<div><i class="fa fa-heartbeat space-right orange-icon"></i>{{ containers | unhealthycontainers }} unhealthy</div>
</div>
<div class="title">{{ containers.length }}</div>
<div class="comment">{{ containers.length === 1 ? 'Container' : 'Containers' }}</div>
</rd-widget-body>
</rd-widget>
<a ng-if="images" ui-sref="docker.images">
<dashboard-item icon="'fa-clone'" type="'Image'" value="images.length" children="imagesTotalSizeComponent"></dashboard-item>
</a>
</div>
<div class="col-xs-12 col-md-6" ng-if="images">
<a ui-sref="docker.images">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-clone"></i>
</div>
<div class="pull-right">
<div><i class="fa fa-chart-pie space-right"></i>{{ images | imagestotalsize | humansize }}</div>
</div>
<div class="title">{{ images.length }}</div>
<div class="comment">{{ images.length === 1 ? 'Image' : 'Images' }}</div>
</rd-widget-body>
</rd-widget>
</a>
</div>
<div class="col-xs-12 col-md-6">
<a ui-sref="docker.volumes">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-hdd"></i>
</div>
<div class="title">{{ volumeCount }}</div>
<div class="comment">{{ volumeCount === 1 ? 'Volume' : 'Volumes' }}</div>
</rd-widget-body>
</rd-widget>
<dashboard-item icon="'fa-hdd'" type="'Volume'" value="volumeCount"></dashboard-item>
</a>
</div>
<div class="col-xs-12 col-md-6">
<a ui-sref="docker.networks">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-sitemap"></i>
</div>
<div class="title">{{ networkCount }}</div>
<div class="comment">{{ networkCount === 1 ? 'Network' : 'Networks' }}</div>
</rd-widget-body>
</rd-widget>
<dashboard-item icon="'fa-sitemap'" type="'Network'" value="networkCount"></dashboard-item>
</a>
</div>
</div>

View File

@ -3,6 +3,8 @@ import _ from 'lodash';
import { isOfflineEndpoint } from '@/portainer/helpers/endpointHelper';
import { PortainerEndpointTypes } from 'Portainer/models/endpoint/models';
import { useContainerStatusComponent } from '@/react/docker/DashboardView/ContainerStatus';
import { useImagesTotalSizeComponent } from '@/react/docker/DashboardView/ImagesTotalSize';
angular.module('portainer.docker').controller('DashboardController', [
'$scope',
@ -60,7 +62,11 @@ angular.module('portainer.docker').controller('DashboardController', [
})
.then(function success(data) {
$scope.containers = data.containers;
$scope.containerStatusComponent = useContainerStatusComponent(data.containers);
$scope.images = data.images;
$scope.imagesTotalSizeComponent = useImagesTotalSizeComponent(imagesTotalSize(data.images));
$scope.volumeCount = data.volumes.length;
$scope.networkCount = data.networks.length;
$scope.serviceCount = data.services.length;
@ -94,3 +100,7 @@ angular.module('portainer.docker').controller('DashboardController', [
initView();
},
]);
function imagesTotalSize(images) {
return images.reduce((acc, image) => acc + image.VirtualSize, 0);
}

View File

@ -31,57 +31,26 @@
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-6" ng-if="ctrl.pools" data-cy="k8sDashboard-namespaces">
<div class="dashboard-grid mx-4">
<div ng-if="ctrl.pools" data-cy="k8sDashboard-namespaces">
<a ui-sref="kubernetes.resourcePools">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-layer-group"></i>
</div>
<div class="title">{{ ctrl.pools.length }}</div>
<div class="comment">{{ ctrl.pools.length === 1 ? 'Namespace' : 'Namespaces' }}</div>
</rd-widget-body>
</rd-widget>
<dashboard-item icon="'fa-layer-group'" type="'Namespace'" value="ctrl.pools.length"></dashboard-item>
</a>
</div>
<div class="col-xs-12 col-md-6" ng-if="ctrl.applications" data-cy="k8sDashboard-applications">
<div ng-if="ctrl.applications" data-cy="k8sDashboard-applications">
<a ui-sref="kubernetes.applications">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-laptop-code"></i>
</div>
<div class="title">{{ ctrl.applications.length }}</div>
<div class="comment">{{ ctrl.applications.length === 1 ? 'Application' : 'Applications' }}</div>
</rd-widget-body>
</rd-widget>
<dashboard-item icon="'fa-laptop-code'" type="'Application'" value="ctrl.applications.length"></dashboard-item>
</a>
</div>
<div class="col-xs-12 col-md-6" ng-if="ctrl.configurations" data-cy="k8sDashboard-configurations">
<div ng-if="ctrl.configurations" data-cy="k8sDashboard-configurations">
<a ui-sref="kubernetes.configurations">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-file-code"></i>
</div>
<div class="title">{{ ctrl.configurations.length }}</div>
<div class="comment">{{ ctrl.configurations.length === 1 ? 'Configuration' : 'Configurations' }}</div>
</rd-widget-body>
</rd-widget>
<dashboard-item icon="'fa-file-code'" type="'Configuration'" value="ctrl.configurations.length"></dashboard-item>
</a>
</div>
<div class="col-xs-12 col-md-6" ng-if="ctrl.volumes" data-cy="k8sDashboard-volumes">
<div ng-if="ctrl.volumes" data-cy="k8sDashboard-volumes">
<a ui-sref="kubernetes.volumes">
<rd-widget>
<rd-widget-body>
<div class="widget-icon blue pull-left">
<i class="fa fa-database"></i>
</div>
<div class="title">{{ ctrl.volumes.length }}</div>
<div class="comment">{{ ctrl.volumes.length === 1 ? 'Volume' : 'Volumes' }}</div>
</rd-widget-body>
</rd-widget>
<dashboard-item icon="'fa-database'" type="'Volume'" value="ctrl.volumes.length"></dashboard-item>
</a>
</div>
</div>

View File

@ -10,6 +10,7 @@ import { Loading } from '@@/Widget/Loading';
import { PasswordCheckHint } from '@@/PasswordCheckHint';
import { ViewLoading } from '@@/ViewLoading';
import { Tooltip } from '@@/Tip/Tooltip';
import { DashboardItem } from '@@/DashboardItem';
import { fileUploadField } from './file-upload-field';
import { switchField } from './switch-field';
@ -38,4 +39,8 @@ export const componentsModule = angular
'prIcon',
r2a(Icon, ['className', 'feather', 'icon', 'mode', 'size'])
)
.component('reactQueryDevTools', r2a(ReactQueryDevtoolsWrapper, [])).name;
.component('reactQueryDevTools', r2a(ReactQueryDevtoolsWrapper, []))
.component(
'dashboardItem',
r2a(DashboardItem, ['featherIcon', 'icon', 'type', 'value', 'children'])
).name;

View File

@ -0,0 +1,3 @@
.dashboard-grid {
@apply grid grid-cols-2 gap-3;
}

View File

@ -0,0 +1,7 @@
import { PropsWithChildren } from 'react';
import './DashboardGrid.css';
export function DashboardGrid({ children }: PropsWithChildren<unknown>) {
return <div className="dashboard-grid">{children}</div>;
}

View File

@ -34,3 +34,11 @@ export function WithLink() {
</Link>
);
}
export function WithChildren() {
return (
<DashboardItem value={1} icon="fa fa-th-list" type="Example resource">
<div>Children</div>
</DashboardItem>
);
}

View File

@ -1,8 +1,8 @@
import { ReactNode } from 'react';
import clsx from 'clsx';
import { Icon, IconProps } from '@/react/components/Icon';
import { Widget, WidgetBody } from '@@/Widget';
import { pluralize } from '@/portainer/helpers/strings';
interface Props extends IconProps {
value?: number;
@ -18,21 +18,29 @@ export function DashboardItem({
featherIcon,
}: Props) {
return (
<div className="col-sm-12 col-md-6" aria-label={type}>
<Widget>
<WidgetBody>
<div className="widget-icon blue pull-left">
<Icon icon={icon} feather={featherIcon} />
<div
className={clsx(
'border-solid rounded-lg border-2 hover:border-2 border-gray-5 hover:border-blue-7',
'bg-gray-2 hover:bg-blue-2',
'p-3'
)}
>
<div className="flex items-center" aria-label={type}>
<div className="icon-badge text-2xl bg-blue-3 text-blue-8 mr-4">
<Icon icon={icon} feather={featherIcon} className="feather" />
</div>
<div className="flex flex-col justify-around">
<div className="text-gray-9 font-medium text-2xl" aria-label="value">
{typeof value !== 'undefined' ? value : '-'}
</div>
<div className="pull-right">{children}</div>
<div className="title" aria-label="value">
{value}
<div className="text-gray-7 text-xl" aria-label="resourceType">
{pluralize(value || 0, type)}
</div>
<div className="comment" aria-label="resourceType">
{type}
</div>
</WidgetBody>
</Widget>
</div>
<div className="ml-auto">{children}</div>
</div>
</div>
);
}

View File

@ -0,0 +1,52 @@
import { DockerContainer } from '../containers/types';
interface Props {
containers: DockerContainer[];
}
export function useContainerStatusComponent(containers: DockerContainer[]) {
return <ContainerStatus containers={containers} />;
}
export function ContainerStatus({ containers }: Props) {
return (
<>
<div className="pull-right pl-1">
<div>
<i className="fa fa-power-off space-right green-icon" />
{runningContainersFilter(containers)} running
</div>
<div>
<i className="fa fa-power-off space-right red-icon" />
{stoppedContainersFilter(containers)} stopped
</div>
</div>
<div className="pull-right pr-5">
<div>
<i className="fa fa-heartbeat space-right green-icon" />
{healthyContainersFilter(containers)} healthy
</div>
<div>
<i className="fa fa-heartbeat space-right orange-icon" />
{unhealthyContainersFilter(containers)} unhealthy
</div>
</div>
</>
);
}
function runningContainersFilter(containers: DockerContainer[]) {
return containers.filter((container) => container.Status === 'running')
.length;
}
function stoppedContainersFilter(containers: DockerContainer[]) {
return containers.filter((container) => container.Status === 'exited').length;
}
function healthyContainersFilter(containers: DockerContainer[]) {
return containers.filter((container) => container.Status === 'healthy')
.length;
}
function unhealthyContainersFilter(containers: DockerContainer[]) {
return containers.filter((container) => container.Status === 'unhealthy')
.length;
}

View File

@ -0,0 +1,18 @@
import { humanize } from '@/portainer/filters/filters';
interface Props {
imagesTotalSize: number;
}
export function useImagesTotalSizeComponent(imagesTotalSize: number) {
return <ImagesTotalSize imagesTotalSize={imagesTotalSize} />;
}
export function ImagesTotalSize({ imagesTotalSize }: Props) {
return (
<div>
<i className="fa fa-chart-pie space-right" />
{humanize(imagesTotalSize)}
</div>
);
}