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

Url params #371

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
03c986b
Pages Params on Log Table
Koustavd18 Nov 6, 2024
053d4c5
Upgrade Logs Loader Hook to use useQuery
Koustavd18 Nov 7, 2024
164b2ad
merge main
Koustavd18 Nov 7, 2024
ef11b0c
Pages params added
Koustavd18 Nov 11, 2024
8803f9f
Merge branch 'main' into urlParams
Koustavd18 Nov 11, 2024
de04132
Fields Params:Clean up
Koustavd18 Nov 11, 2024
fc8179b
offset from page number
Koustavd18 Nov 11, 2024
aa7ac92
Display accurate Page number as shown in UI
Koustavd18 Nov 11, 2024
50ea54b
Clean Up
Koustavd18 Nov 11, 2024
1e5bea4
URL params : Page, fields
Koustavd18 Nov 12, 2024
05a39b3
Conflicts resolved
Koustavd18 Nov 12, 2024
e0e26fc
Handling edge cases
Koustavd18 Nov 13, 2024
849c5bb
removed extra query call
Koustavd18 Nov 13, 2024
0a74a0a
highlight selected page
Koustavd18 Nov 13, 2024
1e438d3
clean up
Koustavd18 Nov 13, 2024
b792f74
removed package lock
Koustavd18 Nov 13, 2024
8a464a9
Merge branch 'main' into urlParams
Koustavd18 Nov 13, 2024
82c70e0
Edge case handled
Koustavd18 Nov 13, 2024
802e337
Handled time range
Koustavd18 Nov 14, 2024
7f8f7ed
Merge branch 'main' into urlParams
Koustavd18 Nov 14, 2024
4171890
Set initial on offset change
Koustavd18 Nov 14, 2024
4502b59
Merge branch 'main' into urlParams
Koustavd18 Nov 27, 2024
0660b40
Merge branch 'main' into urlParams
Koustavd18 Nov 28, 2024
cdfe4b9
Merge branch 'main' into urlParams
Koustavd18 Nov 30, 2024
d2c46c1
Filter query offset handled
Koustavd18 Dec 16, 2024
da0186b
removed unused code
Koustavd18 Dec 16, 2024
d4f0d67
Merge with main
Koustavd18 Dec 16, 2024
e1d7f70
Merge branch 'main' into urlParams
Koustavd18 Dec 18, 2024
53352ce
Build Fix
Koustavd18 Dec 18, 2024
3177eba
Filter deletion
Koustavd18 Dec 18, 2024
6610e8a
Rebase
Koustavd18 Dec 18, 2024
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
35 changes: 15 additions & 20 deletions src/pages/Stream/Views/Explore/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useCallback } from 'react';
import { FC, useCallback, useEffect } from 'react';
import { useLogsStore, logsStoreReducers, LOAD_LIMIT, LOG_QUERY_LIMITS } from '../../providers/LogsProvider';
import { usePagination } from '@mantine/hooks';
import { Box, Center, Group, Loader, Menu, Pagination, Stack, Tooltip } from '@mantine/core';
Expand Down Expand Up @@ -96,7 +96,19 @@ const Footer = (props: { loaded: boolean; hasNoData: boolean; isFetchingCount: b
setLogsStore((store) => setPageAndPageData(store, page));
}, []);

const pagination = usePagination({ total: totalPages ?? 1, initialPage: 1, onChange: onPageChange });
useEffect(() => {
const initialPageFromUrl = currentPage % Math.ceil(currentOffset / perPage);
if (!initialPageFromUrl) return;

pagination.setPage(currentPage % Math.ceil(currentOffset / perPage));
}, [currentOffset, perPage]);

const pagination = usePagination({
total: totalPages ?? 1,
initialPage: 1,
onChange: onPageChange,
});

