forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get popularities from docs-internal.popular-pages (github#35583)
- Loading branch information
Showing
10 changed files
with
103 additions
and
1,042 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,49 @@ | ||
import fs from 'fs/promises' | ||
|
||
const POPULAR_PAGES_JSON = './lib/search/popular-pages.json' | ||
export default async function getPopularPages(filePath, redirects) { | ||
const popularPagesRaw = await fs.readFile(filePath, 'utf-8') | ||
|
||
export default async function getPopularPages(redirects) { | ||
const popularPages = {} | ||
try { | ||
const popularPagesRaw = await fs.readFile(POPULAR_PAGES_JSON, 'utf-8') | ||
let biggestCount = 0 | ||
for (const line of popularPagesRaw.split('\n')) { | ||
if (!line.trim()) continue | ||
const { path_article: path, path_count: count } = JSON.parse(line) | ||
// The root page or any other potentially dirty record that is empty. | ||
if (!path) continue | ||
// This is safe because the `POPULAR_PAGES_JSON` always lists the | ||
// most popular first. | ||
if (!biggestCount) biggestCount = count | ||
// Don't bother writing massively long floating point numbers | ||
// because reducing it makes the JSON records smaller and we don't | ||
// need any more precision than 7 significant figures. | ||
const ratio = Number((count / biggestCount).toFixed(7)) | ||
// The reason we're heeding redirects is because it's very possible | ||
// that the `POPULAR_PAGES_JSON` file is older/"staler" than the | ||
// content itself. | ||
// Imaging our analytics recorded that `/en/foo` had 1,234 pageviews, | ||
// and someone goes and... `git mv content/foo content/bar` plus | ||
// adding `redirect_from: - /foo` into the front-matter. | ||
// Then, by using the redirects first, we can maintain that popularity | ||
// by now "pretending" that it's `/en/bar` that has 1,234 pageviews. | ||
popularPages[redirects[path] || path] = ratio | ||
// Firt iterate through the array of objects, not making an assumption | ||
// that the first one is the biggest one. | ||
const all = {} | ||
for (const { path_article: path, path_count: count } of JSON.parse(popularPagesRaw)) { | ||
if (!path) { | ||
// Can happen if the SQL query is, for some unknown reason, finding | ||
// a path that is either `null` or an empty string. Treat it as a | ||
// junk entry and skip it. | ||
continue | ||
} | ||
if (path === 'index') { | ||
// That's the home page which doesn't count. It doesn't count because | ||
// people don't arrive on that for the information they seek. It's | ||
// merely a navigation tool. | ||
continue | ||
} | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
console.warn(`The file ${POPULAR_PAGES_JSON} can not be found.`) | ||
} else { | ||
throw error | ||
if (path.startsWith('early-access/')) { | ||
// We never index these anyway so their popularity is never relevant. | ||
continue | ||
} | ||
all[path] = count | ||
} | ||
|
||
const biggestCount = Math.max(...Object.values(all)) | ||
const popularPages = {} | ||
for (const [path, count] of Object.entries(all)) { | ||
// Don't bother writing massively long floating point numbers | ||
// because reducing it makes the JSON records smaller and we don't | ||
// need any more precision than 7 significant figures. | ||
const ratio = Number((count / biggestCount).toFixed(7)) | ||
|
||
// The reason we're heeding redirects is because it's possible | ||
// that the JSON file is older/"staler" than the | ||
// content itself. | ||
// Imaging our analytics recorded that `/en/foo` had 1,234 pageviews, | ||
// and someone goes and... `git mv content/foo content/bar` plus | ||
// adding `redirect_from: - /foo` into the front-matter. | ||
// Then, by using the redirects first, we can maintain that popularity | ||
// by now "pretending" that it's `/en/bar` that has 1,234 pageviews. | ||
popularPages[redirects[path] || path] = ratio | ||
} | ||
|
||
return popularPages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters