Skip to content
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
Expand Up @@ -17,7 +17,7 @@ import {
isRootStreamDefinition,
Streams,
} from '@kbn/streams-schema';
import { parseDurationInSeconds } from '../../../../stream_management/data_management/stream_detail_lifecycle/helpers/helpers';
import { parseDurationInSeconds } from '../../../../../util/parse_duration';

const SORTABLE_FIELDS = ['nameSortKey'] as const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
NO_DATA_SHORT_LABEL,
NO_RETENTION_LABEL,
} from './translations';
import { getTimeSizeAndUnitLabel } from '../stream_management/data_management/stream_detail_lifecycle/helpers/format_size_units';
import { getTimeSizeAndUnitLabel } from '../../util/format_size_units';

export function RetentionColumn({
lifecycle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { WiredStreamsStatus } from '@kbn/streams-plugin/public';
import { isDslLifecycle, isIlmLifecycle } from '@kbn/streams-schema';
import type { Direction } from '@elastic/eui';
import type { QualityIndicators } from '@kbn/dataset-quality-plugin/common/types';
import { parseDurationInSeconds } from '../stream_management/data_management/stream_detail_lifecycle/helpers/helpers';
import { parseDurationInSeconds } from '../../util/parse_duration';

const SORTABLE_FIELDS = ['nameSortKey', 'retentionMs'] as const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { IlmPolicyPhases, PhaseName, Streams } from '@kbn/streams-schema';
import { capitalize } from 'lodash';
import moment from 'moment';
import React, { useMemo } from 'react';
import { orderIlmPhases } from '../helpers/helpers';
import { orderIlmPhases } from '../../../../../util/ilm_policy_phases';
import { formatBytes } from '../helpers/format_bytes';
import { useIlmPhasesColorAndDescription } from '../hooks/use_ilm_phases_color_and_description';
import type { StreamAggregations } from '../hooks/use_ingestion_rate';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { EuiFlexItemProps } from '@elastic/eui';
import type { DownsampleStep } from '@kbn/streams-schema/src/models/ingest/lifecycle';
import { splitSizeAndUnits, toMillis } from '../../helpers/format_size_units';
import { splitSizeAndUnits, toMillis } from '../../../../../../util/format_size_units';

interface BaseLifecycleSegment {
grow: EuiFlexItemProps['grow'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { capitalize } from 'lodash';
import { formatBytes } from '../../helpers/format_bytes';
import { LifecyclePhaseButton } from './lifecycle_phase_button';
import { isZeroAge } from '../../helpers/format_size_units';
import { isZeroAge } from '../../../../../../util/format_size_units';

interface BaseLifecyclePhaseProps {
color?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { PreservedTimeUnit } from './time_unit_types';
import { formatMillisecondsInUnit } from './duration_utils';
import { getTimeSizeAndUnitLabel } from '../../helpers/format_size_units';
import { getTimeSizeAndUnitLabel } from '../../../../../../util/format_size_units';

export const getBoundsHelpTextValues = ({
lowerBoundMs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { splitSizeAndUnits, toMillis } from '../../helpers/format_size_units';
import { splitSizeAndUnits, toMillis } from '../../../../../../util/format_size_units';
import type { PreservedTimeUnit } from './time_unit_types';

export interface DoubledDurationResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { getTimeUnitLabel } from '../../helpers/format_size_units';
import { getTimeUnitLabel } from '../../../../../../util/format_size_units';
import type { TimeUnit } from './time_unit_types';

export const TIME_UNIT_OPTIONS: ReadonlyArray<{ value: TimeUnit; text: string }> = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import type { StreamsTimeUnit } from '../../helpers/format_size_units';
import type { StreamsTimeUnit } from '../../../../../../util/format_size_units';

/**
* Time units offered by the UI selectors by default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { getTimeUnitLabel } from '../../helpers/format_size_units';
import { getTimeUnitLabel } from '../../../../../../util/format_size_units';
import type { PreservedTimeUnit, TimeUnit } from './time_unit_types';

export interface TimeUnitSelectOption {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React from 'react';
import { i18n } from '@kbn/i18n';
import { EuiButton } from '@elastic/eui';
import { BaseMetricCard } from '../../common/base_metric_card';
import { getTimeSizeAndUnitLabel } from '../../helpers/format_size_units';
import { getTimeSizeAndUnitLabel } from '../../../../../../util/format_size_units';
import type { useFailureStoreConfig } from '../../hooks/use_failure_store_config';

export const RetentionCard = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import React from 'react';
import { EuiButton } from '@elastic/eui';
import { BaseMetricCard } from '../../common/base_metric_card';
import { getTimeSizeAndUnitLabel } from '../../helpers/format_size_units';
import { getTimeSizeAndUnitLabel } from '../../../../../../util/format_size_units';
import { IlmLink } from '../ilm_link';

export const RetentionCard = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useBoolean } from '@kbn/react-hooks';
import { parseDuration } from '../../helpers/helpers';
import { parseDuration } from '../../../../../../util/parse_duration';

interface Props {
initialValue: IngestStreamLifecycleAll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { DataStreamStats } from './use_data_stream_stats';
import type { LifecyclePhase } from '../common/data_lifecycle/lifecycle_types';
import type { AffectedResource } from '../downsampling/edit_policy_modal/edit_policy_modal';
import { formatBytes } from '../helpers/format_bytes';
import { getILMRatios } from '../helpers/helpers';
import { getILMRatios } from '../../../../../util/ilm_policy_phases';
import { ILM_PHASE_ORDER } from '../downsampling/edit_ilm_phases_flyout/constants';

type IlmPhaseUiMeta = Record<PhaseName, { color: string; description: string }>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { useStreamsAppRouter } from '../../../../../hooks/use_streams_app_router
import { useStreamDetail } from '../../../../../hooks/use_stream_detail';
import { useStreamsPrivileges } from '../../../../../hooks/use_streams_privileges';
import { Row, RowMetadata } from './row';
import { parseDuration } from '../../stream_detail_lifecycle/helpers/helpers';
import { parseDuration } from '../../../../../util/parse_duration';

interface Setting {
invalid: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,96 +5,11 @@
* 2.0.
*/

import { parseDuration, parseDurationInSeconds, orderIlmPhases, getILMRatios } from './helpers';
import type { IlmPolicyPhases } from '@kbn/streams-schema';

describe('helpers', () => {
describe('parseDuration', () => {
it('should parse valid durations correctly', () => {
expect(parseDuration('30d')).toEqual({ value: 30, unit: 'd' });
expect(parseDuration('24h')).toEqual({ value: 24, unit: 'h' });
expect(parseDuration('60m')).toEqual({ value: 60, unit: 'm' });
expect(parseDuration('60s')).toEqual({ value: 60, unit: 's' });
});

it('should handle single digit durations', () => {
expect(parseDuration('1d')).toEqual({ value: 1, unit: 'd' });
expect(parseDuration('5h')).toEqual({ value: 5, unit: 'h' });
expect(parseDuration('9m')).toEqual({ value: 9, unit: 'm' });
expect(parseDuration('3s')).toEqual({ value: 3, unit: 's' });
});

it('should handle large numbers', () => {
expect(parseDuration('365d')).toEqual({ value: 365, unit: 'd' });
expect(parseDuration('8760h')).toEqual({ value: 8760, unit: 'h' });
});

it('should return undefined for invalid formats', () => {
expect(parseDuration('invalid')).toBeUndefined();
expect(parseDuration('30x')).toBeUndefined();
expect(parseDuration('d30')).toBeUndefined();
expect(parseDuration('30')).toBeUndefined();
expect(parseDuration('')).toBeUndefined();
});

it('should return undefined for undefined/empty input', () => {
expect(parseDuration()).toBeUndefined();
expect(parseDuration('')).toBeUndefined();
});

it('should handle zero values', () => {
expect(parseDuration('0d')).toEqual({ value: 0, unit: 'd' });
expect(parseDuration('0h')).toEqual({ value: 0, unit: 'h' });
});
});

describe('parseDurationInSeconds', () => {
describe('Valid conversions', () => {
it('should convert seconds correctly', () => {
expect(parseDurationInSeconds('60s')).toBe(60);
expect(parseDurationInSeconds('1s')).toBe(1);
expect(parseDurationInSeconds('0s')).toBe(0);
});

it('should convert minutes to seconds', () => {
expect(parseDurationInSeconds('1m')).toBe(60);
expect(parseDurationInSeconds('5m')).toBe(300);
expect(parseDurationInSeconds('60m')).toBe(3600);
});

it('should convert hours to seconds', () => {
expect(parseDurationInSeconds('1h')).toBe(3600);
expect(parseDurationInSeconds('2h')).toBe(7200);
expect(parseDurationInSeconds('24h')).toBe(86400);
});

it('should convert days to seconds', () => {
expect(parseDurationInSeconds('1d')).toBe(86400);
expect(parseDurationInSeconds('7d')).toBe(604800);
expect(parseDurationInSeconds('30d')).toBe(2592000);
});
});

describe('Edge cases', () => {
it('should return 0 for invalid durations', () => {
expect(parseDurationInSeconds('invalid')).toBe(0);
expect(parseDurationInSeconds('30x')).toBe(0);
expect(parseDurationInSeconds('')).toBe(0);
expect(parseDurationInSeconds()).toBe(0);
});

it('should handle zero values', () => {
expect(parseDurationInSeconds('0d')).toBe(0);
expect(parseDurationInSeconds('0h')).toBe(0);
expect(parseDurationInSeconds('0m')).toBe(0);
expect(parseDurationInSeconds('0s')).toBe(0);
});
});

// The current implementation returns 0 for invalid units (parseDuration returns undefined)
// so no explicit throw path is practically reachable with external input. Remove throw test.
});
import { getILMRatios, orderIlmPhases } from './ilm_policy_phases';

describe('ilm_policy_phases', () => {
describe('orderIlmPhases', () => {
it('should order phases correctly when all phases are present', () => {
const phases: IlmPolicyPhases = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,7 @@
import type { IlmPolicyDeletePhase, IlmPolicyPhase, IlmPolicyPhases } from '@kbn/streams-schema';
import { last } from 'lodash';

export const parseDuration = (duration: string = '') => {
const result = /^(\d+)([d|m|s|h])$/.exec(duration);
if (!result) return undefined;
return { value: Number(result[1]), unit: result[2] };
};

export function parseDurationInSeconds(duration: string = ''): number {
const parsed = parseDuration(duration);
if (!parsed) {
return 0;
}

const { value, unit } = parsed;
if (unit === 's') {
return value;
} else if (unit === 'm') {
return value * 60;
} else if (unit === 'h') {
return value * 60 * 60;
} else if (unit === 'd') {
return value * 24 * 60 * 60;
}

throw new Error(`Invalid duration unit [${unit}]`);
}
import { parseDurationInSeconds } from './parse_duration';

export function orderIlmPhases(phases: IlmPolicyPhases) {
const isPhase = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { parseDuration, parseDurationInSeconds } from './parse_duration';

describe('parseDuration', () => {
it('should parse valid durations correctly', () => {
expect(parseDuration('30d')).toEqual({ value: 30, unit: 'd' });
expect(parseDuration('24h')).toEqual({ value: 24, unit: 'h' });
expect(parseDuration('60m')).toEqual({ value: 60, unit: 'm' });
expect(parseDuration('60s')).toEqual({ value: 60, unit: 's' });
});

it('should handle single digit durations', () => {
expect(parseDuration('1d')).toEqual({ value: 1, unit: 'd' });
expect(parseDuration('5h')).toEqual({ value: 5, unit: 'h' });
expect(parseDuration('9m')).toEqual({ value: 9, unit: 'm' });
expect(parseDuration('3s')).toEqual({ value: 3, unit: 's' });
});

it('should handle large numbers', () => {
expect(parseDuration('365d')).toEqual({ value: 365, unit: 'd' });
expect(parseDuration('8760h')).toEqual({ value: 8760, unit: 'h' });
});

it('should return undefined for invalid formats', () => {
expect(parseDuration('invalid')).toBeUndefined();
expect(parseDuration('30x')).toBeUndefined();
expect(parseDuration('d30')).toBeUndefined();
expect(parseDuration('30')).toBeUndefined();
expect(parseDuration('')).toBeUndefined();
});

it('should return undefined for undefined/empty input', () => {
expect(parseDuration()).toBeUndefined();
expect(parseDuration('')).toBeUndefined();
});

it('should handle zero values', () => {
expect(parseDuration('0d')).toEqual({ value: 0, unit: 'd' });
expect(parseDuration('0h')).toEqual({ value: 0, unit: 'h' });
});
});

describe('parseDurationInSeconds', () => {
describe('Valid conversions', () => {
it('should convert seconds correctly', () => {
expect(parseDurationInSeconds('60s')).toBe(60);
expect(parseDurationInSeconds('1s')).toBe(1);
expect(parseDurationInSeconds('0s')).toBe(0);
});

it('should convert minutes to seconds', () => {
expect(parseDurationInSeconds('1m')).toBe(60);
expect(parseDurationInSeconds('5m')).toBe(300);
expect(parseDurationInSeconds('60m')).toBe(3600);
});

it('should convert hours to seconds', () => {
expect(parseDurationInSeconds('1h')).toBe(3600);
expect(parseDurationInSeconds('2h')).toBe(7200);
expect(parseDurationInSeconds('24h')).toBe(86400);
});

it('should convert days to seconds', () => {
expect(parseDurationInSeconds('1d')).toBe(86400);
expect(parseDurationInSeconds('7d')).toBe(604800);
expect(parseDurationInSeconds('30d')).toBe(2592000);
});
});

describe('Edge cases', () => {
it('should return 0 for invalid durations', () => {
expect(parseDurationInSeconds('invalid')).toBe(0);
expect(parseDurationInSeconds('30x')).toBe(0);
expect(parseDurationInSeconds('')).toBe(0);
expect(parseDurationInSeconds()).toBe(0);
});

it('should handle zero values', () => {
expect(parseDurationInSeconds('0d')).toBe(0);
expect(parseDurationInSeconds('0h')).toBe(0);
expect(parseDurationInSeconds('0m')).toBe(0);
expect(parseDurationInSeconds('0s')).toBe(0);
});
});
});
Loading
Loading