const onChangeOffset = useCallback(
(key: 'prev' | 'next') => {
if (key === 'prev') {
Expand Down Expand Up @@ -131,7 +143,7 @@ const Footer = (props: { loaded: boolean; hasNoData: boolean; isFetchingCount: b
total={totalPages}
value={currentPage}
onChange={(page) => {
pagination.setPage(page);
pagination && pagination.setPage(page);
}}
size="sm">
<Group gap={5} justify="center">
Expand Down Expand Up @@ -171,23 +183,6 @@ const Footer = (props: { loaded: boolean; hasNoData: boolean; isFetchingCount: b
) : null}
</Stack>
<Stack w="100%" align="flex-end" style={{ flexDirection: 'row', justifyContent: 'flex-end' }}>
{/* {props.loaded && (
<Menu position="top">
<Menu.Target>
<div>
<IconButton renderIcon={renderExportIcon} />
</div>
</Menu.Target>
<Menu.Dropdown>
<Menu.Item onClick={() => exportHandler('CSV')} style={{ padding: '0.5rem 2.25rem 0.5rem 0.75rem' }}>
CSV
</Menu.Item>
<Menu.Item onClick={() => exportHandler('JSON')} style={{ padding: '0.5rem 2.25rem 0.5rem 0.75rem' }}>
JSON
</Menu.Item>
</Menu.Dropdown>
</Menu>
)} */}
<LimitControl />
</Stack>
</Stack>
Expand Down
39 changes: 37 additions & 2 deletions src/pages/Stream/Views/Explore/LogsView.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Box } from '@mantine/core';
import { useLogsStore } from '../../providers/LogsProvider';
import { useLogsStore, logsStoreReducers } from '../../providers/LogsProvider';
import JsonView from './JSONView';
import LogTable from './StaticLogTable';
import useLogsFetcher from './useLogsFetcher';
import LogsViewConfig from './LogsViewConfig';
import _ from 'lodash';
import { getOffset } from '@/utils';

import { useEffect } from 'react';

const { setPageAndPageData, setTargetPage, setTargetColumns, setDisabledColumns, setCurrentOffset } = logsStoreReducers;

const LogsView = (props: { schemaLoading: boolean; infoLoading: boolean }) => {
const { schemaLoading, infoLoading } = props;
Expand All @@ -13,7 +18,9 @@ const LogsView = (props: { schemaLoading: boolean; infoLoading: boolean }) => {
infoLoading,
});

const [viewMode] = useLogsStore((store) => store.viewMode);
const [tableOpts] = useLogsStore((store) => store.tableOpts);
const { currentPage, targetPage, headers, targetColumns, perPage } = tableOpts;
const [viewMode, setLogsStore] = useLogsStore((store) => store.viewMode);
const viewOpts = {
errorMessage,
hasNoData,
Expand All @@ -22,6 +29,34 @@ const LogsView = (props: { schemaLoading: boolean; infoLoading: boolean }) => {
logsLoading,
};

useEffect(() => {
if (!showTable) return;
if (targetPage) {
const offset = getOffset(targetPage, perPage);
if (offset > 0) {
setLogsStore((store) => setCurrentOffset(store, offset));
setLogsStore((store) => setTargetPage(store, targetPage - Math.ceil(offset / perPage)));
}
setLogsStore((store) => setPageAndPageData(store, targetPage));
if (currentPage === targetPage) {
setLogsStore((store) => setTargetPage(store, undefined));
}
}
}, [showTable, currentPage]);

useEffect(() => {
if (!showTable) return;
if (!_.isEmpty(targetColumns)) {
setLogsStore((store) =>
setDisabledColumns(
store,
headers.filter((el) => !targetColumns.includes(el)),
),
);
setLogsStore((store) => setTargetColumns(store, []));
}
}, [headers]);

return (
<Box style={{ display: 'flex', flex: 1, overflow: 'hidden' }}>
{viewMode === 'table' && (
Expand Down
36 changes: 28 additions & 8 deletions src/pages/Stream/hooks/useParamsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { TimeRange, useLogsStore, logsStoreReducers } from '@/pages/Stream/provi
import { useSearchParams } from 'react-router-dom';
import _ from 'lodash';
import { FIXED_DURATIONS } from '@/constants/timeConstants';
import { LOG_QUERY_LIMITS } from '@/pages/Stream/providers/LogsProvider';
import { LOG_QUERY_LIMITS, columnsToSkip } from '@/pages/Stream/providers/LogsProvider';
import dayjs from 'dayjs';
import timeRangeUtils from '@/utils/timeRangeUtils';
import moment from 'moment-timezone';
import { filterStoreReducers, QueryType, useFilterStore } from '../providers/FilterProvider';
import { generateQueryBuilderASTFromSQL } from '../utils';
import { joinOrSplit } from '@/utils';

const { getRelativeStartAndEndDate, formatDateWithTimezone, getLocalTimezone } = timeRangeUtils;
const { setTimeRange, onToggleView, setPerPage, setCustQuerySearchState } = logsStoreReducers;
const { setTimeRange, onToggleView, setPerPage, setCustQuerySearchState, setTargetPage, setTargetColumns } =
logsStoreReducers;
const { applySavedFilters } = filterStoreReducers;
const timeRangeFormat = 'DD-MMM-YYYY_HH-mmz';
const keys = ['view', 'rows', 'interval', 'from', 'to', 'query', 'filterType'];
const keys = ['view', 'rows', 'page', 'interval', 'from', 'to', 'query', 'filterType', 'fields'];

const dateToParamString = (date: Date) => {
return formatDateWithTimezone(date, timeRangeFormat);
Expand Down Expand Up @@ -58,8 +60,10 @@ const storeToParamsObj = (opts: {
rows: string;
query: string;
filterType: string;
fields: string;
}): Record<string, string> => {
const { timeRange, offset, page, view, rows, query, filterType } = opts;
const { timeRange, offset, page, view, rows, query, filterType, fields } = opts;

const params: Record<string, string> = {
...deriveTimeRangeParams(timeRange),
view,
Expand All @@ -68,6 +72,7 @@ const storeToParamsObj = (opts: {
page,
query,
filterType: query ? filterType : '',
fields,
};
return _.pickBy(params, (val, key) => !_.isEmpty(val) && _.includes(keys, key));
};
Expand All @@ -91,19 +96,24 @@ const useParamsController = () => {
const [timeRange, setLogsStore] = useLogsStore((store) => store.timeRange);
const [, setFilterStore] = useFilterStore((store) => store);

const { currentOffset, currentPage, perPage } = tableOpts;
const { currentOffset, currentPage, targetPage, perPage, headers, disabledColumns, targetColumns } = tableOpts;

const visibleHeaders = headers.filter((el) => !columnsToSkip.includes(el));

const activeHeaders = visibleHeaders.filter((el) => !disabledColumns.includes(el));
const [searchParams, setSearchParams] = useSearchParams();
const pageOffset = Math.ceil(currentOffset / perPage);

useEffect(() => {
const storeAsParams = storeToParamsObj({
timeRange,
offset: `${currentOffset}`,
page: `${currentPage}`,
page: `${targetPage ? targetPage : Math.ceil(currentPage + pageOffset)}`,
view: viewMode,
rows: `${perPage}`,
query: custQuerySearchState.custSearchQuery,
filterType: custQuerySearchState.viewMode,
fields: `${joinOrSplit(!_.isEmpty(targetColumns) ? targetColumns : activeHeaders)}`,
});
const presentParams = paramsStringToParamsObj(searchParams);
if (['table', 'json'].includes(presentParams.view) && presentParams.view !== storeAsParams.view) {
Expand All @@ -113,13 +123,21 @@ const useParamsController = () => {
setLogsStore((store) => setPerPage(store, _.toNumber(presentParams.rows)));
}

if (storeAsParams.page !== presentParams.page) {
setLogsStore((store) => setTargetPage(store, _.toNumber(presentParams.page)));
}

if (storeAsParams.query !== presentParams.query) {
setLogsStore((store) => setCustQuerySearchState(store, presentParams.query, presentParams.filterType));
if (presentParams.filterType === 'filters')
setFilterStore((store) =>
applySavedFilters(store, generateQueryBuilderASTFromSQL(presentParams.query) as QueryType),
);
}

if (storeAsParams.fields !== presentParams.fields) {
setLogsStore((store) => setTargetColumns(store, joinOrSplit(presentParams.fields) as string[]));
}
syncTimeRangeToStore(storeAsParams, presentParams);
setStoreSynced(true);
}, []);
Expand All @@ -129,11 +147,12 @@ const useParamsController = () => {
const storeAsParams = storeToParamsObj({
timeRange,
offset: `${currentOffset}`,
page: `${currentPage}`,
page: `${targetPage ? targetPage : Math.ceil(currentPage + pageOffset)}`,
view: viewMode,
rows: `${perPage}`,
query: custQuerySearchState.custSearchQuery,
filterType: custQuerySearchState.viewMode,
fields: `${joinOrSplit(!_.isEmpty(targetColumns) ? targetColumns : activeHeaders)}`,
});
const presentParams = paramsStringToParamsObj(searchParams);
if (_.isEqual(storeAsParams, presentParams)) return;
Expand All @@ -147,11 +166,12 @@ const useParamsController = () => {
const storeAsParams = storeToParamsObj({
timeRange,
offset: `${currentOffset}`,
page: `${currentPage}`,
page: `${targetPage ? targetPage : Math.ceil(currentPage + pageOffset)}`,
view: viewMode,
rows: `${perPage}`,
query: custQuerySearchState.custSearchQuery,
filterType: custQuerySearchState.viewMode,
fields: `${joinOrSplit(!_.isEmpty(targetColumns) ? targetColumns : activeHeaders)}`,
});
const presentParams = paramsStringToParamsObj(searchParams);

Expand Down
26 changes: 26 additions & 0 deletions src/pages/Stream/providers/LogsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,14 @@ type LogsStore = {
tableOpts: {
disabledColumns: string[];
wrapDisabledColumns: string[];
targetColumns: string[];
pinnedColumns: string[];
pageData: Log[];
totalPages: number;
totalCount: number;
displayedCount: number;
currentPage: number;
targetPage: number | undefined;
perPage: number;
currentOffset: number;
headers: string[];
Expand Down Expand Up @@ -265,6 +267,7 @@ type LogsStoreReducers = {
setCurrentOffset: (store: LogsStore, offset: number) => ReducerOutput;
setPerPage: (store: LogsStore, perPage: number) => ReducerOutput;
setCurrentPage: (store: LogsStore, page: number) => ReducerOutput;
setTargetPage: (store: LogsStore, target: number | undefined) => ReducerOutput;
setTotalCount: (store: LogsStore, totalCount: number) => ReducerOutput;
setPageAndPageData: (store: LogsStore, pageNo: number, perPage?: number) => ReducerOutput;
setAndSortData: (store: LogsStore, sortKey: string, sortOrder: 'asc' | 'desc') => ReducerOutput;
Expand Down Expand Up @@ -293,6 +296,7 @@ type LogsStoreReducers = {
onToggleView: (store: LogsStore, viewMode: 'json' | 'table') => ReducerOutput;
toggleConfigViewType: (store: LogsStore) => ReducerOutput;
setDisabledColumns: (store: LogsStore, columns: string[]) => ReducerOutput;
setTargetColumns: (store: LogsStore, columms: string[]) => ReducerOutput;
setOrderedHeaders: (store: LogsStore, columns: string[]) => ReducerOutput;
toggleWordWrap: (store: LogsStore) => ReducerOutput;
};
Expand All @@ -317,6 +321,7 @@ const initialState: LogsStore = {
},

tableOpts: {
targetColumns: [],
disabledColumns: [],
wrapDisabledColumns: [],
pinnedColumns: [],
Expand All @@ -326,6 +331,7 @@ const initialState: LogsStore = {
displayedCount: 0,
totalPages: 0,
currentPage: 0,
targetPage: undefined,
currentOffset: 0,
headers: [],
orderedHeaders: [],
Expand Down Expand Up @@ -532,6 +538,15 @@ const setDisabledColumns = (store: LogsStore, columns: string[]) => {
};
};

const setTargetColumns = (store: LogsStore, columns: string[]) => {
return {
tableOpts: {
...store.tableOpts,
targetColumns: columns,
},
};
};

const togglePinnedColumns = (store: LogsStore, columnName: string) => {
const { tableOpts } = store;
return {
Expand Down Expand Up @@ -634,6 +649,15 @@ const setPerPage = (store: LogsStore, perPage: number) => {
};
};

const setTargetPage = (store: LogsStore, target: number | undefined) => {
return {
tableOpts: {
...store.tableOpts,
targetPage: target ? target : undefined,
},
};
};

const setCurrentPage = (store: LogsStore, currentPage: number) => {
return {
tableOpts: {
Expand Down Expand Up @@ -994,6 +1018,7 @@ const logsStoreReducers: LogsStoreReducers = {
setStreamSchema,
setPerPage,
setCurrentPage,
setTargetPage,
setCurrentOffset,
setTotalCount,
setPageAndPageData,
Expand All @@ -1015,6 +1040,7 @@ const logsStoreReducers: LogsStoreReducers = {
onToggleView,
toggleConfigViewType,
setDisabledColumns,
setTargetColumns,
setOrderedHeaders,
toggleWordWrap,
toggleWrapDisabledColumns,
Expand Down
14 changes: 14 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,17 @@ export const copyTextToClipboard = async (value: any) => {
return await navigator.clipboard.writeText(JSON.stringify(value, null, 2));
}
};

export const getOffset = (page: number, rowSize: number) => {
const product = page * rowSize;
return Math.floor(product / 1000) * 1000;
};

export const joinOrSplit = (value: string[] | string): string | string[] => {
const joinOperator = ',';
if (Array.isArray(value)) {
return value.join(joinOperator);
} else {
return value.split(joinOperator);
}
};
Loading