Skip to content

Commit a655ea1

Browse files
committed
configurable setting for query duration on templates
1 parent 6b91619 commit a655ea1

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/inferred_mode/inferred_mode.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export default class InferredQueryHandler {
214214
async createQueries(qEdge: TrapiQEdge, qSubject: TrapiQNode, qObject: TrapiQNode): Promise<FilledTemplate[]> {
215215
const templates = await this.findTemplates(qEdge, qSubject, qObject);
216216
// combine creative query with templates
217-
const subQueries = templates.map(({ template, queryGraph }) => {
217+
const subQueries = templates.map(({ template, queryGraph, durationMin }) => {
218218
queryGraph.nodes.creativeQuerySubject.categories = [
219219
...new Set([...queryGraph.nodes.creativeQuerySubject.categories, ...qSubject.categories]),
220220
];
@@ -244,7 +244,7 @@ export default class InferredQueryHandler {
244244
delete queryGraph.nodes.creativeQueryObject.ids;
245245
}
246246

247-
return { template, queryGraph };
247+
return { template, queryGraph, durationMin };
248248
});
249249

250250
return subQueries;
@@ -525,12 +525,13 @@ export default class InferredQueryHandler {
525525

526526

527527
const MAX_TIME = 4.5 * 60 * 1000; // 4 minutes
528-
const QUERY_TIME = 2.5 * 60 * 1000; // 2.5 minutes
528+
const DEFAULT_QUERY_TIME = 2.5 * 60 * 1000; // 2.5 minutes
529529
const start = Date.now();
530530

531-
await async.eachOfSeries(subQueries, async ({ template, queryGraph }, i) => {
532-
if (Date.now() - start > MAX_TIME - QUERY_TIME) {
533-
debug(`Skipping template because the query has been running for ${(Date.now() - start) / 1000} seconds, and this template is projected to take ${QUERY_TIME / 1000} seconds`);
531+
await async.eachOfSeries(subQueries, async ({ template, queryGraph, durationMin }, i) => {
532+
const queryTime = durationMin * 60 * 1000 ?? DEFAULT_QUERY_TIME;
533+
if (Date.now() - start > MAX_TIME - queryTime) {
534+
debug(`Skipping template because the query has been running for ${(Date.now() - start) / 1000} seconds, and this template is projected to take ${queryTime / 1000} seconds`);
534535
return;
535536
}
536537
const span = Telemetry.startSpan({ description: 'creativeTemplate' });

src/inferred_mode/template_lookup.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface TemplateLookup {
1717
export interface MatchedTemplate {
1818
template: string;
1919
queryGraph: TrapiQueryGraph;
20+
durationMin?: number;
2021
}
2122

2223
export interface TemplateGroup {
@@ -77,9 +78,11 @@ export async function getTemplates(lookups: TemplateLookup[]): Promise<MatchedTe
7778
return matches;
7879
}, [] as string[]);
7980
return await async.map(matchingTemplatePaths, async (templatePath: string) => {
81+
const templateData = JSON.parse(await fs.readFile(templatePath, { encoding: 'utf8' }));
8082
return {
8183
template: templatePath.substring(templatePath.lastIndexOf('/') + 1),
82-
queryGraph: JSON.parse(await fs.readFile(templatePath, { encoding: 'utf8' })).message.query_graph,
84+
queryGraph: templateData.message.query_graph,
85+
durationMin: templateData.durationMin
8386
};
8487
});
8588
}

0 commit comments

Comments
 (0)