From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 9DA4C7EB15
 for <pve-devel@lists.proxmox.com>; Thu, 11 Nov 2021 12:07:46 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id DEC349E57
 for <pve-devel@lists.proxmox.com>; Thu, 11 Nov 2021 12:07:15 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id C66209B1E
 for <pve-devel@lists.proxmox.com>; Thu, 11 Nov 2021 12:07:11 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 9A05F43038
 for <pve-devel@lists.proxmox.com>; Thu, 11 Nov 2021 12:07:11 +0100 (CET)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 11 Nov 2021 12:07:07 +0100
Message-Id: <20211111110709.633855-7-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20211111110709.633855-1-d.csapak@proxmox.com>
References: <20211111110709.633855-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.224 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH manager 3/5] api: cluster: add
 jobs/schedule-analyze api call
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 11 Nov 2021 11:07:46 -0000

a simple api call to simulate calendar event triggers
takes a schedule, an optional number (default 10), an optional starttime
(default 'now') and returns a list with unix timestamps, as well as
humanly readable utc timestamps.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 PVE/API2/Cluster.pm       |   5 ++
 PVE/API2/Cluster/Jobs.pm  | 107 ++++++++++++++++++++++++++++++++++++++
 PVE/API2/Cluster/Makefile |   1 +
 3 files changed, 113 insertions(+)
 create mode 100644 PVE/API2/Cluster/Jobs.pm

diff --git a/PVE/API2/Cluster.pm b/PVE/API2/Cluster.pm
index 3b918e55..205f9883 100644
--- a/PVE/API2/Cluster.pm
+++ b/PVE/API2/Cluster.pm
@@ -25,6 +25,7 @@ use PVE::API2::ACMEPlugin;
 use PVE::API2::Backup;
 use PVE::API2::Cluster::BackupInfo;
 use PVE::API2::Cluster::Ceph;
+use PVE::API2::Cluster::Jobs;
 use PVE::API2::Cluster::MetricServer;
 use PVE::API2::ClusterConfig;
 use PVE::API2::Firewall::Cluster;
@@ -84,6 +85,10 @@ __PACKAGE__->register_method ({
     path => 'ceph',
 });
 
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::Cluster::Jobs",
+    path => 'jobs',
+});
 if ($have_sdn) {
     __PACKAGE__->register_method ({
        subclass => "PVE::API2::Network::SDN",
diff --git a/PVE/API2/Cluster/Jobs.pm b/PVE/API2/Cluster/Jobs.pm
new file mode 100644
index 00000000..79a6e85e
--- /dev/null
+++ b/PVE/API2/Cluster/Jobs.pm
@@ -0,0 +1,107 @@
+package PVE::API2::Cluster::Jobs;
+
+use strict;
+use warnings;
+
+use PVE::RESTHandler;
+use PVE::CalendarEvent;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method({
+    name => 'index',
+    path => '',
+    method => 'GET',
+    description => "Index for jobs related endpoints.",
+    parameters => {
+	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'array',
+	description => 'Directory index.',
+	items => {
+	    type => "object",
+	    properties => {
+		subdir => {
+		    type => 'string',
+		    description => 'API sub-directory endpoint',
+		},
+	    },
+	},
+	links => [ { rel => 'child', href => "{subdir}" } ],
+    },
+    code => sub {
+	return [
+	   { subdir => 'schedule-analyze' },
+	];
+    }});
+
+__PACKAGE__->register_method({
+    name => 'schedule-analyze',
+    path => 'schedule-analyze',
+    method => 'GET',
+    description => "Returns a list of future schedule runtimes.",
+    permissions => { user => 'all' },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    schedule => {
+		description => "Backup schedule. The format is a subset of `systemd` calendar events.",
+		type => 'string', format => 'pve-calendar-event',
+		maxLength => 128,
+	    },
+	    starttime => {
+		description => "UNIX timestamp to start the calculation from. Defaults to the current time.",
+		optional => 1,
+		type => 'integer',
+	    },
+	    number => {
+		description => "Number of timestamps to return.",
+		optional => 1,
+		type => 'integer',
+		minimum => 1,
+		maximum => 100,
+		default => 10,
+	    },
+	},
+    },
+    returns => {
+	type => 'array',
+	description => 'Contains the guest objects.',
+	items => {
+	    type => 'object',
+	    properties => {
+		timestamp => {
+		    type => 'integer',
+		    description => 'UNIX timestamp for the run.',
+		},
+		utc => {
+		    type => 'string',
+		    description => "UTC timestamp for the run.",
+		},
+	    },
+	},
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $starttime = $param->{starttime} // time();
+	my $number = $param->{number} // 10;
+	my $schedule = $param->{schedule};
+
+	my $result = [];
+
+	my $event = PVE::CalendarEvent::parse_calendar_event($schedule);
+
+	for (my $count = 0; $count < $number; $count++) {
+	    my $next = PVE::CalendarEvent::compute_next_event($event, $starttime);
+	    push @$result, {
+		timestamp => $next,
+		utc => scalar(gmtime($next)),
+	    };
+	    $starttime = $next;
+	}
+
+	return $result;
+    }});
diff --git a/PVE/API2/Cluster/Makefile b/PVE/API2/Cluster/Makefile
index 742a1007..8d306507 100644
--- a/PVE/API2/Cluster/Makefile
+++ b/PVE/API2/Cluster/Makefile
@@ -5,6 +5,7 @@ include ../../../defines.mk
 PERLSOURCE= 			\
 	BackupInfo.pm		\
 	MetricServer.pm		\
+	Jobs.pm			\
 	Ceph.pm
 
 all:
-- 
2.30.2