import { BaseModel } from '@verb/src/libs/shared/base/base.model'; import { MessageHasher } from '@verb/src/libs/shared/message-hasher/message-hasher.service'; import { VideoTranscodeData, VideoTranscodeModel } from './video-transcode.model'; export class VideoStatusModel implements BaseModel { public constructor( private readonly _messageHasher: MessageHasher, videoStatusData?: VideoStatusData, ) { if (videoStatusData) { this.fromSchema(videoStatusData); } } public get endedAt() { return this._endedAt; } public set endedAt(_endedAt) { this._endedAt = _endedAt; } public get error() { return this._error; } public set error(_error) { this._error = _error; this.finish(this._stepDescription); } public get metadata() { return this._metadata; } public set metadata(_metadata) { this._metadata = _metadata; } public get percentComplete() { return +(Math.min(this._stepNumber / Math.max(this._stepCount, 1), 100) * 100).toFixed(2); } public set progress(_percentComplete: number) { if (_percentComplete !== 100 || Math.floor(this.stepNumber) !== this._stepNumber) { this._stepNumber = Math.floor(this._stepNumber) + +_percentComplete.toFixed(2) / 100; } } public get startedAt() { return this._startedAt; } public set startedAt(_startedAt) { this._startedAt = _startedAt; } public get statusId() { return this._statusId; } public set statusId(_statusId) { this._statusId = _statusId; } public get stepCount() { return this._stepCount; } public set stepCount(_stepCount) { this._stepCount = _stepCount; } public get stepDescription() { return this._stepDescription; } public set stepDescription(_stepDescription) { this._stepDescription = _stepDescription; } public get stepNumber() { return this._stepNumber; } public set stepNumber(_stepNumber) { this._stepNumber = _stepNumber; } public get videoId() { return this._videoId; } public set videoId(_videoId) { this._videoId = _videoId; this._metadata.videoId = _videoId; } public addVideoLocation(format: string, url: string) { this._metadata.addVideoLocation(format, url); } public describe(stepDescription: string) { this._stepDescription = stepDescription; } public finish(stepDescription: string) { this._endedAt = new Date(); this._stepDescription = stepDescription; this._stepNumber = this._stepCount; } public increment(stepDescription: string) { this.describe(stepDescription); this._stepNumber = Math.min(this._stepNumber + 1, this._stepCount); } public start(statusId: string, stepDescription: string, stepCount: number) { this.statusId = statusId; this._startedAt = new Date(); this._stepDescription = stepDescription; this._stepCount = stepCount; this._stepNumber = 1; } public fromSchema(videoStatusData: VideoStatusData) { this._endedAt = videoStatusData.ended_at ? new Date(videoStatusData.ended_at) : null; this._error = videoStatusData.error; this._metadata = new VideoTranscodeModel(this._messageHasher, videoStatusData.metadata); this._startedAt = videoStatusData.started_at ? new Date(videoStatusData.started_at) : null; this._statusId = videoStatusData.status_id; this._stepCount = videoStatusData.step_count; this._stepDescription = videoStatusData.step_description; this._stepNumber = videoStatusData.step_number; this._videoId = videoStatusData.video_id; } public toSchema(): VideoStatusData { return { ended_at: this._endedAt ? this._endedAt.toUTCString() : null, error: this._error, metadata: this._metadata.toSchema(), percent_complete: this.percentComplete, started_at: this._startedAt ? this._startedAt.toUTCString() : null, status_id: this._statusId, step_count: this._stepCount, step_description: this._stepDescription, step_number: this._stepNumber, video_id: this._videoId, }; } private _endedAt: Date | null = null; private _error = ''; private _metadata = new VideoTranscodeModel(this._messageHasher); private _startedAt: Date | null = null; private _statusId = ''; private _stepCount = 0; private _stepDescription = ''; private _stepNumber = 0; private _videoId = ''; } export interface VideoStatusData { ended_at: string | null; error: string; metadata: VideoTranscodeData; percent_complete: number; started_at: string | null; status_id: string; step_count: number; step_description: string; step_number: number; video_id: string; }