Sfoglia il codice sorgente

fix observatory data

Co-Authored-By: Alireza Ahmadi <[email protected]>
mhsanaei 4 mesi fa
parent
commit
315e8af025
1 ha cambiato i file con 45 aggiunte e 58 eliminazioni
  1. 45 58
      web/html/xui/xray.html

+ 45 - 58
web/html/xui/xray.html

@@ -1054,12 +1054,13 @@
                 });
             },
             changeObsCode() {
-                if (this.obsSettings == ''){
-                    return
-                }
                 if(this.cm != null) {
                     this.cm.toTextArea();
                 }
+                if (this.obsSettings == ''){
+                    this.cm = null;
+                    return
+                }
                 textAreaObj = document.getElementById('obsSetting');
                 textAreaObj.value = this[this.obsSettings];
                 this.cm = CodeMirror.fromTextArea(textAreaObj, this.cmOptions);
@@ -1283,21 +1284,11 @@
                             tmpBalancer.strategy = {
                                 'type': balancer.strategy
                             };
-                            if (balancer.strategy == 'leastPing'){
-                                if (!newTemplateSettings.observatory)
-                                    newTemplateSettings.observatory = this.defaultObservatory;
-                                if (!newTemplateSettings.observatory.subjectSelector.includes(balancer.tag))
-                                    newTemplateSettings.observatory.subjectSelector.push(balancer.tag);
-                            }
-                            if (balancer.strategy == 'leastLoad'){
-                                if (!newTemplateSettings.burstObservatory)
-                                    newTemplateSettings.burstObservatory = this.defaultBurstObservatory;
-                                if (!newTemplateSettings.burstObservatory.subjectSelector.includes(balancer.tag))
-                                    newTemplateSettings.burstObservatory.subjectSelector.push(balancer.tag);
-                            }
                         }
                         newTemplateSettings.routing.balancers.push(tmpBalancer);
                         this.templateSettings = newTemplateSettings;
+                        if (balancer.strategy == 'leastPing' || balancer.strategy == 'leastLoad')
+                            this.updateObservatorySelectors();
                         balancerModal.close();
                         this.changeObsCode();
                     },
@@ -1332,18 +1323,6 @@
                             tmpBalancer.strategy = {
                                 'type': balancer.strategy
                             };
-                            if (balancer.strategy == 'leastPing'){
-                                if (!newTemplateSettings.observatory)
-                                    newTemplateSettings.observatory = this.defaultObservatory;
-                                if (!newTemplateSettings.observatory.subjectSelector.includes(balancer.tag))
-                                    newTemplateSettings.observatory.subjectSelector.push(balancer.tag);
-                            }
-                            if (balancer.strategy == 'leastLoad'){
-                                if (!newTemplateSettings.burstObservatory)
-                                    newTemplateSettings.burstObservatory = this.defaultBurstObservatory;
-                                if (!newTemplateSettings.burstObservatory.subjectSelector.includes(balancer.tag))
-                                    newTemplateSettings.burstObservatory.subjectSelector.push(balancer.tag);
-                            }
                         }
 
                         newTemplateSettings.routing.balancers[index] = tmpBalancer;
@@ -1356,12 +1335,47 @@
                             });
                         }
                         this.templateSettings = newTemplateSettings;
+                        if (balancer.strategy == 'leastPing' || balancer.strategy == 'leastLoad')
+                            this.updateObservatorySelectors();
                         balancerModal.close();
                         this.changeObsCode();
                     },
                     isEdit: true
                 });
             },
+            updateObservatorySelectors(){
+                newTemplateSettings = this.templateSettings;
+                const leastPings = this.balancersData.filter((b) => b.strategy == 'leastPing');
+                const leastLoads = this.balancersData.filter((b) => b.strategy == 'leastLoad');
+                if (leastPings.length>0){
+                    if (!newTemplateSettings.observatory)
+                        newTemplateSettings.observatory = this.defaultObservatory;
+                    newTemplateSettings.observatory.subjectSelector = [];
+                    leastPings.forEach((b) => {
+                        b.selector.forEach((s) => {
+                            if (!newTemplateSettings.observatory.subjectSelector.includes(s))
+                                newTemplateSettings.observatory.subjectSelector.push(s);
+                        });
+                    });
+                } else {
+                    delete newTemplateSettings.observatory
+                }
+                if (leastLoads.length>0){
+                    if (!newTemplateSettings.burstObservatory)
+                        newTemplateSettings.burstObservatory = this.defaultBurstObservatory;
+                    newTemplateSettings.burstObservatory.subjectSelector = [];
+                    leastLoads.forEach((b) => {
+                        b.selector.forEach((s) => {
+                            if (!newTemplateSettings.burstObservatory.subjectSelector.includes(s))
+                                newTemplateSettings.burstObservatory.subjectSelector.push(s);
+                        });
+                    });
+                } else {
+                    delete newTemplateSettings.burstObservatory
+                }
+                this.templateSettings = newTemplateSettings;
+                this.changeObsCode();
+            },
             deleteBalancer(index) {
                 let newTemplateSettings = { ...this.templateSettings };
 
@@ -1371,27 +1385,14 @@
                 // Remove from settings
                 let realIndex = newTemplateSettings.routing.balancers.findIndex((b) => b.tag === removedBalancer.tag);
                 newTemplateSettings.routing.balancers.splice(realIndex, 1);
-
-                // Remove tag from observatory
-                if (newTemplateSettings.observatory){
-                    newTemplateSettings.observatory.subjectSelector = newTemplateSettings.observatory.subjectSelector.filter(s => s != removedBalancer.tag);
-                }
-                if (newTemplateSettings.burstObservatory){
-                    newTemplateSettings.burstObservatory.subjectSelector = newTemplateSettings.burstObservatory.subjectSelector.filter(s => s != removedBalancer.tag);
-                }
-
-                // Remove related routing rules
-                newTemplateSettings.routing.rules.forEach((rule) => {
-                    if (rule.balancerTag === removedBalancer.tag) {
-                        delete rule.balancerTag;
-                    }
-                });
                 
                 // Update balancers property to an empty array if there are no more balancers
                 if (newTemplateSettings.routing.balancers.length === 0) {
                     delete newTemplateSettings.routing.balancers;
                 }
                 this.templateSettings = newTemplateSettings;
+                this.updateObservatorySelectors();
+                this.obsSettings = '';
                 this.changeObsCode()
             },
             addDNSServer(){
@@ -1649,22 +1650,8 @@
                     this.templateSettings = newTemplateSettings;
                 },
             },
-            observatoryEnable: {
-                get: function () { return this.templateSettings != null && this.templateSettings.observatory },
-                set: function (v) {
-                    newTemplateSettings = this.templateSettings;
-                    newTemplateSettings.observatory = v ? this.defaultObservatory : undefined;
-                    this.templateSettings = newTemplateSettings;
-                }
-            },
-            burstObservatoryEnable: {
-                get: function () { return this.templateSettings != null && this.templateSettings.burstObservatory },
-                set: function (v) {
-                    newTemplateSettings = this.templateSettings;
-                    newTemplateSettings.burstObservatory = v ? this.defaultBurstObservatory : undefined;
-                    this.templateSettings = newTemplateSettings;
-                }
-            },
+            observatoryEnable: function () { return this.templateSettings != null && this.templateSettings.observatory != undefined },
+            burstObservatoryEnable: function () { return this.templateSettings != null && this.templateSettings.burstObservatory != undefined },
             freedomStrategy: {
                 get: function () {
                     if (!this.templateSettings) return "AsIs";