Преглед на файлове

Merge pull request #620 from Tarow/scrutiny-widget-thresholds

Respect scrutiny device status threshold setting
shamoon преди 2 години
родител
ревизия
826fe15
променени са 2 файла, в които са добавени 39 реда и са изтрити 8 реда
  1. 33 8
      src/widgets/scrutiny/component.jsx
  2. 6 0
      src/widgets/scrutiny/widget.js

+ 33 - 8
src/widgets/scrutiny/component.jsx

@@ -2,16 +2,37 @@ import Container from "components/services/widget/container";
 import Block from "components/services/widget/block";
 import useWidgetAPI from "utils/proxy/use-widget-api";
 
+
+// @see https://github.com/AnalogJ/scrutiny/blob/d8d56f77f9e868127c4849dac74d65512db658e8/webapp/frontend/src/app/shared/device-status.pipe.ts
+const DeviceStatus = {
+  passed: 0,
+  failed_smart: 1,
+  failed_scrutiny: 2,
+  failed_both: 3,
+
+  isFailed(s){ return s > this.passed && s <= this.failed_both},
+  isUnknown(s){ return s < this.passed || s > this.failed_both}
+}
+
+// @see https://github.com/AnalogJ/scrutiny/blob/d8d56f77f9e868127c4849dac74d65512db658e8/webapp/frontend/src/app/core/config/app.config.ts
+const DeviceStatusThreshold = {
+  smart: 1,
+  scrutiny: 2,
+  both: 3
+}
+
 export default function Component({ service }) {
   const { widget } = service;
 
+  const { data: scrutinySettings, error: scrutinySettingsError } = useWidgetAPI(widget, "settings");
   const { data: scrutinyData, error: scrutinyError } = useWidgetAPI(widget, "summary");
 
-  if (scrutinyError) {
-    return <Container error={scrutinyError} />;
+  if (scrutinyError || scrutinySettingsError) {
+    const finalError = scrutinyError ?? scrutinySettingsError;
+    return <Container error={finalError} />;
   }
 
-  if (!scrutinyData) {
+  if (!scrutinyData || !scrutinySettings) {
     return (
       <Container service={service}>
         <Block label="scrutiny.passed" />
@@ -19,13 +40,14 @@ export default function Component({ service }) {
         <Block label="scrutiny.unknown" />
       </Container>
     );
-  }
+  } 
 
   const deviceIds = Object.values(scrutinyData.data.summary);
-  
-  const passed = deviceIds.filter(deviceId => deviceId.device.device_status === 0)?.length || 0;
-  const failed = deviceIds.filter(deviceId => deviceId.device.device_status > 0 && deviceId.device.device_status <= 3)?.length || 0;
-  const unknown = deviceIds.length - (passed + failed) || 0;
+  const statusThreshold = scrutinySettings.settings.metrics.status_threshold;
+
+  const failed = deviceIds.filter(deviceId => (DeviceStatus.isFailed(deviceId.device.device_status) && statusThreshold === DeviceStatusThreshold.both) || [statusThreshold, DeviceStatus.failed_both].includes(deviceId.device.device_status))?.length || 0;
+  const unknown = deviceIds.filter(deviceId => DeviceStatus.isUnknown(deviceId.device.device_status))?.length || 0;
+  const passed = deviceIds.length - (failed + unknown);
 
   return (
     <Container service={service}>
@@ -33,5 +55,8 @@ export default function Component({ service }) {
       <Block label="scrutiny.failed" value={failed} />
       <Block label="scrutiny.unknown" value={unknown} />
     </Container>
+    
   );
+  
 }
+

+ 6 - 0
src/widgets/scrutiny/widget.js

@@ -11,6 +11,12 @@ const widget = {
         "data",
       ]
     },
+    settings: {
+      endpoint: "settings",
+      validate: [
+        "settings",
+      ]
+    }
   },
 };