All files / languages/sparql/services sparql-query-state.ts

100% Statements 18/18
92.85% Branches 13/14
100% Functions 3/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258                                                                                                                                                                                                                                                                                            7x             16x 3x   3x         13x   16x 3x   10x                                                                                                                                   26x   26x   26x                       13x   13x   13x 6x   6x   6x     13x  
import { PrefixMap } from "@src/utilities";
import { Term } from "@rdfjs/types";
import { getFileName } from "@src/utilities/uri";
 
export type SparqlQueryType = 'bindings' | 'boolean' | 'quads' | 'void';
 
/**
 * The state of a SPARQL query execution.
 */
export interface SparqlQueryExecutionState {
	/**
	 * A unique identifier for the query execution.
	 */
	id: string;
	
	/**
	 * The TextDocument where the SPARQL query is defined. In case of a Notebook, this 
	 * is the IRI of the document that is associated with the cell.
	 * For background queries this is initially `undefined` and set only after the user clicks Edit.
	 */
	documentIri?: string;
 
	/**
	 * A human-readable label for the query, used when no document is associated (background queries).
	 * For example: 'List Graphs'.
	 */
	label?: string;
 
	/**
	 * The ID of the SPARQL connection used to execute the query.
	 * Set for background queries that are not associated with a document.
	 */
	connectionId?: string;
 
	/**
	 * The display name of the SPARQL connection used to execute the query.
	 * Used as the tab title for background queries.
	 */
	connectionName?: string;
 
	/**
	 * When `true`, the query was executed as a background query without an open editor document.
	 * Background queries are excluded from the Recent Queries history list.
	 */
	isBackground?: boolean;
 
	/**
	 * When `true`, the `query` text was generated (e.g. rendered from a triplate template) and
	 * differs from the content of the `documentIri` document. In that case the panel's
	 * "Edit query" action reveals the generated query text rather than the source document.
	 */
	isGenerated?: boolean;
 
	/**
	 * The workspace relative URI of the `documentIri`.
	 */
	workspaceIri?: string;
 
	/**
	 * The IRI of the notebook file where the SPARQL query is run, if applicable.
	 */
	notebookIri?: string;
 
	/**
	 * Id of the notebook cell associated with the SPARQL query, if any.
	 */
	cellIndex?: number;
 
	/**
	 * The SPARQL query text.
	 */
	query?: string;
 
	/**
	 * The SPARQL query type.
	 */
	queryType?: SparqlQueryType;
 
	/**
	 * The time when the query was executed in milliseconds since midnight, January 1, 1970 UTC.
	 */
	startTime: number;
 
	/**
	 * The time when the query finished executing in milliseconds since midnight, January 1, 1970 UTC.
	 */
	endTime?: number;
 
	/**
	 * The error that occurred during query execution, if any.
	 */
	error?: any;
 
	/**
	 * The results of the query execution, if any.
	 */
	result?: BindingsResult | BooleanResult | QuadsResult;
 
	/**
	 * The raw, unparsed HTTP response captured from the SPARQL endpoint, if the request
	 * reached the server. Available for remote (HTTP) connections only; the in-memory
	 * workspace store makes no HTTP request and therefore has no raw response. Present for
	 * both successful queries and HTTP error responses (e.g. a `400` with an error body),
	 * but `undefined` when the request never completed (e.g. `TypeError: fetch failed`).
	 */
	rawResponse?: SparqlRawResponse;
}
 
/**
 * The raw, unparsed HTTP response captured from a SPARQL endpoint, for inspection.
 */
export interface SparqlRawResponse {
	/**
	 * The URL the response was received from.
	 */
	url: string;
 
	/**
	 * The HTTP status code of the response.
	 */
	status: number;
 
	/**
	 * The HTTP status text of the response.
	 */
	statusText: string;
 
	/**
	 * The value of the `Content-Type` response header, if any. Used to pick a syntax
	 * highlighting language when the raw response is opened in an editor.
	 */
	contentType?: string;
 
	/**
	 * The raw, unparsed response body. May be truncated if it exceeds the capture limit.
	 */
	body: string;
}
 
/**
 * Maximum length for a truncated connection name used as a tab title.
 */
const MAX_CONNECTION_NAME_LENGTH = 20;
 
/**
 * Get the formatted file name of the associated document or notebook cell.
 * For background queries, returns the truncated connection name.
 */
export function getDisplayName(queryState: SparqlQueryExecutionState): string {
	if (queryState.isBackground && queryState.connectionName) {
		const name = queryState.connectionName;
		
		return name.length > MAX_CONNECTION_NAME_LENGTH
			? name.slice(0, MAX_CONNECTION_NAME_LENGTH - 1) + '…'
			: name;
	}
 
	const fileName = getFileName(queryState.documentIri ?? '');
 
	if (queryState.notebookIri && queryState.cellIndex !== undefined) {
		return `${fileName.split('#')[0]}:Cell-${queryState.cellIndex}`;
	} else {
		return fileName;
	}
}
 
/**
 * Represents the results of a SPARQL query that returns bindings.
 */
export interface BindingsResult {
	/**
	 * The type of the result, which is always 'bindings' for SELECT queries.
	 */
	type: 'bindings';
 
	/**
	 * The column headers of the result table.
	 */
	columns: string[];
 
	/**
	 * The rows of the result table containing the data.
	 */
	rows: Record<string, Term>[];
 
	/**
	 * A map of namespace IRIs to prefixes defined in the document or the workspace.
	 */
	namespaceMap: PrefixMap;
}
 
export interface BooleanResult {
	/**
	 * The type of the result, which is always 'boolean' for ASK queries.
	 */
	type: 'boolean';
 
	/**
	 * Boolean result value for ASK queries.
	 */
	value: boolean;
}
 
export interface QuadsResult {
	/**
	 * The type of the result, which is always 'quads' for CONSTRUCT or DESCRIBE queries.
	 */
	type: 'quads';
 
	/**
	 * The returned RDF document in the given serialization format.
	 */
	document: string;
 
	/**
	 * The MIME type of the returned document.
	 */
	mimeType: string;
}
 
/**
 * Returns a copy of a query execution state without its raw response. The raw
 * response can hold megabytes and is read by the host-side "View Raw Response"
 * command only, so it never travels to a webview or into a notebook output.
 * @param state The query execution state.
 * @returns A shallow copy of the state without the raw response.
 */
export function getQueryStateWithoutRawResponse(state: SparqlQueryExecutionState): SparqlQueryExecutionState {
	const copy: SparqlQueryExecutionState = { ...state };
 
	delete copy.rawResponse;
 
	return copy;
}
 
/**
 * Returns a copy of a query execution state without its result, its raw
 * response and the stack of its error, which are the fields that can hold
 * megabytes. The history persists this compact state, and the results webview
 * receives it for every query except the ones it renders.
 * @param state The query execution state.
 * @returns A shallow copy of the state without the result, the raw response and the error stack.
 */
export function getQueryStateWithoutResult(state: SparqlQueryExecutionState): SparqlQueryExecutionState {
	const copy = getQueryStateWithoutRawResponse(state);
 
	delete copy.result;
 
	if (copy.error) {
		const error = { ...copy.error };
 
		delete error.stack;
 
		copy.error = error;
	}
 
	return copy;
}