Source: lib/ads/ads_stats.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ads.AdsStats');
  7. /**
  8. * This class tracks all the various components (some optional) that are used to
  9. * populate |shaka.extern.AdsStats| which is passed to the app.
  10. *
  11. * @final
  12. */
  13. shaka.ads.AdsStats = class {
  14. /** */
  15. constructor() {
  16. /** @private {!Array<number>} */
  17. this.loadTimes_ = [];
  18. /** @private {number} */
  19. this.started_ = 0;
  20. /** @private {number} */
  21. this.overlayAds_ = 0;
  22. /** @private {number} */
  23. this.playedCompletely_ = 0;
  24. /** @private {number} */
  25. this.skipped_ = 0;
  26. /** @private {number} */
  27. this.errors_ = 0;
  28. }
  29. /**
  30. * Record the time it took to get the final manifest.
  31. *
  32. * @param {number} seconds
  33. */
  34. addLoadTime(seconds) {
  35. this.loadTimes_.push(seconds);
  36. }
  37. /**
  38. * Increase the number of ads started by one.
  39. */
  40. incrementStarted() {
  41. this.started_++;
  42. }
  43. /**
  44. * Increase the number of overlay ads started by one.
  45. */
  46. incrementOverlayAds() {
  47. this.overlayAds_++;
  48. }
  49. /**
  50. * Increase the number of ads played completely by one.
  51. */
  52. incrementPlayedCompletely() {
  53. this.playedCompletely_++;
  54. }
  55. /**
  56. * Increase the number of ads skipped by one.
  57. */
  58. incrementSkipped() {
  59. this.skipped_++;
  60. }
  61. /**
  62. * Increase the number of ads with error by one.
  63. */
  64. incrementErrors() {
  65. this.errors_++;
  66. }
  67. /**
  68. * @return {number}
  69. * @private
  70. */
  71. getAverageLoadTime_() {
  72. if (!this.loadTimes_.length) {
  73. return 0;
  74. }
  75. const sum = this.loadTimes_.reduce(
  76. (accumulator, currentValue) => accumulator + currentValue, 0);
  77. return sum / this.loadTimes_.length;
  78. }
  79. /**
  80. * Create a stats blob that we can pass up to the app. This blob will not
  81. * reference any internal data.
  82. *
  83. * @return {shaka.extern.AdsStats}
  84. */
  85. getBlob() {
  86. return {
  87. loadTimes: this.loadTimes_,
  88. averageLoadTime: this.getAverageLoadTime_(),
  89. started: this.started_,
  90. overlayAds: this.overlayAds_,
  91. playedCompletely: this.playedCompletely_,
  92. skipped: this.skipped_,
  93. errors: this.errors_,
  94. };
  95. }
  96. };