فهرست منبع

Fixed get usage and added threshold to stop

jnfrati 4 سال پیش
والد
کامیت
090cbb7b35
2فایلهای تغییر یافته به همراه18 افزوده شده و 13 حذف شده
  1. 17 12
      dashboard/src/shared/Context.tsx
  2. 1 1
      dashboard/src/shared/types.tsx

+ 17 - 12
dashboard/src/shared/Context.tsx

@@ -57,6 +57,7 @@ export interface GlobalContextType {
   setHasBillingEnabled: (isBillingEnabled: boolean) => void;
   usage: UsageData;
   setUsage: (usage: UsageData) => void;
+  queryUsage: (retry?: number) => Promise<void>;
 }
 
 /**
@@ -159,21 +160,25 @@ class ContextProvider extends Component<PropsType, StateType> {
     setUsage: (usage: UsageData) => {
       this.setState({ usage });
     },
+    queryUsage: async (retry: number = 0) => {
+      api
+        .getUsage("<token>", {}, { project_id: this.state?.currentProject?.id })
+        .then((res) => {
+          if (JSON.stringify(res.data) !== JSON.stringify(this.state.usage)) {
+            this.state.setUsage(res.data);
+          } else {
+            if (retry < 10) {
+              setTimeout(() => {
+                this.state.queryUsage(retry + 1);
+              }, 1000);
+            }
+          }
+        });
+    },
   };
 
-  queryUsage() {
-    api
-      .getUsage("<token>", {}, { project_id: this.state?.currentProject?.id })
-      .then((res) => {
-        this.state.setUsage(res.data);
-      });
-  }
   render() {
-    return (
-      <Provider value={{ ...this.state, queryUsage: this.queryUsage }}>
-        {this.props.children}
-      </Provider>
-    );
+    return <Provider value={{ ...this.state }}>{this.props.children}</Provider>;
   }
 }
 

+ 1 - 1
dashboard/src/shared/types.tsx

@@ -302,7 +302,7 @@ export interface ContextProps {
   setHasBillingEnabled: (isBillingEnabled: boolean) => void;
   usage: UsageData;
   setUsage: (usage: UsageData) => void;
-  queryUsage: () => void;
+  queryUsage: () => Promise<void>;
 }
 
 export enum JobStatusType {