-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[connectors] Google drive parent scripts improvement (#9390)
- Loading branch information
Showing
3 changed files
with
131 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as fs from "fs"; | ||
import * as readline from "readline"; | ||
import { makeScript } from "scripts/helpers"; | ||
|
||
import { dataSourceConfigFromConnector } from "@connectors/lib/api/data_source_config"; | ||
import { updateDocumentParentsField } from "@connectors/lib/data_sources"; | ||
import logger from "@connectors/logger/logger"; | ||
import { ConnectorModel } from "@connectors/resources/storage/models/connector_model"; | ||
|
||
interface LogEntry { | ||
msg: string; | ||
documentId: string; | ||
parents: string[]; | ||
previousParents: string[]; | ||
} | ||
|
||
async function processLogFile( | ||
connector: ConnectorModel, | ||
filePath: string, | ||
execute: boolean | ||
) { | ||
const fileStream = fs.createReadStream(filePath); | ||
const rl = readline.createInterface({ | ||
input: fileStream, | ||
crlfDelay: Infinity, | ||
}); | ||
|
||
const dataSourceConfig = dataSourceConfigFromConnector(connector); | ||
|
||
for await (const line of rl) { | ||
const entry = JSON.parse(line) as LogEntry; | ||
const { msg, documentId, previousParents } = entry; | ||
if ( | ||
msg === "Update parents for document" && | ||
documentId && | ||
previousParents | ||
) { | ||
logger.info( | ||
{ documentId, previousParents }, | ||
"Restoring parent for document" | ||
); | ||
if (execute) { | ||
await updateDocumentParentsField({ | ||
dataSourceConfig, | ||
documentId: documentId, | ||
parents: previousParents, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
makeScript( | ||
{ | ||
connectorId: { type: "number", required: true }, | ||
file: { type: "string", required: true }, | ||
}, | ||
async ({ connectorId, file, execute }) => { | ||
const connector = await ConnectorModel.findByPk(connectorId); | ||
if (connector) { | ||
await processLogFile(connector, file, execute); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters