changeset 2344:a5b87a695469

client: imports: fixed cron string The inputs are now correctly converted to a cron string and the cron string coming from the backend for an existing schedule is now correctly parsed back to the individual inputs for editing a schedule.
author Markus Kottlaender <markus@intevation.de>
date Wed, 20 Feb 2019 10:21:16 +0100
parents 33d2ef9f9e5d
children df6383831ad3
files client/src/components/importschedule/Importscheduledetail.vue client/src/store/importschedule.js
diffstat 2 files changed, 60 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/importschedule/Importscheduledetail.vue	Wed Feb 20 10:00:01 2019 +0100
+++ b/client/src/components/importschedule/Importscheduledetail.vue	Wed Feb 20 10:21:16 2019 +0100
@@ -615,15 +615,22 @@
     },
     calcCronString() {
       let getValue = value => {
-        return this[value] ? this[value] : "*";
+        return this[value] !== null ? this[value] : "*";
       };
-      if (this.cronMode === "15minutes") return "0 */15 * * * *";
+
       const min = getValue("minutes");
       const h = getValue("hour");
       const dm = getValue("dayOfMonth");
       const m = getValue("month");
       const wd = getValue("day");
-      return `0 ${min} ${h} ${dm} ${m} ${wd}`;
+
+      if (this.cronMode === "15minutes") return "0 */15 * * * *";
+      if (this.cronMode === "hour") return `0 ${min} * * * *`;
+      if (this.cronMode === "day") return `0 ${min} ${h} * * *`;
+      if (this.cronMode === "week") return `0 ${min} ${h} * * ${wd}`;
+      if (this.cronMode === "month") return `0 ${min} ${h} ${dm} * *`;
+      if (this.cronMode === "year") return `0 ${min} ${h} ${dm} ${m} *`;
+      return this.cronString;
     },
     validateBottleneckfields() {
       return !!this.url;
@@ -664,11 +671,11 @@
       return /0 \d{1,2} \d{1,2} \d{1,2} \* \*/.test(cron);
     },
     clearInputs() {
-      this.minutes = null;
-      this.month = null;
-      this.hour = null;
-      this.day = null;
-      this.dayOfMonth = null;
+      this.minutes = this.currentSchedule.minutes;
+      this.month = this.currentSchedule.month;
+      this.hour = this.currentSchedule.hour;
+      this.day = this.currentSchedule.day;
+      this.dayOfMonth = this.currentSchedule.dayOfMonth;
     },
     triggerFileUpload() {
       if (!this.uploadFile) return;
--- a/client/src/store/importschedule.js	Wed Feb 20 10:00:01 2019 +0100
+++ b/client/src/store/importschedule.js	Wed Feb 20 10:21:16 2019 +0100
@@ -64,13 +64,13 @@
     eMailNotification: false,
     scheduled: false,
     easyCron: true,
-    cronString: "* * * * ",
-    cronMode: "",
-    minutes: null,
-    month: null,
-    hour: null,
-    day: null,
-    dayOfMonth: null,
+    cronString: "0 */15 * * * *",
+    cronMode: "15minutes",
+    minutes: 0,
+    month: 1,
+    hour: 0,
+    day: 0,
+    dayOfMonth: 1,
     simple: null,
     url: null,
     insecure: false,
@@ -121,8 +121,45 @@
       Vue.set(state.currentSchedule, "id", id);
       if (cron) {
         Vue.set(state.currentSchedule, "scheduled", true);
-        Vue.set(state.currentSchedule, "easyCron", false);
         Vue.set(state.currentSchedule, "cronString", cron);
+
+        // simple weekly  or monthly?
+        if (cron === "0 0 0 * * 0") {
+          Vue.set(state.currentSchedule, "simple", "weekly");
+          Vue.set(state.currentSchedule, "easyCron", true);
+        } else if (cron === "0 0 0 1 * *") {
+          Vue.set(state.currentSchedule, "simple", "monthly");
+          Vue.set(state.currentSchedule, "easyCron", true);
+        } else {
+          Vue.set(state.currentSchedule, "easyCron", false);
+        }
+
+        // set cronMode from cronString
+        if (/0 \d{1,2} \* \* \* \*/.test(cron))
+          Vue.set(state.currentSchedule, "cronMode", "hour");
+        if (/0 \d{1,2} \d{1,2} \* \* \*/.test(cron))
+          Vue.set(state.currentSchedule, "cronMode", "day");
+        if (/0 \d{1,2} \d{1,2} \* \* \d{1}/.test(cron))
+          Vue.set(state.currentSchedule, "cronMode", "week");
+        if (/0 \d{1,2} \d{1,2} \d{1,2} \* \*/.test(cron))
+          Vue.set(state.currentSchedule, "cronMode", "month");
+        if (/0 \d{1,2} \d{1,2} \d{1,2} \d{1,2} \*/.test(cron))
+          Vue.set(state.currentSchedule, "cronMode", "year");
+
+        // set minute, hour, etc from cronString
+        let cronConf = cron.match(
+          /0 (\d{1,2}|\*) (\d{1,2}|\*) (\d{1,2}|\*) (\d{1,2}|\*) (\d{1}|\*)/
+        );
+        // remove first element which is the whole matched string
+        cronConf.shift();
+        cronConf = cronConf.map(field =>
+          field === "*" ? null : Number(field)
+        );
+        Vue.set(state.currentSchedule, "minutes", cronConf[0]);
+        Vue.set(state.currentSchedule, "hour", cronConf[1]);
+        Vue.set(state.currentSchedule, "dayOfMonth", cronConf[2]);
+        Vue.set(state.currentSchedule, "month", cronConf[3]);
+        Vue.set(state.currentSchedule, "day", cronConf[4]);
       }
       if (eMailNotification) {
         Vue.set(state.currentSchedule, "eMailNotification", eMailNotification);