UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

389 lines 18.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StatisticsServiceSdk = void 0; const errors_1 = require("../utils/errors"); const sdk_1 = require("../sdk"); const date_validation_1 = require("../utils/date-validation"); class StatisticsServiceSdk { constructor(authConfig, serviceConfig) { this.authConfig = authConfig; this.config = serviceConfig; } async getSessionStatsSummaryList(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Statistics.getSessionStatsSummaryList(params); } async getInviterPosterList(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.other.getInviterPosterList(params); } async listChannelLotteryRecords(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.listChannelLotteryRecords(params); } async getLiveData(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.getLiveData(params); } async listSessionStats(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.listSessionStats(params); } async listWeixinBookings(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.listWeixinBookings(params); } async listInviteStats(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.listInviteStats(params); } async getChannelPlaySummary(params) { const userId = params.userId || this.authConfig.userId; if (!userId) { throw new errors_1.PolyVValidationError('userId is required', 'userId', userId, 'required'); } const { userId: _userId, ...options } = params; const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getChannelPlaySummary(String(userId), this.compact(options)); } async getRealtimeViewers(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getRealtimeViewers(params); } async getRedpackStats(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getRedpackStats(params); } async getChannelStatistic(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getChannelStatistic(params); } async getSessionStats(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getSessionStats(params); } async getMicDetailList(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getMicDetailList(params); } async getLinkMicDetailList(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getLinkMicDetailList(params); } async getSummary(params) { const { channelId, ...options } = params; const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return (await client.channel.getSummary(channelId, options)) ?? []; } async getProductClickStats(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getProductClickStats(params); } async getProductListStats(params) { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getProductListStats(params); } async getRealtimeViewersV1(params) { const userId = params.userId || this.authConfig.userId; if (!userId) { throw new errors_1.PolyVValidationError('userId is required', 'userId', userId, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getRealtimeViewersV1(params.channelId, String(userId)); } async getViewlogV1(params) { const userId = params.userId || this.authConfig.userId; if (!userId) { throw new errors_1.PolyVValidationError('userId is required', 'userId', userId, 'required'); } const { channelId, userId: _userId, ...options } = params; const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getViewlogV1(channelId, { ...this.compact(options), userId: String(userId) }); } async getViewlog2(params) { const { channelId, ...options } = params; const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.channel.getViewlog2(channelId, options); } async getDailyViewStatistics(options) { this.validateViewOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const result = await client.statistics.getDailyViewStatistics({ channelId: options.channelId, startDay: options.startDay, endDay: options.endDay, }); return result.contents || []; } validateViewOptions(options) { const errors = []; if (!options.channelId || typeof options.channelId !== 'string' || options.channelId.trim().length === 0) { errors.push('channelId is required and must be a non-empty string'); } if (!options.startDay || !(0, date_validation_1.isValidDateFormat)(options.startDay)) { errors.push('startDay is required and must be in yyyy-MM-dd format'); } if (!options.endDay || !(0, date_validation_1.isValidDateFormat)(options.endDay)) { errors.push('endDay is required and must be in yyyy-MM-dd format'); } if (errors.length === 0) { const rangeValidation = (0, date_validation_1.validateDateRange)(options.startDay, options.endDay, date_validation_1.MAX_DATE_RANGE_DAYS); if (!rangeValidation.valid) { errors.push(rangeValidation.error || 'Invalid date range'); } } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Statistics view options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } async getConcurrencyData(options) { this.validateConcurrencyOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const result = await client.statistics.getConcurrencyData({ channelId: options.channelId, startDate: options.startDate, endDate: options.endDate, }); return result.contents || []; } validateConcurrencyOptions(options) { const errors = []; if (!options.channelId || typeof options.channelId !== 'string' || options.channelId.trim().length === 0) { errors.push('channelId is required and must be a non-empty string'); } if (!options.startDate || !(0, date_validation_1.isValidDateFormat)(options.startDate)) { errors.push('startDate is required and must be in yyyy-MM-dd format'); } if (!options.endDate || !(0, date_validation_1.isValidDateFormat)(options.endDate)) { errors.push('endDate is required and must be in yyyy-MM-dd format'); } if (errors.length === 0) { const rangeValidation = (0, date_validation_1.validateDateRange)(options.startDate, options.endDate, date_validation_1.MAX_DATE_RANGE_DAYS); if (!rangeValidation.valid) { errors.push(rangeValidation.error || 'Invalid date range'); } } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Concurrency options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } async getMaxConcurrent(options) { this.validateMaxConcurrentOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const result = await client.statistics.getMaxConcurrent({ channelId: options.channelId, startTime: options.startTime, endTime: options.endTime, }); return result.contents || 0; } validateMaxConcurrentOptions(options) { const errors = []; if (!options.channelId || typeof options.channelId !== 'string' || options.channelId.trim().length === 0) { errors.push('channelId is required and must be a non-empty string'); } if (!(0, date_validation_1.isValidTimestamp)(options.startTime)) { errors.push('startTime is required and must be a valid timestamp'); } if (!(0, date_validation_1.isValidTimestamp)(options.endTime)) { errors.push('endTime is required and must be a valid timestamp'); } if (errors.length === 0) { const rangeValidation = (0, date_validation_1.validateTimestampRange)(options.startTime, options.endTime); if (!rangeValidation.valid) { errors.push(rangeValidation.error || 'Invalid time range'); } } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Max concurrent options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } async getRegionDistribution(options) { this.validateRegionOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const params = { channelId: options.channelId, startTime: options.startTime, endTime: options.endTime, }; if (options.type) { params.type = options.type; } const result = await client.statistics.getRegionDistribution(params); return (result.data || []).map((item) => ({ region: item.city || item.province || item.country || '未知', plays: item.plays, viewers: item.viewers, ips: item.ips, playDuration: item.playDuration, percent: `${item.percent.toFixed(2)}%`, })); } validateRegionOptions(options) { const errors = []; if (!options.channelId || typeof options.channelId !== 'string' || options.channelId.trim().length === 0) { errors.push('channelId is required and must be a non-empty string'); } if (!(0, date_validation_1.isValidTimestamp)(options.startTime)) { errors.push('startTime is required and must be a valid timestamp'); } if (!(0, date_validation_1.isValidTimestamp)(options.endTime)) { errors.push('endTime is required and must be a valid timestamp'); } if (options.type !== undefined) { const validTypes = ['country', 'province', 'city']; if (!validTypes.includes(options.type)) { errors.push('type must be one of: country, province, city'); } } if (errors.length === 0) { const rangeValidation = (0, date_validation_1.validate90DayTimestampRange)(options.startTime, options.endTime); if (!rangeValidation.valid) { errors.push(rangeValidation.error || 'Invalid time range'); } } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Region options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } async getDeviceDistribution(options) { this.validateDeviceOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const result = await client.statistics.getDeviceDistribution({ channelId: options.channelId, startTime: options.startTime, endTime: options.endTime, }); return (result.data || []).map((item) => ({ name: item.name, platform: item.platform, plays: item.plays, viewers: item.viewers, ips: item.ips, playDuration: item.playDuration, percent: `${item.percent.toFixed(2)}%`, })); } validateDeviceOptions(options) { const errors = []; if (!options.channelId || typeof options.channelId !== 'string' || options.channelId.trim().length === 0) { errors.push('channelId is required and must be a non-empty string'); } if (!(0, date_validation_1.isValidTimestamp)(options.startTime)) { errors.push('startTime is required and must be a valid timestamp'); } if (!(0, date_validation_1.isValidTimestamp)(options.endTime)) { errors.push('endTime is required and must be a valid timestamp'); } if (errors.length === 0) { const rangeValidation = (0, date_validation_1.validate90DayTimestampRange)(options.startTime, options.endTime); if (!rangeValidation.valid) { errors.push(rangeValidation.error || 'Invalid time range'); } } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Device options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } async getViewlog(options) { this.validateViewlogOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const params = { channelId: options.channelId, startDate: options.startTime, endDate: options.endTime, }; if (options.watchType) { params.watchType = options.watchType; } if (options.page !== undefined) { params.page = options.page; } if (options.pageSize !== undefined) { params.pageSize = options.pageSize; } const result = await client.statistics.getViewlog(params); return (result.contents || []).map((item) => ({ playId: item.playId, viewerId: item.param1, viewerName: item.param2, watchType: item.param3, playDuration: item.playDuration, stayDuration: item.stayDuration, sessionId: item.sessionId, ipAddress: item.ipAddress, region: `${item.province}/${item.city}`, operatingSystem: item.operatingSystem, browser: item.browser, isMobile: item.isMobile, date: item.currentDay, createdTime: item.createdTime, })); } validateViewlogOptions(options) { const errors = []; if (!options.channelId || typeof options.channelId !== 'string' || options.channelId.trim().length === 0) { errors.push('channelId is required and must be a non-empty string'); } const dateTimeRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/; if (!options.startTime || !dateTimeRegex.test(options.startTime)) { errors.push('startTime is required and must be in yyyy-MM-dd HH:mm:ss format'); } if (!options.endTime || !dateTimeRegex.test(options.endTime)) { errors.push('endTime is required and must be in yyyy-MM-dd HH:mm:ss format'); } if (errors.length === 0) { const startMonth = options.startTime.substring(0, 7); const endMonth = options.endTime.substring(0, 7); if (startMonth !== endMonth) { errors.push('startTime and endTime must be in the same month'); } } if (options.watchType !== undefined) { const validTypes = ['live', 'vod']; if (!validTypes.includes(options.watchType)) { errors.push('watchType must be either "live" or "vod"'); } } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Viewlog options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } async exportSessionStats(options) { this.validateSessionExportOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const result = await client.statistics.exportSessionStats({ channelId: options.channelId, sessionId: options.sessionId, }); const downloadUrl = typeof result === 'string' ? result : result?.downloadUrl; if (typeof downloadUrl !== 'string' || downloadUrl.trim() === '') { throw new Error('场次报表导出失败:接口未返回下载链接,报表可能尚未生成,请稍后重试'); } return { channelId: options.channelId, sessionId: options.sessionId, downloadUrl, expiresIn: '60天', }; } validateSessionExportOptions(options) { const errors = []; if (!options.channelId || typeof options.channelId !== 'string' || options.channelId.trim().length === 0) { errors.push('channelId is required and must be a non-empty string'); } if (!options.sessionId || typeof options.sessionId !== 'string' || options.sessionId.trim().length === 0) { errors.push('sessionId is required and must be a non-empty string'); } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Session export options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } compact(params) { return Object.fromEntries(Object.entries(params).filter(([, value]) => value !== undefined && value !== '')); } } exports.StatisticsServiceSdk = StatisticsServiceSdk; //# sourceMappingURL=statistics.service.sdk.js.map