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

fix: update potentially-stale status dynamically #4905

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { IFeatureToggleListItem } from 'interfaces/featureToggle';
import { PERMISSION, KILLSWITCH } from 'constants/featureToggleTypes';
import { getDiffInDays, expired, toggleExpiryByTypeMap } from '../utils';
import { subDays, parseISO } from 'date-fns';
import { KILLSWITCH, PERMISSION } from 'constants/featureToggleTypes';
import { expired, getDiffInDays } from '../utils';
import { parseISO, subDays } from 'date-fns';
import { FeatureTypeSchema } from 'openapi';

export const formatExpiredAt = (
feature: IFeatureToggleListItem,
featureTypes: FeatureTypeSchema[],
): string | undefined => {
const { type, createdAt } = feature;

if (type === KILLSWITCH || type === PERMISSION) {
const featureType = featureTypes.find(
(featureType) => featureType.name === type,
);

if (
featureType &&
(featureType.name === KILLSWITCH || featureType.name === PERMISSION)
) {
return;
}

const date = parseISO(createdAt);
const now = new Date();
const diff = getDiffInDays(date, now);

if (expired(diff, type)) {
const result = diff - toggleExpiryByTypeMap[type];
if (featureType && expired(diff, featureType)) {
const result = diff - (featureType?.lifetimeDays?.valueOf() || 0);
return subDays(now, result).toISOString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { IFeatureToggleListItem } from 'interfaces/featureToggle';
import { getDiffInDays, expired } from '../utils';
import { PERMISSION, KILLSWITCH } from 'constants/featureToggleTypes';
import { expired, getDiffInDays } from '../utils';
import { KILLSWITCH, PERMISSION } from 'constants/featureToggleTypes';
import { parseISO } from 'date-fns';
import { FeatureTypeSchema } from 'openapi';

export type ReportingStatus = 'potentially-stale' | 'healthy';

export const formatStatus = (
feature: IFeatureToggleListItem,
featureTypes: FeatureTypeSchema[],
): ReportingStatus => {
const { type, createdAt } = feature;

const featureType = featureTypes.find(
(featureType) => featureType.name === type,
);
const date = parseISO(createdAt);
const now = new Date();
const diff = getDiffInDays(date, now);

if (expired(diff, type) && type !== KILLSWITCH && type !== PERMISSION) {
if (
featureType &&
expired(diff, featureType) &&
type !== KILLSWITCH &&
type !== PERMISSION
) {
return 'potentially-stale';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { SearchHighlightProvider } from 'component/common/Table/SearchHighlightC
import { PageHeader } from 'component/common/PageHeader/PageHeader';
import { sortTypes } from 'utils/sortTypes';
import {
useSortBy,
useFlexLayout,
useGlobalFilter,
useSortBy,
useTable,
useFlexLayout,
} from 'react-table';
import { useMediaQuery, useTheme } from '@mui/material';
import { FeatureSeenCell } from 'component/common/Table/cells/FeatureSeenCell/FeatureSeenCell';
Expand All @@ -29,6 +29,7 @@ import { formatExpiredAt } from './ReportExpiredCell/formatExpiredAt';
import { useConditionallyHiddenColumns } from 'hooks/useConditionallyHiddenColumns';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { FeatureEnvironmentSeenCell } from 'component/common/Table/cells/FeatureSeenCell/FeatureEnvironmentSeenCell';
import useFeatureTypes from 'hooks/api/getters/useFeatureTypes/useFeatureTypes';

interface IReportTableProps {
projectId: string;
Expand Down Expand Up @@ -56,6 +57,7 @@ export const ReportTable = ({ projectId, features }: IReportTableProps) => {
const showEnvironmentLastSeen = Boolean(
uiConfig.flags.lastSeenByEnvironment,
);
const { featureTypes } = useFeatureTypes();

const data: IReportTableRow[] = useMemo<IReportTableRow[]>(
() =>
Expand All @@ -65,10 +67,10 @@ export const ReportTable = ({ projectId, features }: IReportTableProps) => {
type: report.type,
stale: report.stale,
environments: report.environments,
status: formatStatus(report),
status: formatStatus(report, featureTypes),
lastSeenAt: report.lastSeenAt,
createdAt: report.createdAt,
expiredAt: formatExpiredAt(report),
expiredAt: formatExpiredAt(report, featureTypes),
})),
[projectId, features],
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import differenceInDays from 'date-fns/differenceInDays';
import { EXPERIMENT, OPERATIONAL, RELEASE } from 'constants/featureToggleTypes';

const FORTY_DAYS = 40;
const SEVEN_DAYS = 7;

export const toggleExpiryByTypeMap: Record<string, number> = {
[EXPERIMENT]: FORTY_DAYS,
[RELEASE]: FORTY_DAYS,
[OPERATIONAL]: SEVEN_DAYS,
};
import { FeatureTypeSchema } from 'openapi';

export const getDiffInDays = (date: Date, now: Date) => {
return Math.abs(differenceInDays(date, now));
};

export const expired = (diff: number, type: string) => {
return diff >= toggleExpiryByTypeMap[type];
export const expired = (diff: number, type: FeatureTypeSchema) => {
if (type.lifetimeDays) return diff >= type?.lifetimeDays?.valueOf();

return false;
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import useSWR, { mutate, SWRConfiguration } from 'swr';
import { useState, useEffect } from 'react';
import { useEffect, useState } from 'react';
import { formatApiPath } from 'utils/formatPath';
import { IFeatureType } from 'interfaces/featureTypes';
import handleErrorResponses from '../httpErrorResponseHandler';
import { FeatureTypeSchema } from '../../../../openapi';

const useFeatureTypes = (options: SWRConfiguration = {}) => {
const fetcher = async () => {
Expand All @@ -27,7 +27,7 @@ const useFeatureTypes = (options: SWRConfiguration = {}) => {
}, [data, error]);

return {
featureTypes: (data?.types as IFeatureType[]) || [],
featureTypes: (data?.types as FeatureTypeSchema[]) || [],
error,
loading,
refetch,
Expand Down
22 changes: 6 additions & 16 deletions src/lib/services/project-health-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { IUnleashConfig } from '../types/option';
import { Logger } from '../logger';
import type { IProject, IProjectHealthReport } from '../types/model';
import type { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
import type {
IFeatureType,
IFeatureTypeStore,
} from '../types/stores/feature-type-store';
import type { IFeatureTypeStore } from '../types/stores/feature-type-store';
import type { IProjectStore } from '../types/stores/project-store';
import ProjectService from './project-service';
import {
calculateProjectHealth,
calculateHealthRating,
calculateProjectHealth,
} from '../domain/project-health/project-health';

export default class ProjectHealthService {
Expand All @@ -23,8 +20,6 @@ export default class ProjectHealthService {

private featureToggleStore: IFeatureToggleStore;

private featureTypes: IFeatureType[];

private projectService: ProjectService;

constructor(
Expand All @@ -43,17 +38,14 @@ export default class ProjectHealthService {
this.projectStore = projectStore;
this.featureTypeStore = featureTypeStore;
this.featureToggleStore = featureToggleStore;
this.featureTypes = [];

this.projectService = projectService;
}

async getProjectHealthReport(
projectId: string,
): Promise<IProjectHealthReport> {
if (this.featureTypes.length === 0) {
this.featureTypes = await this.featureTypeStore.getAll();
}
const featureTypes = await this.featureTypeStore.getAll();

const overview = await this.projectService.getProjectOverview(
projectId,
Expand All @@ -63,7 +55,7 @@ export default class ProjectHealthService {

const healthRating = calculateProjectHealth(
overview.features,
this.featureTypes,
featureTypes,
);

return {
Expand All @@ -73,16 +65,14 @@ export default class ProjectHealthService {
}

async calculateHealthRating(project: IProject): Promise<number> {
if (this.featureTypes.length === 0) {
this.featureTypes = await this.featureTypeStore.getAll();
}
const featureTypes = await this.featureTypeStore.getAll();

const toggles = await this.featureToggleStore.getAll({
project: project.id,
archived: false,
});

return calculateHealthRating(toggles, this.featureTypes);
return calculateHealthRating(toggles, featureTypes);
}

async setHealthRating(): Promise<void> {
Expand Down