🧐 Add exportResults script
This commit is contained in:
119
packages/scripts/exportResults.ts
Normal file
119
packages/scripts/exportResults.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import { PrismaClient } from '@typebot.io/prisma'
|
||||
import * as p from '@clack/prompts'
|
||||
import { promptAndSetEnvironment } from './utils'
|
||||
import cliProgress from 'cli-progress'
|
||||
import { writeFileSync } from 'fs'
|
||||
import {
|
||||
ResultWithAnswers,
|
||||
Typebot,
|
||||
resultWithAnswersSchema,
|
||||
} from '@typebot.io/schemas'
|
||||
import { byId } from '@typebot.io/lib'
|
||||
import { parseResultHeader } from '@typebot.io/lib/results/parseResultHeader'
|
||||
import { convertResultsToTableData } from '@typebot.io/lib/results/convertResultsToTableData'
|
||||
import { parseColumnsOrder } from '@typebot.io/lib/results/parseColumnsOrder'
|
||||
import { parseUniqueKey } from '@typebot.io/lib/parseUniqueKey'
|
||||
import { unparse } from 'papaparse'
|
||||
import { z } from 'zod'
|
||||
|
||||
const exportResults = async () => {
|
||||
await promptAndSetEnvironment('production')
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
const typebotId = (await p.text({
|
||||
message: 'Typebot ID?',
|
||||
})) as string
|
||||
|
||||
if (!typebotId || typeof typebotId !== 'string') {
|
||||
console.log('No id provided')
|
||||
return
|
||||
}
|
||||
|
||||
const progressBar = new cliProgress.SingleBar(
|
||||
{},
|
||||
cliProgress.Presets.shades_classic
|
||||
)
|
||||
|
||||
const typebot = (await prisma.typebot.findUnique({
|
||||
where: {
|
||||
id: typebotId,
|
||||
},
|
||||
})) as Typebot | null
|
||||
|
||||
if (!typebot) {
|
||||
console.log('No typebot found')
|
||||
return
|
||||
}
|
||||
|
||||
const totalResultsToExport = await prisma.result.count({
|
||||
where: {
|
||||
typebotId,
|
||||
hasStarted: true,
|
||||
isArchived: false,
|
||||
},
|
||||
})
|
||||
|
||||
progressBar.start(totalResultsToExport, 0)
|
||||
|
||||
const results: ResultWithAnswers[] = []
|
||||
|
||||
for (let skip = 0; skip < totalResultsToExport; skip += 50) {
|
||||
results.push(
|
||||
...z.array(resultWithAnswersSchema).parse(
|
||||
await prisma.result.findMany({
|
||||
take: 50,
|
||||
skip,
|
||||
where: {
|
||||
typebotId,
|
||||
hasStarted: true,
|
||||
isArchived: false,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
include: { answers: true },
|
||||
})
|
||||
)
|
||||
)
|
||||
progressBar.increment(50)
|
||||
}
|
||||
|
||||
progressBar.stop()
|
||||
|
||||
writeFileSync('logs/results.json', JSON.stringify(results))
|
||||
|
||||
const resultHeader = parseResultHeader(typebot, [])
|
||||
|
||||
const dataToUnparse = convertResultsToTableData(results, resultHeader)
|
||||
|
||||
const headerIds = parseColumnsOrder(
|
||||
typebot?.resultsTablePreferences?.columnsOrder,
|
||||
resultHeader
|
||||
).reduce<string[]>((currentHeaderIds, columnId) => {
|
||||
if (typebot?.resultsTablePreferences?.columnsVisibility[columnId] === false)
|
||||
return currentHeaderIds
|
||||
const columnLabel = resultHeader.find(
|
||||
(headerCell) => headerCell.id === columnId
|
||||
)?.id
|
||||
if (!columnLabel) return currentHeaderIds
|
||||
return [...currentHeaderIds, columnLabel]
|
||||
}, [])
|
||||
|
||||
const data = dataToUnparse.map<{ [key: string]: string }>((data) => {
|
||||
const newObject: { [key: string]: string } = {}
|
||||
headerIds?.forEach((headerId) => {
|
||||
const headerLabel = resultHeader.find(byId(headerId))?.label
|
||||
if (!headerLabel) return
|
||||
const newKey = parseUniqueKey(headerLabel, Object.keys(newObject))
|
||||
newObject[newKey] = data[headerId]?.plainText
|
||||
})
|
||||
return newObject
|
||||
})
|
||||
|
||||
const csv = unparse(data)
|
||||
|
||||
writeFileSync('logs/results.csv', csv)
|
||||
}
|
||||
|
||||
exportResults()
|
||||
@@ -1,203 +0,0 @@
|
||||
import { PrismaClient } from '@typebot.io/prisma'
|
||||
import { writeFileSync } from 'fs'
|
||||
import {
|
||||
Block,
|
||||
BlockOptions,
|
||||
BlockType,
|
||||
defaultEmailInputOptions,
|
||||
Group,
|
||||
InputBlockType,
|
||||
PublicTypebot,
|
||||
publicTypebotSchema,
|
||||
Theme,
|
||||
Typebot,
|
||||
} from '@typebot.io/schemas'
|
||||
import { isDefined, isNotDefined } from '@typebot.io/lib'
|
||||
import { promptAndSetEnvironment } from './utils'
|
||||
import { detailedDiff } from 'deep-object-diff'
|
||||
|
||||
const fixTypebot = (brokenTypebot: Typebot | PublicTypebot) =>
|
||||
({
|
||||
...brokenTypebot,
|
||||
theme: fixTheme(brokenTypebot.theme),
|
||||
groups: fixGroups(brokenTypebot.groups),
|
||||
} satisfies Typebot | PublicTypebot)
|
||||
|
||||
const fixTheme = (brokenTheme: Theme) =>
|
||||
({
|
||||
...brokenTheme,
|
||||
chat: {
|
||||
...brokenTheme.chat,
|
||||
hostAvatar: brokenTheme.chat.hostAvatar
|
||||
? {
|
||||
isEnabled: brokenTheme.chat.hostAvatar.isEnabled,
|
||||
url: brokenTheme.chat.hostAvatar.url ?? undefined,
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
} satisfies Theme)
|
||||
|
||||
const fixGroups = (brokenGroups: Group[]) =>
|
||||
brokenGroups.map(
|
||||
(brokenGroup, index) =>
|
||||
({
|
||||
...brokenGroup,
|
||||
graphCoordinates: {
|
||||
...brokenGroup.graphCoordinates,
|
||||
x: brokenGroup.graphCoordinates.x ?? 0,
|
||||
y: brokenGroup.graphCoordinates.y ?? 0,
|
||||
},
|
||||
blocks: fixBlocks(brokenGroup.blocks, brokenGroup.id, index),
|
||||
} satisfies Group)
|
||||
)
|
||||
|
||||
const fixBlocks = (
|
||||
brokenBlocks: Block[],
|
||||
groupId: string,
|
||||
groupIndex: number
|
||||
) => {
|
||||
if (groupIndex === 0 && brokenBlocks.length > 1) return [brokenBlocks[0]]
|
||||
return brokenBlocks
|
||||
.filter((block) => block && Object.keys(block).length > 0)
|
||||
.map((brokenBlock) => {
|
||||
return removeUndefinedFromObject({
|
||||
...brokenBlock,
|
||||
webhookId:
|
||||
('webhookId' in brokenBlock ? brokenBlock.webhookId : undefined) ??
|
||||
('webhook' in brokenBlock && brokenBlock.webhook
|
||||
? //@ts-ignore
|
||||
brokenBlock.webhook.id
|
||||
: undefined),
|
||||
webhook: undefined,
|
||||
groupId: brokenBlock.groupId ?? groupId,
|
||||
options:
|
||||
brokenBlock && 'options' in brokenBlock && brokenBlock.options
|
||||
? fixBrokenBlockOption(brokenBlock.options, brokenBlock.type)
|
||||
: undefined,
|
||||
})
|
||||
}) as Block[]
|
||||
}
|
||||
|
||||
const fixBrokenBlockOption = (options: BlockOptions, blockType: BlockType) =>
|
||||
removeUndefinedFromObject({
|
||||
...options,
|
||||
sheetId:
|
||||
'sheetId' in options && isDefined(options.sheetId)
|
||||
? options.sheetId.toString()
|
||||
: undefined,
|
||||
step:
|
||||
'step' in options && isDefined(options.step) ? options.step : undefined,
|
||||
value:
|
||||
'value' in options && isDefined(options.value)
|
||||
? options.value
|
||||
: undefined,
|
||||
retryMessageContent: fixRetryMessageContent(
|
||||
//@ts-ignore
|
||||
options.retryMessageContent,
|
||||
blockType
|
||||
),
|
||||
}) as BlockOptions
|
||||
|
||||
const fixRetryMessageContent = (
|
||||
retryMessageContent: string | undefined,
|
||||
blockType: BlockType
|
||||
) => {
|
||||
if (isNotDefined(retryMessageContent) && blockType === InputBlockType.EMAIL)
|
||||
return defaultEmailInputOptions.retryMessageContent
|
||||
if (isNotDefined(retryMessageContent)) return undefined
|
||||
return retryMessageContent
|
||||
}
|
||||
|
||||
const removeUndefinedFromObject = (obj: any) => {
|
||||
Object.keys(obj).forEach((key) => obj[key] === undefined && delete obj[key])
|
||||
return obj
|
||||
}
|
||||
|
||||
const resolve = (path: string, obj: object, separator = '.') => {
|
||||
const properties = Array.isArray(path) ? path : path.split(separator)
|
||||
//@ts-ignore
|
||||
return properties.reduce((prev, curr) => prev?.[curr], obj)
|
||||
}
|
||||
|
||||
const fixTypebots = async () => {
|
||||
await promptAndSetEnvironment()
|
||||
const prisma = new PrismaClient({
|
||||
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'],
|
||||
})
|
||||
|
||||
const typebots = await prisma.publicTypebot.findMany({
|
||||
where: {
|
||||
updatedAt: {
|
||||
gte: new Date('2023-01-01T00:00:00.000Z'),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
writeFileSync('logs/typebots.json', JSON.stringify(typebots))
|
||||
|
||||
const total = typebots.length
|
||||
let totalFixed = 0
|
||||
let progress = 0
|
||||
const fixedTypebots: (Typebot | PublicTypebot)[] = []
|
||||
const diffs: any[] = []
|
||||
for (const typebot of typebots) {
|
||||
progress += 1
|
||||
console.log(
|
||||
`Progress: ${progress}/${total} (${Math.round(
|
||||
(progress / total) * 100
|
||||
)}%) (${totalFixed} fixed typebots)`
|
||||
)
|
||||
const parser = publicTypebotSchema.safeParse({
|
||||
...typebot,
|
||||
updatedAt: new Date(typebot.updatedAt),
|
||||
createdAt: new Date(typebot.createdAt),
|
||||
})
|
||||
if ('error' in parser) {
|
||||
const fixedTypebot = {
|
||||
...fixTypebot(typebot as Typebot | PublicTypebot),
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date(typebot.createdAt),
|
||||
}
|
||||
publicTypebotSchema.parse(fixedTypebot)
|
||||
fixedTypebots.push(fixedTypebot)
|
||||
totalFixed += 1
|
||||
diffs.push({
|
||||
id: typebot.id,
|
||||
failedObject: resolve(parser.error.issues[0].path.join('.'), typebot),
|
||||
...detailedDiff(typebot, fixedTypebot),
|
||||
})
|
||||
}
|
||||
}
|
||||
writeFileSync('logs/fixedTypebots.json', JSON.stringify(fixedTypebots))
|
||||
writeFileSync(
|
||||
'logs/diffs.json',
|
||||
JSON.stringify(diffs.reverse().slice(0, 100))
|
||||
)
|
||||
|
||||
const queries = fixedTypebots.map((fixedTypebot) =>
|
||||
prisma.publicTypebot.updateMany({
|
||||
where: { id: fixedTypebot.id },
|
||||
data: {
|
||||
...fixedTypebot,
|
||||
// theme: fixedTypebot.theme ?? undefined,
|
||||
// settings: fixedTypebot.settings ?? undefined,
|
||||
// resultsTablePreferences:
|
||||
// 'resultsTablePreferences' in fixedTypebot &&
|
||||
// fixedTypebot.resultsTablePreferences
|
||||
// ? fixedTypebot.resultsTablePreferences
|
||||
// : undefined,
|
||||
} as any,
|
||||
})
|
||||
)
|
||||
|
||||
const totalQueries = queries.length
|
||||
progress = 0
|
||||
prisma.$on('query', () => {
|
||||
progress += 1
|
||||
console.log(`Progress: ${progress}/${totalQueries}`)
|
||||
})
|
||||
|
||||
await prisma.$transaction(queries)
|
||||
}
|
||||
|
||||
fixTypebots()
|
||||
@@ -25,14 +25,17 @@
|
||||
"updateWorkspace": "tsx updateWorkspace.ts",
|
||||
"inspectTypebot": "tsx inspectTypebot.ts",
|
||||
"inspectWorkspace": "tsx inspectWorkspace.ts",
|
||||
"getCoupon": "tsx getCoupon.ts"
|
||||
"getCoupon": "tsx getCoupon.ts",
|
||||
"exportResults": "tsx exportResults.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typebot.io/emails": "workspace:*",
|
||||
"@typebot.io/lib": "workspace:*",
|
||||
"@typebot.io/prisma": "workspace:*",
|
||||
"@typebot.io/schemas": "workspace:*",
|
||||
"@types/cli-progress": "^3.11.5",
|
||||
"@types/node": "20.4.2",
|
||||
"@types/papaparse": "5.3.7",
|
||||
"@types/prompts": "2.4.4",
|
||||
"deep-object-diff": "1.1.9",
|
||||
"got": "12.6.0",
|
||||
@@ -44,6 +47,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@clack/prompts": "^0.7.0",
|
||||
"@paralleldrive/cuid2": "2.2.1"
|
||||
"@paralleldrive/cuid2": "2.2.1",
|
||||
"cli-progress": "^3.12.0",
|
||||
"papaparse": "5.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user