* Update version to 2025.0.3, and remove unused code * Upgrade chai to 5.3.1 * Streamline HTTP request method usage
134 lines
3.8 KiB
TypeScript
134 lines
3.8 KiB
TypeScript
import {HttpMethod} from '../net/http/HttpMethod';
|
|
import type {PCastHttpRequests} from './PCastRequests';
|
|
import type {PublishingReportResponse, ViewingReportResponse} from './IResponse';
|
|
import assertUnreachable from '../lang/assertUnreachable';
|
|
|
|
export enum ReportKind {
|
|
Publishing = 0,
|
|
Viewing = 1
|
|
}
|
|
|
|
export type ReportKindType = 'Publishing' | 'Viewing';
|
|
|
|
export class ReportKindMapping {
|
|
public static convertReportKindTypeToReportKind(reportKindType: ReportKindType): ReportKind {
|
|
switch (reportKindType) {
|
|
case 'Publishing':
|
|
return ReportKind.Publishing;
|
|
case 'Viewing':
|
|
return ReportKind.Viewing;
|
|
|
|
default:
|
|
assertUnreachable(reportKindType);
|
|
}
|
|
}
|
|
|
|
public static convertReportKindToReportKindType(reportKind: ReportKind): ReportKindType {
|
|
switch (reportKind) {
|
|
case ReportKind.Publishing:
|
|
return 'Publishing';
|
|
case ReportKind.Viewing:
|
|
return 'Viewing';
|
|
|
|
default:
|
|
assertUnreachable(reportKind);
|
|
}
|
|
}
|
|
}
|
|
|
|
export type PublishingReportOptions = {
|
|
applicationIds?: string[];
|
|
streamIds?: string[];
|
|
channelIds?: string[];
|
|
channelAliases?: string[];
|
|
roomIds?: string[];
|
|
roomAliases?: string[];
|
|
tags?: string[];
|
|
start: string;
|
|
end: string;
|
|
};
|
|
|
|
export enum ViewingReportKind {
|
|
RealTime = 0,
|
|
HLS = 1,
|
|
DASH = 2
|
|
}
|
|
|
|
export type ViewingReportKindType = 'RealTime' | 'HLS' | 'DASH';
|
|
|
|
export type ViewingReportOptions = {
|
|
kind: ViewingReportKind;
|
|
applicationIds?: string[];
|
|
streamIds?: string[];
|
|
sessionIds?: string[];
|
|
originStreamIds?: string[];
|
|
originTags?: string[];
|
|
channelIds?: string[];
|
|
channelAliases?: string[];
|
|
roomIds?: string[];
|
|
roomAliases?: string[];
|
|
tags?: string[];
|
|
start: string;
|
|
end: string;
|
|
};
|
|
|
|
export class Reporting {
|
|
private readonly _httpRequests: PCastHttpRequests;
|
|
|
|
constructor(httpRequests: PCastHttpRequests) {
|
|
this._httpRequests = httpRequests;
|
|
}
|
|
|
|
public async generateReport<ReportOptions extends PublishingReportOptions | ViewingReportOptions>(kind: ReportKind, options: ReportOptions): Promise<string> {
|
|
console.log('[Reporting] generateReport [%o]', ReportKindMapping.convertReportKindToReportKindType(kind));
|
|
|
|
if (kind === ReportKind.Publishing) {
|
|
return this.requestPublishingReport(options as PublishingReportOptions);
|
|
}
|
|
|
|
if (kind === ReportKind.Viewing) {
|
|
return this.requestViewingReport(options as ViewingReportOptions);
|
|
}
|
|
|
|
throw new Error(`[Reporting] Unsupported report kind: ${kind}`);
|
|
}
|
|
|
|
public async requestPublishingReport(options: PublishingReportOptions): Promise<string> {
|
|
if (!(options.start || options.end)) {
|
|
throw new Error('[Reporting] [requestPublishingReport] requires a start and end Date');
|
|
}
|
|
const publishingReportOptions = {
|
|
...options
|
|
};
|
|
|
|
const requestPublishingOptions = {
|
|
body: JSON.stringify({publishingReport: publishingReportOptions})
|
|
};
|
|
const response = await this._httpRequests.request<PublishingReportResponse>(HttpMethod.PUT, '/pcast/reporting/publishing', requestPublishingOptions);
|
|
|
|
if (!response.publishingReport) {
|
|
throw new Error('[Reporting] [requestPublishingReport] Invalid response format - missing publishingReport data');
|
|
}
|
|
|
|
return response.publishingReport;
|
|
}
|
|
|
|
private async requestViewingReport(options: ViewingReportOptions): Promise<string> {
|
|
const viewingReportOptions = {
|
|
...options
|
|
};
|
|
|
|
const requestViewingOptions = {
|
|
body: JSON.stringify({viewingReport: viewingReportOptions})
|
|
};
|
|
|
|
const response = await this._httpRequests.request<ViewingReportResponse>(HttpMethod.PUT, '/pcast/reporting/viewing', requestViewingOptions);
|
|
|
|
if (!response.viewingReport) {
|
|
throw new Error('[Reporting] [requestViewingReport] Invalid response format - missing viewingReport data');
|
|
}
|
|
|
|
return response.viewingReport;
|
|
}
|
|
}
|