2022-08-11 11:45:28 +05:30
|
|
|
import { blue, orange } from '@ant-design/colors';
|
2023-02-03 18:06:26 +05:30
|
|
|
import { Input } from 'antd';
|
2023-03-17 15:21:02 +05:30
|
|
|
import { ColumnsType } from 'antd/es/table';
|
|
|
|
|
import Editor from 'components/Editor';
|
2022-08-11 11:45:28 +05:30
|
|
|
import AddToQueryHOC from 'components/Logs/AddToQueryHOC';
|
|
|
|
|
import CopyClipboardHOC from 'components/Logs/CopyClipboardHOC';
|
2023-02-03 18:06:26 +05:30
|
|
|
import { ResizeTable } from 'components/ResizeTable';
|
2022-08-11 11:45:28 +05:30
|
|
|
import flatten from 'flat';
|
|
|
|
|
import { fieldSearchFilter } from 'lib/logs/fieldSearch';
|
2023-03-17 15:21:02 +05:30
|
|
|
import { isEmpty } from 'lodash-es';
|
2022-08-11 11:45:28 +05:30
|
|
|
import React, { useMemo, useState } from 'react';
|
2022-08-19 17:16:04 +05:30
|
|
|
import { ILog } from 'types/api/logs/log';
|
2022-08-11 11:45:28 +05:30
|
|
|
|
|
|
|
|
import ActionItem from './ActionItem';
|
2023-03-17 15:21:02 +05:30
|
|
|
import { recursiveParseJSON } from './utils';
|
2022-08-11 11:45:28 +05:30
|
|
|
|
2022-08-16 18:53:34 +05:30
|
|
|
// Fields which should be restricted from adding it to query
|
|
|
|
|
const RESTRICTED_FIELDS = ['timestamp'];
|
|
|
|
|
|
2022-08-19 17:16:04 +05:30
|
|
|
interface TableViewProps {
|
|
|
|
|
logData: ILog;
|
|
|
|
|
}
|
|
|
|
|
function TableView({ logData }: TableViewProps): JSX.Element | null {
|
2022-08-11 11:45:28 +05:30
|
|
|
const [fieldSearchInput, setFieldSearchInput] = useState<string>('');
|
|
|
|
|
|
2022-08-19 17:16:04 +05:30
|
|
|
const flattenLogData: Record<string, never> | null = useMemo(
|
|
|
|
|
() => (logData ? flatten(logData) : null),
|
|
|
|
|
[logData],
|
|
|
|
|
);
|
2022-08-11 11:45:28 +05:30
|
|
|
if (logData === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 17:16:04 +05:30
|
|
|
const dataSource =
|
|
|
|
|
flattenLogData !== null &&
|
|
|
|
|
Object.keys(flattenLogData)
|
|
|
|
|
.filter((field) => fieldSearchFilter(field, fieldSearchInput))
|
2023-01-24 18:53:04 +05:30
|
|
|
.map((key) => ({
|
|
|
|
|
key,
|
|
|
|
|
field: key,
|
|
|
|
|
value: JSON.stringify(flattenLogData[key]),
|
|
|
|
|
}));
|
2022-08-19 17:16:04 +05:30
|
|
|
|
|
|
|
|
if (!dataSource) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-08-11 11:45:28 +05:30
|
|
|
|
2023-03-17 15:21:02 +05:30
|
|
|
const columns: ColumnsType<DataType> = [
|
2022-08-11 11:45:28 +05:30
|
|
|
{
|
|
|
|
|
title: 'Action',
|
2023-03-17 15:21:02 +05:30
|
|
|
width: 15,
|
2022-08-19 17:16:04 +05:30
|
|
|
render: (fieldData: Record<string, string>): JSX.Element | null => {
|
2022-08-16 18:53:34 +05:30
|
|
|
const fieldKey = fieldData.field.split('.').slice(-1);
|
|
|
|
|
if (!RESTRICTED_FIELDS.includes(fieldKey[0])) {
|
|
|
|
|
return <ActionItem fieldKey={fieldKey} fieldValue={fieldData.value} />;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
2022-08-11 11:45:28 +05:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Field',
|
|
|
|
|
dataIndex: 'field',
|
|
|
|
|
key: 'field',
|
2023-03-17 15:21:02 +05:30
|
|
|
width: 30,
|
|
|
|
|
ellipsis: true,
|
2022-08-19 17:16:04 +05:30
|
|
|
render: (field: string): JSX.Element => {
|
2022-08-16 18:53:34 +05:30
|
|
|
const fieldKey = field.split('.').slice(-1);
|
|
|
|
|
const renderedField = <span style={{ color: blue[4] }}>{field}</span>;
|
|
|
|
|
|
|
|
|
|
if (!RESTRICTED_FIELDS.includes(fieldKey[0])) {
|
|
|
|
|
return (
|
2022-08-19 17:16:04 +05:30
|
|
|
<AddToQueryHOC fieldKey={fieldKey[0]} fieldValue={flattenLogData[field]}>
|
2022-08-16 18:53:34 +05:30
|
|
|
{renderedField}
|
|
|
|
|
</AddToQueryHOC>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return renderedField;
|
|
|
|
|
},
|
2022-08-11 11:45:28 +05:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Value',
|
|
|
|
|
dataIndex: 'value',
|
|
|
|
|
key: 'value',
|
2023-02-02 16:53:15 +05:30
|
|
|
width: 80,
|
2022-08-11 11:45:28 +05:30
|
|
|
ellipsis: false,
|
2023-03-17 15:21:02 +05:30
|
|
|
render: (field, record): JSX.Element => {
|
|
|
|
|
if (record.field === 'body') {
|
|
|
|
|
const parsedBody = recursiveParseJSON(field);
|
|
|
|
|
if (!isEmpty(parsedBody)) {
|
|
|
|
|
return (
|
|
|
|
|
<Editor
|
|
|
|
|
value={JSON.stringify(parsedBody, null, 2)}
|
|
|
|
|
readOnly
|
|
|
|
|
height="70vh"
|
|
|
|
|
options={{
|
|
|
|
|
minimap: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CopyClipboardHOC textToCopy={field}>
|
|
|
|
|
<span style={{ color: orange[6] }}>{field}</span>
|
|
|
|
|
</CopyClipboardHOC>
|
|
|
|
|
);
|
|
|
|
|
},
|
2022-08-11 11:45:28 +05:30
|
|
|
},
|
|
|
|
|
];
|
2023-02-02 16:53:15 +05:30
|
|
|
|
2022-08-11 11:45:28 +05:30
|
|
|
return (
|
2023-03-17 15:21:02 +05:30
|
|
|
<>
|
2022-08-11 11:45:28 +05:30
|
|
|
<Input
|
|
|
|
|
placeholder="Search field names"
|
|
|
|
|
size="large"
|
|
|
|
|
value={fieldSearchInput}
|
2022-08-19 17:16:04 +05:30
|
|
|
onChange={(e): void => setFieldSearchInput(e.target.value)}
|
2022-08-11 11:45:28 +05:30
|
|
|
/>
|
2023-02-03 18:06:26 +05:30
|
|
|
<ResizeTable
|
2023-03-17 15:21:02 +05:30
|
|
|
columns={columns}
|
2023-02-03 18:06:26 +05:30
|
|
|
tableLayout="fixed"
|
|
|
|
|
dataSource={dataSource}
|
|
|
|
|
pagination={false}
|
|
|
|
|
/>
|
2023-03-17 15:21:02 +05:30
|
|
|
</>
|
2022-08-11 11:45:28 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 15:21:02 +05:30
|
|
|
interface DataType {
|
|
|
|
|
key: string;
|
|
|
|
|
field: string;
|
|
|
|
|
value: string;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 11:45:28 +05:30
|
|
|
export default TableView;
|