modified lib-jitsi-meet dev repo
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

statistics.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. * @param {String} roomJid - The room jid where the speaker event occurred.
  433. */
  434. Statistics.prototype.sendDominantSpeakerEvent = function(roomJid) {
  435. for (const cs of this.callsStatsInstances.values()) {
  436. cs.sendDominantSpeakerEvent();
  437. }
  438. // xmpp send dominant speaker event
  439. this.xmpp.sendDominantSpeakerEvent(roomJid);
  440. };
  441. /**
  442. * Notifies about active device.
  443. * @param {{deviceList: {String:String}}} devicesData - list of devices with
  444. * their data
  445. */
  446. Statistics.sendActiveDeviceListEvent = function(devicesData) {
  447. const globalSet = Statistics._getAllCallStatsInstances();
  448. if (globalSet.size) {
  449. for (const cs of globalSet) {
  450. CallStats.sendActiveDeviceListEvent(devicesData, cs);
  451. }
  452. } else {
  453. CallStats.sendActiveDeviceListEvent(devicesData, null);
  454. }
  455. };
  456. /* eslint-disable max-params */
  457. /**
  458. * Lets the underlying statistics module know where is given SSRC rendered by
  459. * providing renderer tag ID.
  460. * @param {TraceablePeerConnection} tpc the connection to which the stream
  461. * belongs to
  462. * @param {number} ssrc the SSRC of the stream
  463. * @param {boolean} isLocal
  464. * @param {string} userId
  465. * @param {string} usageLabel meaningful usage label of this stream like
  466. * 'microphone', 'camera' or 'screen'.
  467. * @param {string} containerId the id of media 'audio' or 'video' tag which
  468. * renders the stream.
  469. */
  470. Statistics.prototype.associateStreamWithVideoTag = function(
  471. tpc,
  472. ssrc,
  473. isLocal,
  474. userId,
  475. usageLabel,
  476. containerId) {
  477. const instance = this.callsStatsInstances.get(tpc.id);
  478. if (instance) {
  479. instance.associateStreamWithVideoTag(
  480. ssrc,
  481. isLocal,
  482. userId,
  483. usageLabel,
  484. containerId);
  485. }
  486. };
  487. /* eslint-enable max-params */
  488. /**
  489. * Notifies CallStats that getUserMedia failed.
  490. *
  491. * @param {Error} e error to send
  492. */
  493. Statistics.sendGetUserMediaFailed = function(e) {
  494. const error
  495. = e instanceof JitsiTrackError
  496. ? formatJitsiTrackErrorForCallStats(e) : e;
  497. const globalSet = Statistics._getAllCallStatsInstances();
  498. if (globalSet.size) {
  499. for (const cs of globalSet) {
  500. CallStats.sendGetUserMediaFailed(error, cs);
  501. }
  502. } else {
  503. CallStats.sendGetUserMediaFailed(error, null);
  504. }
  505. };
  506. /**
  507. * Notifies CallStats that peer connection failed to create offer.
  508. *
  509. * @param {Error} e error to send
  510. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  511. */
  512. Statistics.prototype.sendCreateOfferFailed = function(e, tpc) {
  513. const instance = this.callsStatsInstances.get(tpc.id);
  514. if (instance) {
  515. instance.sendCreateOfferFailed(e);
  516. }
  517. };
  518. /**
  519. * Notifies CallStats that peer connection failed to create answer.
  520. *
  521. * @param {Error} e error to send
  522. * @param {TraceablePeerConnection} tpc connection on which failure occured.
  523. */
  524. Statistics.prototype.sendCreateAnswerFailed = function(e, tpc) {
  525. const instance = this.callsStatsInstances.get(tpc.id);
  526. if (instance) {
  527. instance.sendCreateAnswerFailed(e);
  528. }
  529. };
  530. /**
  531. * Notifies CallStats that peer connection failed to set local description.
  532. *
  533. * @param {Error} e error to send
  534. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  535. */
  536. Statistics.prototype.sendSetLocalDescFailed = function(e, tpc) {
  537. const instance = this.callsStatsInstances.get(tpc.id);
  538. if (instance) {
  539. instance.sendSetLocalDescFailed(e);
  540. }
  541. };
  542. /**
  543. * Notifies CallStats that peer connection failed to set remote description.
  544. *
  545. * @param {Error} e error to send
  546. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  547. */
  548. Statistics.prototype.sendSetRemoteDescFailed = function(e, tpc) {
  549. const instance = this.callsStatsInstances.get(tpc.id);
  550. if (instance) {
  551. instance.sendSetRemoteDescFailed(e);
  552. }
  553. };
  554. /**
  555. * Notifies CallStats that peer connection failed to add ICE candidate.
  556. *
  557. * @param {Error} e error to send
  558. * @param {TraceablePeerConnection} tpc connection on which failure occurred.
  559. */
  560. Statistics.prototype.sendAddIceCandidateFailed = function(e, tpc) {
  561. const instance = this.callsStatsInstances.get(tpc.id);
  562. if (instance) {
  563. instance.sendAddIceCandidateFailed(e);
  564. }
  565. };
  566. /**
  567. * Adds to CallStats an application log.
  568. *
  569. * @param {String} m a log message to send or an {Error} object to be reported
  570. */
  571. Statistics.sendLog = function(m) {
  572. const globalSubSet = new Set();
  573. // FIXME we don't want to duplicate logs over P2P instance, but
  574. // here we should go over instances and call this method for each
  575. // unique conference ID rather than selecting the first one.
  576. // We don't have such use case though, so leaving as is for now.
  577. for (const stats of Statistics.instances) {
  578. if (stats.callsStatsInstances.size) {
  579. globalSubSet.add(stats.callsStatsInstances.values().next().value);
  580. }
  581. }
  582. if (globalSubSet.size) {
  583. for (const csPerStats of globalSubSet) {
  584. CallStats.sendApplicationLog(m, csPerStats);
  585. }
  586. } else {
  587. CallStats.sendApplicationLog(m, null);
  588. }
  589. };
  590. /**
  591. * Sends the given feedback through CallStats.
  592. *
  593. * @param overall an integer between 1 and 5 indicating the user's rating.
  594. * @param comment the comment from the user.
  595. */
  596. Statistics.prototype.sendFeedback = function(overall, comment) {
  597. CallStats.sendFeedback(this._getCallStatsConfID(), overall, comment);
  598. Statistics.analytics.sendEvent(
  599. FEEDBACK,
  600. {
  601. rating: overall,
  602. comment
  603. });
  604. };
  605. Statistics.LOCAL_JID = require('../../service/statistics/constants').LOCAL_JID;
  606. /**
  607. * Reports global error to CallStats.
  608. *
  609. * @param {Error} error
  610. */
  611. Statistics.reportGlobalError = function(error) {
  612. if (error instanceof JitsiTrackError && error.gum) {
  613. Statistics.sendGetUserMediaFailed(error);
  614. } else {
  615. Statistics.sendLog(error);
  616. }
  617. };
  618. /**
  619. * Sends event to analytics and logs a message to the logger/console. Console
  620. * messages might also be logged to callstats automatically.
  621. *
  622. * @param {string | Object} event the event name, or an object which
  623. * represents the entire event.
  624. * @param {Object} properties properties to attach to the event (if an event
  625. * name as opposed to an event object is provided).
  626. */
  627. Statistics.sendAnalyticsAndLog = function(event, properties = {}) {
  628. if (!event) {
  629. logger.warn('No event or event name given.');
  630. return;
  631. }
  632. let eventToLog;
  633. // Also support an API with a single object as an event.
  634. if (typeof event === 'object') {
  635. eventToLog = event;
  636. } else {
  637. eventToLog = {
  638. name: event,
  639. properties
  640. };
  641. }
  642. logger.log(JSON.stringify(eventToLog));
  643. // We do this last, because it may modify the object which is passed.
  644. this.analytics.sendEvent(event, properties);
  645. };
  646. /**
  647. * Sends event to analytics.
  648. *
  649. * @param {string | Object} eventName the event name, or an object which
  650. * represents the entire event.
  651. * @param {Object} properties properties to attach to the event
  652. */
  653. Statistics.sendAnalytics = function(eventName, properties = {}) {
  654. this.analytics.sendEvent(eventName, properties);
  655. };