forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_optimizer_state.ts
156 lines (133 loc) · 4.91 KB
/
log_optimizer_state.ts
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { inspect } from 'util';
import { ToolingLog } from '@osd/dev-utils';
import { tap } from 'rxjs/operators';
import { OptimizerConfig } from './optimizer';
import { OptimizerUpdate$ } from './run_optimizer';
import { CompilerMsg, pipeClosure, ALL_THEMES } from './common';
export function logOptimizerState(log: ToolingLog, config: OptimizerConfig) {
return pipeClosure((update$: OptimizerUpdate$) => {
const bundleStates = new Map<string, CompilerMsg['type']>();
const bundlesThatWereBuilt = new Set<string>();
let loggedInit = false;
return update$.pipe(
tap((update) => {
const { event, state } = update;
if (event?.type === 'worker stdio') {
log.warning(`worker`, event.stream, event.line);
}
if (event?.type === 'bundle not cached') {
log.debug(
`[${event.bundle.id}] bundle not cached because [${event.reason}]${
event.diff ? `, diff:\n${event.diff}` : ''
}`
);
}
if (event?.type === 'bundle cached') {
log.debug(`[${event.bundle.id}] bundle cached`);
}
if (event?.type === 'worker started') {
let moduleCount = 0;
let workUnits = 0;
for (const bundle of event.bundles) {
moduleCount += bundle.cache.getModuleCount() ?? NaN;
workUnits += bundle.cache.getWorkUnits() ?? NaN;
}
log.info(
`starting worker [${event.bundles.length} ${
event.bundles.length === 1 ? 'bundle' : 'bundles'
}]`
);
log.debug(`modules [${moduleCount}] work units [${workUnits}]`);
}
if (state.phase === 'reallocating') {
log.debug(`changes detected...`);
return;
}
if (state.phase === 'initialized') {
if (!loggedInit) {
loggedInit = true;
log.info(`initialized, ${state.offlineBundles.length} bundles cached`);
if (config.themeTags.length !== ALL_THEMES.length) {
log.warning(
`only building [${config.themeTags}] themes, customize with the OSD_OPTIMIZER_THEMES environment variable`
);
}
}
return;
}
for (const { bundleId: id, type } of state.compilerStates) {
const prevBundleState = bundleStates.get(id);
if (type === prevBundleState) {
continue;
}
if (type === 'running') {
bundlesThatWereBuilt.add(id);
}
bundleStates.set(id, type);
log.debug(
`[${id}] state = "${type}"${type !== 'running' ? ` after ${state.durSec} sec` : ''}`
);
}
if (state.phase === 'running' || state.phase === 'initializing') {
return;
}
if (state.phase === 'issue') {
log.error(`webpack compile errors`);
log.indent(4);
for (const b of state.compilerStates) {
if (b.type === 'compiler issue') {
log.error(`[${b.bundleId}] build`);
log.indent(4);
log.error(b.failure);
log.indent(-4);
}
}
log.indent(-4);
return;
}
if (state.phase === 'success') {
const buildCount = bundlesThatWereBuilt.size;
bundlesThatWereBuilt.clear();
if (state.offlineBundles.length && buildCount === 0) {
log.success(`all bundles cached, success after ${state.durSec} sec`);
} else {
log.success(
`${buildCount} bundles compiled successfully after ${state.durSec} sec` +
(config.watch ? ', watching for changes' : '')
);
}
return;
}
throw new Error(`unhandled optimizer message: ${inspect(update)}`);
})
);
});
}