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.

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