@talkpilot/core-db 1.3.40 → 1.3.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -41
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/talkpilot/calls/calls.types.d.ts +0 -1
- package/dist/talkpilot/calls/calls.types.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +0 -39
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +1 -92
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +0 -4
- package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/index.d.ts +0 -1
- package/dist/talkpilot/clientsConfig/index.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/index.js +0 -1
- package/dist/talkpilot/clientsConfig/index.js.map +1 -1
- package/dist/test-utils/factories/index.d.ts +1 -1
- package/dist/test-utils/factories/index.d.ts.map +1 -1
- package/dist/test-utils/factories/index.js +1 -1
- package/dist/test-utils/factories/index.js.map +1 -1
- package/dist/test-utils/factories/websitalk/scans.d.ts.map +1 -1
- package/dist/test-utils/factories/websitalk/scans.js +0 -5
- package/dist/test-utils/factories/websitalk/scans.js.map +1 -1
- package/dist/websitalk/index.d.ts +1 -1
- package/dist/websitalk/index.d.ts.map +1 -1
- package/dist/websitalk/index.js +1 -1
- package/dist/websitalk/index.js.map +1 -1
- package/dist/websitalk/scans/scans.getters.d.ts.map +1 -1
- package/dist/websitalk/scans/scans.getters.js +2 -8
- package/dist/websitalk/scans/scans.getters.js.map +1 -1
- package/dist/websitalk/scans/scans.types.d.ts +0 -5
- package/dist/websitalk/scans/scans.types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/talkpilot/calls/calls.types.ts +0 -1
- package/src/talkpilot/clientsConfig/__tests__/clientsConfig.getters.spec.ts +0 -318
- package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +2 -116
- package/src/talkpilot/clientsConfig/clientsConfig.types.ts +0 -4
- package/src/talkpilot/clientsConfig/index.ts +0 -1
- package/src/test-utils/factories/index.ts +1 -1
- package/src/test-utils/factories/websitalk/knowledgeDocuments.ts +28 -0
- package/src/test-utils/factories/websitalk/scans.ts +0 -5
- package/src/websitalk/index.ts +1 -1
- package/src/websitalk/knowledgeDocuments/__tests__/knowledgeDocuments.spec.ts +123 -0
- package/src/websitalk/knowledgeDocuments/index.ts +3 -0
- package/src/websitalk/knowledgeDocuments/knowledgeDocuments.constants.ts +19 -0
- package/src/websitalk/knowledgeDocuments/knowledgeDocuments.getters.ts +85 -0
- package/src/websitalk/knowledgeDocuments/knowledgeDocuments.types.ts +37 -0
- package/src/websitalk/scans/scans.getters.ts +2 -8
- package/src/websitalk/scans/scans.types.ts +0 -9
- package/src/talkpilot/clientsConfig/clientsConfig.constants.ts +0 -2
- package/src/test-utils/factories/websitalk/scanResources.ts +0 -26
- package/src/websitalk/scanResources/__tests__/scanResources.spec.ts +0 -136
- package/src/websitalk/scanResources/index.ts +0 -4
- package/src/websitalk/scanResources/scanResources.constants.ts +0 -47
- package/src/websitalk/scanResources/scanResources.getters.ts +0 -94
- package/src/websitalk/scanResources/scanResources.types.ts +0 -90
- package/src/websitalk/scanResources/scanResources.utils.ts +0 -28
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const KNOWLEDGE_DOCUMENTS_COLLECTION = "knowledgeDocuments";
|
|
2
|
+
|
|
3
|
+
export const KNOWLEDGE_DOCUMENT_STATUSES = {
|
|
4
|
+
UPLOADING: "UPLOADING",
|
|
5
|
+
EXTRACTING: "EXTRACTING",
|
|
6
|
+
PROCESSING: "PROCESSING",
|
|
7
|
+
READY: "READY",
|
|
8
|
+
FAILED: "FAILED",
|
|
9
|
+
} as const;
|
|
10
|
+
|
|
11
|
+
export type KnowledgeDocumentStatus =
|
|
12
|
+
(typeof KNOWLEDGE_DOCUMENT_STATUSES)[keyof typeof KNOWLEDGE_DOCUMENT_STATUSES];
|
|
13
|
+
|
|
14
|
+
export const KNOWLEDGE_FILE_TYPES = {
|
|
15
|
+
PDF: "pdf",
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
export type KnowledgeFileType =
|
|
19
|
+
(typeof KNOWLEDGE_FILE_TYPES)[keyof typeof KNOWLEDGE_FILE_TYPES];
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Collection, ObjectId as MongoObjectId } from "mongodb";
|
|
2
|
+
import { getDb, ObjectId } from "../index";
|
|
3
|
+
import {
|
|
4
|
+
KNOWLEDGE_DOCUMENTS_COLLECTION,
|
|
5
|
+
KNOWLEDGE_DOCUMENT_STATUSES,
|
|
6
|
+
} from "./knowledgeDocuments.constants";
|
|
7
|
+
import type {
|
|
8
|
+
CreateKnowledgeDocumentInput,
|
|
9
|
+
KnowledgeDocument,
|
|
10
|
+
KnowledgeDocumentDoc,
|
|
11
|
+
UpdateKnowledgeDocumentInput,
|
|
12
|
+
} from "./knowledgeDocuments.types";
|
|
13
|
+
|
|
14
|
+
export const getKnowledgeDocumentsCollection = (): Collection<KnowledgeDocument> => {
|
|
15
|
+
return getDb().collection<KnowledgeDocument>(KNOWLEDGE_DOCUMENTS_COLLECTION);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const createKnowledgeDocument = async (
|
|
19
|
+
input: CreateKnowledgeDocumentInput,
|
|
20
|
+
): Promise<MongoObjectId> => {
|
|
21
|
+
const now = new Date();
|
|
22
|
+
const { insertedId } = await getKnowledgeDocumentsCollection().insertOne({
|
|
23
|
+
clientId: input.clientId,
|
|
24
|
+
filename: input.filename,
|
|
25
|
+
contentType: input.contentType,
|
|
26
|
+
fileType: input.fileType,
|
|
27
|
+
storagePath: input.storagePath,
|
|
28
|
+
markdownStoragePath: input.markdownStoragePath,
|
|
29
|
+
byteSize: input.byteSize,
|
|
30
|
+
pageCount: input.pageCount,
|
|
31
|
+
charCount: input.charCount,
|
|
32
|
+
contentHash: input.contentHash,
|
|
33
|
+
status: input.status ?? KNOWLEDGE_DOCUMENT_STATUSES.UPLOADING,
|
|
34
|
+
errorMessage: input.errorMessage,
|
|
35
|
+
progressPercent: input.progressPercent ?? 0,
|
|
36
|
+
createdAt: now,
|
|
37
|
+
updatedAt: now,
|
|
38
|
+
});
|
|
39
|
+
return insertedId;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const getKnowledgeDocumentById = async (
|
|
43
|
+
id: string,
|
|
44
|
+
): Promise<KnowledgeDocumentDoc | null> => {
|
|
45
|
+
return getKnowledgeDocumentsCollection().findOne({ _id: new ObjectId(id) });
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const listKnowledgeDocumentsByClientId = async (
|
|
49
|
+
clientId: string,
|
|
50
|
+
): Promise<KnowledgeDocumentDoc[]> => {
|
|
51
|
+
return getKnowledgeDocumentsCollection()
|
|
52
|
+
.find({ clientId })
|
|
53
|
+
.sort({ createdAt: -1 })
|
|
54
|
+
.toArray();
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const listReadyKnowledgeDocumentIdsByClientId = async (
|
|
58
|
+
clientId: string,
|
|
59
|
+
): Promise<string[]> => {
|
|
60
|
+
const docs = await getKnowledgeDocumentsCollection()
|
|
61
|
+
.find(
|
|
62
|
+
{ clientId, status: KNOWLEDGE_DOCUMENT_STATUSES.READY },
|
|
63
|
+
{ projection: { _id: 1 } },
|
|
64
|
+
)
|
|
65
|
+
.toArray();
|
|
66
|
+
return docs.map((doc) => doc._id.toString());
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const updateKnowledgeDocument = async (
|
|
70
|
+
id: string,
|
|
71
|
+
updates: UpdateKnowledgeDocumentInput,
|
|
72
|
+
): Promise<KnowledgeDocumentDoc | null> => {
|
|
73
|
+
return getKnowledgeDocumentsCollection().findOneAndUpdate(
|
|
74
|
+
{ _id: new ObjectId(id) },
|
|
75
|
+
{ $set: { ...updates, updatedAt: new Date() } },
|
|
76
|
+
{ returnDocument: "after" },
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const deleteKnowledgeDocument = async (id: string): Promise<boolean> => {
|
|
81
|
+
const result = await getKnowledgeDocumentsCollection().deleteOne({
|
|
82
|
+
_id: new ObjectId(id),
|
|
83
|
+
});
|
|
84
|
+
return result.deletedCount === 1;
|
|
85
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ObjectId } from "mongodb";
|
|
2
|
+
import type {
|
|
3
|
+
KnowledgeDocumentStatus,
|
|
4
|
+
KnowledgeFileType,
|
|
5
|
+
} from "./knowledgeDocuments.constants";
|
|
6
|
+
|
|
7
|
+
export type KnowledgeDocument = {
|
|
8
|
+
clientId: string;
|
|
9
|
+
filename: string;
|
|
10
|
+
contentType: string;
|
|
11
|
+
fileType: KnowledgeFileType;
|
|
12
|
+
storagePath: string;
|
|
13
|
+
markdownStoragePath?: string;
|
|
14
|
+
byteSize: number;
|
|
15
|
+
pageCount?: number;
|
|
16
|
+
charCount?: number;
|
|
17
|
+
contentHash?: string;
|
|
18
|
+
status: KnowledgeDocumentStatus;
|
|
19
|
+
errorMessage?: string;
|
|
20
|
+
progressPercent: number;
|
|
21
|
+
createdAt: Date;
|
|
22
|
+
updatedAt: Date;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type KnowledgeDocumentDoc = KnowledgeDocument & { _id: ObjectId };
|
|
26
|
+
|
|
27
|
+
export type CreateKnowledgeDocumentInput = Omit<
|
|
28
|
+
KnowledgeDocument,
|
|
29
|
+
"createdAt" | "updatedAt" | "progressPercent" | "status"
|
|
30
|
+
> & {
|
|
31
|
+
status?: KnowledgeDocumentStatus;
|
|
32
|
+
progressPercent?: number;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type UpdateKnowledgeDocumentInput = Partial<
|
|
36
|
+
Omit<KnowledgeDocument, "clientId" | "createdAt" | "updatedAt">
|
|
37
|
+
>;
|
|
@@ -57,7 +57,6 @@ export const createScanDoc = (
|
|
|
57
57
|
scan: Pick<Scan, "clientId" | "websiteUrlId"> &
|
|
58
58
|
Partial<Pick<Scan, "notifyPhoneNumber">>,
|
|
59
59
|
) => {
|
|
60
|
-
const now = new Date();
|
|
61
60
|
return getScansCollection().insertOne({
|
|
62
61
|
...scan,
|
|
63
62
|
status: SCAN_STATUSES.CHECKING,
|
|
@@ -66,13 +65,8 @@ export const createScanDoc = (
|
|
|
66
65
|
pagesSkipped: 0,
|
|
67
66
|
skipped: [],
|
|
68
67
|
siteChrome: {} as Scan["siteChrome"],
|
|
69
|
-
createdAt:
|
|
70
|
-
updatedAt:
|
|
71
|
-
startedAt: now,
|
|
72
|
-
completedAt: null,
|
|
73
|
-
pagesCompleted: 0,
|
|
74
|
-
pagesFailedPermanent: 0,
|
|
75
|
-
pagesDuplicate: 0,
|
|
68
|
+
createdAt: new Date(),
|
|
69
|
+
updatedAt: new Date(),
|
|
76
70
|
});
|
|
77
71
|
};
|
|
78
72
|
|
|
@@ -40,15 +40,6 @@ export type Scan = {
|
|
|
40
40
|
siteChrome: SiteChromeEntry;
|
|
41
41
|
createdAt: Date;
|
|
42
42
|
updatedAt: Date;
|
|
43
|
-
|
|
44
|
-
// Terminal-only snapshot, written once at finalize (Brain, outside core-db).
|
|
45
|
-
// Never read for live progress — an active scan's KPIs must come from
|
|
46
|
-
// getScanKpiCounts, not these fields, which are not kept in sync mid-scan.
|
|
47
|
-
startedAt?: Date;
|
|
48
|
-
completedAt?: Date | null;
|
|
49
|
-
pagesCompleted?: number;
|
|
50
|
-
pagesFailedPermanent?: number;
|
|
51
|
-
pagesDuplicate?: number;
|
|
52
43
|
};
|
|
53
44
|
|
|
54
45
|
export type ScanDoc = WithId<Scan>;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Factory } from "fishery";
|
|
2
|
-
import { faker } from "@faker-js/faker";
|
|
3
|
-
import { ObjectId } from "mongodb";
|
|
4
|
-
import {
|
|
5
|
-
URL_TASK_STATUSES,
|
|
6
|
-
type ScanResource,
|
|
7
|
-
} from "../../../websitalk/scanResources";
|
|
8
|
-
|
|
9
|
-
export const scanResourceFactory = Factory.define<ScanResource>(() => ({
|
|
10
|
-
urlHashId: faker.string.uuid(),
|
|
11
|
-
clientId: faker.string.uuid(),
|
|
12
|
-
scanId: new ObjectId().toString(),
|
|
13
|
-
websiteUrlId: new ObjectId().toString(),
|
|
14
|
-
url: faker.internet.url(),
|
|
15
|
-
status: URL_TASK_STATUSES.URL_PENDING,
|
|
16
|
-
depth: 0,
|
|
17
|
-
retryCount: 0,
|
|
18
|
-
createdAt: new Date(),
|
|
19
|
-
updatedAt: new Date(),
|
|
20
|
-
}));
|
|
21
|
-
|
|
22
|
-
export function createScanResourceFixture(
|
|
23
|
-
overrides?: Partial<ScanResource>,
|
|
24
|
-
): ScanResource {
|
|
25
|
-
return scanResourceFactory.build(overrides);
|
|
26
|
-
}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { ObjectId } from "mongodb";
|
|
2
|
-
import {
|
|
3
|
-
getScanResourcesCollection,
|
|
4
|
-
getScanKpiCounts,
|
|
5
|
-
getScanPages,
|
|
6
|
-
getScanResourceById,
|
|
7
|
-
getScanResourceCountsByScanIds,
|
|
8
|
-
} from "../scanResources.getters";
|
|
9
|
-
import { boardColumn } from "../scanResources.utils";
|
|
10
|
-
import { createScanResourceFixture } from "../../../test-utils/factories";
|
|
11
|
-
|
|
12
|
-
describe("db.websitalk.scanResources", () => {
|
|
13
|
-
describe("boardColumn()", () => {
|
|
14
|
-
it("folds URL_FAILED into FAILED, not READY_TO_PROCESS", () => {
|
|
15
|
-
expect(boardColumn("URL_FAILED")).toBe("FAILED");
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("folds URL_PERMANENT_FAILURE into FAILED", () => {
|
|
19
|
-
expect(boardColumn("URL_PERMANENT_FAILURE")).toBe("FAILED");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("folds duplicate content into COMPLETED", () => {
|
|
23
|
-
expect(boardColumn("URL_DUPLICATE_CONTENT")).toBe("COMPLETED");
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
describe("getScanKpiCounts()", () => {
|
|
28
|
-
it("sums to the scan's total task count and omits zero-count statuses", async () => {
|
|
29
|
-
const scanId = new ObjectId().toString();
|
|
30
|
-
await getScanResourcesCollection().insertMany([
|
|
31
|
-
createScanResourceFixture({ scanId, status: "URL_COMPLETED" }),
|
|
32
|
-
createScanResourceFixture({ scanId, status: "URL_COMPLETED" }),
|
|
33
|
-
createScanResourceFixture({ scanId, status: "URL_PERMANENT_FAILURE" }),
|
|
34
|
-
]);
|
|
35
|
-
|
|
36
|
-
const counts = await getScanKpiCounts(scanId);
|
|
37
|
-
|
|
38
|
-
expect(counts.URL_COMPLETED).toBe(2);
|
|
39
|
-
expect(counts.URL_PERMANENT_FAILURE).toBe(1);
|
|
40
|
-
expect(counts.URL_PENDING).toBeUndefined();
|
|
41
|
-
const total = Object.values(counts).reduce((sum, n) => sum + (n ?? 0), 0);
|
|
42
|
-
expect(total).toBe(3);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe("getScanPages()", () => {
|
|
47
|
-
it("never returns context or stageLogs, and paginates within one column", async () => {
|
|
48
|
-
const scanId = new ObjectId().toString();
|
|
49
|
-
await getScanResourcesCollection().insertMany(
|
|
50
|
-
Array.from({ length: 3 }, () =>
|
|
51
|
-
createScanResourceFixture({
|
|
52
|
-
scanId,
|
|
53
|
-
status: "URL_COMPLETED",
|
|
54
|
-
context: "heavy markdown content",
|
|
55
|
-
stageLogs: [{ from: null, to: "URL_COMPLETED", occurredAt: new Date(), details: {} }],
|
|
56
|
-
}),
|
|
57
|
-
),
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
const page1 = await getScanPages({ scanId, column: "COMPLETED", limit: 2 });
|
|
61
|
-
expect(page1.items).toHaveLength(2);
|
|
62
|
-
expect(page1.nextCursor).not.toBeNull();
|
|
63
|
-
page1.items.forEach((item) => {
|
|
64
|
-
expect(item).not.toHaveProperty("context");
|
|
65
|
-
expect(item).not.toHaveProperty("stageLogs");
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const page2 = await getScanPages({
|
|
69
|
-
scanId,
|
|
70
|
-
column: "COMPLETED",
|
|
71
|
-
limit: 2,
|
|
72
|
-
cursor: page1.nextCursor,
|
|
73
|
-
});
|
|
74
|
-
expect(page2.items).toHaveLength(1);
|
|
75
|
-
expect(page2.nextCursor).toBeNull();
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("folds URL_FAILED into the FAILED column query", async () => {
|
|
79
|
-
const scanId = new ObjectId().toString();
|
|
80
|
-
await getScanResourcesCollection().insertMany([
|
|
81
|
-
createScanResourceFixture({ scanId, status: "URL_READY_TO_PROCESS" }),
|
|
82
|
-
createScanResourceFixture({ scanId, status: "URL_FAILED" }),
|
|
83
|
-
createScanResourceFixture({ scanId, status: "URL_PERMANENT_FAILURE" }),
|
|
84
|
-
]);
|
|
85
|
-
|
|
86
|
-
const ready = await getScanPages({ scanId, column: "READY_TO_PROCESS" });
|
|
87
|
-
expect(ready.items).toHaveLength(1);
|
|
88
|
-
|
|
89
|
-
const failed = await getScanPages({ scanId, column: "FAILED" });
|
|
90
|
-
expect(failed.items).toHaveLength(2);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
describe("getScanResourceCountsByScanIds()", () => {
|
|
95
|
-
it("returns per-scan totals and omits scans with no resources", async () => {
|
|
96
|
-
const scanA = new ObjectId().toString();
|
|
97
|
-
const scanB = new ObjectId().toString();
|
|
98
|
-
await getScanResourcesCollection().insertMany([
|
|
99
|
-
createScanResourceFixture({ scanId: scanA, status: "URL_COMPLETED" }),
|
|
100
|
-
createScanResourceFixture({ scanId: scanA, status: "URL_COMPLETED" }),
|
|
101
|
-
createScanResourceFixture({ scanId: scanB, status: "URL_PENDING" }),
|
|
102
|
-
]);
|
|
103
|
-
|
|
104
|
-
const counts = await getScanResourceCountsByScanIds([scanA, scanB, "missing"]);
|
|
105
|
-
expect(counts[scanA]).toBe(2);
|
|
106
|
-
expect(counts[scanB]).toBe(1);
|
|
107
|
-
expect(counts.missing).toBeUndefined();
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe("getScanResourceById()", () => {
|
|
112
|
-
it("returns the full document including stageLogs in insertion order", async () => {
|
|
113
|
-
const scanId = new ObjectId().toString();
|
|
114
|
-
const stageLogs = [
|
|
115
|
-
{ from: null, to: "URL_PENDING" as const, occurredAt: new Date("2024-01-01"), details: {} },
|
|
116
|
-
{ from: "URL_PENDING" as const, to: "URL_SCANNING" as const, occurredAt: new Date("2024-01-02"), details: {} },
|
|
117
|
-
];
|
|
118
|
-
const fixture = createScanResourceFixture({ scanId, stageLogs, context: "full content" });
|
|
119
|
-
const { insertedId } = await getScanResourcesCollection().insertOne(fixture);
|
|
120
|
-
|
|
121
|
-
const result = await getScanResourceById(scanId, insertedId.toString());
|
|
122
|
-
|
|
123
|
-
expect(result?.context).toBe("full content");
|
|
124
|
-
expect(result?.stageLogs).toEqual(stageLogs);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it("returns null when the scan does not match", async () => {
|
|
128
|
-
const fixture = createScanResourceFixture();
|
|
129
|
-
const { insertedId } = await getScanResourcesCollection().insertOne(fixture);
|
|
130
|
-
|
|
131
|
-
const result = await getScanResourceById(new ObjectId().toString(), insertedId.toString());
|
|
132
|
-
|
|
133
|
-
expect(result).toBeNull();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
});
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
export const SCAN_RESOURCES_COLLECTION = "scanResources";
|
|
2
|
-
|
|
3
|
-
export const URL_TASK_STATUSES = {
|
|
4
|
-
URL_PENDING: "URL_PENDING",
|
|
5
|
-
URL_SCANNING: "URL_SCANNING",
|
|
6
|
-
URL_READY_TO_PROCESS: "URL_READY_TO_PROCESS",
|
|
7
|
-
URL_PENDING_CHUNKS: "URL_PENDING_CHUNKS",
|
|
8
|
-
URL_CHUNKING: "URL_CHUNKING",
|
|
9
|
-
URL_EMBEDDING: "URL_EMBEDDING",
|
|
10
|
-
URL_COMPLETED: "URL_COMPLETED",
|
|
11
|
-
URL_FAILED: "URL_FAILED",
|
|
12
|
-
URL_DUPLICATE_CONTENT: "URL_DUPLICATE_CONTENT",
|
|
13
|
-
URL_PERMANENT_FAILURE: "URL_PERMANENT_FAILURE",
|
|
14
|
-
} as const;
|
|
15
|
-
|
|
16
|
-
export const BOARD_COLUMN_STATUSES = {
|
|
17
|
-
PENDING: [URL_TASK_STATUSES.URL_PENDING],
|
|
18
|
-
SCANNING: [URL_TASK_STATUSES.URL_SCANNING],
|
|
19
|
-
READY_TO_PROCESS: [URL_TASK_STATUSES.URL_READY_TO_PROCESS],
|
|
20
|
-
CHUNKING: [URL_TASK_STATUSES.URL_CHUNKING, URL_TASK_STATUSES.URL_PENDING_CHUNKS],
|
|
21
|
-
PROCESSING: [URL_TASK_STATUSES.URL_EMBEDDING],
|
|
22
|
-
COMPLETED: [
|
|
23
|
-
URL_TASK_STATUSES.URL_COMPLETED,
|
|
24
|
-
URL_TASK_STATUSES.URL_DUPLICATE_CONTENT,
|
|
25
|
-
],
|
|
26
|
-
FAILED: [
|
|
27
|
-
URL_TASK_STATUSES.URL_PERMANENT_FAILURE,
|
|
28
|
-
URL_TASK_STATUSES.URL_FAILED,
|
|
29
|
-
],
|
|
30
|
-
} as const;
|
|
31
|
-
|
|
32
|
-
export const SCAN_RESOURCE_BOARD_PROJECTION = {
|
|
33
|
-
url: 1,
|
|
34
|
-
title: 1,
|
|
35
|
-
status: 1,
|
|
36
|
-
statusEnteredAt: 1,
|
|
37
|
-
retryCount: 1,
|
|
38
|
-
processRetryCount: 1,
|
|
39
|
-
globalResurrections: 1,
|
|
40
|
-
noRetry: 1,
|
|
41
|
-
errorMessage: 1,
|
|
42
|
-
updatedAt: 1,
|
|
43
|
-
sourceType: 1,
|
|
44
|
-
} as const;
|
|
45
|
-
|
|
46
|
-
export const DEFAULT_PAGE_LIMIT = 4;
|
|
47
|
-
export const MAX_PAGE_LIMIT = 50;
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { Collection, Filter, ObjectId } from "mongodb";
|
|
2
|
-
import { getDb } from "../index";
|
|
3
|
-
import {
|
|
4
|
-
DEFAULT_PAGE_LIMIT,
|
|
5
|
-
MAX_PAGE_LIMIT,
|
|
6
|
-
SCAN_RESOURCES_COLLECTION,
|
|
7
|
-
SCAN_RESOURCE_BOARD_PROJECTION,
|
|
8
|
-
} from "./scanResources.constants";
|
|
9
|
-
import type {
|
|
10
|
-
PagedResult,
|
|
11
|
-
ScanPagesQuery,
|
|
12
|
-
ScanResource,
|
|
13
|
-
ScanResourceBoardProjection,
|
|
14
|
-
ScanResourceDoc,
|
|
15
|
-
UrlTaskStatus,
|
|
16
|
-
} from "./scanResources.types";
|
|
17
|
-
import { decodeCursor, encodeCursor, statusesForColumn } from "./scanResources.utils";
|
|
18
|
-
|
|
19
|
-
export const getScanResourcesCollection = (): Collection<ScanResource> =>
|
|
20
|
-
getDb().collection<ScanResource>(SCAN_RESOURCES_COLLECTION);
|
|
21
|
-
|
|
22
|
-
export const getScanKpiCounts = async (
|
|
23
|
-
scanId: string,
|
|
24
|
-
): Promise<Partial<Record<UrlTaskStatus, number>>> => {
|
|
25
|
-
const rows = await getScanResourcesCollection()
|
|
26
|
-
.aggregate<{ _id: UrlTaskStatus; count: number }>([
|
|
27
|
-
{ $match: { scanId } },
|
|
28
|
-
{ $group: { _id: "$status", count: { $sum: 1 } } },
|
|
29
|
-
])
|
|
30
|
-
.toArray();
|
|
31
|
-
|
|
32
|
-
return rows.reduce<Partial<Record<UrlTaskStatus, number>>>((acc, row) => {
|
|
33
|
-
acc[row._id] = row.count;
|
|
34
|
-
return acc;
|
|
35
|
-
}, {});
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export const getScanResourceCountsByScanIds = async (
|
|
39
|
-
scanIds: string[],
|
|
40
|
-
): Promise<Record<string, number>> => {
|
|
41
|
-
if (scanIds.length === 0) return {};
|
|
42
|
-
|
|
43
|
-
const rows = await getScanResourcesCollection()
|
|
44
|
-
.aggregate<{ _id: string; count: number }>([
|
|
45
|
-
{ $match: { scanId: { $in: scanIds } } },
|
|
46
|
-
{ $group: { _id: "$scanId", count: { $sum: 1 } } },
|
|
47
|
-
])
|
|
48
|
-
.toArray();
|
|
49
|
-
|
|
50
|
-
return Object.fromEntries(rows.map((row) => [row._id, row.count]));
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export const getScanPages = async (
|
|
54
|
-
query: ScanPagesQuery,
|
|
55
|
-
): Promise<PagedResult<ScanResourceBoardProjection>> => {
|
|
56
|
-
const limit = Math.min(query.limit ?? DEFAULT_PAGE_LIMIT, MAX_PAGE_LIMIT);
|
|
57
|
-
|
|
58
|
-
const filter: Filter<ScanResource> = {
|
|
59
|
-
scanId: query.scanId,
|
|
60
|
-
status: { $in: [...statusesForColumn(query.column)] },
|
|
61
|
-
};
|
|
62
|
-
if (query.cursor) {
|
|
63
|
-
filter._id = { $gt: decodeCursor(query.cursor) };
|
|
64
|
-
}
|
|
65
|
-
if (query.search) {
|
|
66
|
-
filter.$or = [
|
|
67
|
-
{ url: { $regex: query.search, $options: "i" } },
|
|
68
|
-
{ title: { $regex: query.search, $options: "i" } },
|
|
69
|
-
];
|
|
70
|
-
}
|
|
71
|
-
if (query.since) {
|
|
72
|
-
filter.updatedAt = { $gte: query.since };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const docs = await getScanResourcesCollection()
|
|
76
|
-
.find(filter, {
|
|
77
|
-
projection: SCAN_RESOURCE_BOARD_PROJECTION,
|
|
78
|
-
sort: { _id: 1 },
|
|
79
|
-
limit: limit + 1,
|
|
80
|
-
})
|
|
81
|
-
.toArray();
|
|
82
|
-
|
|
83
|
-
const hasMore = docs.length > limit;
|
|
84
|
-
const items = (hasMore ? docs.slice(0, limit) : docs) as ScanResourceBoardProjection[];
|
|
85
|
-
const nextCursor = hasMore ? encodeCursor(items[items.length - 1]._id) : null;
|
|
86
|
-
|
|
87
|
-
return { items, nextCursor };
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export const getScanResourceById = async (
|
|
91
|
-
scanId: string,
|
|
92
|
-
pageId: string,
|
|
93
|
-
): Promise<ScanResourceDoc | null> =>
|
|
94
|
-
getScanResourcesCollection().findOne({ scanId, _id: new ObjectId(pageId) });
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { WithId } from "mongodb";
|
|
2
|
-
import type {
|
|
3
|
-
BOARD_COLUMN_STATUSES,
|
|
4
|
-
URL_TASK_STATUSES,
|
|
5
|
-
} from "./scanResources.constants";
|
|
6
|
-
|
|
7
|
-
export type UrlTaskStatus =
|
|
8
|
-
(typeof URL_TASK_STATUSES)[keyof typeof URL_TASK_STATUSES];
|
|
9
|
-
|
|
10
|
-
export type BoardColumn = keyof typeof BOARD_COLUMN_STATUSES;
|
|
11
|
-
|
|
12
|
-
export type StageLogEntry = {
|
|
13
|
-
from: UrlTaskStatus | null;
|
|
14
|
-
to: UrlTaskStatus;
|
|
15
|
-
occurredAt: Date;
|
|
16
|
-
details: Record<string, unknown>;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export type ScanResource = {
|
|
20
|
-
urlHashId: string;
|
|
21
|
-
clientId: string;
|
|
22
|
-
scanId: string;
|
|
23
|
-
websiteUrlId: string;
|
|
24
|
-
url: string;
|
|
25
|
-
urlCanonical?: string | null;
|
|
26
|
-
title?: string | null;
|
|
27
|
-
h1?: string | null;
|
|
28
|
-
language?: string | null;
|
|
29
|
-
sourceType?: string | null;
|
|
30
|
-
context?: string | null;
|
|
31
|
-
contentHash?: string | null;
|
|
32
|
-
duplicateOfTaskId?: string | null;
|
|
33
|
-
breadcrumbs?: unknown;
|
|
34
|
-
description?: string | null;
|
|
35
|
-
sourceUrl?: string | null;
|
|
36
|
-
errorMessage?: string | null;
|
|
37
|
-
|
|
38
|
-
status: UrlTaskStatus;
|
|
39
|
-
depth: number;
|
|
40
|
-
retryCount: number;
|
|
41
|
-
|
|
42
|
-
processRetryCount?: number;
|
|
43
|
-
globalResurrections?: number;
|
|
44
|
-
noRetry?: boolean;
|
|
45
|
-
reviveAt?: Date | null;
|
|
46
|
-
statusEnteredAt?: Date;
|
|
47
|
-
|
|
48
|
-
httpStatus?: number;
|
|
49
|
-
finalUrl?: string;
|
|
50
|
-
batchDone?: number;
|
|
51
|
-
batchTotal?: number;
|
|
52
|
-
|
|
53
|
-
stageLogs?: StageLogEntry[];
|
|
54
|
-
|
|
55
|
-
createdAt: Date;
|
|
56
|
-
updatedAt: Date;
|
|
57
|
-
fetchedAt?: Date | null;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export type ScanResourceDoc = WithId<ScanResource>;
|
|
61
|
-
|
|
62
|
-
export type ScanResourceBoardProjection = Pick<
|
|
63
|
-
ScanResourceDoc,
|
|
64
|
-
| "_id"
|
|
65
|
-
| "url"
|
|
66
|
-
| "title"
|
|
67
|
-
| "status"
|
|
68
|
-
| "statusEnteredAt"
|
|
69
|
-
| "retryCount"
|
|
70
|
-
| "processRetryCount"
|
|
71
|
-
| "globalResurrections"
|
|
72
|
-
| "noRetry"
|
|
73
|
-
| "errorMessage"
|
|
74
|
-
| "updatedAt"
|
|
75
|
-
| "sourceType"
|
|
76
|
-
>;
|
|
77
|
-
|
|
78
|
-
export type ScanPagesQuery = {
|
|
79
|
-
scanId: string;
|
|
80
|
-
column: BoardColumn;
|
|
81
|
-
search?: string;
|
|
82
|
-
cursor?: string | null;
|
|
83
|
-
since?: Date;
|
|
84
|
-
limit?: number;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export type PagedResult<T> = {
|
|
88
|
-
items: T[];
|
|
89
|
-
nextCursor: string | null;
|
|
90
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ObjectId } from "mongodb";
|
|
2
|
-
import { BOARD_COLUMN_STATUSES } from "./scanResources.constants";
|
|
3
|
-
import type { BoardColumn, UrlTaskStatus } from "./scanResources.types";
|
|
4
|
-
|
|
5
|
-
const STATUS_TO_COLUMN: Record<UrlTaskStatus, BoardColumn> = Object.entries(
|
|
6
|
-
BOARD_COLUMN_STATUSES,
|
|
7
|
-
).reduce(
|
|
8
|
-
(acc, [column, statuses]) => {
|
|
9
|
-
for (const status of statuses) {
|
|
10
|
-
acc[status] = column as BoardColumn;
|
|
11
|
-
}
|
|
12
|
-
return acc;
|
|
13
|
-
},
|
|
14
|
-
{} as Record<UrlTaskStatus, BoardColumn>,
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
export const boardColumn = (status: UrlTaskStatus): BoardColumn =>
|
|
18
|
-
STATUS_TO_COLUMN[status];
|
|
19
|
-
|
|
20
|
-
export const statusesForColumn = (
|
|
21
|
-
column: BoardColumn,
|
|
22
|
-
): readonly UrlTaskStatus[] => BOARD_COLUMN_STATUSES[column];
|
|
23
|
-
|
|
24
|
-
export const encodeCursor = (id: ObjectId): string =>
|
|
25
|
-
Buffer.from(id.toHexString()).toString("base64");
|
|
26
|
-
|
|
27
|
-
export const decodeCursor = (cursor: string): ObjectId =>
|
|
28
|
-
new ObjectId(Buffer.from(cursor, "base64").toString("utf-8"));
|