Skip to content
87 changes: 86 additions & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,21 @@
/>
</svg>
Execute
<!-- Shown while "clear cache before every query" is pinned -->
<svg
id="executeClearsCacheIndicator"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="2.2"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
class="size-3 ml-0.5 opacity-85 hidden"
>
<path d="M20 11a8 8 0 0 0-14.9-3.5M4 4v4h4" />
<path d="M4 13a8 8 0 0 0 14.9 3.5M20 20v-4h-4" />
</svg>
</span>
<span class="hidden items-center gap-1 text-gray-100">
<svg
Expand Down Expand Up @@ -779,7 +794,7 @@
</div>
<div
id="clearCacheButton"
class="border-button-border border-x bg-slate-100 dark:bg-slate-700 active:bg-slate-400 dark:active:bg-gray-950 hover:bg-button-hover flex cursor-pointer justify-center py-0.5 px-2"
class="relative border-button-border border-x bg-slate-100 dark:bg-slate-700 active:bg-slate-400 dark:active:bg-gray-950 hover:bg-button-hover has-aria-pressed:bg-green-600/12 has-aria-pressed:text-green-700 dark:has-aria-pressed:text-green-400 has-aria-pressed:hover:bg-button-hover flex cursor-pointer justify-center py-0.5 pl-2 pr-7"
>
<span class="inline-flex items-center gap-1">
<svg
Expand All @@ -798,6 +813,29 @@
</svg>
Clear Cache
</span>
<!-- Pin: clear the cache before every query. aria-pressed="true" also tints the whole Clear Cache cell. -->
<button
id="clearCachePinButton"
type="button"
aria-pressed="false"
title="Pin: clear before every query"
class="absolute right-1 top-1/2 -translate-y-1/2 flex items-center justify-center size-5 rounded text-gray-500 dark:text-gray-400 opacity-45 aria-pressed:text-green-600 aria-pressed:opacity-100 hover:opacity-100 hover:text-green-600 hover:bg-green-600/16 cursor-pointer"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.8"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
class="size-3.25 rotate-45 in-aria-pressed:fill-current"
>
<path d="M12 17v5" />
<path d="M9 10.8V5h6v5.8l2.5 3.2v2h-11v-2Z" />
<path d="M8 3h8" />
</svg>
</button>
</div>
<div
id="analysisButton"
Expand Down Expand Up @@ -1269,6 +1307,28 @@ <h1 class="text-black dark:text-white font-semibold text-md">
</div>
</span>
</div>
<div
id="queryColdRun"
class="border-l py-1 px-2 text-green-600 hidden"
title="The cache was cleared before this query ran, so timings reflect a cold run."
>
<span class="flex items-center gap-1.5">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="2"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
class="size-3.5"
>
<path d="M20 11a8 8 0 0 0-14.9-3.5M4 4v4h4" />
<path d="M4 13a8 8 0 0 0 14.9 3.5M20 20v-4h-4" />
</svg>
Cold run
</span>
</div>
</div>
<a
id="mapViewButton"
Expand Down Expand Up @@ -3226,6 +3286,31 @@ <h2 class="dialog-title">Settings</h2>
<div
class="flex flex-col divide-y divide-neutral-200 dark:divide-neutral-800"
>
<div class="flex flex-row py-4 items-center">
<div class="flex flex-col grow">
<span
class="font-semibold text-neutral-800 dark:text-neutral-200 text-[16px]"
>
Clear cache before every query
</span>
<span
class="font-normal text-neutral-400 dark:text-neutral-500"
>
Run each query cold for comparable timings. Also toggled
with the pin on Clear Cache.
</span>
</div>
<label class="cursor-pointer">
<input
id="settings-general-clearCacheBeforeExecute"
type="checkbox"
value=""
class="sr-only peer"
/>
<div class="toggle-switch"></div>
<span class="sr-only">Clear cache before every query</span>
</label>
</div>
<div class="flex flex-row py-4 items-center">
<div class="flex flex-col grow">
<span
Expand Down
90 changes: 63 additions & 27 deletions frontend/src/buttons/clear_cache.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
import type { Editor } from '../editor/init';
import { setClearCacheBeforeExecute } from '../settings/init';
import type { QlueLsServiceConfig } from '../types/backend';

