Skip to content

Instantly share code, notes, and snippets.

@feniljain
Last active March 24, 2023 09:48
Show Gist options
  • Select an option

  • Save feniljain/2b98f9a51fc66d353c3cb6f5ac5112e6 to your computer and use it in GitHub Desktop.

Select an option

Save feniljain/2b98f9a51fc66d353c3cb6f5ac5112e6 to your computer and use it in GitHub Desktop.
IVS Get Metrics Script
import {
CloudWatchClient,
GetMetricDataCommand,
} from '@aws-sdk/client-cloudwatch';
let cloudWatchClient = new CloudWatchClient({ region: 'ap-south-1' });
async function getMetrics(livestreamSession) {
console.log('before startedTime: ', livestreamSession.startedTime);
console.log('before stoppedTime: ', livestreamSession.stoppedTime);
const channelArn = livestreamSession.externalId.split('/')[1];
console.log('channelArn: ', channelArn);
// Round StartedTime to ceil minute
livestreamSession.startedTime.setSeconds(0);
// Round StoppedTime to floor minute
livestreamSession.stoppedTime.setMinutes(livestreamSession.stoppedTime.getMinutes() + 1);
livestreamSession.stoppedTime.setSeconds(0);
console.log('startedTime: ', livestreamSession.startedTime);
console.log('stoppedTime: ', livestreamSession.stoppedTime);
const getMetricDataCommand = new GetMetricDataCommand({
// StartTime: new Date("2022-06-01 09:49:17.856"),
// EndTime: new Date("2022-06-04 09:51:23.856"),
// StartTime: new Date("2022-06-05 12:44:05.856"),
// EndTime: new Date("2022-06-05 12:44:48.856"),
StartTime: livestreamSession.startedTime,
EndTime: livestreamSession.stoppedTime,
// EndTime: new Date(Date.now()),
MetricDataQueries: [
{
Id: 'm1',
ReturnData: true,
MetricStat: {
Metric: {
Dimensions: [
{
Name: 'Channel',
Value: channelArn,
},
],
Namespace: 'AWS/IVS',
MetricName: 'LiveInputTime',
},
Period: 300,
Stat: 'Sum',
},
},
{
Id: 'm2',
ReturnData: true,
MetricStat: {
Metric: {
Dimensions: [
{
Name: 'Channel',
Value: channelArn,
},
],
Namespace: 'AWS/IVS',
MetricName: 'LiveDeliveredTime',
},
Period: 300,
Stat: 'Sum',
},
},
],
});
const getMetricDataCommandResponse = await cloudWatchClient.send(
getMetricDataCommand,
);
console.log('getMetricDataCommandResponse: ', getMetricDataCommandResponse);
const metricDataResultsValues =
getMetricDataCommandResponse.MetricDataResults.values();
let ingestSeconds = 0;
let viewerSeconds = 0;
// eslint-disable-next-line no-restricted-syntax
for (const ele of metricDataResultsValues) {
for (let i = 0; i < ele.Timestamps.length; i += 1) {
console.log('timestamp: ', new Date(ele.Timestamps[i]), 'value: ', ele.Values[i]);
}
if (ele.Id === 'm1') {
for (let i = 0; i < ele.Timestamps.length; i += 1) {
ingestSeconds += ele.Values[i];
}
} else if (ele.Id === 'm2') {
for (let i = 0; i < ele.Timestamps.length; i += 1) {
viewerSeconds += ele.Values[i];
}
}
}
// TODO: Make it such that `ingestMinutes` gets added to old
// `ingestMinutes`, same for viewerMinutes ( this is most probs not relevant now )
return {
ingestSeconds: ingestSeconds ?? 0,
viewerSeconds: viewerSeconds ?? 0,
};
}
let livestreamSession = {
startedTime: new Date('2023-03-24T08:38:07Z'),
stoppedTime: new Date('2023-03-24T08:39:14Z'),
externalId: 'arn:aws:ivs:ap-south-1:944908621410:channel/2OqnsJ986AHM'
};
// function getTimeZone(time) {
// var offset = new Date(time).getTimezoneOffset(), o = Math.abs(offset);
// return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
// }
// console.log('UTC time of started time: ', getTimeZone(livestreamSession.startedTime));
// console.log('UTC time of stopped time: ', getTimeZone(livestreamSession.stoppedTime));
const metrics = await getMetrics(livestreamSession);
console.log('metrics obtained: ', metrics);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment