Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creative-mode timer #193

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ export default class TRAPIQueryHandler {
'INFO',
null,
`Execution Summary: (${KGNodes}) nodes / (${kgEdges}) edges / (${results}) results; (${resultQueries}/${queries}) queries${cached ? ` (${cached} cached qEdges)` : ''
} returned results from(${sources.length}) unique API${sources.length === 1 ? 's' : ''}`,
} returned results from (${sources.length}) unique API${sources.length !== 1 ? 's' : ''}`,
).getLog(),
new LogEntry('INFO', null, `APIs: ${sources.join(', ')} `).getLog(),
];
Expand Down
42 changes: 15 additions & 27 deletions src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface CombinedResponseReport {
querySuccess: number;
queryHadResults: boolean;
mergedResults: { [resultID: string]: number };
creativeLimitHit: boolean | number;
}

// MatchedTemplate, but with IDs, etc. filled in
Expand Down Expand Up @@ -214,7 +213,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 +243,7 @@ export default class InferredQueryHandler {
delete queryGraph.nodes.creativeQueryObject.ids;
}

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

return subQueries;
Expand All @@ -263,7 +262,6 @@ export default class InferredQueryHandler {
querySuccess: 0,
queryHadResults: false,
mergedResults: {},
creativeLimitHit: false,
};
let mergedThisTemplate = 0;
const resultIDsFromPrevious = new Set(Object.keys(combinedResponse.message.results));
Expand Down Expand Up @@ -428,9 +426,6 @@ export default class InferredQueryHandler {
}
report.querySuccess = 1;

if (Object.keys(combinedResponse.message.results).length >= this.CREATIVE_LIMIT && !report.creativeLimitHit) {
report.creativeLimitHit = Object.keys(newResponse.message.results).length;
}
span.finish();
return report;
}
Expand Down Expand Up @@ -523,7 +518,17 @@ export default class InferredQueryHandler {
[resultID: string]: number;
} = {};

await async.eachOfSeries(subQueries, async ({ template, queryGraph }, i) => {

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

await async.eachOfSeries(subQueries, async ({ template, queryGraph, durationMin }, i) => {
const queryTime = (typeof durationMin == 'number') ? 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' });
span.setData('template', (i as number) + 1);
i = i as number;
Expand All @@ -540,7 +545,7 @@ export default class InferredQueryHandler {
// make query and combine results/kg/logs/etc
handler.setQueryGraph(queryGraph);
await handler.query();
const { querySuccess, queryHadResults, mergedResults, creativeLimitHit } = this.combineResponse(
const { querySuccess, queryHadResults, mergedResults } = this.combineResponse(
i,
handler,
qEdgeID,
Expand All @@ -554,23 +559,6 @@ export default class InferredQueryHandler {
mergedResultsCount[result] =
result in mergedResultsCount ? mergedResultsCount[result] + countMerged : countMerged;
});
// log to user if we should stop
if (creativeLimitHit) {
stop = true;
const message = [
`Addition of ${creativeLimitHit} results from Template ${i + 1}`,
Object.keys(combinedResponse.message.results).length === this.CREATIVE_LIMIT ? ' meets ' : ' exceeds ',
`creative result maximum of ${this.CREATIVE_LIMIT} (reaching ${
Object.keys(combinedResponse.message.results).length
} merged). `,
`Response will be truncated to top-scoring ${this.CREATIVE_LIMIT} results. Skipping remaining ${
subQueries.length - (i + 1)
} `,
subQueries.length - (i + 1) === 1 ? `template.` : `templates.`,
].join('');
debug(message);
combinedResponse.logs.push(new LogEntry(`INFO`, null, message).getLog());
}
span.finish();
} catch (error) {
handler.logs.forEach((log) => {
Expand Down Expand Up @@ -604,7 +592,7 @@ export default class InferredQueryHandler {
`Final result count`,
Object.keys(combinedResponse.message.results).length > this.CREATIVE_LIMIT ? ' (before truncation):' : ':',
` ${Object.keys(combinedResponse.message.results).length}`,
].join(''),
].join('')
).getLog(),
);
}
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
Loading