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

statistics.js 23KB

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