Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa committed Mar 7, 2024
1 parent 4aa176b commit fcc9ee9
Show file tree
Hide file tree
Showing 14 changed files with 768 additions and 47 deletions.
4 changes: 2 additions & 2 deletions server/src/ffmpegInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { exec } from 'child_process';
import { FfmpegSettings } from '@tunarr/types';
import { exec } from 'child_process';

export class FFMPEGInfo {
private ffmpegPath: string;
Expand Down Expand Up @@ -27,7 +27,7 @@ export class FFMPEGInfo {
return m[1];
} catch (err) {
console.error('Error getting ffmpeg version', err);
return 'Error';
return 'unknown';
}
}
}
2 changes: 1 addition & 1 deletion shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"scripts": {
"build": "tsup",
"clean": "rimraf build/",
"dev": "pnpm run build -- --watch"
"dev": "tsup --watch"
},
"dependencies": {
"@tunarr/types": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { mod as dayjsMod } from './util/dayjsExtensions.js';
export { scheduleRandomSlots } from './services/randomSlotsService.js';
export { scheduleTimeSlots } from './services/timeSlotService.js';
export { mod as dayjsMod } from './util/dayjsExtensions.js';
29 changes: 18 additions & 11 deletions shared/src/services/randomSlotsService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
ChannelProgram,
ChannelProgramming,
ContentProgram,
FlexProgram,
isContentProgram,
Expand All @@ -14,6 +13,7 @@ import relativeTime from 'dayjs/plugin/relativeTime.js';
import utc from 'dayjs/plugin/utc.js';
import {
chain,
first,
forEach,
isNil,
isNull,
Expand Down Expand Up @@ -159,7 +159,6 @@ export function distributeFlex(

const div = Math.floor(remainingTime / schedule.padMs);
const mod = remainingTime % schedule.padMs;
console.log({ remainingTime, 'schedule.padMs': schedule.padMs, div, mod });
// Add leftover flex to end
last(programs)!.padMs += mod;
last(programs)!.totalDuration += mod;
Expand All @@ -176,7 +175,6 @@ export function distributeFlex(
if (i < div % programs.length) {
q++;
}
console.log(q);
const extraPadding = q * schedule.padMs;
programs[sortedPads[i].index].padMs += extraPadding;
programs[sortedPads[i].index].totalDuration += extraPadding;
Expand Down Expand Up @@ -248,15 +246,13 @@ function advanceIterator(
}

// eslint-disable-next-line @typescript-eslint/require-await
export default async function scheduleRandomSlots(
export async function scheduleRandomSlots(
schedule: RandomSlotSchedule,
channelProgramming: ChannelProgramming,
channelProgramming: ChannelProgram[],
) {
// Load programs
// TODO include redirects and custom programs!
const allPrograms = reject(channelProgramming.programs, (p) =>
isFlexProgram(p),
);
const allPrograms = reject(channelProgramming, (p) => isFlexProgram(p));
const programBySlotType = createProgramMap(allPrograms);

const programmingIteratorsById = reduce(
Expand Down Expand Up @@ -354,9 +350,9 @@ export default async function scheduleRandomSlots(
}
}

n += slot.weight!; // why
n += slot.weight; // why

if (random.bool(slot.weight!, n)) {
if (random.bool(slot.weight, n)) {
currSlot = slot;
// slotIndex = i;
remaining = slot.durationMs;
Expand Down Expand Up @@ -446,8 +442,19 @@ export default async function scheduleRandomSlots(
// "shuffle" ordering, it won't work for "in order" shows in slots.
// TODO: Implement greedy filling.
// TODO: Handle padStyle === 'episode'
if (schedule.flexPreference === 'distribute') {
if (
schedule.flexPreference === 'distribute' &&
schedule.padStyle === 'episode'
) {
distributeFlex(paddedPrograms, schedule, remainingTimeInSlot);
} else if (schedule.flexPreference === 'distribute') {
const div = Math.floor(remaining / paddedPrograms.length);
let totalAdded = 0;
forEach(paddedPrograms, (paddedProgram) => {
paddedProgram.padMs += div;
totalAdded += div;
});
first(paddedPrograms)!.padMs += remaining - totalAdded;
} else {
const lastProgram = last(paddedPrograms)!;
lastProgram.padMs += remainingTimeInSlot;
Expand Down
1 change: 1 addition & 0 deletions shared/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default defineConfig({
splitting: false,
format: 'esm',
outDir: 'build',
sourcemap: true,
});
2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"clean": {},
"build": {
"dependsOn": ["^build"],
"outputs": ["build/**"]
"outputs": ["build/**", "dist/**"]
},
"bundle": {
"dependsOn": ["clean", "build"],
Expand Down
2 changes: 1 addition & 1 deletion types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "tsup",
"clean": "rimraf build",
"dev": "pnpm run build -- --watch"
"dev": "tsup --watch"
},
"main": "./build/src/index.js",
"module": "./build/src/index.js",
Expand Down
4 changes: 2 additions & 2 deletions types/src/api/Scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ export type RandomSlotProgramming = Alias<
>;

export const RandomSlotSchema = z.object({
order: z.string(),
order: z.string().optional(), // Present for show slots only
startTime: z.number().optional(), // Offset from midnight millis
cooldownMs: z.number(),
periodMs: z.number().optional(),
durationMs: z.number(),
weight: z.number().optional(), // Percentage
weight: z.number(),
programming: RandomSlotProgrammingSchema,
});

Expand Down
2 changes: 1 addition & 1 deletion types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"strictNullChecks": true
},
"include": [
"./**/*.ts",
"./src/**/*.ts",
"__tests__/**/*"
],
"exclude": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ export function ChannelProgrammingOrganizeOptions({
title="This is similar to Time Slots, but instead of time sections, you pick a probability to play each tv show and the length of the block. Once a channel has been configured with random slots, the reload button can re-evaluate them again, with the saved settings."
placement="right"
>
<MenuItem
onClick={() => {
// To do
handleClose();
}}
>
<MenuItem component={Link} to="random-slot-editor">
<ShuffleIcon /> Random Slots
</MenuItem>
</Tooltip>
Expand Down
20 changes: 20 additions & 0 deletions web/src/components/slot_scheduler/commonSlotSchedulerOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export type DropdownOption<T extends string | number> = {
value: T;
description: string;
};

export type ProgramOption = DropdownOption<string>;

export const padOptions: DropdownOption<number>[] = [
{ value: 1, description: 'Do not pad' },
{ value: 5 * 60 * 1000, description: '0:00, 0:05, 0:10, ..., 0:55' },
{ value: 10 * 60 * 1000, description: '0:00, 0:10, 0:20, ..., 0:50' },
{ value: 15 * 60 * 1000, description: '0:00, 0:15, 0:30, ..., 0:45' },
{ value: 30 * 60 * 1000, description: '0:00, 0:30' },
{ value: 1 * 60 * 60 * 1000, description: '0:00' },
];

export const flexOptions: DropdownOption<'end' | 'distribute'>[] = [
{ value: 'distribute', description: 'Between videos' },
{ value: 'end', description: 'End of the slot' },
];
Loading

0 comments on commit fcc9ee9

Please sign in to comment.