refactor: step 2

pull/12097/head
AkaraChen 1 year ago
parent 94eb069a97
commit a77aa169b4

@ -1,7 +1,8 @@
import groupBy from 'lodash-es/groupBy' import groupBy from 'lodash-es/groupBy'
import { useMutation } from '@tanstack/react-query'
import { fetchFileIndexingEstimate } from './datasets'
import type { IndexingType } from '@/app/components/datasets/create/step-two' import type { IndexingType } from '@/app/components/datasets/create/step-two'
import type { CrawlOptions, CrawlResultItem, CustomFile, DocForm, IndexingEstimateParams, NotionInfo, ProcessRule } from '@/models/datasets' import type { CrawlOptions, CrawlResultItem, CustomFile, DataSourceType, DocForm, IndexingEstimateParams, NotionInfo, ProcessRule } from '@/models/datasets'
import { DataSourceType } from '@/models/datasets'
import type { DataSourceProvider, NotionPage } from '@/models/common' import type { DataSourceProvider, NotionPage } from '@/models/common'
const getNotionInfo = ( const getNotionInfo = (
@ -47,22 +48,33 @@ const getWebsiteInfo = (
} }
} }
type GetFileIndexingEstimateParamsOption = { type GetFileIndexingEstimateParamsOptionBase = {
docForm: DocForm docForm: DocForm
docLanguage: string docLanguage: string
dataSourceType: DataSourceType
files: CustomFile[]
indexingTechnique: IndexingType indexingTechnique: IndexingType
processRule: ProcessRule processRule: ProcessRule
dataset_id: string dataset_id: string
notionPages?: NotionPage[] }
websitePages?: CrawlResultItem[]
type GetFileIndexingEstimateParamsOptionFile = GetFileIndexingEstimateParamsOptionBase & {
dataSourceType: DataSourceType.FILE
files: CustomFile[]
}
type GetFileIndexingEstimateParamsOptionNotion = GetFileIndexingEstimateParamsOptionBase & {
dataSourceType: DataSourceType.NOTION
notionPages: NotionPage[]
}
type GetFileIndexingEstimateParamsOptionWeb = GetFileIndexingEstimateParamsOptionBase & {
dataSourceType: DataSourceType.WEB
websitePages: CrawlResultItem[]
crawlOptions?: CrawlOptions crawlOptions?: CrawlOptions
websiteCrawlProvider?: DataSourceProvider websiteCrawlProvider: DataSourceProvider
websiteCrawlJobId?: string websiteCrawlJobId: string
} }
const getFileIndexingEstimateParams = ({ const getFileIndexingEstimateParamsForFile = ({
docForm, docForm,
docLanguage, docLanguage,
dataSourceType, dataSourceType,
@ -70,13 +82,7 @@ const getFileIndexingEstimateParams = ({
indexingTechnique, indexingTechnique,
processRule, processRule,
dataset_id, dataset_id,
notionPages, }: GetFileIndexingEstimateParamsOptionFile): IndexingEstimateParams => {
websitePages,
crawlOptions,
websiteCrawlProvider,
websiteCrawlJobId,
}: GetFileIndexingEstimateParamsOption): IndexingEstimateParams | undefined => {
if (dataSourceType === DataSourceType.FILE) {
return { return {
info_list: { info_list: {
data_source_type: dataSourceType, data_source_type: dataSourceType,
@ -91,13 +97,20 @@ const getFileIndexingEstimateParams = ({
dataset_id, dataset_id,
} }
} }
if (dataSourceType === DataSourceType.NOTION) {
const getFileIndexingEstimateParamsForNotion = ({
docForm,
docLanguage,
dataSourceType,
notionPages,
indexingTechnique,
processRule,
dataset_id,
}: GetFileIndexingEstimateParamsOptionNotion): IndexingEstimateParams => {
return { return {
info_list: { info_list: {
data_source_type: dataSourceType, data_source_type: dataSourceType,
notion_info_list: getNotionInfo( notion_info_list: getNotionInfo(notionPages),
notionPages as NotionPage[],
),
}, },
indexing_technique: indexingTechnique, indexing_technique: indexingTechnique,
process_rule: processRule, process_rule: processRule,
@ -106,14 +119,26 @@ const getFileIndexingEstimateParams = ({
dataset_id, dataset_id,
} }
} }
if (dataSourceType === DataSourceType.WEB) {
const getFileIndexingEstimateParamsForWeb = ({
docForm,
docLanguage,
dataSourceType,
websitePages,
crawlOptions,
websiteCrawlProvider,
websiteCrawlJobId,
indexingTechnique,
processRule,
dataset_id,
}: GetFileIndexingEstimateParamsOptionWeb): IndexingEstimateParams => {
return { return {
info_list: { info_list: {
data_source_type: dataSourceType, data_source_type: dataSourceType,
website_info_list: getWebsiteInfo({ website_info_list: getWebsiteInfo({
websiteCrawlProvider: websiteCrawlProvider as DataSourceProvider, websiteCrawlProvider,
websiteCrawlJobId: websiteCrawlJobId as string, websiteCrawlJobId,
websitePages: websitePages as CrawlResultItem[], websitePages,
crawlOptions, crawlOptions,
}), }),
}, },
@ -124,8 +149,33 @@ const getFileIndexingEstimateParams = ({
dataset_id, dataset_id,
} }
} }
export const useFetchFileIndexingEstimateForFile = (
options: GetFileIndexingEstimateParamsOptionFile,
) => {
return useMutation({
mutationFn: async () => {
return fetchFileIndexingEstimate(getFileIndexingEstimateParamsForFile(options))
},
})
} }
export const useFetchFileIndexingEstimate = () => { export const useFetchFileIndexingEstimateForNotion = (
options: GetFileIndexingEstimateParamsOptionNotion,
) => {
return useMutation({
mutationFn: async () => {
return fetchFileIndexingEstimate(getFileIndexingEstimateParamsForNotion(options))
},
})
}
export const useFetchFileIndexingEstimateForWeb = (
options: GetFileIndexingEstimateParamsOptionWeb,
) => {
return useMutation({
mutationFn: async () => {
return fetchFileIndexingEstimate(getFileIndexingEstimateParamsForWeb(options))
},
})
} }

Loading…
Cancel
Save