You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

statistics.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. import EventEmitter from 'events';
  2. import { FEEDBACK } from '../../service/statistics/AnalyticsEvents';
  3. import analytics from './AnalyticsAdapter';
  4. import CallStats from './CallStats';
  5. import LocalStats from './LocalStatsCollector';
  6. import RTPStats from './RTPStatsCollector';
  7. import browser from '../browser';
  8. import Settings from '../settings/Settings';
  9. import ScriptUtil from '../util/ScriptUtil';
  10. import JitsiTrackError from '../../JitsiTrackError';
  11. import * as StatisticsEvents from '../../service/statistics/Events';
  12. const logger = require('jitsi-meet-logger').getLogger(__filename);
  13. /**
  14. * Stores all active {@link Statistics} instances.
  15. * @type {Set<Statistics>}
  16. */
  17. let _instances;
  18. /**
  19. * True if callstats API is loaded
  20. */
  21. let isCallstatsLoaded = false;
  22. /**
  23. * Since callstats.io is a third party, we cannot guarantee the quality of their
  24. * service. More specifically, their server may take noticeably long time to
  25. * respond. Consequently, it is in our best interest (in the sense that the
  26. * intergration of callstats.io is pretty important to us but not enough to
  27. * allow it to prevent people from joining a conference) to (1) start
  28. * downloading their API as soon as possible and (2) do the downloading
  29. * asynchronously.
  30. *
  31. * @param {StatisticsOptions} options - Options to use for downloading and
  32. * initializing callstats backend.
  33. */
  34. function loadCallStatsAPI(options) {
  35. if (!isCallstatsLoaded) {
  36. ScriptUtil.loadScript(
  37. options.customScriptUrl
  38. || 'https://api.callstats.io/static/callstats-ws.min.js',
  39. /* async */ true,
  40. /* prepend */ true,
  41. /* relativeURL */ undefined,
  42. /* loadCallback */ () => _initCallStatsBackend(options)
  43. );
  44. isCallstatsLoaded = true;
  45. }
  46. }
  47. /**
  48. * Initializes Callstats backend.
  49. *
  50. * @param {StatisticsOptions} options - The options to use for initializing
  51. * callstats backend.
  52. * @private
  53. */
  54. function _initCallStatsBackend(options) {
  55. if (CallStats.isBackendInitialized()) {
  56. return;
  57. }
  58. const userName = Settings.callStatsUserName;
  59. if (!CallStats.initBackend({
  60. callStatsID: options.callStatsID,
  61. callStatsSecret: options.callStatsSecret,
  62. userName: options.swapUserNameAndAlias
  63. ? options.callStatsAliasName : userName,
  64. aliasName: options.swapUserNameAndAlias
  65. ? userName : options.callStatsAliasName,
  66. applicationName: options.applicationName,
  67. getWiFiStatsMethod: options.getWiFiStatsMethod
  68. })) {
  69. logger.error('CallStats Backend initialization failed bad');
  70. }
  71. }
  72. /**
  73. * callstats strips any additional fields from Error except for "name", "stack",
  74. * "message" and "constraintName". So we need to bundle additional information
  75. * from JitsiTrackError into error passed to callstats to preserve valuable
  76. * information about error.
  77. * @param {JitsiTrackError} error
  78. */
  79. function formatJitsiTrackErrorForCallStats(error) {
  80. const err = new Error();
  81. // Just copy original stack from error
  82. err.stack = error.stack;
  83. // Combine name from error's name plus (possibly) name of original GUM error
  84. err.name = (error.name || 'Unknown error') + (error.gum && error.gum.error
  85. && error.gum.error.name ? ` - ${error.gum.error.name}` : '');
  86. // Put all constraints into this field. For constraint failed errors we will
  87. // still know which exactly constraint failed as it will be a part of
  88. // message.
  89. err.constraintName = error.gum && error.gum.constraints
  90. ? JSON.stringify(error.gum.constraints) : '';
  91. // Just copy error's message.
  92. err.message = error.message;
  93. return err;
  94. }
  95. /**
  96. * Init statistic options
  97. * @param options
  98. */
  99. Statistics.init = function(options) {
  100. Statistics.audioLevelsEnabled = !options.disableAudioLevels;
  101. if (typeof options.audioLevelsInterval === 'number') {
  102. Statistics.audioLevelsInterval = options.audioLevelsInterval;
  103. }
  104. Statistics.disableThirdPartyRequests = options.disableThirdPartyRequests;
  105. };
  106. /**
  107. * The options to configure Statistics.
  108. * @typedef {Object} StatisticsOptions
  109. * @property {string} applicationName - The application name to pass to
  110. * callstats.
  111. * @property {string} callStatsAliasName - The alias name to use when
  112. * initializing callstats.
  113. * @property {string} callStatsConfIDNamespace - A namespace to prepend the
  114. * callstats conference ID with.
  115. * @property {string} callStatsID - Callstats credentials - the id.
  116. * @property {string} callStatsSecret - Callstats credentials - the secret.
  117. * @property {string} customScriptUrl - A custom lib url to use when downloading
  118. * callstats library.
  119. * @property {string} roomName - The room name we are currently in.
  120. * @property {boolean} swapUserNameAndAlias - Whether to swap the places of
  121. * username and alias when initiating callstats.
  122. */
  123. /**
  124. *
  125. * @param xmpp
  126. * @param {StatisticsOptions} options - The options to use creating the
  127. * Statistics.
  128. */
  129. export default function Statistics(xmpp, options) {
  130. /**
  131. * {@link RTPStats} mapped by {@link TraceablePeerConnection.id} which
  132. * collect RTP statistics for each peerconnection.
  133. * @type {Map<string, RTPStats}
  134. */
  135. this.rtpStatsMap = new Map();
  136. this.eventEmitter = new EventEmitter();
  137. this.xmpp = xmpp;
  138. this.options = options || {};
  139. this.callStatsIntegrationEnabled
  140. = this.options.callStatsID && this.options.callStatsSecret
  141. // Even though AppID and AppSecret may be specified, the integration
  142. // of callstats.io may be disabled because of globally-disallowed
  143. // requests to any third parties.
  144. && (Statistics.disableThirdPartyRequests !== true);
  145. if (this.callStatsIntegrationEnabled) {
  146. if (browser.isReactNative()) {
  147. _initCallStatsBackend(this.options);
  148. } else {
  149. loadCallStatsAPI(this.options);
  150. }
  151. if (!this.options.callStatsConfIDNamespace) {
  152. logger.warn('"callStatsConfIDNamespace" is not defined');
  153. }
  154. }
  155. /**
  156. * Stores {@link CallStats} instances for each
  157. * {@link TraceablePeerConnection} (one {@link CallStats} instance serves
  158. * one TPC). The instances are mapped by {@link TraceablePeerConnection.id}.
  159. * @type {Map<number, CallStats>}
  160. */
  161. this.callsStatsInstances = new Map();
  162. Statistics.instances.add(this);
  163. }
  164. Statistics.audioLevelsEnabled = false;
  165. Statistics.audioLevelsInterval = 200;
  166. Statistics.disableThirdPartyRequests = false;
  167. Statistics.analytics = analytics;
  168. Object.defineProperty(Statistics, 'instances', {
  169. /**
  170. * Returns the Set holding all active {@link Statistics} instances. Lazily
  171. * initializes the Set to allow any Set polyfills to be applied.
  172. * @type {Set<Statistics>}
  173. */
  174. get() {
  175. if (!_instances) {
  176. _instances = new Set();
  177. }
  178. return _instances;
  179. }
  180. });
  181. /**
  182. * Starts collecting RTP stats for given peerconnection.
  183. * @param {TraceablePeerConnection} peerconnection
  184. */
  185. Statistics.prototype.startRemoteStats = function(peerconnection) {
  186. this.stopRemoteStats(peerconnection);
  187. try {
  188. const rtpStats
  189. = new RTPStats(
  190. peerconnection,
  191. Statistics.audioLevelsInterval,
  192. 2000,
  193. this.eventEmitter);
  194. rtpStats.start(Statistics.audioLevelsEnabled);
  195. this.rtpStatsMap.set(peerconnection.id, rtpStats);
  196. } catch (e) {
  197. logger.error(`Failed to start collecting remote statistics: ${e}`);
  198. }
  199. };
  200. Statistics.localStats = [];
  201. Statistics.startLocalStats = function(stream, callback) {
  202. if (!Statistics.audioLevelsEnabled) {
  203. return;
  204. }
  205. const localStats = new LocalStats(stream, Statistics.audioLevelsInterval,
  206. callback);
  207. this.localStats.push(localStats);
  208. localStats.start();
  209. };
  210. Statistics.prototype.addAudioLevelListener = function(listener) {
  211. if (!Statistics.audioLevelsEnabled) {
  212. return;
  213. }
  214. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  215. };
  216. Statistics.prototype.removeAudioLevelListener = function(listener) {
  217. if (!Statistics.audioLevelsEnabled) {
  218. return;
  219. }
  220. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  221. };
  222. Statistics.prototype.addBeforeDisposedListener = function(listener) {
  223. this.eventEmitter.on(StatisticsEvents.BEFORE_DISPOSED, listener);
  224. };
  225. Statistics.prototype.removeBeforeDisposedListener = function(listener) {
  226. this.eventEmitter.removeListener(
  227. StatisticsEvents.BEFORE_DISPOSED, listener);
  228. };
  229. Statistics.prototype.addConnectionStatsListener = function(listener) {
  230. this.eventEmitter.on(StatisticsEvents.CONNECTION_STATS, listener);
  231. };
  232. Statistics.prototype.removeConnectionStatsListener = function(listener) {
  233. this.eventEmitter.removeListener(
  234. StatisticsEvents.CONNECTION_STATS,
  235. listener);
  236. };
  237. Statistics.prototype.addByteSentStatsListener = function(listener) {
  238. this.eventEmitter.on(StatisticsEvents.BYTE_SENT_STATS, listener);
  239. };
  240. Statistics.prototype.removeByteSentStatsListener = function(listener) {
  241. this.eventEmitter.removeListener(StatisticsEvents.BYTE_SENT_STATS,
  242. listener);
  243. };
  244. Statistics.prototype.dispose = function() {
  245. try {
  246. // NOTE Before reading this please see the comment in stopCallStats...
  247. //
  248. // Here we prevent from emitting the event twice in case it will be
  249. // triggered from stopCallStats.
  250. // If the event is triggered from here it means that the logs will not
  251. // be submitted anyway (because there is no CallStats instance), but
  252. // we're doing that for the sake of some kind of consistency.
  253. if (!this.callsStatsInstances.size) {
  254. this.eventEmitter.emit(StatisticsEvents.BEFORE_DISPOSED);
  255. }
  256. for (const callStats of this.callsStatsInstances.values()) {
  257. this.stopCallStats(callStats.tpc);
  258. }
  259. for (const tpcId of this.rtpStatsMap.keys()) {
  260. this._stopRemoteStats(tpcId);
  261. }
  262. if (this.eventEmitter) {
  263. this.eventEmitter.removeAllListeners();
  264. }
  265. } finally {
  266. Statistics.instances.delete(this);
  267. }
  268. };
  269. Statistics.stopLocalStats = function(stream) {
  270. if (!Statistics.audioLevelsEnabled) {
  271. return;
  272. }
  273. for (let i = 0; i < Statistics.localStats.length; i++) {
  274. if (Statistics.localStats[i].stream === stream) {
  275. const localStats = Statistics.localStats.splice(i, 1);
  276. localStats[0].stop();
  277. break;
  278. }
  279. }
  280. };
  281. /**
  282. * Stops remote RTP stats for given peerconnection ID.
  283. * @param {string} tpcId {@link TraceablePeerConnection.id}
  284. * @private
  285. */
  286. Statistics.prototype._stopRemoteStats = function(tpcId) {
  287. const rtpStats = this.rtpStatsMap.get(tpcId);
  288. if (rtpStats) {
  289. rtpStats.stop();
  290. this.rtpStatsMap.delete(tpcId);
  291. }
  292. };
  293. /**
  294. * Stops collecting RTP stats for given peerconnection
  295. * @param {TraceablePeerConnection} tpc
  296. */
  297. Statistics.prototype.stopRemoteStats = function(tpc) {
  298. this._stopRemoteStats(tpc.id);
  299. };
  300. // CALSTATS METHODS
  301. /**
  302. * Initializes the callstats.io API.
  303. * @param {TraceablePeerConnection} tpc the {@link TraceablePeerConnection}
  304. * instance for which CalStats will be started.
  305. * @param {string} remoteUserID
  306. */
  307. Statistics.prototype.startCallStats = function(tpc, remoteUserID) {
  308. if (!this.callStatsIntegrationEnabled) {
  309. return;
  310. } else if (this.callsStatsInstances.has(tpc.id)) {
  311. logger.error('CallStats instance for ${tpc} exists already');
  312. return;
  313. }
  314. logger.info(`Starting CallStats for ${tpc}...`);
  315. const newInstance
  316. = new CallStats(
  317. tpc,
  318. {
  319. confID: this._getCallStatsConfID(),
  320. remoteUserID
  321. });
  322. this.callsStatsInstances.set(tpc.id, newInstance);
  323. };
  324. /**
  325. * Obtains the list of *all* {@link CallStats} instances collected from every
  326. * valid {@link Statistics} instance.
  327. * @return {Set<CallStats>}
  328. * @private
  329. */
  330. Statistics._getAllCallStatsInstances = function() {
  331. const csInstances = new Set();
  332. for (const statistics of Statistics.instances) {
  333. for (const cs of statistics.callsStatsInstances.values()) {
  334. csInstances.add(cs);
  335. }
  336. }
  337. return csInstances;
  338. };
  339. /**
  340. * Constructs the CallStats conference ID based on the options currently
  341. * configured in this instance.
  342. * @return {string}
  343. * @private
  344. */
  345. Statistics.prototype._getCallStatsConfID = function() {
  346. // The conference ID is case sensitive!!!
  347. return this.options.callStatsConfIDNamespace
  348. ? `${this.options.callStatsConfIDNamespace}/${this.options.roomName}`
  349. : this.options.roomName;
  350. };
  351. /**
  352. * Removes the callstats.io instances.
  353. */
  354. Statistics.prototype.stopCallStats = function(tpc) {
  355. const callStatsInstance = this.callsStatsInstances.get(tpc.id);
  356. if (callStatsInstance) {
  357. // FIXME the original purpose of adding BEFORE_DISPOSED event was to be
  358. // able to submit the last log batch from jitsi-meet to CallStats. After
  359. // recent changes we dispose the CallStats earlier
  360. // (before Statistics.dispose), so we need to emit this event here to
  361. // give this last chance for final log batch submission.
  362. //
  363. // Eventually there should be a separate module called "log storage"
  364. // which should emit proper events when it's underlying
  365. // CallStats instance is going away.
  366. if (this.callsStatsInstances.size === 1) {
  367. this.eventEmitter.emit(StatisticsEvents.BEFORE_DISPOSED);
  368. }
  369. this.callsStatsInstances.delete(tpc.id);
  370. // The fabric needs to be terminated when being stopped
  371. callStatsInstance.sendTerminateEvent();
  372. }
  373. };
  374. /**
  375. * Returns true if the callstats integration is enabled, otherwise returns
  376. * false.
  377. *
  378. * @returns true if the callstats integration is enabled, otherwise returns
  379. * false.
  380. */
  381. Statistics.prototype.isCallstatsEnabled = function() {
  382. return this.callStatsIntegrationEnabled;
  383. };
  384. /**
  385. * Logs either resume or hold event for the given peer connection.
  386. * @param {TraceablePeerConnection} tpc the connection for which event will be
  387. * reported
  388. * @param {boolean} isResume true for resume or false for hold
  389. */
  390. Statistics.prototype.sendConnectionResumeOrHoldEvent = function(tpc, isResume) {
  391. const instance = this.callsStatsInstances.get(tpc.id);
  392. if (instance) {
  393. instance.sendResumeOrHoldEvent(isResume);
  394. }
  395. };
  396. /**
  397. * Notifies CallStats and analytics (if present) for ice connection failed
  398. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  399. */
  400. Statistics.prototype.sendIceConnectionFailedEvent = function(tpc) {
  401. const instance = this.callsStatsInstances.get(tpc.id);
  402. if (instance) {
  403. instance.sendIceConnectionFailedEvent();
  404. }
  405. };
  406. /**
  407. * Notifies CallStats for mute events
  408. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  409. * @param {boolean} muted true for muted and false for not muted
  410. * @param {String} type "audio"/"video"
  411. */
  412. Statistics.prototype.sendMuteEvent = function(tpc, muted, type) {
  413. const instance = tpc && this.callsStatsInstances.get(tpc.id);
  414. CallStats.sendMuteEvent(muted, type, instance);
  415. };
  416. /**
  417. * Notifies CallStats for screen sharing events
  418. * @param start {boolean} true for starting screen sharing and
  419. * false for not stopping
  420. * @param {string|null} ssrc - optional ssrc value, used only when
  421. * starting screen sharing.
  422. */
  423. Statistics.prototype.sendScreenSharingEvent
  424. = function(start, ssrc) {
  425. for (const cs of this.callsStatsInstances.values()) {
  426. cs.sendScreenSharingEvent(start, ssrc);
  427. }
  428. };
  429. /**
  430. * Notifies the statistics module that we are now the dominant speaker of the
  431. * conference.
  432. */
  433. Statistics.prototype.sendDominantSpeakerEvent = function() {
  434. for (const cs of this.callsStatsInstances.values()) {
  435. cs.sendDominantSpeakerEvent();
  436. }
  437. };
  438. /**
  439. * Notifies about active device.
  440. * @param {{deviceList: {String:String}}} devicesData - list of devices with
  441. * their data
  442. */
  443. Statistics.sendActiveDeviceListEvent = function(devicesData) {
  444. const globalSet = Statistics._getAllCallStatsInstances();
  445. if (globalSet.size) {
  446. for (const cs of globalSet) {
  447. CallStats.sendActiveDeviceListEvent(devicesData, cs);
  448. }
  449. } else {
  450. CallStats.sendActiveDeviceListEvent(devicesData, null);
  451. }
  452. };
  453. /* eslint-disable max-params */
  454. /**
  455. * Lets the underlying statistics module know where is given SSRC rendered by
  456. * providing renderer tag ID.
  457. * @param {TraceablePeerConnection} tpc the connection to which the stream
  458. * belongs to
  459. * @param {number} ssrc the SSRC of the stream
  460. * @param {boolean} isLocal
  461. * @param {string} userId
  462. * @param {string} usageLabel meaningful usage label of this stream like
  463. * 'microphone', 'camera' or 'screen'.
  464. * @param {string} containerId the id of media 'audio' or 'video' tag which
  465. * renders the stream.
  466. */
  467. Statistics.prototype.associateStreamWithVideoTag = function(
  468. tpc,
  469. ssrc,
  470. isLocal,
  471. userId,
  472. usageLabel,
  473. containerId) {
  474. const instance = this.callsStatsInstances.get(tpc.id);
  475. if (instance) {
  476. instance.associateStreamWithVideoTag(
  477. ssrc,
  478. isLocal,
  479. userId,
  480. usageLabel,
  481. containerId);
  482. }
  483. };
  484. /* eslint-enable max-params */
  485. /**
  486. * Notifies CallStats that getUserMedia failed.
  487. *
  488. * @param {Error} e error to send
  489. */
  490. Statistics.sendGetUserMediaFailed = function(e) {
  491. const error
  492. = e instanceof JitsiTrackError
  493. ? formatJitsiTrackErrorForCallStats(e) : e;
  494. const globalSet = Statistics._getAllCallStatsInstances();
  495. if (globalSet.size) {
  496. for (const cs of globalSet) {
  497. CallStats.sendGetUserMediaFailed(error, cs);
  498. }
  499. } else {
  500. CallStats.sendGetUserMediaFailed(error, null);
  501. }
  502. };
  503. /**
  504. * Notifies CallStats that peer connection failed to create offer.
  505. *
  506. * @param {Error} e error to send
  507. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  508. */
  509. Statistics.prototype.sendCreateOfferFailed = function(e, tpc) {
  510. const instance = this.callsStatsInstances.get(tpc.id);
  511. if (instance) {
  512. instance.sendCreateOfferFailed(e);
  513. }
  514. };
  515. /**
  516. * Notifies CallStats that peer connection failed to create answer.
  517. *
  518. * @param {Error} e error to send
  519. * @param {TraceablePeerConnection} tpc connection on which failure occured.
  520. */
  521. Statistics.prototype.sendCreateAnswerFailed = function(e, tpc) {
  522. const instance = this.callsStatsInstances.get(tpc.id);
  523. if (instance) {
  524. instance.sendCreateAnswerFailed(e);
  525. }
  526. };
  527. /**
  528. * Notifies CallStats that peer connection failed to set local description.
  529. *
  530. * @param {Error} e error to send
  531. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  532. */
  533. Statistics.prototype.sendSetLocalDescFailed = function(e, tpc) {
  534. const instance = this.callsStatsInstances.get(tpc.id);
  535. if (instance) {
  536. instance.sendSetLocalDescFailed(e);
  537. }
  538. };
  539. /**
  540. * Notifies CallStats that peer connection failed to set remote description.
  541. *
  542. * @param {Error} e error to send
  543. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  544. */
  545. Statistics.prototype.sendSetRemoteDescFailed = function(e, tpc) {
  546. const instance = this.callsStatsInstances.get(tpc.id);
  547. if (instance) {
  548. instance.sendSetRemoteDescFailed(e);
  549. }
  550. };
  551. /**
  552. * Notifies CallStats that peer connection failed to add ICE candidate.
  553. *
  554. * @param {Error} e error to send
  555. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  556. */
  557. Statistics.prototype.sendAddIceCandidateFailed = function(e, tpc) {
  558. const instance = this.callsStatsInstances.get(tpc.id);
  559. if (instance) {
  560. instance.sendAddIceCandidateFailed(e);
  561. }
  562. };
  563. /**
  564. * Adds to CallStats an application log.
  565. *
  566. * @param {String} m a log message to send or an {Error} object to be reported
  567. */
  568. Statistics.sendLog = function(m) {
  569. const globalSubSet = new Set();
  570. // FIXME we don't want to duplicate logs over P2P instance, but
  571. // here we should go over instances and call this method for each
  572. // unique conference ID rather than selecting the first one.
  573. // We don't have such use case though, so leaving as is for now.
  574. for (const stats of Statistics.instances) {
  575. if (stats.callsStatsInstances.size) {
  576. globalSubSet.add(stats.callsStatsInstances.values().next().value);
  577. }
  578. }
  579. if (globalSubSet.size) {
  580. for (const csPerStats of globalSubSet) {
  581. CallStats.sendApplicationLog(m, csPerStats);
  582. }
  583. } else {
  584. CallStats.sendApplicationLog(m, null);
  585. }
  586. };
  587. /**
  588. * Sends the given feedback through CallStats.
  589. *
  590. * @param overall an integer between 1 and 5 indicating the user's rating.
  591. * @param comment the comment from the user.
  592. */
  593. Statistics.prototype.sendFeedback = function(overall, comment) {
  594. CallStats.sendFeedback(this._getCallStatsConfID(), overall, comment);
  595. Statistics.analytics.sendEvent(
  596. FEEDBACK,
  597. {
  598. rating: overall,
  599. comment
  600. });
  601. };
  602. Statistics.LOCAL_JID = require('../../service/statistics/constants').LOCAL_JID;
  603. /**
  604. * Reports global error to CallStats.
  605. *
  606. * @param {Error} error
  607. */
  608. Statistics.reportGlobalError = function(error) {
  609. if (error instanceof JitsiTrackError && error.gum) {
  610. Statistics.sendGetUserMediaFailed(error);
  611. } else {
  612. Statistics.sendLog(error);
  613. }
  614. };
  615. /**
  616. * Sends event to analytics and logs a message to the logger/console. Console
  617. * messages might also be logged to callstats automatically.
  618. *
  619. * @param {string | Object} event the event name, or an object which
  620. * represents the entire event.
  621. * @param {Object} properties properties to attach to the event (if an event
  622. * name as opposed to an event object is provided).
  623. */
  624. Statistics.sendAnalyticsAndLog = function(event, properties = {}) {
  625. if (!event) {
  626. logger.warn('No event or event name given.');
  627. return;
  628. }
  629. let eventToLog;
  630. // Also support an API with a single object as an event.
  631. if (typeof event === 'object') {
  632. eventToLog = event;
  633. } else {
  634. eventToLog = {
  635. name: event,
  636. properties
  637. };
  638. }
  639. logger.log(JSON.stringify(eventToLog));
  640. // We do this last, because it may modify the object which is passed.
  641. this.analytics.sendEvent(event, properties);
  642. };
  643. /**
  644. * Sends event to analytics.
  645. *
  646. * @param {string | Object} eventName the event name, or an object which
  647. * represents the entire event.
  648. * @param {Object} properties properties to attach to the event
  649. */
  650. Statistics.sendAnalytics = function(eventName, properties = {}) {
  651. this.analytics.sendEvent(eventName, properties);
  652. };