Skip to content

Commit

Permalink
feat(ui): add a filter for auto sync (argoproj#11357)
Browse files Browse the repository at this point in the history
Signed-off-by: emirot <emirot.nolan@gmail.com>
  • Loading branch information
alexef authored and emirot committed Jan 27, 2023
1 parent 722a8e5 commit dcf3d87
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {useData, Checkbox} from 'argo-ui/v2';
import * as minimatch from 'minimatch';
import * as React from 'react';
import {Context} from '../../../shared/context';
import {Application, ApplicationDestination, Cluster, HealthStatusCode, HealthStatuses, SyncStatusCode, SyncStatuses} from '../../../shared/models';
import {Application, ApplicationDestination, Cluster, HealthStatusCode, HealthStatuses, SyncPolicy, SyncStatusCode, SyncStatuses} from '../../../shared/models';
import {AppsListPreferences, services} from '../../../shared/services';
import {Filter, FiltersGroup} from '../filter/filter';
import * as LabelSelector from '../label-selector';
Expand All @@ -11,6 +11,7 @@ import {ComparisonStatusIcon, HealthStatusIcon} from '../utils';
export interface FilterResult {
repos: boolean;
sync: boolean;
autosync: boolean;
health: boolean;
namespaces: boolean;
clusters: boolean;
Expand All @@ -22,12 +23,20 @@ export interface FilteredApp extends Application {
filterResult: FilterResult;
}

function getAutoSyncStatus(syncPolicy?: SyncPolicy) {
if (!syncPolicy || !syncPolicy.automated) {
return 'Disabled';
}
return 'Enabled';
}

export function getFilterResults(applications: Application[], pref: AppsListPreferences): FilteredApp[] {
return applications.map(app => ({
...app,
filterResult: {
repos: pref.reposFilter.length === 0 || pref.reposFilter.includes(app.spec.source.repoURL),
sync: pref.syncFilter.length === 0 || pref.syncFilter.includes(app.status.sync.status),
autosync: pref.autoSyncFilter.length === 0 || pref.autoSyncFilter.includes(getAutoSyncStatus(app.spec.syncPolicy)),
health: pref.healthFilter.length === 0 || pref.healthFilter.includes(app.status.health.status),
namespaces: pref.namespacesFilter.length === 0 || pref.namespacesFilter.some(ns => app.spec.destination.namespace && minimatch(app.spec.destination.namespace, ns)),
favourite: !pref.showFavorites || (pref.favoritesAppList && pref.favoritesAppList.includes(app.metadata.name)),
Expand Down Expand Up @@ -241,6 +250,32 @@ const FavoriteFilter = (props: AppFilterProps) => {
);
};

function getAutoSyncOptions(apps: FilteredApp[]) {
const counts = getCounts(apps, 'autosync', app => getAutoSyncStatus(app.spec.syncPolicy), ['Enabled', 'Disabled']);
return [
{
label: 'Enabled',
icon: <i className='fa fa-circle-play' />,
count: counts.get('Enabled')
},
{
label: 'Disabled',
icon: <i className='fa fa-ban' />,
count: counts.get('Disabled')
}
];
}

const AutoSyncFilter = (props: AppFilterProps) => (
<Filter
label='AUTO SYNC'
selected={props.pref.autoSyncFilter}
setSelected={s => props.onChange({...props.pref, autoSyncFilter: s})}
options={getAutoSyncOptions(props.apps)}
collapsed={props.collapsed || false}
/>
);

export const ApplicationsFilter = (props: AppFilterProps) => {
return (
<FiltersGroup content={props.children} collapsed={props.collapsed}>
Expand All @@ -251,6 +286,7 @@ export const ApplicationsFilter = (props: AppFilterProps) => {
<ProjectFilter {...props} />
<ClusterFilter {...props} />
<NamespaceFilter {...props} />
<AutoSyncFilter {...props} collapsed={true} />
</FiltersGroup>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ const ViewPref = ({children}: {children: (pref: AppsListPreferences & {page: num
.split(',')
.filter(item => !!item);
}
if (params.get('autoSync') != null) {
viewPref.autosyncFilter = params
.get('autoSync')
.split(',')
.filter(item => !!item);
}
if (params.get('health') != null) {
viewPref.healthFilter = params
.get('health')
Expand Down Expand Up @@ -332,6 +338,7 @@ export const ApplicationsList = (props: RouteComponentProps<{}>) => {
{
proj: newPref.projectsFilter.join(','),
sync: newPref.syncFilter.join(','),
autoSync: newPref.autoSyncFilter.join(','),
health: newPref.healthFilter.join(','),
namespace: newPref.namespacesFilter.join(','),
cluster: newPref.clustersFilter.join(','),
Expand Down
3 changes: 2 additions & 1 deletion ui/src/app/applications/components/filter/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface FilterProps {
retry?: () => void;
loading?: boolean;
radio?: boolean;
collapsed?: boolean;
}

export interface CheckboxOption {
Expand Down Expand Up @@ -77,7 +78,7 @@ export const Filter = (props: FilterProps) => {
const [values, setValues] = React.useState(init);
const [tags, setTags] = React.useState([]);
const [input, setInput] = React.useState('');
const [collapsed, setCollapsed] = React.useState(false);
const [collapsed, setCollapsed] = React.useState(props.collapsed || false);
const [options, setOptions] = React.useState(props.options);

React.useEffect(() => {
Expand Down
3 changes: 3 additions & 0 deletions ui/src/app/shared/services/view-preferences-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ export class AppsListPreferences {
pref.projectsFilter = [];
pref.reposFilter = [];
pref.syncFilter = [];
pref.autoSyncFilter = [];
pref.showFavorites = false;
}

public labelsFilter: string[];
public projectsFilter: string[];
public reposFilter: string[];
public syncFilter: string[];
public autoSyncFilter: string[];
public healthFilter: string[];
public namespacesFilter: string[];
public clustersFilter: string[];
Expand Down Expand Up @@ -127,6 +129,7 @@ const DEFAULT_PREFERENCES: ViewPreferences = {
clustersFilter: new Array<string>(),
reposFilter: new Array<string>(),
syncFilter: new Array<string>(),
autoSyncFilter: new Array<string>(),
healthFilter: new Array<string>(),
hideFilters: false,
showFavorites: false,
Expand Down

0 comments on commit dcf3d87

Please sign in to comment.