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

tighten TraceArchive type to more strictly enforce correct state #623

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -16,7 +16,13 @@ import { createActions, handleActions, ActionFunctionAny } from 'redux-actions';

import { archiveTrace } from '../../../actions/jaeger-api';
import { ApiError } from '../../../types/api-error';
import { TracesArchive } from '../../../types/archive';
import {
AcknowledgedTraceArchive,
ErrorTraceArchive,
LoadingTraceArchive,
TraceArchive,
TracesArchive,
} from '../../../types/archive';
import generateActionTypes from '../../../utils/generate-action-types';

type ArchiveAction = {
Expand All @@ -39,35 +45,45 @@ export const actions: { [actionType: string]: ActionFunctionAny<any> } = {
archiveTrace,
};

function acknowledge(state: TracesArchive, { payload }: ArchiveAction) {
function isLoading(traceArchive: TraceArchive): traceArchive is LoadingTraceArchive {
return traceArchive && 'isLoading' in traceArchive && traceArchive.isLoading;
}

function acknowledge(state: TracesArchive, { payload }: ArchiveAction): TracesArchive {
const traceID = typeof payload === 'string' ? payload : null;
if (!traceID) {
// make flow happy
throw new Error('Invalid state, missing traceID for archive acknowledge');
}
const traceArchive = state[traceID];
if (traceArchive && traceArchive.isLoading) {
if (isLoading(traceArchive)) {
// acknowledgement during loading is invalid (should not happen)
return state;
}
const next = { ...traceArchive, isAcknowledged: true };

const next: AcknowledgedTraceArchive = { ...traceArchive, isAcknowledged: true };
return { ...state, [traceID]: next };
}

function archiveStarted(state: TracesArchive, { meta }: ArchiveAction) {
function archiveStarted(state: TracesArchive, { meta }: ArchiveAction): TracesArchive {
return { ...state, [meta.id]: { isLoading: true } };
}

function archiveDone(state: TracesArchive, { meta }: ArchiveAction) {
function archiveDone(state: TracesArchive, { meta }: ArchiveAction): TracesArchive {
return { ...state, [meta.id]: { isArchived: true, isAcknowledged: false } };
}

function archiveErred(state: TracesArchive, { meta, payload }: ArchiveAction) {
function archiveErred(state: TracesArchive, { meta, payload }: ArchiveAction): TracesArchive {
if (!payload) {
// make flow happy
throw new Error('Invalid state, missing API error details');
}
const traceArchive = { error: payload, isArchived: false, isError: true, isAcknowledged: false };
const traceArchive: ErrorTraceArchive = {
error: payload,
isArchived: false,
isError: true,
isAcknowledged: false,
};
return { ...state, [meta.id]: traceArchive };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ function getNextNotifiedState(props: Props) {
if (!archivedState) {
return null;
}
if (archivedState.isLoading) {
if ('isLoading' in archivedState && archivedState.isLoading) {
return ENotifiedState.Progress;
}
return archivedState.isAcknowledged ? null : ENotifiedState.Outcome;
return 'isAcknowledged' in archivedState && archivedState.isAcknowledged ? null : ENotifiedState.Outcome;
}

function updateNotification(oldState: ENotifiedState | null, nextState: ENotifiedState | null, props: Props) {
Expand All @@ -65,7 +65,7 @@ function updateNotification(oldState: ENotifiedState | null, nextState: ENotifie
}
const { acknowledge, archivedState } = props;
if (nextState === ENotifiedState.Outcome) {
if (archivedState && archivedState.error) {
if (archivedState && 'error' in archivedState) {
const { error } = archivedState;
notification.warn({
key: ENotifiedState.Outcome,
Expand All @@ -76,7 +76,7 @@ function updateNotification(oldState: ENotifiedState | null, nextState: ENotifie
icon: <Icon type="clock-circle-o" className="ArchiveNotifier--errorIcon" />,
onClose: acknowledge,
});
} else if (archivedState && archivedState.isArchived) {
} else if (archivedState && 'isArchived' in archivedState && archivedState.isArchived) {
notification.success({
key: ENotifiedState.Outcome,
description: null,
Expand Down
17 changes: 11 additions & 6 deletions packages/jaeger-ui/src/types/archive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

import { ApiError } from './api-error';

export type TraceArchive = {
isLoading?: boolean;
isArchived?: boolean;
isError?: boolean;
error?: ApiError;
isAcknowledged?: boolean;
export type LoadingTraceArchive = { isLoading: true };
export type SuccessfulTraceArchive = { isAcknowledged: false; isArchived: true };
export type ErrorTraceArchive = { error: ApiError; isAcknowledged: false; isArchived: false; isError: true };
export type AcknowledgedTraceArchive = Omit<SuccessfulTraceArchive | ErrorTraceArchive, 'isAcknowledged'> & {
isAcknowledged: true;
};

export type TraceArchive =
| LoadingTraceArchive
| SuccessfulTraceArchive
| ErrorTraceArchive
| AcknowledgedTraceArchive;

export type TracesArchive = Record<string, TraceArchive>;