2025-01-07 16:16:01 -03:00
|
|
|
import { redisConnection } from "../../services/queue-service";
|
|
|
|
|
import { logger as _logger } from "../logger";
|
|
|
|
|
|
2025-01-14 01:45:50 -03:00
|
|
|
export enum ExtractStep {
|
|
|
|
|
INITIAL = "initial",
|
|
|
|
|
MULTI_ENTITY = "multi-entity",
|
|
|
|
|
MULTI_ENTITY_SCRAPE = "multi-entity-scrape",
|
|
|
|
|
MULTI_ENTITY_EXTRACT = "multi-entity-extract",
|
|
|
|
|
SCRAPE = "scrape",
|
|
|
|
|
MAP = "map",
|
|
|
|
|
EXTRACT = "extract",
|
|
|
|
|
COMPLETE = "complete",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type ExtractedStep = {
|
|
|
|
|
step: ExtractStep;
|
|
|
|
|
startedAt: number;
|
|
|
|
|
finishedAt: number;
|
|
|
|
|
error?: any;
|
|
|
|
|
discoveredLinks?: string[];
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-07 16:16:01 -03:00
|
|
|
export type StoredExtract = {
|
|
|
|
|
id: string;
|
|
|
|
|
team_id: string;
|
|
|
|
|
plan?: string;
|
|
|
|
|
createdAt: number;
|
|
|
|
|
status: "processing" | "completed" | "failed" | "cancelled";
|
2025-01-07 16:21:51 -03:00
|
|
|
error?: any;
|
2025-01-14 01:45:50 -03:00
|
|
|
showSteps?: boolean;
|
|
|
|
|
steps?: ExtractedStep[];
|
2025-01-07 16:16:01 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function saveExtract(id: string, extract: StoredExtract) {
|
|
|
|
|
_logger.debug("Saving extract " + id + " to Redis...");
|
|
|
|
|
await redisConnection.set("extract:" + id, JSON.stringify(extract));
|
|
|
|
|
await redisConnection.expire("extract:" + id, 24 * 60 * 60, "NX");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getExtract(id: string): Promise<StoredExtract | null> {
|
|
|
|
|
const x = await redisConnection.get("extract:" + id);
|
|
|
|
|
return x ? JSON.parse(x) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 18:35:10 -03:00
|
|
|
export async function updateExtract(
|
|
|
|
|
id: string,
|
|
|
|
|
extract: Partial<StoredExtract>,
|
|
|
|
|
) {
|
2025-01-07 16:16:01 -03:00
|
|
|
const current = await getExtract(id);
|
|
|
|
|
if (!current) return;
|
2025-01-14 01:45:50 -03:00
|
|
|
|
|
|
|
|
// Handle steps aggregation
|
|
|
|
|
if (extract.steps && current.steps) {
|
|
|
|
|
extract.steps = [...current.steps, ...extract.steps];
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 18:35:10 -03:00
|
|
|
await redisConnection.set(
|
|
|
|
|
"extract:" + id,
|
|
|
|
|
JSON.stringify({ ...current, ...extract }),
|
|
|
|
|
);
|
2025-01-07 16:16:01 -03:00
|
|
|
await redisConnection.expire("extract:" + id, 24 * 60 * 60, "NX");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getExtractExpiry(id: string): Promise<Date> {
|
|
|
|
|
const d = new Date();
|
|
|
|
|
const ttl = await redisConnection.pttl("extract:" + id);
|
|
|
|
|
d.setMilliseconds(d.getMilliseconds() + ttl);
|
|
|
|
|
d.setMilliseconds(0);
|
|
|
|
|
return d;
|
|
|
|
|
}
|