Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions flowview_schedules.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
21600 => __('Every %d Hours', 6, 'flowview'),
43200 => __('Every %d Hours', 12, 'flowview'),
86400 => __('Every Day', 'flowview'),
432000 => __('Every Week', 'flowview'),
864000 => __('Every %d Weeks', 2, 'flowview'),
1728000 => __('Every Month', 'flowview'),
604800 => __('Every Week', 'flowview'),
1209600 => __('Every %d Weeks', 2, 'flowview'),
2592000 => __('Every Month', 'flowview'),
);

$formats = reports_get_format_files();
Expand Down Expand Up @@ -415,6 +415,8 @@ function download_log_data(): void {
* @return void
*/
function save_schedules(): void {
global $sendinterval_arr;

/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('query_id');
Expand All @@ -438,27 +440,38 @@ function save_schedules(): void {
}

$t = time();
$d = strtotime(get_nfilter_request_var('start'));
$i = $save['sendinterval'];
$i = (int) $save['sendinterval'];

if (!array_key_exists($i, $sendinterval_arr)) {
$_SESSION['sess_error_fields']['sendinterval'] = 'sendinterval';

raise_message('sched_interval', __('You must select a valid Send Interval!', 'flowview'), MESSAGE_LEVEL_ERROR);
}

$lastsent = flowview_schedule_initial_lastsent((string) $save['start'], $i, $t);

if ($lastsent === false) {
$_SESSION['sess_error_fields']['start'] = 'start';

raise_message('sched_start', __('You must specify a valid Scheduled Base Time!', 'flowview'), MESSAGE_LEVEL_ERROR);
}

if (is_error_message()) {
header('Location: flowview_schedules.php?tab=general&header=false&action=edit&id=' . get_filter_request_var('id'));
exit;
}

if (isset_request_var('id')) {
$save['id'] = get_request_var('id');

$q = flowview_db_fetch_row('SELECT * FROM plugin_flowview_schedules WHERE id = ' . $save['id']);
if (!isset($q['lastsent']) || $save['start'] != $q['start'] || $save['sendinterval'] != $q['sendinterval']) {
while ($d < $t) {
$d += $i;
}

$save['lastsent'] = $d - $i;
$save['lastsent'] = $lastsent;
}
} else {
$save['id'] = '';

while ($d < $t) {
$d += $i;
}

$save['lastsent'] = $d - $i;
$save['lastsent'] = $lastsent;
}

if (isset_request_var('enabled')) {
Expand Down Expand Up @@ -813,6 +826,9 @@ function edit_general($header_label, $report): void {
numberOfMonths: 1,
timeFormat: 'HH:mm',
dateFormat: 'yy-mm-dd',
// use the server offset so the picker's current time matches the server
// wall time; start is submitted and stored as a wall-time string
timezone: <?php print intval(date('Z') / 60); ?>,
showButtonPanel: false
});
});
Expand Down Expand Up @@ -1027,7 +1043,7 @@ function clearFilter() {

form_selectable_cell('<a class="linkEditMain" href="' . html_escape('flowview_schedules.php?tab=general&action=edit&id=' . $row['id']) . '">' . html_escape($row['title']) . '</a>', $row['id']);
form_selectable_cell($row['name'], $row['id']);
form_selectable_cell($sendinterval_arr[$row['sendinterval']], $row['id']);
form_selectable_cell($sendinterval_arr[$row['sendinterval']] ?? __('%s Seconds', number_format_i18n($row['sendinterval'], 0), 'flowview'), $row['id']);
form_selectable_cell($row['start'], $row['id']);
form_selectable_cell(date('Y-m-d H:i:00', $row['lastsent']+$row['sendinterval']), $row['id']);
form_selectable_cell($row['email'], $row['id']);
Expand Down
26 changes: 26 additions & 0 deletions flowview_upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,32 @@ function flowview_upgrade($current, $old): void {
flowview_db_execute('ALTER TABLE plugin_flowview_devices ENGINE=InnoDB');
}

// the week, two week and month intervals used to be 5, 10 and 20 days
$interval_map = [
432000 => 604800,
864000 => 1209600,
1728000 => 2592000,
];

$short_intervals = flowview_db_fetch_assoc('SELECT id, sendinterval, lastsent
FROM plugin_flowview_schedules
WHERE sendinterval IN (' . implode(', ', array_keys($interval_map)) . ')');

if (cacti_sizeof($short_intervals)) {
cacti_log('Correcting the Week, 2 Week and Month Send Intervals in the plugin_flowview_schedules table.', true, 'FLOWVIEW');

// keep each schedule's next send where it is, only the interval after it changes
foreach ($short_intervals as $schedule) {
$old = (int) $schedule['sendinterval'];
$new = $interval_map[$old];

flowview_db_execute_prepared('UPDATE plugin_flowview_schedules
SET sendinterval = ?, lastsent = ?
WHERE id = ?',
[$new, flowview_schedule_rebase_lastsent((int) $schedule['lastsent'], $old, $new), $schedule['id']]);
}
}

flowview_db_execute("CREATE TABLE IF NOT EXISTS `plugin_flowview_device_streams` (
device_id int(11) unsigned NOT NULL default '0',
ex_addr varchar(46) NOT NULL default '',
Expand Down
75 changes: 75 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,81 @@ function get_port_name($port_num, int $port_proto = 6) {
}
}

/**
* Calculate the lastsent anchor for a schedule saved with base time $start,
* so the next send lands on the first $start + n * $interval slot at or
* after $now.
*
* @param string $start the Scheduled Base Time as entered
* @param int $interval the send interval in seconds
* @param int $now the current time
*
* @return int|false false when the base time or interval is unusable
*/
function flowview_schedule_initial_lastsent(string $start, int $interval, int $now): int|false {
if ($interval <= 0 || trim($start) === '') {
return false;
}

$base = strtotime($start);

if ($base === false) {
return false;
}

$steps = 0;

if ($base < $now) {
$steps = intdiv($now - $base + $interval - 1, $interval);
}

return $base + ($steps - 1) * $interval;
}

/**
* Advance lastsent to the latest scheduled slot at or before $now, so a
* late or manual run does not shift the schedule off its base time.
*
* @param int $lastsent the stored lastsent anchor
* @param int $interval the send interval in seconds
* @param int $now the time of the run
*
* @return int
*/
function flowview_schedule_advance_lastsent(int $lastsent, int $interval, int $now): int {
if ($interval <= 0 || $lastsent <= 0) {
return intval($now / 60) * 60;
}

// a manual send before the first scheduled slot keeps the future anchor
if ($now < $lastsent) {
return $lastsent;
}

return $lastsent + intdiv($now - $lastsent, $interval) * $interval;
}

/**
* Rebase lastsent for a schedule whose send interval is changing, so the
* next send (lastsent + interval) stays where the old interval put it.
*
* @param int $lastsent the stored lastsent anchor
* @param int $old_interval the interval the anchor was calculated with
* @param int $new_interval the interval being stored
*
* @return int
*/
function flowview_schedule_rebase_lastsent(int $lastsent, int $old_interval, int $new_interval): int {
$rebased = $lastsent + $old_interval - $new_interval;

// without a usable anchor the schedule is already due, keep it that way
if ($lastsent <= 0 || $rebased <= 0) {
return $lastsent;
}

return $rebased;
}

/**
* @param mixed $id
* @param mixed $report_id
Expand Down
10 changes: 9 additions & 1 deletion locales/po/cacti.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Cacti\n"
"Report-Msgid-Bugs-To: developers@cacti.net\n"
"POT-Creation-Date: 2026-11-26 03:27-0800\n"
"POT-Creation-Date: 2026-12-05 09:01-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: Cacti Developers <developers@cacti.net>\n"
"Language-Team: Cacti Developers <developers@cacti.net>\n"
Expand Down Expand Up @@ -1229,6 +1229,14 @@ msgstr ""
msgid "You must specify Emails, a Notification List or both!"
msgstr ""

#: flowview_schedules.php
msgid "You must select a valid Send Interval!"
msgstr ""

#: flowview_schedules.php
msgid "You must specify a valid Scheduled Base Time!"
msgstr ""

#: flowview_schedules.php
#, php-format
msgid "Scheduled Report: [edit: %s]"
Expand Down
3 changes: 2 additions & 1 deletion run_schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@
}

$t = time();
$r = intval($t / 60) * 60;
$start = microtime(true);

$schedule = flowview_db_fetch_row_prepared('SELECT *
Expand All @@ -175,6 +174,8 @@
[$id]);

if (cacti_sizeof($schedule)) {
$r = flowview_schedule_advance_lastsent((int) $schedule['lastsent'], (int) $schedule['sendinterval'], $t);

flowview_db_execute_prepared('UPDATE plugin_flowview_schedules
SET lastsent = ?, start = ?
WHERE id = ?',
Expand Down
61 changes: 61 additions & 0 deletions tests/Unit/ScheduleRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,64 @@

expect(implode("\n", $GLOBALS['__test_cacti_log']))->toContain('Flowview Schedule 7 no longer exists');
});

/*
* Schedule anchoring, kept in step with Cacti core's api_scheduler fixes: a
* zero interval or unparsable base time is refused instead of looping or
* counting from 1970, and runs advance in whole intervals from the base
* time rather than drifting to whenever the poller picked them up.
*/
it('anchors a past base time to the slot before the next send', function () {
$base = strtotime('2026-01-01 08:00:00');
$now = $base + 3 * 86400 + 3600;

expect(flowview_schedule_initial_lastsent('2026-01-01 08:00:00', 86400, $now))->toBe($base + 3 * 86400);
});

it('anchors a future base time one interval before it', function () {
$base = strtotime('2026-01-10 08:00:00');

expect(flowview_schedule_initial_lastsent('2026-01-10 08:00:00', 3600, $base - 60))->toBe($base - 3600);
});

it('refuses a zero interval or an unusable base time', function () {
expect(flowview_schedule_initial_lastsent('2026-01-01 08:00:00', 0, time()))->toBeFalse()
->and(flowview_schedule_initial_lastsent('', 3600, time()))->toBeFalse()
->and(flowview_schedule_initial_lastsent('not a date', 3600, time()))->toBeFalse();
});

it('advances lastsent in whole intervals from the schedule', function () {
$lastsent = strtotime('2026-01-01 08:00:00');

// picked up late, and after several missed slots
expect(flowview_schedule_advance_lastsent($lastsent, 3600, $lastsent + 3600 + 290))->toBe($lastsent + 3600)
->and(flowview_schedule_advance_lastsent($lastsent, 3600, $lastsent + 5 * 3600 + 10))->toBe($lastsent + 5 * 3600);
});

it('leaves lastsent alone for a manual send between slots', function () {
$lastsent = strtotime('2026-01-01 08:00:00');

expect(flowview_schedule_advance_lastsent($lastsent, 86400, $lastsent + 3600))->toBe($lastsent);
});

it('keeps a future anchor for a manual send before the first slot', function () {
$lastsent = strtotime('2026-01-10 07:00:00');

expect(flowview_schedule_advance_lastsent($lastsent, 3600, strtotime('2026-01-01 12:00:00')))->toBe($lastsent);
});

it('rebases lastsent so a corrected interval keeps the next send', function () {
$base = strtotime('2026-01-10 08:00:00');

// a weekly schedule saved under the old 5 day week still sends at its base time
expect(flowview_schedule_rebase_lastsent($base - 432000, 432000, 604800))->toBe($base - 604800)
->and(flowview_schedule_rebase_lastsent(0, 432000, 604800))->toBe(0)
->and(flowview_schedule_rebase_lastsent(100000, 432000, 604800))->toBe(100000);
});

it('falls back to the run minute without a usable anchor', function () {
$now = strtotime('2026-01-01 08:07:42');

expect(flowview_schedule_advance_lastsent(0, 3600, $now))->toBe(strtotime('2026-01-01 08:07:00'))
->and(flowview_schedule_advance_lastsent(strtotime('2026-01-01 07:00:00'), 0, $now))->toBe(strtotime('2026-01-01 08:07:00'));
});