77
78
update(totals: IChatStreamUpdate): IChatStreamStats | undefined {
80
>
if (wordCount === this._data.lastWordCount) {
81
this.trace('Update- no new words');
82
return undefined;
83
}
85
>
const now = Date.now();
86
>
const newWords = wordCount - this._data.lastWordCount;
87
>
const hadNoWordsBeforeUpdate = this._data.lastWordCount === 0;
88
>
let firstMarkdownTime = this._data.firstMarkdownTime;
89
>
let wordCountAtBootstrapExit = this._data.wordCountAtBootstrapExit;
90
>
if (typeof firstMarkdownTime !== 'number' && wordCount > 0) {
91
>
firstMarkdownTime = now;
92
>
}
93
>
const updatesWithNewWords = this._data.updatesWithNewWords + 1;
94
>
95
>
if (hadNoWordsBeforeUpdate) {
96
>
this._data.lastUpdateTime = now;
97
>
}
98
>
99
>
const intervalCap = newWords > WORDS_FOR_LARGE_CHUNK ? LARGE_UPDATE_MAX_INTERVAL_TIME : MAX_INTERVAL_TIME;
100
>
const timeDiff = Math.min(now - this._data.lastUpdateTime, intervalCap);
101
>
let totalTime = this._data.totalTime + timeDiff;
102
>
const minBootstrapTotalTime = hadNoWordsBeforeUpdate && wordCount > WORDS_FOR_LARGE_CHUNK ? LARGE_BOOTSTRAP_MIN_TOTAL_TIME : MIN_BOOTSTRAP_TOTAL_TIME;
103
>
104
>
let bootstrapActive = this._data.bootstrapActive;
105
>
if (bootstrapActive) {
106
>
const stableStartTime = firstMarkdownTime;
107
>
const hasStableData = typeof stableStartTime === 'number'
108
>
&& updatesWithNewWords >= MIN_UPDATES_FOR_STABLE_RATE
109
&& wordCount >= WORDS_FOR_LARGE_CHUNK;
111
bootstrapActive = false;
112
totalTime = Math.max(now - stableStartTime, timeDiff);
113
wordCountAtBootstrapExit = this._data.lastWordCount;
114
this.trace('Has stable data');
116
>
totalTime = Math.max(totalTime, minBootstrapTotalTime);
117
>
}
118
>
}
119
>
120
>
const wordsSinceBootstrap = typeof wordCountAtBootstrapExit === 'number' ? Math.max(wordCount - wordCountAtBootstrapExit, 0) : wordCount;
121
>
const effectiveTime = totalTime;
122
>
const effectiveWordCount = bootstrapActive ? wordCount : wordsSinceBootstrap;
123
>
const impliedWordLoadRate = effectiveTime > 0 ? effectiveWordCount / (effectiveTime / 1000) : 0;
124
>
this._data = {
125
>
totalTime,
126
>
lastUpdateTime: now,
127
>
impliedWordLoadRate,
128
>
lastWordCount: wordCount,
129
>
firstMarkdownTime,
130
>
bootstrapActive,
131
>
wordCountAtBootstrapExit,
132
>
updatesWithNewWords
133
>
};
134
>
this._publicData = {
135
>
impliedWordLoadRate,
136
>
lastWordCount: wordCount
137
>
};
138
>
139
>
const traceWords = bootstrapActive ? wordCount : wordsSinceBootstrap;
140
>
this.trace(`Update- got ${traceWords} words over last ${totalTime}ms = ${impliedWordLoadRate} words/s`);
141
>
return this._data;
142
>
}
143
144
private trace(message: string): void {
145
>
this.logService.trace(`ChatStreamStatsTracker#update: ${message}`);
chatStreamStats.ts
146
>
}
147
}