Skip to content

Commit

Permalink
[8.17] [Infra] Fix call to service api (#203451) (#203576)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.17`:
- [[Infra] Fix call to service api
(#203451)](#203451)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Carlos
Crespo","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-12-10T13:33:32Z","message":"[Infra]
Fix call to service api (#203451)\n\nfixes
[203389](https://github.com/elastic/kibana/issues/203389)\r\n##
Summary\r\n\r\nFix the call to `/api/infra/services` when using a
relative date
range\r\n\r\n\r\n![service_api_relative_date_range](https://github.com/user-attachments/assets/772bba2c-07c8-4031-8d8a-61bdc7ab6d70)\r\n\r\n\r\n###
How to test\r\n- Navigate to host detail view, and change the data
picker to use\r\nrelative dates\r\n- Click on
Submit","sha":"1a20fda7021fa49d054c19d4757592dda385f5de","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","ci:project-deploy-observability","Team:obs-ux-infra_services","backport:version","v8.17.0","v8.16.2"],"title":"[Infra]
Fix call to service
api","number":203451,"url":"https://github.com/elastic/kibana/pull/203451","mergeCommit":{"message":"[Infra]
Fix call to service api (#203451)\n\nfixes
[203389](https://github.com/elastic/kibana/issues/203389)\r\n##
Summary\r\n\r\nFix the call to `/api/infra/services` when using a
relative date
range\r\n\r\n\r\n![service_api_relative_date_range](https://github.com/user-attachments/assets/772bba2c-07c8-4031-8d8a-61bdc7ab6d70)\r\n\r\n\r\n###
How to test\r\n- Navigate to host detail view, and change the data
picker to use\r\nrelative dates\r\n- Click on
Submit","sha":"1a20fda7021fa49d054c19d4757592dda385f5de"}},"sourceBranch":"main","suggestedTargetBranches":["8.17","8.16"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/203451","number":203451,"mergeCommit":{"message":"[Infra]
Fix call to service api (#203451)\n\nfixes
[203389](https://github.com/elastic/kibana/issues/203389)\r\n##
Summary\r\n\r\nFix the call to `/api/infra/services` when using a
relative date
range\r\n\r\n\r\n![service_api_relative_date_range](https://github.com/user-attachments/assets/772bba2c-07c8-4031-8d8a-61bdc7ab6d70)\r\n\r\n\r\n###
How to test\r\n- Navigate to host detail view, and change the data
picker to use\r\nrelative dates\r\n- Click on
Submit","sha":"1a20fda7021fa49d054c19d4757592dda385f5de"}},{"branch":"8.17","label":"v8.17.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.16","label":"v8.16.2","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Carlos Crespo <[email protected]>
  • Loading branch information
kibanamachine and crespocarlos authored Dec 10, 2024
1 parent b76f7e9 commit 0f232c3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FormattedMessage } from '@kbn/i18n-react';
import type { TimeRange } from '@kbn/es-query';
import { useLinkProps } from '@kbn/observability-shared-plugin/public';
import { decodeOrThrow } from '@kbn/io-ts-utils';
import { useTimeRange } from '../../../../hooks/use_time_range';
import { ServicesAPIResponseRT } from '../../../../../common/http_api';
import { isPending, useFetcher } from '../../../../hooks/use_fetcher';
import { Section } from '../../components/section';
Expand Down Expand Up @@ -44,13 +45,18 @@ export const ServicesContent = ({
app: 'apm',
pathname: '/onboarding',
});

const parsedDateRange = useTimeRange({
rangeFrom: dateRange.from,
rangeTo: dateRange.to,
});

const params = useMemo(
() => ({
filters: { [HOST_NAME_FIELD]: hostName },
from: dateRange.from,
to: dateRange.to,
...parsedDateRange,
}),
[hostName, dateRange.from, dateRange.to]
[hostName, parsedDateRange]
);

const query = useMemo(() => ({ ...params, filters: JSON.stringify(params.filters) }), [params]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 { renderHook } from '@testing-library/react-hooks';
import { useTimeRange } from './use_time_range';
import * as datemath from '../utils/datemath';

jest.mock('../utils/datemath');

describe('useTimeRange', () => {
const mockParseDateRange = datemath.parseDateRange as jest.Mock;

beforeEach(() => {
Date.now = jest.fn(() => new Date(Date.UTC(2021, 0, 1, 12)).valueOf());
});

afterEach(() => {
jest.clearAllMocks();
});

it('returns default timestamps when rangeFrom and rangeTo are not provided', () => {
const { result } = renderHook(() => useTimeRange({}));

const now = Date.now();
const expectedFrom = new Date(now - 15 * 60000).toISOString();
const expectedTo = new Date(now).toISOString();

expect(result.current.from).toBe(expectedFrom);
expect(result.current.to).toBe(expectedTo);
});

it('returns parsed date range when rangeFrom and rangeTo are provided', () => {
const mockFrom = '2021-01-01T00:00:00.000Z';
const mockTo = '2021-01-01T01:00:00.000Z';
mockParseDateRange.mockReturnValue({ from: mockFrom, to: mockTo });

const { result } = renderHook(() => useTimeRange({ rangeFrom: 'now-15m', rangeTo: 'now' }));

expect(result.current.from).toBe(mockFrom);
expect(result.current.to).toBe(mockTo);
});

it('returns default timestamps when parseDateRange returns undefined values', () => {
mockParseDateRange.mockReturnValue({ from: undefined, to: undefined });

const { result } = renderHook(() => useTimeRange({ rangeFrom: 'now-15m', rangeTo: 'now' }));

const now = Date.now();
const expectedFrom = new Date(now - 15 * 60000).toISOString();
const expectedTo = new Date(now).toISOString();

expect(result.current.from).toBe(expectedFrom);
expect(result.current.to).toBe(expectedTo);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 { useMemo } from 'react';
import { parseDateRange } from '../utils/datemath';

const DEFAULT_FROM_IN_MILLISECONDS = 15 * 60000;

const getDefaultTimestamps = () => {
const now = Date.now();

return {
from: new Date(now - DEFAULT_FROM_IN_MILLISECONDS).toISOString(),
to: new Date(now).toISOString(),
};
};

export const useTimeRange = ({ rangeFrom, rangeTo }: { rangeFrom?: string; rangeTo?: string }) => {
const parsedDateRange = useMemo(() => {
const defaults = getDefaultTimestamps();

if (!rangeFrom || !rangeTo) {
return defaults;
}

const { from = defaults.from, to = defaults.to } = parseDateRange({
from: rangeFrom,
to: rangeTo,
});

return { from, to };
}, [rangeFrom, rangeTo]);

return parsedDateRange;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* 2.0.
*/
import createContainer from 'constate';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { buildEsQuery, Filter, fromKueryExpression, TimeRange, type Query } from '@kbn/es-query';
import { Subscription, map, tap } from 'rxjs';
import deepEqual from 'fast-deep-equal';
import useEffectOnce from 'react-use/lib/useEffectOnce';
import { useKibanaQuerySettings } from '@kbn/observability-shared-plugin/public';
import { useTimeRange } from '../../../../hooks/use_time_range';
import { useSearchSessionContext } from '../../../../hooks/use_search_session';
import { parseDateRange } from '../../../../utils/datemath';
import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana';
import { telemetryTimeRangeFormatter } from '../../../../../common/formatters/telemetry_time_range';
import { useMetricsDataViewContext } from '../../../../containers/metrics_source';
Expand All @@ -38,17 +38,6 @@ const buildQuerySubmittedPayload = (
};
};

const DEFAULT_FROM_IN_MILLISECONDS = 15 * 60000;

const getDefaultTimestamps = () => {
const now = Date.now();

return {
from: new Date(now - DEFAULT_FROM_IN_MILLISECONDS).toISOString(),
to: new Date(now).toISOString(),
};
};

export const useUnifiedSearch = () => {
const [error, setError] = useState<Error | null>(null);
const [searchCriteria, setSearch] = useHostsUrlState();
Expand All @@ -57,6 +46,11 @@ export const useUnifiedSearch = () => {
const { services } = useKibanaContextForPlugin();
const kibanaQuerySettings = useKibanaQuerySettings();

const parsedDateRange = useTimeRange({
rangeFrom: searchCriteria.dateRange.from,
rangeTo: searchCriteria.dateRange.to,
});

const {
data: {
query: { filterManager: filterManagerService, queryString: queryStringService },
Expand Down Expand Up @@ -120,14 +114,6 @@ export const useUnifiedSearch = () => {
[onDateRangeChange, updateSearchSessionId]
);

const parsedDateRange = useMemo(() => {
const defaults = getDefaultTimestamps();

const { from = defaults.from, to = defaults.to } = parseDateRange(searchCriteria.dateRange);

return { from, to };
}, [searchCriteria.dateRange]);

const getDateRangeAsTimestamp = useCallback(() => {
const from = new Date(parsedDateRange.from).getTime();
const to = new Date(parsedDateRange.to).getTime();
Expand Down

0 comments on commit 0f232c3

Please sign in to comment.