basic functionality...
This commit is contained in:
132
src/pcast/Reporting.ts
Normal file
132
src/pcast/Reporting.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
import assertUnreachable from '../lang/assertUnreachable';
|
||||
import {HttpMethod} from '../net/http/HttpMethod';
|
||||
import type IResponse from './IResponse';
|
||||
import type { PCastHttpRequests } from './PCastRequests';
|
||||
|
||||
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<IResponse<'publishingReport', string>>(HttpMethod.PUT, '/pcast/reporting/publishing', requestPublishingOptions);
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
private async requestViewingReport(options: ViewingReportOptions): Promise<string> {
|
||||
const viewingReportOptions = {
|
||||
...options
|
||||
};
|
||||
|
||||
const requestViewingOptions = {
|
||||
body: JSON.stringify({viewingReport: viewingReportOptions})
|
||||
};
|
||||
|
||||
const response = await this._httpRequests.request<IResponse<'viewingReport', string>>(HttpMethod.PUT, '/pcast/reporting/viewing', requestViewingOptions);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user