Skip to content

Commit

Permalink
Merge pull request #313 from hearchco/as/feat/scrape-time
Browse files Browse the repository at this point in the history
feat(timing): added backend scrape time
  • Loading branch information
aleksasiriski authored Jun 19, 2024
2 parents ba5ab2f + 3605839 commit 28a141e
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 168 deletions.
7 changes: 4 additions & 3 deletions src/lib/components/header/stats.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
/**
* @typedef {object} Props
* @property {number} numOfResults
* @property {number} timing
* @property {TimingType} timing
*/
/** @type {Props} */
let { numOfResults, timing } = $props();
const timingString = $derived((timing / 1000).toFixed(2));
const timingFetchString = $derived((timing.fetch / 1000).toFixed(2));
const timingScrapeString = $derived((timing.scrape / 1000).toFixed(2));
</script>

<div id="stats" class="mx-auto px-2 pt-2 max-w-screen-sm">
<p class="text-sm text-neutral-800 dark:text-neutral-400">
Hearched {numOfResults} results in {timingString}s 🐹
Hearched {numOfResults} results in {timingFetchString}s (scraped in {timingScrapeString}s) 🐹
</p>
</div>
1 change: 0 additions & 1 deletion src/lib/components/results/images/preview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
/** @type {Props} */
// eslint-disable-next-line no-unused-vars
let { result, imagePreview = $bindable() } = $props();
</script>

Expand Down
1 change: 0 additions & 1 deletion src/lib/components/searchbox/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
homepage = false,
query = '',
category = CategoryEnum.GENERAL,
// eslint-disable-next-line no-unused-vars
loading = $bindable(false)
} = $props();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/functions/api/additionalresults.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fetchResults } from './fetchresults';
* @returns {Promise<ResultType[]>}
*/
export async function fetchAdditionalResults(oldResults, params) {
const newResults = await fetchResults(params);
const newResults = (await fetchResults(params)).results;

// get the last rank from old results
const lastRank = oldResults[oldResults.length - 1].rank;
Expand Down
10 changes: 5 additions & 5 deletions src/lib/functions/api/fetchresults.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createApiUrl } from '$lib/functions/api/createurl.js';
/**
* @param {URLSearchParams} params
* @param {typeof fetch} [fetcher]
* @returns {Promise<ResultType[]>}
* @returns {Promise<ResultsResponseType>}
*/
export async function fetchResults(params, fetcher = fetch) {
/** @type {URL} */
Expand All @@ -31,7 +31,7 @@ export async function fetchResults(params, fetcher = fetch) {
throw error(502, `Failed to fetch results: ${err.message}`);
}

/** @type {ResultType[] | ErrorResponseType} */
/** @type {ResultsResponseType | ErrorResponseType} */
let jsonResponse;
try {
jsonResponse = await response.json();
Expand All @@ -45,8 +45,8 @@ export async function fetchResults(params, fetcher = fetch) {
throw error(response.status, `API error: ${jsonResponse.message}: ${jsonResponse.value}`);
}

/** @type {ResultType[]} */
const results = jsonResponse;
/** @type {ResultsResponseType} */
const resultsResponse = jsonResponse;

return results;
return resultsResponse;
}
6 changes: 0 additions & 6 deletions src/lib/types/search/error.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/lib/types/search/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Defines the structure for error responses.
* @typedef {Object} ErrorResponseType
* @property {string} message - The error message.
* @property {string} value - The error value or code.
*/

/**
* Defines the structure for results responses.
* @typedef {Object} ResultsResponseType
* @property {string} version - The version of the API.
* @property {number} duration - The duration of the search in milliseconds.
* @property {ResultType[]} results - The search results.
*/
6 changes: 6 additions & 0 deletions src/lib/types/search/timing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Defines the structure for timing.
* @typedef {Object} TimingType
* @property {number} fetch - The time it took to fetch the results in milliseconds.
* @property {number} scrape - The time it took to scrape the results in milliseconds.
*/
13 changes: 10 additions & 3 deletions src/routes/search/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,22 @@ export async function load({ url, fetch }) {

// Fetch results
const timerStart = Date.now();
const results = await fetchResults(newSearchParams, fetch);
const res = await fetchResults(newSearchParams, fetch);
const timerEnd = Date.now();

/** @type {TimingType} */
const timing = {
fetch: timerEnd - timerStart,
scrape: res.duration
};

return {
apiVersion: res.version,
query: queryWithoutCategory,
currentPage: currentPage,
maxPages: maxPages,
category: category,
results: results,
timing: timerEnd - timerStart
results: res.results,
timing: timing
};
}
8 changes: 0 additions & 8 deletions src/routes/searchbad/+error.svelte

This file was deleted.

89 changes: 0 additions & 89 deletions src/routes/searchbad/+page.js

This file was deleted.

51 changes: 0 additions & 51 deletions src/routes/searchbad/+page.svelte

This file was deleted.

0 comments on commit 28a141e

Please sign in to comment.