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.playedCompletely_ = 0;
  22. /** @private {number} */
  23. this.skipped_ = 0;
  24. /** @private {number} */
  25. this.errors_ = 0;
  26. }
  27. /**
  28. * Record the time it took to get the final manifest.
  29. *
  30. * @param {number} seconds
  31. */
  32. addLoadTime(seconds) {
  33. this.loadTimes_.push(seconds);
  34. }
  35. /**
  36. * Increase the number of ads started by one.
  37. */
  38. incrementStarted() {
  39. this.started_++;
  40. }
  41. /**
  42. * Increase the number of ads played completely by one.
  43. */
  44. incrementPlayedCompletely() {
  45. this.playedCompletely_++;
  46. }
  47. /**
  48. * Increase the number of ads skipped by one.
  49. */
  50. incrementSkipped() {
  51. this.skipped_++;
  52. }
  53. /**
  54. * Increase the number of ads with error by one.
  55. */
  56. incrementErrors() {
  57. this.errors_++;
  58. }
  59. /**
  60. * @return {number}
  61. * @private
  62. */
  63. getAverageLoadTime_() {
  64. if (!this.loadTimes_.length) {
  65. return 0;
  66. }
  67. const sum = this.loadTimes_.reduce(
  68. (accumulator, currentValue) => accumulator + currentValue, 0);
  69. return sum / this.loadTimes_.length;
  70. }
  71. /**
  72. * Create a stats blob that we can pass up to the app. This blob will not
  73. * reference any internal data.
  74. *
  75. * @return {shaka.extern.AdsStats}
  76. */
  77. getBlob() {
  78. return {
  79. loadTimes: this.loadTimes_,
  80. averageLoadTime: this.getAverageLoadTime_(),
  81. started: this.started_,
  82. playedCompletely: this.playedCompletely_,
  83. skipped: this.skipped_,
  84. errors: this.errors_,
  85. };
  86. }
  87. };