-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Query api #289
Query api #289
Changes from 13 commits
2196f64
2cabd50
60a309b
4954fc2
e855aa8
d6b1a11
de68e81
1f0a8a5
322743e
4fe1672
e025d10
49b523f
d67716d
d2bf1c0
fc1ee1f
76bef80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,19 +11,19 @@ type QueryLogs = { | |
pageOffset: number; | ||
}; | ||
|
||
type QueryParams = Record<string, string>; | ||
|
||
// to optimize performace, it has been decided to round off the time at the given level | ||
// so making the end-time inclusive | ||
const optimizeEndTime = (endTime: Date) => { | ||
return dayjs(endTime).add(1, 'minute').toDate(); | ||
}; | ||
|
||
export const getQueryLogs = (logsQuery: QueryLogs) => { | ||
export const getQueryLogs = (logsQuery: QueryLogs, queryParams?: QueryParams) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should always receive an object. Empty {} if no params. |
||
const { startTime, endTime, streamName, limit, pageOffset } = logsQuery; | ||
|
||
const query = `SELECT * FROM ${streamName} LIMIT ${limit} OFFSET ${pageOffset}`; | ||
|
||
return Axios().post( | ||
LOG_QUERY_URL, | ||
LOG_QUERY_URL(queryParams && queryParams), | ||
{ | ||
query, | ||
startTime, | ||
|
@@ -33,11 +33,11 @@ export const getQueryLogs = (logsQuery: QueryLogs) => { | |
); | ||
}; | ||
|
||
export const getQueryResult = (logsQuery: LogsQuery, query = '') => { | ||
export const getQueryResult = (logsQuery: LogsQuery, query = '', queryParams?: QueryParams) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should always receive an object. Empty {} if no params. |
||
const { startTime, endTime } = logsQuery; | ||
|
||
return Axios().post( | ||
LOG_QUERY_URL, | ||
LOG_QUERY_URL(queryParams && queryParams), | ||
{ | ||
query, | ||
startTime, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ import { useLogsStore, logsStoreReducers, LOAD_LIMIT, isJqSearch } from '@/pages | |
import { useAppStore } from '@/layouts/MainLayout/providers/AppProvider'; | ||
import { useQueryResult } from './useQueryResult'; | ||
import _ from 'lodash'; | ||
import { useStreamStore } from '@/pages/Stream/providers/StreamProvider'; | ||
import { AxiosError } from 'axios'; | ||
import jqSearch from '@/utils/jqSearch'; | ||
|
||
|
@@ -43,7 +42,6 @@ export const useQueryLogs = () => { | |
}, | ||
}); | ||
const [currentStream] = useAppStore((store) => store.currentStream); | ||
const [schema] = useStreamStore((store) => store.schema); | ||
const [ | ||
{ | ||
timeRange, | ||
|
@@ -88,24 +86,28 @@ export const useQueryLogs = () => { | |
setError(null); | ||
|
||
const logsQueryRes = isQuerySearchActive | ||
? await getQueryResult({ ...logsQuery, access: [] }, appendOffsetToQuery(custSearchQuery, logsQuery.pageOffset)) | ||
: await getQueryLogs(logsQuery); | ||
? await getQueryResult( | ||
{ ...logsQuery, access: [] }, | ||
appendOffsetToQuery(custSearchQuery, logsQuery.pageOffset), | ||
{ fields: 'true' }, | ||
) | ||
: await getQueryLogs(logsQuery, { fields: 'true' }); | ||
|
||
const data = logsQueryRes.data; | ||
const data = logsQueryRes.data.records; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a possibility where server could just send you {} or null. |
||
|
||
if (logsQueryRes.status === StatusCodes.OK) { | ||
const jqFilteredData = isJqSearch(instantSearchValue) ? await jqSearch(data, instantSearchValue) : []; | ||
return setLogsStore((store) => setData(store, data, schema, jqFilteredData)); | ||
return setLogsStore((store) => setData(store, data, logsQueryRes.data, jqFilteredData)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
why do both |
||
} | ||
if (typeof data === 'string' && data.includes('Stream is not initialized yet')) { | ||
return setLogsStore((store) => setData(store, [], schema)); | ||
return setLogsStore((store) => setData(store, [], logsQueryRes.data)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you trying to set the data when the stream is not initialized ? |
||
} | ||
setError('Failed to query log'); | ||
} catch (e) { | ||
const axiosError = e as AxiosError; | ||
const errorMessage = axiosError?.response?.data; | ||
setError(_.isString(errorMessage) && !_.isEmpty(errorMessage) ? errorMessage : 'Failed to query log'); | ||
return setLogsStore((store) => setData(store, [], schema)); | ||
return setLogsStore((store) => setData(store, [], null)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explain why's the third argument is null ? |
||
} finally { | ||
setLoading(false); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Log } from '@/@types/parseable/api/query'; | ||
import { LogStreamData, LogStreamSchemaData } from '@/@types/parseable/api/stream'; | ||
import { LogStreamData, LogStreamQueryWithFields, LogStreamSchemaData } from '@/@types/parseable/api/stream'; | ||
import { FIXED_DURATIONS, FixedDuration } from '@/constants/timeConstants'; | ||
import initContext from '@/utils/initContext'; | ||
import dayjs, { Dayjs } from 'dayjs'; | ||
import { addOrRemoveElement } from '@/utils'; | ||
import { getPageSlice, makeHeadersFromSchema, makeHeadersfromData } from '../utils'; | ||
import { getPageSlice, makeHeadersFromQueryFields, makeHeadersfromData } from '../utils'; | ||
import _ from 'lodash'; | ||
import { sanitizeCSVData } from '@/utils/exportHelpers'; | ||
|
||
|
@@ -260,7 +260,12 @@ type LogsStoreReducers = { | |
getCleanStoreForRefetch: (store: LogsStore) => ReducerOutput; | ||
|
||
// data reducers | ||
setData: (store: LogsStore, data: Log[], schema: LogStreamSchemaData | null, jqFilteredData?: Log[]) => ReducerOutput; | ||
setData: ( | ||
store: LogsStore, | ||
data: Log[], | ||
queryResponse: LogStreamQueryWithFields | null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With logs provider context, both deal with almost the same values. Rmv one with less significant. |
||
jqFilteredData?: Log[], | ||
) => ReducerOutput; | ||
setStreamSchema: (store: LogsStore, schema: LogStreamSchemaData) => ReducerOutput; | ||
applyCustomQuery: ( | ||
store: LogsStore, | ||
|
@@ -272,7 +277,7 @@ type LogsStoreReducers = { | |
getUniqueValues: (data: Log[], key: string) => string[]; | ||
makeExportData: (data: Log[], headers: string[], type: string) => Log[]; | ||
setRetention: (store: LogsStore, retention: { description: string; duration: string }) => ReducerOutput; | ||
setTableHeaders: (store: LogsStore, schema: LogStreamSchemaData) => ReducerOutput; | ||
setTableHeaders: (store: LogsStore, queryResponse: LogStreamQueryWithFields) => ReducerOutput; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for setting table headers, why'd you need entire |
||
|
||
setCleanStoreForStreamChange: (store: LogsStore) => ReducerOutput; | ||
updateSavedFilterId: (store: LogsStore, savedFilterId: string | null) => ReducerOutput; | ||
|
@@ -359,7 +364,7 @@ const setTimeRange = ( | |
return { | ||
...cleanStore, | ||
timeRange: { ...store.timeRange, startTime: startTime.toDate(), endTime: endTime.toDate(), label, interval, type }, | ||
viewMode: store.viewMode | ||
viewMode: store.viewMode, | ||
}; | ||
}; | ||
|
||
|
@@ -527,13 +532,13 @@ const searchAndSortData = (opts: { searchValue: string }, data: Log[]) => { | |
return sortedData; | ||
}; | ||
|
||
const setTableHeaders = (store: LogsStore, schema: LogStreamSchemaData) => { | ||
const setTableHeaders = (store: LogsStore, queryResponse: LogStreamQueryWithFields) => { | ||
const { data: existingData, custQuerySearchState, tableOpts } = store; | ||
const { filteredData } = existingData; | ||
const newHeaders = | ||
filteredData && custQuerySearchState.isQuerySearchActive | ||
? makeHeadersfromData(filteredData) | ||
: makeHeadersFromSchema(schema); | ||
: makeHeadersFromQueryFields(queryResponse); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's straight forward. You do not need a function here. |
||
return { | ||
tableOpts: { | ||
...tableOpts, | ||
|
@@ -546,7 +551,12 @@ export const isJqSearch = (value: string) => { | |
return _.startsWith(value, 'jq .'); | ||
}; | ||
|
||
const setData = (store: LogsStore, data: Log[], schema: LogStreamSchemaData | null, jqFilteredData?: Log[]) => { | ||
const setData = ( | ||
store: LogsStore, | ||
data: Log[], | ||
queryResponse: LogStreamQueryWithFields | null, | ||
jqFilteredData?: Log[], | ||
) => { | ||
const { | ||
data: existingData, | ||
tableOpts, | ||
|
@@ -563,7 +573,7 @@ const setData = (store: LogsStore, data: Log[], schema: LogStreamSchemaData | nu | |
: filterAndSortData(tableOpts, data); | ||
const newPageSlice = filteredData && getPageSlice(currentPage, tableOpts.perPage, filteredData); | ||
const newHeaders = | ||
isQuerySearchActive && activeMode === 'sql' ? makeHeadersfromData(data) : makeHeadersFromSchema(schema); | ||
isQuerySearchActive && activeMode === 'sql' ? makeHeadersfromData(data) : makeHeadersFromQueryFields(queryResponse); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why'd you want to make headers from data when |
||
|
||
return { | ||
tableOpts: { | ||
|
@@ -871,7 +881,10 @@ const toggleSideBar = (store: LogsStore) => { | |
|
||
const onToggleView = (store: LogsStore, viewMode: 'json' | 'table') => { | ||
const { data, tableOpts } = store; | ||
const filteredData = filterAndSortData({ sortOrder: defaultSortOrder, sortKey: defaultSortKey, filters: {} }, data.rawData); | ||
const filteredData = filterAndSortData( | ||
{ sortOrder: defaultSortOrder, sortKey: defaultSortKey, filters: {} }, | ||
data.rawData, | ||
); | ||
const currentPage = tableOpts.currentPage; | ||
const newPageSlice = getPageSlice(currentPage, tableOpts.perPage, filteredData); | ||
|
||
|
@@ -888,7 +901,7 @@ const onToggleView = (store: LogsStore, viewMode: 'json' | 'table') => { | |
currentPage, | ||
totalPages: getTotalPages(filteredData, tableOpts.perPage), | ||
}, | ||
viewMode | ||
viewMode, | ||
}; | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a query. Rename the type.