fase(4): crawling infrastructure with migrated code
This commit is contained in:
79
dist/modules/crawling/infrastructure/repositories/KyselyCrawlSessionRepository.js
vendored
Normal file
79
dist/modules/crawling/infrastructure/repositories/KyselyCrawlSessionRepository.js
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.KyselyCrawlSessionRepository = void 0;
|
||||
const CrawlSession_1 = require("../../domain/entities/CrawlSession");
|
||||
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
||||
class KyselyCrawlSessionRepository {
|
||||
constructor(db) {
|
||||
this.db = db;
|
||||
}
|
||||
async save(session) {
|
||||
const row = {
|
||||
id: session.id.toString(),
|
||||
url: session.url,
|
||||
status: session.status,
|
||||
seed: session.seed,
|
||||
max_states: session.maxStates,
|
||||
states_visited: session.statesVisited,
|
||||
anomalies_found: 0,
|
||||
started_at: Date.now(),
|
||||
finished_at: null,
|
||||
config_json: JSON.stringify(session.config),
|
||||
};
|
||||
await this.db
|
||||
.insertInto('sessions')
|
||||
.values(row)
|
||||
.execute();
|
||||
}
|
||||
async findById(id) {
|
||||
const row = await this.db
|
||||
.selectFrom('sessions')
|
||||
.selectAll()
|
||||
.where('id', '=', id.toString())
|
||||
.executeTakeFirst();
|
||||
if (!row)
|
||||
return null;
|
||||
return this.toDomain(row);
|
||||
}
|
||||
async findAll() {
|
||||
const rows = await this.db
|
||||
.selectFrom('sessions')
|
||||
.selectAll()
|
||||
.orderBy('started_at', 'desc')
|
||||
.execute();
|
||||
return rows.map((row) => this.toDomain(row));
|
||||
}
|
||||
async update(session) {
|
||||
const isTerminal = session.status === 'completed' || session.status === 'failed' || session.status === 'stopped';
|
||||
await this.db
|
||||
.updateTable('sessions')
|
||||
.set({
|
||||
status: session.status,
|
||||
states_visited: session.statesVisited,
|
||||
finished_at: isTerminal ? Date.now() : null,
|
||||
config_json: JSON.stringify(session.config),
|
||||
})
|
||||
.where('id', '=', session.id.toString())
|
||||
.execute();
|
||||
}
|
||||
toDomain(row) {
|
||||
const props = {
|
||||
url: row.url,
|
||||
status: row.status,
|
||||
seed: row.seed,
|
||||
maxStates: row.max_states,
|
||||
statesVisited: row.states_visited,
|
||||
config: this.parseJson(row.config_json),
|
||||
};
|
||||
return CrawlSession_1.CrawlSession.reconstitute(props, UniqueId_1.UniqueId.from(row.id));
|
||||
}
|
||||
parseJson(json) {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
}
|
||||
catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.KyselyCrawlSessionRepository = KyselyCrawlSessionRepository;
|
||||
72
dist/modules/crawling/infrastructure/repositories/KyselyStateRepository.js
vendored
Normal file
72
dist/modules/crawling/infrastructure/repositories/KyselyStateRepository.js
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.KyselyStateRepository = void 0;
|
||||
const CrawlState_1 = require("../../domain/entities/CrawlState");
|
||||
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
||||
class KyselyStateRepository {
|
||||
constructor(db) {
|
||||
this.db = db;
|
||||
}
|
||||
async save(state) {
|
||||
const row = {
|
||||
id: state.id.toString(),
|
||||
session_id: state.sessionId,
|
||||
url: state.url,
|
||||
title: state.title,
|
||||
dom_snapshot_path: null,
|
||||
visit_count: state.visitCount,
|
||||
discovered_at: Date.now(),
|
||||
};
|
||||
await this.db
|
||||
.insertInto('states')
|
||||
.values(row)
|
||||
.execute();
|
||||
}
|
||||
async findById(id) {
|
||||
const row = await this.db
|
||||
.selectFrom('states')
|
||||
.selectAll()
|
||||
.where('id', '=', id.toString())
|
||||
.executeTakeFirst();
|
||||
if (!row)
|
||||
return null;
|
||||
return this.toDomain(row);
|
||||
}
|
||||
async findAll() {
|
||||
const rows = await this.db
|
||||
.selectFrom('states')
|
||||
.selectAll()
|
||||
.execute();
|
||||
return rows.map((row) => this.toDomain(row));
|
||||
}
|
||||
async findBySessionId(sessionId) {
|
||||
const rows = await this.db
|
||||
.selectFrom('states')
|
||||
.selectAll()
|
||||
.where('session_id', '=', sessionId)
|
||||
.execute();
|
||||
return rows.map((row) => this.toDomain(row));
|
||||
}
|
||||
async update(state) {
|
||||
await this.db
|
||||
.updateTable('states')
|
||||
.set({
|
||||
visit_count: state.visitCount,
|
||||
url: state.url,
|
||||
title: state.title,
|
||||
})
|
||||
.where('id', '=', state.id.toString())
|
||||
.execute();
|
||||
}
|
||||
toDomain(row) {
|
||||
return CrawlState_1.CrawlState.create({
|
||||
url: row.url,
|
||||
title: row.title,
|
||||
domSnapshot: '',
|
||||
visitCount: row.visit_count,
|
||||
stateId: row.id,
|
||||
sessionId: row.session_id,
|
||||
}, UniqueId_1.UniqueId.from(row.id));
|
||||
}
|
||||
}
|
||||
exports.KyselyStateRepository = KyselyStateRepository;
|
||||
Reference in New Issue
Block a user