Skip to content

Commit

Permalink
configurable setting for query duration on templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Apr 27, 2024
1 parent 6b91619 commit a655ea1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export default class InferredQueryHandler {
async createQueries(qEdge: TrapiQEdge, qSubject: TrapiQNode, qObject: TrapiQNode): Promise<FilledTemplate[]> {
const templates = await this.findTemplates(qEdge, qSubject, qObject);
// combine creative query with templates
const subQueries = templates.map(({ template, queryGraph }) => {
const subQueries = templates.map(({ template, queryGraph, durationMin }) => {
queryGraph.nodes.creativeQuerySubject.categories = [
...new Set([...queryGraph.nodes.creativeQuerySubject.categories, ...qSubject.categories]),
];
Expand Down Expand Up @@ -244,7 +244,7 @@ export default class InferredQueryHandler {
delete queryGraph.nodes.creativeQueryObject.ids;
}

return { template, queryGraph };
return { template, queryGraph, durationMin };
});

return subQueries;
Expand Down Expand Up @@ -525,12 +525,13 @@ export default class InferredQueryHandler {


const MAX_TIME = 4.5 * 60 * 1000; // 4 minutes
const QUERY_TIME = 2.5 * 60 * 1000; // 2.5 minutes
const DEFAULT_QUERY_TIME = 2.5 * 60 * 1000; // 2.5 minutes
const start = Date.now();

await async.eachOfSeries(subQueries, async ({ template, queryGraph }, i) => {
if (Date.now() - start > MAX_TIME - QUERY_TIME) {
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`);
await async.eachOfSeries(subQueries, async ({ template, queryGraph, durationMin }, i) => {
const queryTime = durationMin * 60 * 1000 ?? DEFAULT_QUERY_TIME;
if (Date.now() - start > MAX_TIME - queryTime) {
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`);
return;
}
const span = Telemetry.startSpan({ description: 'creativeTemplate' });
Expand Down
5 changes: 4 additions & 1 deletion src/inferred_mode/template_lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface TemplateLookup {
export interface MatchedTemplate {
template: string;
queryGraph: TrapiQueryGraph;
durationMin?: number;
}

export interface TemplateGroup {
Expand Down Expand Up @@ -77,9 +78,11 @@ export async function getTemplates(lookups: TemplateLookup[]): Promise<MatchedTe
return matches;
}, [] as string[]);
return await async.map(matchingTemplatePaths, async (templatePath: string) => {
const templateData = JSON.parse(await fs.readFile(templatePath, { encoding: 'utf8' }));
return {
template: templatePath.substring(templatePath.lastIndexOf('/') + 1),
queryGraph: JSON.parse(await fs.readFile(templatePath, { encoding: 'utf8' })).message.query_graph,
queryGraph: templateData.message.query_graph,
durationMin: templateData.durationMin
};
});
}
Expand Down

0 comments on commit a655ea1

Please sign in to comment.