Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

statistics.js 23KB

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