Skip to content

Commit

Permalink
Merge pull request #351 from vexorian/20210808_dev
Browse files Browse the repository at this point in the history
Fix #350 : random slots bug
  • Loading branch information
vexorian committed Aug 8, 2021
2 parents 43fc475 + 0ff6495 commit f1db474
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/api.js
Expand Up @@ -1053,6 +1053,19 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService
delete program.streams;
delete program.durationStr;
delete program.commercials;
if (
(typeof(program.duration) === 'undefined')
||
(program.duration <= 0)
) {
console.error(`Input contained a program with invalid duration: ${program.duration}. This program has been deleted`);
return [];
}
if (! Number.isInteger(program.duration) ) {
console.error(`Input contained a program with invalid duration: ${program.duration}. Duration got fixed to be integer.`);
program.duration = Math.ceil(program.duration);
}
return [ program ];
}

function cleanUpChannel(channel) {
Expand All @@ -1063,10 +1076,15 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService
) {
channel.groupTitle = "dizqueTV";
}
channel.programs.forEach( cleanUpProgram );
channel.programs = channel.programs.flatMap( cleanUpProgram );
delete channel.fillerContent;
delete channel.filler;
channel.fallback.forEach( cleanUpProgram );
channel.fallback = channel.fallback.flatMap( cleanUpProgram );
channel.duration = 0;
for (let i = 0; i < channel.programs.length; i++) {
channel.duration += channel.programs[i].duration;
}

}

async function streamToolResult(toolRes, res) {
Expand Down
45 changes: 44 additions & 1 deletion src/database-migration.js
Expand Up @@ -20,7 +20,7 @@
const path = require('path');
var fs = require('fs');

const TARGET_VERSION = 802;
const TARGET_VERSION = 803;

const STEPS = [
// [v, v2, x] : if the current version is v, call x(db), and version becomes v2
Expand All @@ -42,6 +42,7 @@ const STEPS = [
[ 702, 800, (db,channels,dir) => reAddIcon(dir) ],
[ 800, 801, (db) => addImageCache(db) ],
[ 801, 802, () => addGroupTitle() ],
[ 802, 803, () => fixNonIntegerDurations() ],
]

const { v4: uuidv4 } = require('uuid');
Expand Down Expand Up @@ -834,6 +835,48 @@ function addGroupTitle() {
console.log("Done migrating group titles in channels.");
}

function fixNonIntegerDurations() {

function migrateChannel(channel) {
let programs = channel.programs;
let fixedCount = 0;
channel.duration = 0;
for (let i = 0; i < programs.length; i++) {
let program = programs[i];
if ( ! Number.isInteger(program.duration) ) {
fixedCount++;
program.duration = Math.ceil(program.duration);
programs[i] = program;
}
channel.duration += program.duration;
}
if (fixedCount != 0) {
console.log(`Found ${fixedCount} non-integer durations in channel ${channel.number}, they were fixed but you should consider running random slots again so that the milliseconds are accurate.`);
}

return {
fixed: (fixedCount != 0),
newChannel: channel,
};
}

console.log("Checking channels to make sure they weren't corrupted by random slots bug #350...");
let channels = path.join(process.env.DATABASE, 'channels');
let channelFiles = fs.readdirSync(channels);
for (let i = 0; i < channelFiles.length; i++) {
if (path.extname( channelFiles[i] ) === '.json') {
console.log("Checking durations in channel : " + channelFiles[i] +"..." );
let channelPath = path.join(channels, channelFiles[i]);
let channel = JSON.parse(fs.readFileSync(channelPath, 'utf-8'));
let { fixed, newChannel } = migrateChannel(channel);

if (fixed) {
fs.writeFileSync( channelPath, JSON.stringify(newChannel), 'utf-8');
}
}
}
console.log("Done checking channels.");
}



Expand Down
6 changes: 5 additions & 1 deletion src/services/random-slots-service.js
Expand Up @@ -405,10 +405,14 @@ module.exports = async( programs, schedule ) => {
}
} else if (flexBetween) {
//just distribute it equitatively
let div = rem / pads.length;
let div = Math.floor( rem / pads.length );
let totalAdded = 0;
for (let i = 0; i < pads.length; i++) {
pads[i].pad += div;
totalAdded += div;
}
pads[0].pad += rem - totalAdded;

} else {
//also add div to the latest item
pads[ pads.length - 1].pad += rem;
Expand Down

0 comments on commit f1db474

Please sign in to comment.