-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
996 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { getIntegrationKindsByCategory } from "@homarr/definitions"; | ||
import { mediaTranscodingRequestHandler } from "@homarr/request-handler/media-transcoding"; | ||
|
||
import type { IntegrationAction } from "../../middlewares/integration"; | ||
import { createOneIntegrationMiddleware } from "../../middlewares/integration"; | ||
import { createTRPCRouter, publicProcedure } from "../../trpc"; | ||
|
||
const createIndexerManagerIntegrationMiddleware = (action: IntegrationAction) => | ||
createOneIntegrationMiddleware(action, ...getIntegrationKindsByCategory("mediaTranscoding")); | ||
|
||
export const mediaTranscodingRouter = createTRPCRouter({ | ||
getDataAsync: publicProcedure | ||
.unstable_concat(createIndexerManagerIntegrationMiddleware("query")) | ||
.query(async ({ ctx }) => { | ||
const innerHandler = mediaTranscodingRequestHandler.handler(ctx.integration, { pageOffset: 0, pageSize: 10 }); | ||
const { data } = await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false }); | ||
|
||
return { | ||
integrationId: ctx.integration.id, | ||
data, | ||
}; | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/cron-jobs/src/jobs/integrations/media-transcoding.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { EVERY_5_MINUTES } from "@homarr/cron-jobs-core/expressions"; | ||
import { createRequestIntegrationJobHandler } from "@homarr/request-handler/lib/cached-request-integration-job-handler"; | ||
import { mediaTranscodingRequestHandler } from "@homarr/request-handler/media-transcoding"; | ||
|
||
import { createCronJob } from "../../lib"; | ||
|
||
export const mediaTranscodingJob = createCronJob("mediaTranscoding", EVERY_5_MINUTES).withCallback( | ||
createRequestIntegrationJobHandler(mediaTranscodingRequestHandler.handler, { | ||
widgetKinds: ["mediaTranscoding"], | ||
getInput: { | ||
mediaTranscoding: () => ({ pageOffset: 0, pageSize: 10 }), | ||
}, | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/integrations/src/interfaces/media-transcoding/queue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export interface TdarrQueue { | ||
array: { | ||
id: string; | ||
healthCheck: string; | ||
transcode: string; | ||
filePath: string; | ||
fileSize: number; | ||
container: string; | ||
codec: string; | ||
resolution: string; | ||
type: "transcode" | "health check"; | ||
}[]; | ||
totalCount: number; | ||
startIndex: number; | ||
endIndex: number; | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/integrations/src/interfaces/media-transcoding/statistics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export interface TdarrPieSegment { | ||
name: string; | ||
value: number; | ||
}; | ||
|
||
export interface TdarrStatistics { | ||
totalFileCount: number; | ||
totalTranscodeCount: number; | ||
totalHealthCheckCount: number; | ||
failedTranscodeCount: number; | ||
failedHealthCheckCount: number; | ||
stagedTranscodeCount: number; | ||
stagedHealthCheckCount: number; | ||
pies: { | ||
libraryName: string; | ||
libraryId: string; | ||
totalFiles: number; | ||
totalTranscodes: number; | ||
savedSpace: number; | ||
totalHealthChecks: number; | ||
transcodeStatus: TdarrPieSegment[]; | ||
healthCheckStatus: TdarrPieSegment[]; | ||
videoCodecs: TdarrPieSegment[]; | ||
videoContainers: TdarrPieSegment[]; | ||
videoResolutions: TdarrPieSegment[]; | ||
audioCodecs: TdarrPieSegment[]; | ||
audioContainers: TdarrPieSegment[]; | ||
}[]; | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/integrations/src/interfaces/media-transcoding/workers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface TdarrWorker { | ||
id: string; | ||
filePath: string; | ||
fps: number; | ||
percentage: number; | ||
ETA: string; | ||
jobType: string; | ||
status: string; | ||
step: string; | ||
originalSize: number; | ||
estimatedSize: number | null; | ||
outputSize: number | null; | ||
}; |
172 changes: 172 additions & 0 deletions
172
packages/integrations/src/media-transcoding/tdarr-integration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import { z } from "@homarr/validation"; | ||
|
||
import { Integration } from "../base/integration"; | ||
import { TdarrQueue } from "../interfaces/media-transcoding/queue"; | ||
import type { TdarrStatistics } from "../interfaces/media-transcoding/statistics"; | ||
import { TdarrWorker } from "../interfaces/media-transcoding/workers"; | ||
import { getNodesResponseSchema, getStatisticsSchema, getStatusTableSchema } from "./tdarr-validation-schemas"; | ||
|
||
export class TdarrIntegration extends Integration { | ||
public async testConnectionAsync(): Promise<void> { | ||
const url = this.url("/api/v2/status"); | ||
const response = await fetch(url); | ||
if (response.status !== 200) { | ||
throw new Error(`Unexpected status code: ${response.status}`); | ||
} | ||
|
||
await z.object({ status: z.string() }).parseAsync(await response.json()); | ||
} | ||
|
||
public async getStatisticsAsync(): Promise<TdarrStatistics> { | ||
const url = this.url("/api/v2/cruddb"); | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: {'content-type': 'application/json'}, | ||
body: JSON.stringify({ | ||
data: { | ||
collection: "StatisticsJSONDB", | ||
mode: "getById", | ||
docID: "statistics", | ||
}, | ||
}), | ||
}); | ||
|
||
const statisticsData = await getStatisticsSchema.parseAsync(await response.json()); | ||
|
||
return { | ||
totalFileCount: statisticsData.totalFileCount, | ||
totalTranscodeCount: statisticsData.totalTranscodeCount, | ||
totalHealthCheckCount: statisticsData.totalHealthCheckCount, | ||
failedTranscodeCount: statisticsData.table3Count, | ||
failedHealthCheckCount: statisticsData.table6Count, | ||
stagedTranscodeCount: statisticsData.table1Count, | ||
stagedHealthCheckCount: statisticsData.table4Count, | ||
pies: statisticsData.pies.map((pie) => ({ | ||
libraryName: pie[0], | ||
libraryId: pie[1], | ||
totalFiles: pie[2], | ||
totalTranscodes: pie[3], | ||
savedSpace: pie[4] * 1_000_000_000, // file_size is in GB, convert to bytes, | ||
totalHealthChecks: pie[5], | ||
transcodeStatus: pie[6], | ||
healthCheckStatus: pie[7], | ||
videoCodecs: pie[8], | ||
videoContainers: pie[9], | ||
videoResolutions: pie[10], | ||
audioCodecs: pie[11], | ||
audioContainers: pie[12], | ||
})), | ||
}; | ||
} | ||
|
||
public async getWorkersAsync(): Promise<TdarrWorker[]> { | ||
const url = this.url("/api/v2/get-nodes"); | ||
const response = await fetch(url, { | ||
method: "GET", | ||
headers: {'content-type': 'application/json'}, | ||
}); | ||
|
||
const nodesData = await getNodesResponseSchema.parseAsync(await response.json()); | ||
const workers = Object.values(nodesData).flatMap((node) => { | ||
return Object.values(node.workers); | ||
}); | ||
|
||
return workers.map((worker) => ({ | ||
id: worker._id, | ||
filePath: worker.file, | ||
fps: worker.fps, | ||
percentage: worker.percentage, | ||
ETA: worker.ETA, | ||
jobType: worker.job.type, | ||
status: worker.status, | ||
step: worker.lastPluginDetails?.number ?? "", | ||
originalSize: worker.originalfileSizeInGbytes * 1_000_000_000, // file_size is in GB, convert to bytes, | ||
estimatedSize: worker.estSize ? worker.estSize * 1_000_000_000 : null, // file_size is in GB, convert to bytes, | ||
outputSize: worker.outputFileSizeInGbytes ? worker.outputFileSizeInGbytes * 1_000_000_000 : null, // file_size is in GB, convert to bytes, | ||
})); | ||
} | ||
|
||
public async getQueueAsync(firstItemIndex: number, pageSize: number): Promise<TdarrQueue> { | ||
const transcodingQueue = await this.getTranscodingQueueAsync(firstItemIndex, pageSize); | ||
const healthChecks = await this.getHealthCheckDataAsync(firstItemIndex, pageSize, transcodingQueue.totalCount); | ||
|
||
const combinedArray = [...transcodingQueue.array, ...healthChecks.array].slice(0, pageSize); | ||
return { | ||
array: combinedArray, | ||
totalCount: transcodingQueue.totalCount + healthChecks.totalCount, | ||
startIndex: firstItemIndex, | ||
endIndex: firstItemIndex + combinedArray.length - 1, | ||
}; | ||
} | ||
|
||
private async getTranscodingQueueAsync(firstItemIndex: number, pageSize: number) { | ||
const url = this.url("/api/v2/client/status-tables"); | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: {'content-type': 'application/json'}, | ||
body: JSON.stringify({ | ||
data: { | ||
start: firstItemIndex, | ||
pageSize: pageSize, | ||
filters: [], | ||
sorts: [], | ||
opts: { table: "table1" }, | ||
}, | ||
}), | ||
}); | ||
const transcodesQueueData = await getStatusTableSchema.parseAsync(await response.json()); | ||
|
||
return { | ||
array: transcodesQueueData.array.map((item) => ({ | ||
id: item._id, | ||
healthCheck: item.HealthCheck, | ||
transcode: item.TranscodeDecisionMaker, | ||
filePath: item.file, | ||
fileSize: item.file_size * 1_000_000, // file_size is in MB, convert to bytes | ||
container: item.container, | ||
codec: item.video_codec_name, | ||
resolution: item.video_resolution, | ||
type: "transcode" as const, | ||
})), | ||
totalCount: transcodesQueueData.totalCount, | ||
startIndex: firstItemIndex, | ||
endIndex: firstItemIndex + transcodesQueueData.array.length - 1, | ||
}; | ||
} | ||
|
||
private async getHealthCheckDataAsync(firstItemIndex: number, pageSize: number, totalQueueCount: number) { | ||
const url = this.url("/api/v2/client/status-tables"); | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: {'content-type': 'application/json'}, | ||
body: JSON.stringify({ | ||
data: { | ||
start: Math.max(firstItemIndex - totalQueueCount, 0), | ||
pageSize: pageSize, | ||
filters: [], | ||
sorts: [], | ||
opts: { | ||
table: "table4", | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
const healthCheckData = await getStatusTableSchema.parseAsync(await response.json()); | ||
|
||
return { | ||
array: healthCheckData.array.map((item) => ({ | ||
id: item._id, | ||
healthCheck: item.HealthCheck, | ||
transcode: item.TranscodeDecisionMaker, | ||
filePath: item.file, | ||
fileSize: item.file_size * 1_000_000, // file_size is in MB, convert to bytes | ||
container: item.container, | ||
codec: item.video_codec_name, | ||
resolution: item.video_resolution, | ||
type: "health check" as const, | ||
})), | ||
totalCount: healthCheckData.totalCount, | ||
}; | ||
} | ||
} |
Oops, something went wrong.