134 lines
4.1 KiB
TypeScript
134 lines
4.1 KiB
TypeScript
import assert from "node:assert";
|
|
import { lineToCreateLineDto, ObjeniousLine, ObjeniousLineResponse } from "sim-shared/domain/objeniousLine.js";
|
|
import { tryCatch, Result } from "sim-shared/domain/Result.js";
|
|
import { HttpClient } from "sim-shared/infrastructure/HTTPClient.js";
|
|
import { ObjeniousLinesRepository } from "../infranstructure/ObjeniousLinesRepository.js";
|
|
import { AxiosResponse } from "axios";
|
|
import { constants } from "node:buffer";
|
|
|
|
const MAX_PAGE_SIZE = 100
|
|
|
|
export class TaskVolcadoLineas {
|
|
constructor(
|
|
private readonly httpClient: HttpClient,
|
|
private readonly linesRepository: ObjeniousLinesRepository,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Mover al repo
|
|
*/
|
|
private async * getLinesByStatus(args?: {
|
|
pageSize?: number,
|
|
pageNumber?: number,
|
|
status?: string
|
|
}): AsyncGenerator<Result<string, ObjeniousLine[]>, Result<string, ObjeniousLine[]>, any> {
|
|
|
|
const path = "/lines"
|
|
const pageSize = args?.pageSize ?? MAX_PAGE_SIZE;
|
|
|
|
let currentPage = args?.pageNumber ?? 0;
|
|
let totalPages: number | undefined = undefined; // Como limite de paginas, igual es pasarse pero hasta que se lea
|
|
|
|
const params: Record<string, string | number> = {}
|
|
|
|
const loadNextLine = async (page: number): Promise<Result<string, ObjeniousLine[]>> => {
|
|
if (args?.status != undefined) params["simStatus"] = args.status
|
|
params["pageSize"] = pageSize
|
|
params["pageNumber"] = page
|
|
console.log("Params", params)
|
|
console.log(`[i] Cargando pagina ${currentPage} de ${totalPages ?? "(desc)"}`)
|
|
const nextPage = await tryCatch<AxiosResponse<ObjeniousLineResponse>>(this.httpClient.client.get(path, {
|
|
params: params
|
|
}))
|
|
|
|
if (nextPage.error != undefined) {
|
|
console.error(nextPage.error.msg)
|
|
return {
|
|
error: nextPage.error.msg.message
|
|
}
|
|
}
|
|
|
|
// Se aumenta para la siguiente ejecucion
|
|
console.log(`[i] Página ${currentPage} completa, total: ${nextPage.data.data.totalPages}`)
|
|
totalPages = nextPage.data.data.totalPages
|
|
|
|
return {
|
|
data: nextPage.data.data.content
|
|
}
|
|
|
|
}
|
|
|
|
// El inicio se ejecuta siempre
|
|
const lines = await loadNextLine(currentPage)
|
|
|
|
if (lines.error != undefined) {
|
|
console.error("[x] Error obteniendo las lineas, cancelando operación");
|
|
return {
|
|
error: "Error cargando lineas"
|
|
}
|
|
}
|
|
|
|
currentPage++;
|
|
|
|
yield {
|
|
data: lines.data
|
|
}
|
|
|
|
// Copia para evitar bucles infinitos por error de la api
|
|
const maxPages = totalPages
|
|
assert.ok(maxPages != undefined, "No se ha defindo el numero de paginas") // Nunca deberia pasar pero así se evitan bucles infnitos
|
|
console.log("maxPages", maxPages)
|
|
for (let i = currentPage; i < maxPages!; i++) {
|
|
console.log("Bucle i:", i, "page: ", currentPage)
|
|
yield await loadNextLine(currentPage);
|
|
currentPage++;
|
|
}
|
|
|
|
return {
|
|
data: []
|
|
}
|
|
}
|
|
|
|
private async saveLines(lines: ObjeniousLine[]) {
|
|
const linesToCreate = lines.map(lineToCreateLineDto)
|
|
let created: number[] = []
|
|
|
|
|
|
for (const line of linesToCreate) {
|
|
// Si es lento pasar a Promise.all
|
|
const res = await this.linesRepository.insertOrUpdate(line)
|
|
if (res?.id != undefined)
|
|
created.push(res.id)
|
|
}
|
|
}
|
|
|
|
public async loadLines() {
|
|
console.log("[i] Iniciando task de volcado de lineas de Objenious")
|
|
// Carga todas las lineas en memoria, hay que comprobar que no se gaste demasiada
|
|
|
|
const linesIterator = this.getLinesByStatus()
|
|
let lines = await linesIterator.next()
|
|
|
|
if (lines.value.error != undefined || lines.value.data == undefined) {
|
|
console.error("[x] Error cargando las lineas a volcar", lines.value.error)
|
|
return;
|
|
}
|
|
|
|
await this.saveLines(lines.value.data)
|
|
|
|
while (!lines.done) {
|
|
console.log()
|
|
lines = await linesIterator.next()
|
|
if (lines.value.error != undefined || lines.value.data == undefined) {
|
|
console.error("[x] Error cargando las lineas a volcar", lines.value.error)
|
|
return;
|
|
}
|
|
await this.saveLines(lines.value.data)
|
|
}
|
|
|
|
console.log("[i] Terminado task de volcado de lineas de Objenious")
|
|
}
|
|
|
|
}
|