From abd7265f76c5d8f9f8e9512a5e17ed8b58e288a2 Mon Sep 17 00:00:00 2001
From: Vitalii Lysak <v.lysak@dunice.net>
Date: Wed, 24 Aug 2022 16:31:27 +0300
Subject: SILABMC-256: add limits for Fans

---
 src/views/_sila/Fans/Dynamic/FanSpeedSystem.vue | 131 ++++++++++++++++++------
 1 file changed, 99 insertions(+), 32 deletions(-)

(limited to 'src/views/_sila/Fans/Dynamic/FanSpeedSystem.vue')

diff --git a/src/views/_sila/Fans/Dynamic/FanSpeedSystem.vue b/src/views/_sila/Fans/Dynamic/FanSpeedSystem.vue
index bcf0b375..2271ef5c 100644
--- a/src/views/_sila/Fans/Dynamic/FanSpeedSystem.vue
+++ b/src/views/_sila/Fans/Dynamic/FanSpeedSystem.vue
@@ -8,41 +8,68 @@
       <img src="@/assets/images/_sila/collapsed/fan.svg" />
     </template>
     <page-section>
-      <!-- <b-row class="align-items-end limit-container">
-        <b-col xs="12" md="6" xl="3" class="pt-4">
-          <b-form-group :label="$t('pageFans.labels.warning')">
-            <b-form-input
-              v-model="warning"
-              type="number"
-              :min="0"
-              :max="shutdown"
+      <b-form novalidate>
+        <b-row class="align-items-end limit-container">
+          <b-col xs="12" md="6" xl="3" class="pt-4">
+            <b-form-group :label="$t('pageFans.labels.warning')">
+              <b-form-input
+                v-model.number="warning"
+                :disabled="$store.getters['authentication/role'] === 'ReadOnly'"
+                type="number"
+                :state="getValidationState($v.warning)"
+                @blur="$v.warning.$touch()"
+              >
+              </b-form-input>
+              <b-form-invalid-feedback class="absolute" role="alert">
+                <div v-if="!$v.warning.between">
+                  {{ $t('global.form.invalidValue') }}
+                </div>
+                <div v-if="!$v.warning.required">
+                  {{ $t('global.form.fieldRequired') }}
+                </div>
+              </b-form-invalid-feedback>
+            </b-form-group>
+          </b-col>
+          <b-col xs="12" md="6" xl="3" class="pt-4">
+            <b-form-group :label="$t('pageFans.labels.critical')">
+              <b-form-input
+                v-model.number="critical"
+                :disabled="$store.getters['authentication/role'] === 'ReadOnly'"
+                type="number"
+                :state="getValidationState($v.critical)"
+                @blur="$v.critical.$touch()"
+              ></b-form-input>
+              <b-form-invalid-feedback class="absolute" role="alert">
+                <div v-if="!$v.critical.between">
+                  {{ $t('global.form.invalidValue') }}
+                </div>
+                <div v-if="!$v.critical.required">
+                  {{ $t('global.form.fieldRequired') }}
+                </div>
+              </b-form-invalid-feedback>
+            </b-form-group>
+          </b-col>
+          <b-col xs="12" md="6" xl="3" class="pt-4">
+            <b-button
+              variant="primary"
+              style="height: 35px"
+              :disabled="
+                loading || $store.getters['authentication/role'] === 'ReadOnly'
+              "
+              @click="saveLimit"
             >
-            </b-form-input>
-          </b-form-group>
-        </b-col>
-        <b-col xs="12" md="6" xl="3" class="pt-4">
-          <b-form-group :label="$t('pageFans.labels.shutdown')">
-            <b-form-input
-              v-model="shutdown"
-              :min="warning"
-              :max="10000"
-              type="number"
-            ></b-form-input>
-          </b-form-group>
-        </b-col>
-        <b-col xs="12" md="6" xl="3" class="pt-4">
-          <b-button variant="primary" style="height: 35px">
-            {{ $t('global.action.save') }}
-          </b-button>
-        </b-col>
-      </b-row> -->
+              {{ $t('global.action.save') }}
+            </b-button>
+          </b-col>
+        </b-row>
+      </b-form>
       <chart
         type="fans"
         :colors="colors"
         :time-scale="timeScale"
         :data="filteredForChart"
-        :warning="warning"
-        :critical="critical"
+        :warning="warningLimit"
+        :critical="criticalLimit"
         :max="maxLimit"
       ></chart>
 
@@ -93,16 +120,27 @@ import Chart from '@/components/_sila/Global/Chart';
 import PageSection from '@/components/Global/PageSection';
 
 import DataFormatterMixin from '@/components/_sila/Mixins/DataFormatterMixin';
-import LoadingBarMixin from '@/components/_sila/Mixins/LoadingBarMixin';
+import LoadingBarMixin, {
+  loading,
+} from '@/components/_sila/Mixins/LoadingBarMixin';
 import TableFilterMixin from '@/components/_sila/Mixins/TableFilterMixin';
+import BVToastMixin from '@/components/_sila/Mixins/BVToastMixin';
+import VuelidateMixin from '@/components/_sila/Mixins/VuelidateMixin.js';
 import Collapse from '@/components/_sila/Global/Collapse';
 
 import { getGroups, getItems } from '@/utilities/_sila/metricProperties';
+import { between, required } from 'vuelidate/lib/validators';
 import { fanFilter } from '@/utilities/_sila/psuFilter';
 
 export default {
   components: { PageSection, Chart, Collapse },
-  mixins: [DataFormatterMixin, LoadingBarMixin, TableFilterMixin],
+  mixins: [
+    DataFormatterMixin,
+    LoadingBarMixin,
+    TableFilterMixin,
+    BVToastMixin,
+    VuelidateMixin,
+  ],
   props: {
     timeScale: {
       type: String,
@@ -111,6 +149,7 @@ export default {
   },
   data() {
     return {
+      loading,
       warning: null,
       critical: null,
       isBusy: true,
@@ -151,9 +190,22 @@ export default {
     };
   },
 
+  validations() {
+    return {
+      warning: {
+        required,
+        between: between(1, this.critical),
+      },
+      critical: {
+        required,
+        between: between(this.warning, this.maxLimit),
+      },
+    };
+  },
+
   computed: {
     groups() {
-      return getGroups(this.filteredSensors);
+      return getGroups(this.filteredForChart);
     },
 
     items() {
@@ -244,6 +296,21 @@ export default {
   },
 
   methods: {
+    saveLimit() {
+      this.$v.$touch();
+      if (this.$v.$invalid) return;
+
+      this.startLoader();
+      this.$store
+        .dispatch('fan/patchLimits', {
+          warning: this.warning,
+          critical: this.critical,
+          groups: this.groups,
+        })
+        .catch(({ message }) => this.errorToast(message))
+        .finally(() => this.endLoader());
+    },
+
     getPwmByCpu(cpu) {
       switch (cpu) {
         case 'System_Fan_1':
-- 
cgit v1.2.3

