@dxos/util 0.6.4 → 0.6.5-staging.097cf0c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -218,9 +218,14 @@ var CircularBuffer = class {
218
218
  this._buffer = new Array(size);
219
219
  }
220
220
  push(element) {
221
+ const evicted = this._elementCount === this._buffer.length ? this._buffer[this._nextIndex] : void 0;
221
222
  this._buffer[this._nextIndex] = element;
222
223
  this._nextIndex = (this._nextIndex + 1) % this._buffer.length;
223
224
  this._elementCount = Math.min(this._buffer.length, this._elementCount + 1);
225
+ return evicted;
226
+ }
227
+ get elementCount() {
228
+ return this._elementCount;
224
229
  }
225
230
  getLast() {
226
231
  if (this._elementCount === 0) {
@@ -1750,6 +1755,62 @@ var WeakDictionary = class {
1750
1755
  this._finalization.unregister(value);
1751
1756
  }
1752
1757
  };
1758
+
1759
+ // packages/common/util/src/sliding-window-summary.ts
1760
+ import { invariant as invariant5 } from "@dxos/invariant";
1761
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/common/util/src/sliding-window-summary.ts";
1762
+ var SlidingWindowSummary = class {
1763
+ constructor(options) {
1764
+ this._sum = 0;
1765
+ this._buffer = new CircularBuffer(options.dataPoints);
1766
+ if (options.precision != null) {
1767
+ invariant5(options.precision >= 0, void 0, {
1768
+ F: __dxlog_file5,
1769
+ L: 26,
1770
+ S: this,
1771
+ A: [
1772
+ "options.precision >= 0",
1773
+ ""
1774
+ ]
1775
+ });
1776
+ this._precision = Math.pow(10, options.precision);
1777
+ }
1778
+ }
1779
+ record(value) {
1780
+ const evicted = this._buffer.push(value);
1781
+ this._sum += value - (evicted ?? 0);
1782
+ }
1783
+ average() {
1784
+ return this._buffer.elementCount === 0 ? 0 : this._withPrecision(this._sum / this._buffer.elementCount);
1785
+ }
1786
+ computeWindowSummary() {
1787
+ const mean = this.average();
1788
+ const sortedElements = [
1789
+ ...this._buffer
1790
+ ].sort();
1791
+ const median2 = this._withPrecision(sortedElements.length % 2 === 0 ? (sortedElements[sortedElements.length / 2] + sortedElements[sortedElements.length / 2 - 1]) / 2 : sortedElements[sortedElements.length / 2]);
1792
+ const p90 = this._withPrecision(sortedElements[Math.round(sortedElements.length * 0.9)]);
1793
+ const variance = sortedElements.reduce((acc, v) => acc + Math.pow(v - mean, 2)) / sortedElements.length;
1794
+ const stdDev = this._withPrecision(Math.sqrt(variance));
1795
+ const histogram = sortedElements.reduce((acc, v) => {
1796
+ acc[v] += 1;
1797
+ return acc;
1798
+ }, {});
1799
+ return {
1800
+ mean,
1801
+ median: median2,
1802
+ p90,
1803
+ stdDev,
1804
+ histogram
1805
+ };
1806
+ }
1807
+ _withPrecision(value) {
1808
+ if (this._precision == null) {
1809
+ return value;
1810
+ }
1811
+ return Math.round(value * this._precision) / this._precision;
1812
+ }
1813
+ };
1753
1814
  export {
1754
1815
  BitField,
1755
1816
  Callback,
@@ -1760,6 +1821,7 @@ export {
1760
1821
  MapEntry,
1761
1822
  ParamKeyAnnotation,
1762
1823
  Params,
1824
+ SlidingWindowSummary,
1763
1825
  Tracer,
1764
1826
  WeakDictionary,
1765
1827
  accessBy,