Skip to content

Commit

Permalink
Updated the metrics reporting url (#36)
Browse files Browse the repository at this point in the history
* Added more tests

* Pointing to the new reporting base url

* Added more tests

* Updated the base URL for the metrics reporting

* Added a console warning when not able to report metrics

* fix: update URL to point to playback.livepeer.fun

Co-authored-by: Chase Adams <c@cadams.io>
  • Loading branch information
ClaCla and 0xcadams authored Sep 8, 2022
1 parent d3aa654 commit 3284d85
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
6 changes: 5 additions & 1 deletion packages/core/src/video/hls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ export const createNewHls = (
}
});

// TODO: re-enable after testing and before merging
const metricReportingUrl = createMetricsReportingUrl(source);
if (metricReportingUrl) {
reportVideoMetrics(element, metricReportingUrl);
} else {
console.log(
'Not able to report player metrics given the source url',
source,
);
}
});

Expand Down
62 changes: 54 additions & 8 deletions packages/core/src/video/metrics/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,59 @@ import { describe, expect, it } from 'vitest';
import { createMetricsReportingUrl } from './utils';

describe('createMetricsReportingUrl', () => {
it('creates', () => {
const url = createMetricsReportingUrl(
'https://livepeercdn.com/recordings/c34af47b-bbf2-40ed-ad2d-77abd43860c9/index.m3u8',
);

expect(url).toMatchInlineSnapshot(
'"wss://sao-canary-catalyst-0.livepeer.fun/json_video+c34af47b-bbf2-40ed-ad2d-77abd43860c9.js"',
);
describe('asset url', () => {
it('given a valid url then it should return a reporting url', () => {
// Given
const sourceUrl =
'https://livepeercdn.com/hls/172159gos7h0pq17/index.m3u8';
// When
const reportingUrl = createMetricsReportingUrl(sourceUrl);
// Then
expect(reportingUrl).toMatchInlineSnapshot(
'"wss://playback.livepeer.fun/json_video+172159gos7h0pq17.js"',
);
});

it('given invalid urls then it should not return a reporting urls', () => {
// Given
const sourceUrls = [
'https://livepeercdn.com/dash/172159gos7h0pq17/index.m3u8',
'https://livepeercdn.com/hls/172159gos7h0pq17/master.m3u8',
];
// When
const reportingUrls = sourceUrls.map((url) =>
createMetricsReportingUrl(url),
);
// Then
expect(reportingUrls).toEqual([undefined, undefined]);
});
});

describe('recording url', () => {
it('given a valid url then it should return a reporting url', () => {
// Given
const sourceUrl =
'https://livepeercdn.com/recordings/c34af47b-bbf2-40ed-ad2d-77abd43860c9/index.m3u8';
// When
const reportingUrl = createMetricsReportingUrl(sourceUrl);
// Then
expect(reportingUrl).toMatchInlineSnapshot(
'"wss://playback.livepeer.fun/json_video+c34af47b-bbf2-40ed-ad2d-77abd43860c9.js"',
);
});

it('given invalid urls then it should not return a reporting urls', () => {
// Given
const sourceUrls = [
'https://livepeercdn.com/static/c34af47b-bbf2-40ed-ad2d-77abd43860c9/index.m3u8',
'https://livepeercdn.com/recordings/c34af47b-bbf2-40ed-ad2d-77abd43860c9/master.m3u8',
];
// When
const reportingUrls = sourceUrls.map((url) =>
createMetricsReportingUrl(url),
);
// Then
expect(reportingUrls).toEqual([undefined, undefined]);
});
});
});
28 changes: 21 additions & 7 deletions packages/core/src/video/metrics/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
// Temporarily hardcoded catalyst node
const METRICS_REPORTING_BASE_URL = 'wss://sao-canary-catalyst-0.livepeer.fun';
// Temporarily hardcoded catalyst node, in production we need to point to .studio
const METRICS_REPORTING_BASE_URL = 'wss://playback.livepeer.fun';

function videoNameFromPlaybackUrl(url: string): string | undefined {
const filename: string[] = url.split('/');
const videoName = filename[filename.length - 2];
return videoName ? videoName.split('.')[0] : undefined;
const PLAYLIST_NAME = 'index.m3u8';
const ASSET_URL_PART_VALUE = 'hls';
const RECORDING_URL_PART_VALUE = 'recordings';

// Example url the playback id needs to be found in
// https://livepeercdn.com/hls/<playback-id>/index.m3u8
// https://livepeercdn.com/recordings/<playback-id>/index.m3u8
function playbackIdFromPlaybackUrl(url: string): string | undefined {
const parts = url.split('/');
const playlistPartIndex = parts.indexOf(PLAYLIST_NAME);
const assetPartIndex = parts.indexOf(ASSET_URL_PART_VALUE);
const recordingPartIndex = parts.indexOf(RECORDING_URL_PART_VALUE);

// Check if the url is valid
return (assetPartIndex !== -1 || recordingPartIndex !== -1) &&
playlistPartIndex !== -1
? parts[playlistPartIndex - 1]
: undefined;
}

export function createMetricsReportingUrl(src: string): string | undefined {
const videoName = videoNameFromPlaybackUrl(src);
const videoName = playbackIdFromPlaybackUrl(src);
return videoName
? `${METRICS_REPORTING_BASE_URL}/json_video+${videoName}.js`
: undefined;
Expand Down

1 comment on commit 3284d85

@vercel
Copy link

@vercel vercel bot commented on 3284d85 Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.