let cacheClearedUrl: string | null = null;

export async function setupClearCache(editor: Editor) {
const clearCacheButton = document.getElementById('clearCacheButton')!;

const clearCachePinButton = document.getElementById('clearCachePinButton')! as HTMLButtonElement;
clearCacheButton.addEventListener('click', async () => {
clearCache(editor);
});
clearCachePinButton.addEventListener('click', async (e) => {
e.stopPropagation();
const pinned = clearCachePinButton.ariaPressed !== 'true';
setClearCacheBeforeExecute(pinned);
});
}

export async function clearCache(editor: Editor) {
/**
* Asks the configured SPARQL endpoint to clear its cache.
*
* Also used as the `clearCache` command handler, so options come in as
* command parameters:
* - `quiet`: don't show any toasts ("Cache cleared.", "Could not clear cache.",
* "No SPARQL endpoint configured.").
*
* @example clearCache(editor, ['quiet']); // or `clearCache quiet` in the command prompt
*/
export async function clearCache(editor: Editor, params: string[] = []) {
const quiet = params.includes('quiet');
const backend = (await editor.languageClient.sendRequest('qlueLs/getBackend', {})) as
| QlueLsServiceConfig
| { error: string };
if ('error' in backend) {
document.dispatchEvent(
new CustomEvent('toast', {
detail: {
type: 'warning',
message: 'No SPARQL endpoint configured.',
duration: 2000,
},
})
);
if (!quiet) {
document.dispatchEvent(
new CustomEvent('toast', {
detail: {
type: 'warning',
message: 'No SPARQL endpoint configured.',
duration: 2000,
},
})
);
}
} else {
fetch(backend.url, {
return fetch(backend.url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
Expand All @@ -33,29 +54,44 @@ export async function clearCache(editor: Editor) {
})
.then((response) => {
if (response.ok) {
cacheClearedUrl = backend.url;
if (!quiet) {
document.dispatchEvent(
new CustomEvent('toast', {
detail: {
type: 'success',
message: 'Cache cleared.',
duration: 2000,
},
})
);
}
} else {
throw new Error();
}
})
.catch((_err) => {
if (!quiet) {
document.dispatchEvent(
new CustomEvent('toast', {
detail: {
type: 'success',
message: 'Cache cleared.',
type: 'error',
message: 'Could not clear cache.',
duration: 2000,
},
})
);
} else {
throw new Error();
}
})
.catch((_err) => {
document.dispatchEvent(
new CustomEvent('toast', {
detail: {
type: 'error',
message: 'Could not clear cache.',
duration: 2000,
},
})
);
});
}
}

/**
* True if the cache of the endpoint at `url` was cleared since the last query
* started. Resets the flag.
*/
export function takeCacheCleared(url: string): boolean {
const cleared = cacheClearedUrl === url;
cacheClearedUrl = null;
return cleared;
}
6 changes: 6 additions & 0 deletions frontend/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Editor } from '../editor/init';
import { openExamples } from '../examples/utils';
import { openParseTree } from '../parse_tree/init';
import { openQueryAnalysis } from '../query_analysis/init';
import { setClearCacheBeforeExecute, settings } from '../settings/init';
import { openTemplatesEditor } from '../templates/init';
import { displayVersion } from '../utils';
import {
Expand Down Expand Up @@ -36,6 +37,11 @@ export function setupCommands(editor: Editor) {
registerCommand('analysis', openQueryAnalysis, 'Show the query analysis');
registerCommand('templates', openTemplatesEditor, 'Edit query templates');
registerCommand('clearCache', clearCache, 'Clear the triplestore cache');
registerCommand(
'toggleClearCacheBeforeQuery',
() => setClearCacheBeforeExecute(!settings.general.clearCacheBeforeExecute),
'Toggle clearing the cache before every query'
);
registerCommand('toggleWideMode', toggleWideMode, 'Toggle wide editor layout');
registerCommand('version', displayVersion, 'Show the build version');

Expand Down
7 changes: 5 additions & 2 deletions frontend/src/query_analysis/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { clearCache } from '../buttons/clear_cache';
import type { Editor } from '../editor/init';
import { settings } from '../settings/init';
import { getActiveTabName } from '../tabs/init';
import type { QlueLsServiceConfig } from '../types/backend';
import { SparqlEngine } from '../types/lsp_messages';
Expand Down Expand Up @@ -53,9 +54,11 @@ export function watchQueryExecution(queryId: string): Promise<void> {
export function setupQueryAnalysis(editor: Editor) {
const analysisButton = document.getElementById('analysisButton')!;

const { openModal, render, renderStats, clear, setQueryName } = setupQueryAnalysisUi(() => {
const { openModal, render, renderStats, clear, setQueryName } = setupQueryAnalysisUi(async () => {
if (!queryRunning) {
clearCache(editor);
if (!settings.general.clearCacheBeforeExecute) {
await clearCache(editor).catch(() => {});
}
window.dispatchEvent(new Event('execute-start-request'));
}
});
Expand Down
31 changes: 30 additions & 1 deletion frontend/src/results/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from './utils';
import 'sparql-results';
import type { PetrimapsRenderConfig, SparqlResults, TableRenderConfig } from 'sparql-results';
import { clearCache, takeCacheCleared } from '../buttons/clear_cache';
import { watchQueryExecution } from '../query_analysis/init';
import { render_query_error } from './error';

Expand Down Expand Up @@ -79,10 +80,25 @@ export async function setupResults(editor: Editor) {
}

function handleSignals(editor: Editor) {
window.addEventListener('execute-start-request', () => {
window.addEventListener('execute-start-request', async () => {
if (queryStatus === 'idle') {
queryStatus = 'running';
window.dispatchEvent(new CustomEvent('execute-started'));
// NOTE: With the `Clear Cache` pin set, wait for the clear before
// running, otherwise the query might still hit the cache.
if (settings.general.clearCacheBeforeExecute) {
await clearCache(editor, ['quiet']).catch(() => {});
// NOTE: A cancel during the clear arrives before executeQuery
// listens for it, so handle it here.
// The cast undoes TS narrowing to 'running', which doesn't see the
// cancel listener changing queryStatus during the await.
if ((queryStatus as QueryStatus) === 'canceling') {
window.dispatchEvent(
new CustomEvent('execute-ended', { detail: { result: 'canceled' } })
);
return;
}
}
executeQueryAndShowResults(editor);
} else {
document.dispatchEvent(
Expand Down Expand Up @@ -125,6 +141,18 @@ async function executeQueryAndShowResults(editor: Editor) {

showLoadingScreen();
clearQueryStats();
const coldRun = takeCacheCleared(backend.url);
if (settings.general.clearCacheBeforeExecute && !coldRun) {
document.dispatchEvent(
new CustomEvent('toast', {
detail: {
type: 'warning',
message: 'Could not clear cache, this is not a cold run.',
duration: 3000,
},
})
);
}
element.clear();
clearTable();

Expand All @@ -143,6 +171,7 @@ async function executeQueryAndShowResults(editor: Editor) {
0
)
.then((result) => {
document.getElementById('queryColdRun')!.classList.toggle('hidden', !coldRun);
showResults();
stopQueryTimer(timer);
const operationTime = getOperationTimeMs(result);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/results/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function clearQueryStats() {
document.getElementById('queryTimeTotal')!.innerText = '0';
document.getElementById('queryTimeCompute')!.innerText = '0';
document.getElementById('queryTimeComputeContainer')!.classList.add('hidden');
document.getElementById('queryColdRun')!.classList.add('hidden');
}

export function showQueryMetaData(meta: Meta) {
Expand Down
Loading
Loading