Skip to content

Commit

Permalink
questions: Crear módulo utils
Browse files Browse the repository at this point in the history
  • Loading branch information
saulvaldelvira committed Mar 9, 2025
1 parent e03ff76 commit 988a7e0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 396 deletions.
13 changes: 1 addition & 12 deletions questions/__tests/services/question-db-services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { router } from '../../routes/question-routes'; // Ajusta la ruta si es n
import * as bodyParser from 'body-parser';
import { QuestionDBService } from "../../services/question-db-service";
import { WikidataEntity } from "../../services/wikidata/index";
import { Question } from "../../services/question-data-model";
import { Question } from "../../services/question-data-model";

let mongoServer;
let app = express();
Expand Down Expand Up @@ -74,18 +74,7 @@ describe('QuestionDBService', () => {
await service.generateQuestions(2);

expect(saveMock).toHaveBeenCalledTimes(2);

});
});

describe('resolvePendingPromises', () => {
it('should resolve pending promises', async () => {
// Simular que hay una promesa pendiente
service.pendingPromises.push(Promise.resolve('Resolved'));

await service.resolvePendingPromises();

expect(service.pendingPromises).toHaveLength(0);
});
});

Expand Down
31 changes: 8 additions & 23 deletions questions/services/question-db-service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as mongoose from 'mongoose';
import { Question, IQuestion } from '../../questions/services/question-data-model.ts';
import "../utils/array-chunks.ts"

import { WikidataEntity, Q } from "./wikidata";
import { WikidataQueryBuilder } from "./wikidata/query_builder.ts";

import * as dotenv from "dotenv";
import { PromiseStore } from '../utils/promises.ts';

dotenv.config();

Expand Down Expand Up @@ -41,28 +43,16 @@ export class WikidataQuestion {
}
}

declare global {
interface Array<T> {
chunks(n: number) : Array<Array<T>>;
}
}

function chunks<T>(this: Array<T>, n: number) : Array<Array<T>> {
return this.map((_,i) => i%n ===0 && this.slice(i,i+n))
}

Array.prototype.chunks = chunks;

export class QuestionDBService {
pendingPromises: Promise<any>[] = [];
export class QuestionDBService extends PromiseStore {
private questionsCache: Set<Number> = new Set();

private constructor() {
this.pendingPromises.push(
super();
this.addPromise(
Question.deleteMany().then(() => {
this.generateQuestions(20)
})
)
);
}

private static _instance: QuestionDBService = new QuestionDBService()
Expand All @@ -71,17 +61,12 @@ export class QuestionDBService {
return this._instance
}

async resolvePendingPromises() {
await Promise.all(this.pendingPromises);
this.pendingPromises = []
}

async getRandomQuestions(n: number = 1) : Promise<WikidataQuestion[]> {
/* At this point, we need to sync previously postponed promises.
* Like question deletion, or the initial question generation
* from the constructor.
*/
await this.resolvePendingPromises();
await this.syncPendingPromises();

return this.getRandomEntities(n * 4).then((entities) => {
return entities.chunks(4).map((chunk) => {
Expand Down Expand Up @@ -119,7 +104,7 @@ export class QuestionDBService {
return Question.deleteOne({_id: e._id})
.then(() => { /*console.log("Deleted " + e.wdUri) */ })
})
this.pendingPromises.push(Promise.all(deletions));
this.addPromise(Promise.all(deletions));

return q.map((q: IQuestion) => new WikidataEntity(q.image_url, q.common_name, q.taxon_name))
}
Expand Down
15 changes: 15 additions & 0 deletions questions/utils/array-chunks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare global {
interface Array<T> {
/*
* Return an array, containing the elements of this
* array, split into n chunks
**/
chunks(n: number) : Array<Array<T>>;
}
}

export function chunks<T>(this: Array<T>, n: number) : Array<Array<T>> {
return this.filter((_,i) => i % n === 0).map((_,i) => this.slice(i,i+n))
}

Array.prototype.chunks = chunks;
25 changes: 0 additions & 25 deletions questions/utils/generalQuestions.js

This file was deleted.

18 changes: 18 additions & 0 deletions questions/utils/promises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

export interface IPromiseStore {
addPromise(p: Promise<any>) : void;
syncPendingPromises() : void;
}

export class PromiseStore implements IPromiseStore {
private pendingPromises: Promise<any>[] = [];

addPromise(p: Promise<any>) : void {
this.pendingPromises.push(p);
}

async syncPendingPromises() : Promise<void> {
await Promise.all(this.pendingPromises);
this.pendingPromises = [];
}
}
Loading

0 comments on commit 988a7e0

Please sign in to comment.