Skip to content

Commit 39e3b23

Browse files
committed
feat(search): KTL-1516: use TeamCity external deps for stats
1 parent 727098b commit 39e3b23

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

.teamcity/builds/kotlinlang/buidTypes/BuildSearchIndex.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import jetbrains.buildServer.configs.kotlin.buildSteps.ScriptBuildStep
1010
import jetbrains.buildServer.configs.kotlin.buildSteps.script
1111
import jetbrains.buildServer.configs.kotlin.triggers.schedule
1212
import vcsRoots.KotlinLangOrg
13+
import java.io.File
14+
import java.nio.file.Paths
1315

16+
private fun readScript(name: String): String {
17+
val file = File(Paths.get("scripts/$name.mjs").toAbsolutePath().toString())
18+
return file.readText()
19+
}
1420

1521
object BuildSearchIndex : BuildType({
1622
name = "Build Site Search Index"
@@ -24,6 +30,11 @@ object BuildSearchIndex : BuildType({
2430
param("env.WH_SEARCH_KEY", "%ALGOLIA_WRITE_API_KEY%")
2531
}
2632

33+
artifactRules = """
34+
page_views_list.json
35+
page_views_map.json
36+
""".trimIndent()
37+
2738
vcs {
2839
root(KotlinLangOrg)
2940

@@ -33,6 +44,19 @@ object BuildSearchIndex : BuildType({
3344

3445
steps {
3546
script {
47+
name = "Prepare page views"
48+
scriptContent = """
49+
#!/usr/bin/env bash
50+
":" //# comment; exec /usr/bin/env node --input-type=module - "${'$'}@" < "${'$'}0"
51+
52+
${readScript("stats/pageviews")}
53+
""".trimIndent()
54+
dockerImage = "node:lts-slim"
55+
dockerImagePlatform = ScriptBuildStep.ImagePlatform.Linux
56+
dockerPull = true
57+
}
58+
script {
59+
name = "Push search index"
3660
scriptContent = """
3761
#!/bin/bash
3862
@@ -71,6 +95,14 @@ object BuildSearchIndex : BuildType({
7195
onDependencyFailure = FailureAction.FAIL_TO_START
7296
onDependencyCancel = FailureAction.CANCEL
7397
}
98+
99+
artifacts(AbsoluteId("WebTeam_BuildsForDeploymentJetBrainsCom_Algolia_PageViewsFromGoogle")) {
100+
buildRule = lastSuccessful()
101+
artifactRules = """
102+
+:unique_pageviews_pages_000000000000.json => data
103+
""".trimIndent()
104+
}
105+
74106
dependency(BuildSitePages) {
75107
snapshot {}
76108

.teamcity/scripts/stats/pageviews.mjs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { open } from 'node:fs/promises';
2+
3+
const INPUT_FILE_PATH = 'data/unique_pageviews_pages_000000000000.json';
4+
const input = await open(INPUT_FILE_PATH, 'r');
5+
6+
async function openReportFile() {
7+
const file = await open('page_views_list.json', 'w');
8+
await file.truncate(0);
9+
return file;
10+
}
11+
12+
const [listViews, mapViews] = await Promise.all([
13+
openReportFile('page_views_list.json'),
14+
openReportFile('page_views_map.json'),
15+
]);
16+
17+
async function append(line) {
18+
const { webpage: url, unique_pageviews: views } = JSON.parse(line);
19+
20+
const pageviews = Number(views);
21+
22+
if (views === '' || isNaN(pageviews)) {
23+
console.warn(`${url} has incorrect unique_pageviews=${views}`);
24+
return;
25+
}
26+
27+
if (pageviews < 1) return;
28+
if (!(new URL(url).host.includes('kotlinlang.org'))) return;
29+
30+
await Promise.all([
31+
listViews.appendFile(JSON.stringify({ url, pageviews }) + ','),
32+
mapViews.appendFile(`${JSON.stringify(url)}: ${pageviews},`)
33+
]);
34+
}
35+
36+
const lines = [];
37+
38+
await Promise.all([
39+
listViews.write('['),
40+
mapViews.write('{')
41+
]);
42+
43+
const readlineInterface = input.readLines();
44+
45+
readlineInterface.on('line', line => {
46+
lines.push(append(line));
47+
});
48+
49+
const waitInputRead = new Promise(resolve => {
50+
readlineInterface.on('close', () => {
51+
resolve();
52+
});
53+
});
54+
55+
await waitInputRead;
56+
await Promise.all(lines);
57+
58+
async function replaceLastCharacter(file, ch) {
59+
const { size } = await file.stat();
60+
file.write(ch, size - 1);
61+
}
62+
63+
await Promise.all([
64+
replaceLastCharacter(listViews, ']'),
65+
replaceLastCharacter(mapViews, '}'),
66+
]);
67+
68+
await Promise.all([
69+
input.close(),
70+
listViews.close(),
71+
mapViews.close(),
72+
]);

0 commit comments

Comments
 (0)