github.com/minio/console@v1.4.1/web-app/src/screens/Console/Common/ObjectManager/TrafficMonitor.tsx (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import React, { Fragment, useEffect } from "react";
    18  import { AppState, useAppDispatch } from "../../../../store";
    19  import { useSelector } from "react-redux";
    20  import {
    21    callForObjectID,
    22    formDataFromID,
    23  } from "../../ObjectBrowser/transferManager";
    24  import {
    25    newDownloadInit,
    26    newUploadInit,
    27  } from "../../ObjectBrowser/objectBrowserSlice";
    28  
    29  const TrafficMonitor = () => {
    30    const dispatch = useAppDispatch();
    31  
    32    const objects = useSelector(
    33      (state: AppState) => state.objectBrowser.objectManager.objectsToManage,
    34    );
    35  
    36    const limitVars = useSelector((state: AppState) =>
    37      state.console.session ? state.console.session.envConstants : null,
    38    );
    39  
    40    const currentDIP = useSelector(
    41      (state: AppState) => state.objectBrowser.objectManager.currentDownloads,
    42    );
    43  
    44    const currentUIP = useSelector(
    45      (state: AppState) => state.objectBrowser.objectManager.currentUploads,
    46    );
    47  
    48    const limitUploads = limitVars?.maxConcurrentUploads || 10;
    49    const limitDownloads = limitVars?.maxConcurrentDownloads || 20;
    50  
    51    useEffect(() => {
    52      if (objects.length > 0) {
    53        const filterDownloads = objects.filter(
    54          (object) =>
    55            object.type === "download" &&
    56            !object.done &&
    57            !currentDIP.includes(object.ID),
    58        );
    59        const filterUploads = objects.filter(
    60          (object) =>
    61            object.type === "upload" &&
    62            !object.done &&
    63            !currentUIP.includes(object.ID),
    64        );
    65  
    66        const remainingDownloadSlots = limitDownloads - currentDIP.length;
    67  
    68        if (
    69          filterDownloads.length > 0 &&
    70          (remainingDownloadSlots > 0 || limitDownloads === 0)
    71        ) {
    72          const itemsToDownload = filterDownloads.slice(
    73            0,
    74            remainingDownloadSlots,
    75          );
    76  
    77          itemsToDownload.forEach((item) => {
    78            const objectRequest = callForObjectID(item.ID);
    79  
    80            if (objectRequest) {
    81              objectRequest.send();
    82            }
    83  
    84            dispatch(newDownloadInit(item.ID));
    85          });
    86        }
    87  
    88        const remainingUploadSlots = limitUploads - currentUIP.length;
    89  
    90        if (
    91          filterUploads.length > 0 &&
    92          (remainingUploadSlots > 0 || limitUploads === 0)
    93        ) {
    94          const itemsToUpload = filterUploads.slice(0, remainingUploadSlots);
    95  
    96          itemsToUpload.forEach((item) => {
    97            const uploadRequest = callForObjectID(item.ID);
    98            const formDataID = formDataFromID(item.ID);
    99  
   100            if (uploadRequest && formDataID) {
   101              uploadRequest.send(formDataID);
   102            }
   103            dispatch(newUploadInit(item.ID));
   104          });
   105        }
   106      }
   107    }, [objects, limitUploads, limitDownloads, currentDIP, currentUIP, dispatch]);
   108  
   109    return <Fragment />;
   110  };
   111  
   112  export default TrafficMonitor;