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.

ConnectionStatsTable.tsx 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import clsx from 'clsx';
  4. import React, { Component } from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import { isMobileBrowser } from '../../base/environment/utils';
  7. import { translate } from '../../base/i18n/functions';
  8. import ContextMenu from '../../base/ui/components/web/ContextMenu';
  9. type DownloadUpload = {
  10. download: number;
  11. upload: number;
  12. };
  13. /**
  14. * The type of the React {@code Component} props of
  15. * {@link ConnectionStatsTable}.
  16. */
  17. interface IProps extends WithTranslation {
  18. /**
  19. * The audio SSRC of this client.
  20. */
  21. audioSsrc: number;
  22. /**
  23. * Statistics related to bandwidth.
  24. * {{
  25. * download: Number,
  26. * upload: Number
  27. * }}.
  28. */
  29. bandwidth: DownloadUpload;
  30. /**
  31. * Statistics related to bitrate.
  32. * {{
  33. * download: Number,
  34. * upload: Number
  35. * }}.
  36. */
  37. bitrate: DownloadUpload;
  38. /**
  39. * The number of bridges (aka media servers) currently used in the
  40. * conference.
  41. */
  42. bridgeCount: number;
  43. /**
  44. * An object containing the CSS classes.
  45. */
  46. classes: any;
  47. /**
  48. * Audio/video codecs in use for the connection.
  49. */
  50. codec: {
  51. [key: string]: {
  52. audio: string | undefined;
  53. video: string | undefined;
  54. };
  55. };
  56. /**
  57. * A message describing the connection quality.
  58. */
  59. connectionSummary: string;
  60. /**
  61. * Whether or not should display the "Show More" link.
  62. */
  63. disableShowMoreStats: boolean;
  64. /**
  65. * Whether or not the participant was verified.
  66. */
  67. e2eeVerified: boolean;
  68. /**
  69. * Whether or not should display the "Save Logs" link.
  70. */
  71. enableSaveLogs: boolean;
  72. /**
  73. * Statistics related to frame rates for each ssrc.
  74. * {{
  75. * [ ssrc ]: Number
  76. * }}.
  77. */
  78. framerate: {
  79. [ssrc: string]: number;
  80. };
  81. /**
  82. * Whether or not the statistics are for local video.
  83. */
  84. isLocalVideo: boolean;
  85. /**
  86. * Whether we are in narrow layout mode or not.
  87. */
  88. isNarrowLayout: boolean;
  89. /**
  90. * Whether or not the statistics are for screen share.
  91. */
  92. isVirtualScreenshareParticipant: boolean;
  93. /**
  94. * The send-side max enabled resolution (aka the highest layer that is not
  95. * suspended on the send-side).
  96. */
  97. maxEnabledResolution: number;
  98. /**
  99. * Callback to invoke when the user clicks on the download logs link.
  100. */
  101. onSaveLogs: () => void;
  102. /**
  103. * Callback to invoke when the show additional stats link is clicked.
  104. */
  105. onShowMore: (e?: React.MouseEvent) => void;
  106. /**
  107. * Statistics related to packet loss.
  108. * {{
  109. * download: Number,
  110. * upload: Number
  111. * }}.
  112. */
  113. packetLoss: DownloadUpload;
  114. /**
  115. * The endpoint id of this client.
  116. */
  117. participantId: string;
  118. /**
  119. * The region that we think the client is in.
  120. */
  121. region: string;
  122. /**
  123. * Statistics related to display resolutions for each ssrc.
  124. * {{
  125. * [ ssrc ]: {
  126. * height: Number,
  127. * width: Number
  128. * }
  129. * }}.
  130. */
  131. resolution: {
  132. [ssrc: string]: {
  133. height: number;
  134. width: number;
  135. };
  136. };
  137. /**
  138. * The region of the media server that we are connected to.
  139. */
  140. serverRegion: string;
  141. /**
  142. * Whether or not additional stats about bandwidth and transport should be
  143. * displayed. Will not display even if true for remote participants.
  144. */
  145. shouldShowMore: boolean;
  146. /**
  147. * Statistics related to transports.
  148. */
  149. transport: Array<{
  150. ip: string;
  151. localCandidateType: string;
  152. localip: string;
  153. p2p: boolean;
  154. remoteCandidateType: string;
  155. transportType: string;
  156. type: string;
  157. }>;
  158. /**
  159. * The video SSRC of this client.
  160. */
  161. videoSsrc: number;
  162. }
  163. /**
  164. * Click handler.
  165. *
  166. * @param {SyntheticEvent} event - The click event.
  167. * @returns {void}
  168. */
  169. function onClick(event: React.MouseEvent) {
  170. // If the event is propagated to the thumbnail container the participant will be pinned. That's why the propagation
  171. // needs to be stopped.
  172. event.stopPropagation();
  173. }
  174. const styles = (theme: Theme) => {
  175. return {
  176. actions: {
  177. margin: '10px auto',
  178. textAlign: 'center' as const
  179. },
  180. connectionStatsTable: {
  181. '&, & > table': {
  182. fontSize: '12px',
  183. fontWeight: '400',
  184. '& td': {
  185. padding: '2px 0'
  186. }
  187. },
  188. '& > table': {
  189. whiteSpace: 'nowrap'
  190. },
  191. '& td:nth-child(n-1)': {
  192. paddingLeft: '5px'
  193. },
  194. '& $upload, & $download': {
  195. marginRight: '2px'
  196. }
  197. },
  198. contextMenu: {
  199. position: 'relative' as const,
  200. marginTop: 0,
  201. right: 'auto',
  202. padding: `${theme.spacing(2)} ${theme.spacing(1)}`,
  203. marginLeft: theme.spacing(1),
  204. marginRight: theme.spacing(1),
  205. marginBottom: theme.spacing(1)
  206. },
  207. download: {},
  208. mobile: {
  209. margin: theme.spacing(3)
  210. },
  211. status: {
  212. fontWeight: 'bold'
  213. },
  214. upload: {}
  215. };
  216. };
  217. /**
  218. * React {@code Component} for displaying connection statistics.
  219. *
  220. * @augments Component
  221. */
  222. class ConnectionStatsTable extends Component<IProps> {
  223. /**
  224. * Implements React's {@link Component#render()}.
  225. *
  226. * @inheritdoc
  227. * @returns {ReactElement}
  228. */
  229. render() {
  230. const {
  231. classes,
  232. disableShowMoreStats,
  233. enableSaveLogs,
  234. isVirtualScreenshareParticipant,
  235. isLocalVideo,
  236. isNarrowLayout
  237. } = this.props;
  238. const className = clsx(classes.connectionStatsTable, { [classes.mobile]: isMobileBrowser() || isNarrowLayout });
  239. if (isVirtualScreenshareParticipant) {
  240. return this._renderScreenShareStatus();
  241. }
  242. return (
  243. <ContextMenu
  244. className = { classes.contextMenu }
  245. hidden = { false }
  246. inDrawer = { true }>
  247. <div
  248. className = { className }
  249. onClick = { onClick }>
  250. { this._renderStatistics() }
  251. <div className = { classes.actions }>
  252. { isLocalVideo && enableSaveLogs ? this._renderSaveLogs() : null}
  253. { !disableShowMoreStats && this._renderShowMoreLink() }
  254. </div>
  255. { this.props.shouldShowMore ? this._renderAdditionalStats() : null }
  256. </div>
  257. </ContextMenu>
  258. );
  259. }
  260. /**
  261. * Creates a ReactElement that will display connection statistics for a screen share thumbnail.
  262. *
  263. * @private
  264. * @returns {ReactElement}
  265. */
  266. _renderScreenShareStatus() {
  267. const { classes } = this.props;
  268. const className = clsx(classes.connectionStatsTable, { [classes.mobile]: isMobileBrowser() });
  269. return (<ContextMenu
  270. className = { classes.contextMenu }
  271. hidden = { false }
  272. inDrawer = { true }>
  273. <div
  274. className = { className }
  275. onClick = { onClick }>
  276. <tbody>
  277. { this._renderResolution() }
  278. { this._renderFrameRate() }
  279. </tbody>
  280. </div>
  281. </ContextMenu>);
  282. }
  283. /**
  284. * Creates a table as ReactElement that will display additional statistics
  285. * related to bandwidth and transport for the local user.
  286. *
  287. * @private
  288. * @returns {ReactElement}
  289. */
  290. _renderAdditionalStats() {
  291. const { isLocalVideo } = this.props;
  292. return (
  293. <table>
  294. <tbody>
  295. { isLocalVideo ? this._renderBandwidth() : null }
  296. { isLocalVideo ? this._renderTransport() : null }
  297. { this._renderRegion() }
  298. { isLocalVideo ? this._renderBridgeCount() : null }
  299. { this._renderAudioSsrc() }
  300. { this._renderVideoSsrc() }
  301. { this._renderParticipantId() }
  302. </tbody>
  303. </table>
  304. );
  305. }
  306. /**
  307. * Creates a table row as a ReactElement for displaying bandwidth related
  308. * statistics.
  309. *
  310. * @private
  311. * @returns {ReactElement}
  312. */
  313. _renderBandwidth() {
  314. const { classes } = this.props;
  315. const { download, upload } = this.props.bandwidth || {};
  316. return (
  317. <tr>
  318. <td>
  319. { this.props.t('connectionindicator.bandwidth') }
  320. </td>
  321. <td>
  322. <span className = { classes.download }>
  323. &darr;
  324. </span>
  325. { download ? `${download} Kbps` : 'N/A' }
  326. <span className = { classes.upload }>
  327. &uarr;
  328. </span>
  329. { upload ? `${upload} Kbps` : 'N/A' }
  330. </td>
  331. </tr>
  332. );
  333. }
  334. /**
  335. * Creates a a table row as a ReactElement for displaying bitrate related
  336. * statistics.
  337. *
  338. * @private
  339. * @returns {ReactElement}
  340. */
  341. _renderBitrate() {
  342. const { classes } = this.props;
  343. const { download, upload } = this.props.bitrate || {};
  344. return (
  345. <tr>
  346. <td>
  347. <span>
  348. { this.props.t('connectionindicator.bitrate') }
  349. </span>
  350. </td>
  351. <td>
  352. <span className = { classes.download }>
  353. &darr;
  354. </span>
  355. { download ? `${download} Kbps` : 'N/A' }
  356. <span className = { classes.upload }>
  357. &uarr;
  358. </span>
  359. { upload ? `${upload} Kbps` : 'N/A' }
  360. </td>
  361. </tr>
  362. );
  363. }
  364. /**
  365. * Creates a table row as a ReactElement for displaying the audio ssrc.
  366. * This will typically be something like "Audio SSRC: 12345".
  367. *
  368. * @returns {JSX.Element}
  369. * @private
  370. */
  371. _renderAudioSsrc() {
  372. const { audioSsrc, t } = this.props;
  373. return (
  374. <tr>
  375. <td>
  376. <span>{ t('connectionindicator.audio_ssrc') }</span>
  377. </td>
  378. <td>{ audioSsrc || 'N/A' }</td>
  379. </tr>
  380. );
  381. }
  382. /**
  383. * Creates a table row as a ReactElement for displaying the video ssrc.
  384. * This will typically be something like "Video SSRC: 12345".
  385. *
  386. * @returns {JSX.Element}
  387. * @private
  388. */
  389. _renderVideoSsrc() {
  390. const { videoSsrc, t } = this.props;
  391. return (
  392. <tr>
  393. <td>
  394. <span>{ t('connectionindicator.video_ssrc') }</span>
  395. </td>
  396. <td>{ videoSsrc || 'N/A' }</td>
  397. </tr>
  398. );
  399. }
  400. /**
  401. * Creates a table row as a ReactElement for displaying the endpoint id.
  402. * This will typically be something like "Endpoint id: 1e8fbg".
  403. *
  404. * @returns {JSX.Element}
  405. * @private
  406. */
  407. _renderParticipantId() {
  408. const { participantId, t } = this.props;
  409. return (
  410. <tr>
  411. <td>
  412. <span>{ t('connectionindicator.participant_id') }</span>
  413. </td>
  414. <td>{ participantId || 'N/A' }</td>
  415. </tr>
  416. );
  417. }
  418. /**
  419. * Creates a a table row as a ReactElement for displaying codec, if present.
  420. * This will typically be something like "Codecs (A/V): Opus, vp8".
  421. *
  422. * @private
  423. * @returns {ReactElement}
  424. */
  425. _renderCodecs() {
  426. const { audioSsrc, codec, t, videoSsrc } = this.props;
  427. let codecString = 'N/A';
  428. if (codec) {
  429. const audioCodec = codec[audioSsrc]?.audio;
  430. const videoCodec = codec[videoSsrc]?.video;
  431. if (audioCodec || videoCodec) {
  432. codecString = [ audioCodec, videoCodec ].filter(Boolean).join(', ');
  433. }
  434. }
  435. return (
  436. <tr>
  437. <td>
  438. <span>{ t('connectionindicator.codecs') }</span>
  439. </td>
  440. <td>{ codecString }</td>
  441. </tr>
  442. );
  443. }
  444. /**
  445. * Creates a a table row as a ReactElement for displaying e2ee verication status, if present.
  446. *
  447. * @private
  448. * @returns {ReactElement}
  449. */
  450. _renderE2EEVerified() {
  451. const { e2eeVerified, t } = this.props;
  452. if (e2eeVerified === undefined) {
  453. return;
  454. }
  455. const status = e2eeVerified ? '\u{2705}' : '\u{274C}';
  456. return (
  457. <tr>
  458. <td>
  459. <span>{ t('connectionindicator.e2eeVerified') }</span>
  460. </td>
  461. <td>{ status }</td>
  462. </tr>
  463. );
  464. }
  465. /**
  466. * Creates a table row as a ReactElement for displaying a summary message
  467. * about the current connection status.
  468. *
  469. * @private
  470. * @returns {ReactElement}
  471. */
  472. _renderConnectionSummary() {
  473. const { classes } = this.props;
  474. return (
  475. <tr className = { classes.status }>
  476. <td>
  477. <span>{ this.props.t('connectionindicator.status') }</span>
  478. </td>
  479. <td>{ this.props.connectionSummary }</td>
  480. </tr>
  481. );
  482. }
  483. /**
  484. * Creates a table row as a ReactElement for displaying the "connected to"
  485. * information.
  486. *
  487. * @returns {ReactElement}
  488. * @private
  489. */
  490. _renderRegion() {
  491. const { region, serverRegion, t } = this.props;
  492. let str = serverRegion;
  493. if (!serverRegion) {
  494. return;
  495. }
  496. if (region && serverRegion && region !== serverRegion) {
  497. str += ` from ${region}`;
  498. }
  499. return (
  500. <tr>
  501. <td>
  502. <span>{ t('connectionindicator.connectedTo') }</span>
  503. </td>
  504. <td>{ str }</td>
  505. </tr>
  506. );
  507. }
  508. /**
  509. * Creates a table row as a ReactElement for displaying the "bridge count"
  510. * information.
  511. *
  512. * @returns {*}
  513. * @private
  514. */
  515. _renderBridgeCount() {
  516. const { bridgeCount, t } = this.props;
  517. // 0 is valid, but undefined/null/NaN aren't.
  518. if (!bridgeCount && bridgeCount !== 0) {
  519. return;
  520. }
  521. return (
  522. <tr>
  523. <td>
  524. <span>{ t('connectionindicator.bridgeCount') }</span>
  525. </td>
  526. <td>{ bridgeCount }</td>
  527. </tr>
  528. );
  529. }
  530. /**
  531. * Creates a table row as a ReactElement for displaying frame rate related
  532. * statistics.
  533. *
  534. * @private
  535. * @returns {ReactElement}
  536. */
  537. _renderFrameRate() {
  538. const { framerate, t, videoSsrc } = this.props;
  539. let frameRateString = 'N/A';
  540. if (framerate) {
  541. frameRateString = String(framerate[videoSsrc] ?? 'N/A');
  542. }
  543. return (
  544. <tr>
  545. <td>
  546. <span>{ t('connectionindicator.framerate') }</span>
  547. </td>
  548. <td>{ frameRateString }</td>
  549. </tr>
  550. );
  551. }
  552. /**
  553. * Creates a tables row as a ReactElement for displaying packet loss related
  554. * statistics.
  555. *
  556. * @private
  557. * @returns {ReactElement}
  558. */
  559. _renderPacketLoss() {
  560. const { classes, packetLoss, t } = this.props;
  561. let packetLossTableData;
  562. if (packetLoss) {
  563. const { download, upload } = packetLoss;
  564. packetLossTableData = (
  565. <td>
  566. <span className = { classes.download }>
  567. &darr;
  568. </span>
  569. { download === null ? 'N/A' : `${download}%` }
  570. <span className = { classes.upload }>
  571. &uarr;
  572. </span>
  573. { upload === null ? 'N/A' : `${upload}%` }
  574. </td>
  575. );
  576. } else {
  577. packetLossTableData = <td>N/A</td>;
  578. }
  579. return (
  580. <tr>
  581. <td>
  582. <span>
  583. { t('connectionindicator.packetloss') }
  584. </span>
  585. </td>
  586. { packetLossTableData }
  587. </tr>
  588. );
  589. }
  590. /**
  591. * Creates a table row as a ReactElement for displaying resolution related
  592. * statistics.
  593. *
  594. * @private
  595. * @returns {ReactElement}
  596. */
  597. _renderResolution() {
  598. const { isVirtualScreenshareParticipant, maxEnabledResolution, resolution, t, videoSsrc } = this.props;
  599. let resolutionString = 'N/A';
  600. if (resolution && videoSsrc) {
  601. const { width, height } = resolution[videoSsrc] ?? { };
  602. if (width && height) {
  603. resolutionString = `${width}x${height}`;
  604. if (maxEnabledResolution && maxEnabledResolution < 720 && !isVirtualScreenshareParticipant) {
  605. const maxEnabledResolutionTitle = t('connectionindicator.maxEnabledResolution');
  606. resolutionString += ` (${maxEnabledResolutionTitle} ${maxEnabledResolution}p)`;
  607. }
  608. }
  609. }
  610. return (
  611. <tr>
  612. <td>
  613. <span>{ t('connectionindicator.resolution') }</span>
  614. </td>
  615. <td>{ resolutionString }</td>
  616. </tr>
  617. );
  618. }
  619. /**
  620. * Creates a ReactElement for display a link to save the logs.
  621. *
  622. * @private
  623. * @returns {ReactElement}
  624. */
  625. _renderSaveLogs() {
  626. return (
  627. <span>
  628. <a
  629. className = 'savelogs link'
  630. onClick = { this.props.onSaveLogs }
  631. role = 'button'
  632. tabIndex = { 0 }>
  633. { this.props.t('connectionindicator.savelogs') }
  634. </a>
  635. <span> | </span>
  636. </span>
  637. );
  638. }
  639. /**
  640. * Creates a ReactElement for display a link to toggle showing additional
  641. * statistics.
  642. *
  643. * @private
  644. * @returns {ReactElement}
  645. */
  646. _renderShowMoreLink() {
  647. const translationKey
  648. = this.props.shouldShowMore
  649. ? 'connectionindicator.less'
  650. : 'connectionindicator.more';
  651. return (
  652. <a
  653. className = 'showmore link'
  654. onClick = { this.props.onShowMore }
  655. role = 'button'
  656. tabIndex = { 0 }>
  657. { this.props.t(translationKey) }
  658. </a>
  659. );
  660. }
  661. /**
  662. * Creates a table as a ReactElement for displaying connection statistics.
  663. *
  664. * @private
  665. * @returns {ReactElement}
  666. */
  667. _renderStatistics() {
  668. return (
  669. <table>
  670. <tbody>
  671. { this._renderConnectionSummary() }
  672. { this._renderBitrate() }
  673. { this._renderPacketLoss() }
  674. { this._renderResolution() }
  675. { this._renderFrameRate() }
  676. { this._renderCodecs() }
  677. { this._renderE2EEVerified() }
  678. </tbody>
  679. </table>
  680. );
  681. }
  682. /**
  683. * Creates table rows as ReactElements for displaying transport related
  684. * statistics.
  685. *
  686. * @private
  687. * @returns {ReactElement[]}
  688. */
  689. _renderTransport() {
  690. const { t, transport } = this.props;
  691. if (!transport || transport.length === 0) {
  692. const NA = (
  693. <tr key = 'address'>
  694. <td>
  695. <span>{ t('connectionindicator.address') }</span>
  696. </td>
  697. <td>
  698. N/A
  699. </td>
  700. </tr>
  701. );
  702. return [ NA ];
  703. }
  704. const data: {
  705. localIP: string[];
  706. localPort: string[];
  707. remoteIP: string[];
  708. remotePort: string[];
  709. transportType: string[];
  710. } = {
  711. localIP: [],
  712. localPort: [],
  713. remoteIP: [],
  714. remotePort: [],
  715. transportType: []
  716. };
  717. for (let i = 0; i < transport.length; i++) {
  718. const ip = getIP(transport[i].ip);
  719. const localIP = getIP(transport[i].localip);
  720. const localPort = getPort(transport[i].localip);
  721. const port = getPort(transport[i].ip);
  722. if (!data.remoteIP.includes(ip)) {
  723. data.remoteIP.push(ip);
  724. }
  725. if (!data.localIP.includes(localIP)) {
  726. data.localIP.push(localIP);
  727. }
  728. if (!data.localPort.includes(localPort)) {
  729. data.localPort.push(localPort);
  730. }
  731. if (!data.remotePort.includes(port)) {
  732. data.remotePort.push(port);
  733. }
  734. if (!data.transportType.includes(transport[i].type)) {
  735. data.transportType.push(transport[i].type);
  736. }
  737. }
  738. // All of the transports should be either P2P or JVB
  739. let isP2P = false, isTURN = false;
  740. if (transport.length) {
  741. isP2P = transport[0].p2p;
  742. isTURN = transport[0].localCandidateType === 'relay'
  743. || transport[0].remoteCandidateType === 'relay';
  744. }
  745. const additionalData = [];
  746. if (isP2P) {
  747. additionalData.push(
  748. <span> (p2p)</span>);
  749. }
  750. if (isTURN) {
  751. additionalData.push(<span> (turn)</span>);
  752. }
  753. // First show remote statistics, then local, and then transport type.
  754. const tableRowConfigurations = [
  755. {
  756. additionalData,
  757. data: data.remoteIP,
  758. key: 'remoteaddress',
  759. label: t('connectionindicator.remoteaddress',
  760. { count: data.remoteIP.length })
  761. },
  762. {
  763. data: data.remotePort,
  764. key: 'remoteport',
  765. label: t('connectionindicator.remoteport',
  766. { count: transport.length })
  767. },
  768. {
  769. data: data.localIP,
  770. key: 'localaddress',
  771. label: t('connectionindicator.localaddress',
  772. { count: data.localIP.length })
  773. },
  774. {
  775. data: data.localPort,
  776. key: 'localport',
  777. label: t('connectionindicator.localport',
  778. { count: transport.length })
  779. },
  780. {
  781. data: data.transportType,
  782. key: 'transport',
  783. label: t('connectionindicator.transport',
  784. { count: data.transportType.length })
  785. }
  786. ];
  787. return tableRowConfigurations.map(this._renderTransportTableRow);
  788. }
  789. /**
  790. * Creates a table row as a ReactElement for displaying a transport related
  791. * statistic.
  792. *
  793. * @param {Object} config - Describes the contents of the row.
  794. * @param {ReactElement} config.additionalData - Extra data to display next
  795. * to the passed in config.data.
  796. * @param {Array} config.data - The transport statistics to display.
  797. * @param {string} config.key - The ReactElement's key. Must be unique for
  798. * iterating over multiple child rows.
  799. * @param {string} config.label - The text to display describing the data.
  800. * @private
  801. * @returns {ReactElement}
  802. */
  803. _renderTransportTableRow(config: any) {
  804. const { additionalData, data, key, label } = config;
  805. return (
  806. <tr key = { key }>
  807. <td>
  808. <span>
  809. { label }
  810. </span>
  811. </td>
  812. <td>
  813. { getStringFromArray(data) }
  814. { additionalData || null }
  815. </td>
  816. </tr>
  817. );
  818. }
  819. }
  820. /**
  821. * Utility for getting the IP from a transport statistics object's
  822. * representation of an IP.
  823. *
  824. * @param {string} value - The transport's IP to parse.
  825. * @private
  826. * @returns {string}
  827. */
  828. function getIP(value: string) {
  829. if (!value) {
  830. return '';
  831. }
  832. return value.substring(0, value.lastIndexOf(':'));
  833. }
  834. /**
  835. * Utility for getting the port from a transport statistics object's
  836. * representation of an IP.
  837. *
  838. * @param {string} value - The transport's IP to parse.
  839. * @private
  840. * @returns {string}
  841. */
  842. function getPort(value: string) {
  843. if (!value) {
  844. return '';
  845. }
  846. return value.substring(value.lastIndexOf(':') + 1, value.length);
  847. }
  848. /**
  849. * Utility for concatenating values in an array into a comma separated string.
  850. *
  851. * @param {Array} array - Transport statistics to concatenate.
  852. * @private
  853. * @returns {string}
  854. */
  855. function getStringFromArray(array: string[]) {
  856. let res = '';
  857. for (let i = 0; i < array.length; i++) {
  858. res += (i === 0 ? '' : ', ') + array[i];
  859. }
  860. return res;
  861. }
  862. export default translate(withStyles(styles)(ConnectionStatsTable));