mirror of
				https://github.com/optim-enterprises-bv/OptimCloud-gw-ui.git
				synced 2025-11-03 19:57:46 +00:00 
			
		
		
		
	Merge pull request #57 from stephb9959/feature/39-view-more-health-logs
View more buttons added to health/logs
This commit is contained in:
		@@ -3,7 +3,7 @@ import { CFooter } from '@coreui/react';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const TheFooter = () => (
 | 
					const TheFooter = () => (
 | 
				
			||||||
  <CFooter fixed={false}>
 | 
					  <CFooter fixed={false}>
 | 
				
			||||||
    <div>Version 0.0.10</div>
 | 
					    <div>Version 0.0.11</div>
 | 
				
			||||||
    <div className="mfs-auto">
 | 
					    <div className="mfs-auto">
 | 
				
			||||||
      <span className="mr-1">Powered by</span>
 | 
					      <span className="mr-1">Powered by</span>
 | 
				
			||||||
      <a href="https://coreui.io/react" target="_blank" rel="noopener noreferrer">
 | 
					      <a href="https://coreui.io/react" target="_blank" rel="noopener noreferrer">
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,17 +14,21 @@ import {
 | 
				
			|||||||
import CIcon from '@coreui/icons-react';
 | 
					import CIcon from '@coreui/icons-react';
 | 
				
			||||||
import DatePicker from 'react-widgets/DatePicker';
 | 
					import DatePicker from 'react-widgets/DatePicker';
 | 
				
			||||||
import PropTypes from 'prop-types';
 | 
					import PropTypes from 'prop-types';
 | 
				
			||||||
import { prettyDate, addDays, dateToUnix } from '../../utils/helper';
 | 
					import { prettyDate, dateToUnix } from '../../utils/helper';
 | 
				
			||||||
import axiosInstance from '../../utils/axiosInstance';
 | 
					import axiosInstance from '../../utils/axiosInstance';
 | 
				
			||||||
import { getToken } from '../../utils/authHelper';
 | 
					import { getToken } from '../../utils/authHelper';
 | 
				
			||||||
 | 
					import LoadingButton from '../../components/LoadingButton';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const DeviceHealth = ({ selectedDeviceId }) => {
 | 
					const DeviceHealth = ({ selectedDeviceId }) => {
 | 
				
			||||||
  const [collapse, setCollapse] = useState(false);
 | 
					  const [collapse, setCollapse] = useState(false);
 | 
				
			||||||
  const [details, setDetails] = useState([]);
 | 
					  const [details, setDetails] = useState([]);
 | 
				
			||||||
  const [loading, setLoading] = useState(false);
 | 
					  const [loading, setLoading] = useState(false);
 | 
				
			||||||
  const [healthChecks, setHealthChecks] = useState([]);
 | 
					  const [healthChecks, setHealthChecks] = useState([]);
 | 
				
			||||||
  const [start, setStart] = useState(addDays(new Date(), -3).toString());
 | 
					  const [start, setStart] = useState('');
 | 
				
			||||||
  const [end, setEnd] = useState(new Date().toString());
 | 
					  const [end, setEnd] = useState('');
 | 
				
			||||||
 | 
					  const [logLimit, setLogLimit] = useState(25);
 | 
				
			||||||
 | 
					  const [loadingMore, setLoadingMore] = useState(false);
 | 
				
			||||||
 | 
					  const [showLoadingMore, setShowLoadingMore] = useState(true);
 | 
				
			||||||
  let sanityLevel;
 | 
					  let sanityLevel;
 | 
				
			||||||
  let barColor;
 | 
					  let barColor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -41,11 +45,14 @@ const DeviceHealth = ({ selectedDeviceId }) => {
 | 
				
			|||||||
    setEnd(value);
 | 
					    setEnd(value);
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const showMoreLogs = () => {
 | 
				
			||||||
 | 
					    setLogLimit(logLimit + 50);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const getDeviceHealth = () => {
 | 
					  const getDeviceHealth = () => {
 | 
				
			||||||
    if (loading) return;
 | 
					    if (loading) return;
 | 
				
			||||||
 | 
					    setLoadingMore(true);
 | 
				
			||||||
    setLoading(true);
 | 
					    setLoading(true);
 | 
				
			||||||
    const utcStart = new Date(start).toISOString();
 | 
					 | 
				
			||||||
    const utcEnd = new Date(end).toISOString();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const options = {
 | 
					    const options = {
 | 
				
			||||||
      headers: {
 | 
					      headers: {
 | 
				
			||||||
@@ -53,19 +60,28 @@ const DeviceHealth = ({ selectedDeviceId }) => {
 | 
				
			|||||||
        Authorization: `Bearer ${getToken()}`,
 | 
					        Authorization: `Bearer ${getToken()}`,
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      params: {
 | 
					      params: {
 | 
				
			||||||
        startDate: dateToUnix(utcStart),
 | 
					        limit: logLimit
 | 
				
			||||||
        endDate: dateToUnix(utcEnd),
 | 
					 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let extraParams = '?newest=true';
 | 
				
			||||||
 | 
					    if(start !=='' && end !==''){
 | 
				
			||||||
 | 
					      const utcStart = new Date(start).toISOString();
 | 
				
			||||||
 | 
					      const utcEnd = new Date(end).toISOString();
 | 
				
			||||||
 | 
					      options.params.startDate = dateToUnix(utcStart);
 | 
				
			||||||
 | 
					      options.params.endDate = dateToUnix(utcEnd);
 | 
				
			||||||
 | 
					      extraParams='';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    axiosInstance
 | 
					    axiosInstance
 | 
				
			||||||
      .get(`/device/${encodeURIComponent(selectedDeviceId)}/healthchecks`, options)
 | 
					      .get(`/device/${encodeURIComponent(selectedDeviceId)}/healthchecks${extraParams}`, options)
 | 
				
			||||||
      .then((response) => {
 | 
					      .then((response) => {
 | 
				
			||||||
        setHealthChecks(response.data.values);
 | 
					        setHealthChecks(response.data.values);
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
      .catch(() => {})
 | 
					      .catch(() => {})
 | 
				
			||||||
      .finally(() => {
 | 
					      .finally(() => {
 | 
				
			||||||
        setLoading(false);
 | 
					        setLoading(false);
 | 
				
			||||||
 | 
					        setLoadingMore(false);
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -103,9 +119,38 @@ const DeviceHealth = ({ selectedDeviceId }) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  useEffect(() => {
 | 
					  useEffect(() => {
 | 
				
			||||||
    if (selectedDeviceId) {
 | 
					    if (selectedDeviceId) {
 | 
				
			||||||
 | 
					      setLogLimit(25);
 | 
				
			||||||
 | 
					      setLoadingMore(false);
 | 
				
			||||||
 | 
					      setShowLoadingMore(true);
 | 
				
			||||||
 | 
					      setStart('');
 | 
				
			||||||
 | 
					      setEnd('');
 | 
				
			||||||
      getDeviceHealth();
 | 
					      getDeviceHealth();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }, [selectedDeviceId, start, end]);
 | 
					  }, [selectedDeviceId]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    if (logLimit !== 25) {
 | 
				
			||||||
 | 
					      getDeviceHealth();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }, [logLimit]);
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    if (healthChecks.length === 0 || (healthChecks.length > 0 && healthChecks.length < logLimit)) {
 | 
				
			||||||
 | 
					      setShowLoadingMore(false);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else {
 | 
				
			||||||
 | 
					      setShowLoadingMore(true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }, [healthChecks]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    if (selectedDeviceId && start !== '' && end !== '') {
 | 
				
			||||||
 | 
					      getDeviceHealth();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if(selectedDeviceId && start === '' && end === ''){
 | 
				
			||||||
 | 
					      getDeviceHealth();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }, [start, end, selectedDeviceId]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (healthChecks && healthChecks.length > 0) {
 | 
					  if (healthChecks && healthChecks.length > 0) {
 | 
				
			||||||
    sanityLevel = healthChecks[healthChecks.length - 1].sanity;
 | 
					    sanityLevel = healthChecks[healthChecks.length - 1].sanity;
 | 
				
			||||||
@@ -132,8 +177,6 @@ const DeviceHealth = ({ selectedDeviceId }) => {
 | 
				
			|||||||
              <CCol>
 | 
					              <CCol>
 | 
				
			||||||
                From:
 | 
					                From:
 | 
				
			||||||
                <DatePicker
 | 
					                <DatePicker
 | 
				
			||||||
                  selected={start === '' ? new Date() : new Date(start)}
 | 
					 | 
				
			||||||
                  value={start === '' ? new Date() : new Date(start)}
 | 
					 | 
				
			||||||
                  includeTime
 | 
					                  includeTime
 | 
				
			||||||
                  onChange={(date) => modifyStart(date)}
 | 
					                  onChange={(date) => modifyStart(date)}
 | 
				
			||||||
                />
 | 
					                />
 | 
				
			||||||
@@ -141,8 +184,6 @@ const DeviceHealth = ({ selectedDeviceId }) => {
 | 
				
			|||||||
              <CCol>
 | 
					              <CCol>
 | 
				
			||||||
                To:
 | 
					                To:
 | 
				
			||||||
                <DatePicker
 | 
					                <DatePicker
 | 
				
			||||||
                  selected={end === '' ? new Date() : new Date(end)}
 | 
					 | 
				
			||||||
                  value={end === '' ? new Date() : new Date(end)}
 | 
					 | 
				
			||||||
                  includeTime
 | 
					                  includeTime
 | 
				
			||||||
                  onChange={(date) => modifyEnd(date)}
 | 
					                  onChange={(date) => modifyEnd(date)}
 | 
				
			||||||
                />
 | 
					                />
 | 
				
			||||||
@@ -190,6 +231,17 @@ const DeviceHealth = ({ selectedDeviceId }) => {
 | 
				
			|||||||
                    ),
 | 
					                    ),
 | 
				
			||||||
                  }}
 | 
					                  }}
 | 
				
			||||||
                />
 | 
					                />
 | 
				
			||||||
 | 
					                <CRow  style={{marginBottom: '1%', marginRight: '1%'}}>
 | 
				
			||||||
 | 
					                    {showLoadingMore && 
 | 
				
			||||||
 | 
					                     <LoadingButton
 | 
				
			||||||
 | 
					                        label="View More"
 | 
				
			||||||
 | 
					                        isLoadingLabel="Loading More..."
 | 
				
			||||||
 | 
					                        isLoading={loadingMore}
 | 
				
			||||||
 | 
					                        action={showMoreLogs}
 | 
				
			||||||
 | 
					                        variant="outline"
 | 
				
			||||||
 | 
					                      />
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                </CRow>
 | 
				
			||||||
              </div>
 | 
					              </div>
 | 
				
			||||||
            </CCard>
 | 
					            </CCard>
 | 
				
			||||||
          </CCollapse>
 | 
					          </CCollapse>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,17 +13,21 @@ import {
 | 
				
			|||||||
import CIcon from '@coreui/icons-react';
 | 
					import CIcon from '@coreui/icons-react';
 | 
				
			||||||
import DatePicker from 'react-widgets/DatePicker';
 | 
					import DatePicker from 'react-widgets/DatePicker';
 | 
				
			||||||
import PropTypes from 'prop-types';
 | 
					import PropTypes from 'prop-types';
 | 
				
			||||||
import { addDays, prettyDate, dateToUnix } from '../../utils/helper';
 | 
					import { prettyDate, dateToUnix } from '../../utils/helper';
 | 
				
			||||||
import axiosInstance from '../../utils/axiosInstance';
 | 
					import axiosInstance from '../../utils/axiosInstance';
 | 
				
			||||||
import { getToken } from '../../utils/authHelper';
 | 
					import { getToken } from '../../utils/authHelper';
 | 
				
			||||||
 | 
					import LoadingButton from '../../components/LoadingButton';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const DeviceLogs = ({ selectedDeviceId }) => {
 | 
					const DeviceLogs = ({ selectedDeviceId }) => {
 | 
				
			||||||
  const [collapse, setCollapse] = useState(false);
 | 
					  const [collapse, setCollapse] = useState(false);
 | 
				
			||||||
  const [details, setDetails] = useState([]);
 | 
					  const [details, setDetails] = useState([]);
 | 
				
			||||||
  const [loading, setLoading] = useState(false);
 | 
					  const [loading, setLoading] = useState(false);
 | 
				
			||||||
  const [logs, setLogs] = useState([]);
 | 
					  const [logs, setLogs] = useState([]);
 | 
				
			||||||
  const [start, setStart] = useState(addDays(new Date(), -3).toString());
 | 
					  const [start, setStart] = useState('');
 | 
				
			||||||
  const [end, setEnd] = useState(new Date().toString());
 | 
					  const [end, setEnd] = useState('');
 | 
				
			||||||
 | 
					  const [logLimit, setLogLimit] = useState(25);
 | 
				
			||||||
 | 
					  const [loadingMore, setLoadingMore] = useState(false);
 | 
				
			||||||
 | 
					  const [showLoadingMore, setShowLoadingMore] = useState(true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const toggle = (e) => {
 | 
					  const toggle = (e) => {
 | 
				
			||||||
    setCollapse(!collapse);
 | 
					    setCollapse(!collapse);
 | 
				
			||||||
@@ -38,11 +42,14 @@ const DeviceLogs = ({ selectedDeviceId }) => {
 | 
				
			|||||||
    setEnd(value);
 | 
					    setEnd(value);
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const showMoreLogs = () => {
 | 
				
			||||||
 | 
					    setLogLimit(logLimit + 50);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const getLogs = () => {
 | 
					  const getLogs = () => {
 | 
				
			||||||
    if (loading) return;
 | 
					    if (loading) return;
 | 
				
			||||||
 | 
					    setLoadingMore(true);
 | 
				
			||||||
    setLoading(true);
 | 
					    setLoading(true);
 | 
				
			||||||
    const utcStart = new Date(start).toISOString();
 | 
					 | 
				
			||||||
    const utcEnd = new Date(end).toISOString();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const options = {
 | 
					    const options = {
 | 
				
			||||||
      headers: {
 | 
					      headers: {
 | 
				
			||||||
@@ -50,19 +57,28 @@ const DeviceLogs = ({ selectedDeviceId }) => {
 | 
				
			|||||||
        Authorization: `Bearer ${getToken()}`,
 | 
					        Authorization: `Bearer ${getToken()}`,
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      params: {
 | 
					      params: {
 | 
				
			||||||
        startDate: dateToUnix(utcStart),
 | 
					        limit: logLimit
 | 
				
			||||||
        endDate: dateToUnix(utcEnd),
 | 
					 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let extraParams = '?newest=true';
 | 
				
			||||||
 | 
					    if(start !=='' && end !==''){
 | 
				
			||||||
 | 
					      const utcStart = new Date(start).toISOString();
 | 
				
			||||||
 | 
					      const utcEnd = new Date(end).toISOString();
 | 
				
			||||||
 | 
					      options.params.startDate = dateToUnix(utcStart);
 | 
				
			||||||
 | 
					      options.params.endDate = dateToUnix(utcEnd);
 | 
				
			||||||
 | 
					      extraParams='';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    axiosInstance
 | 
					    axiosInstance
 | 
				
			||||||
      .get(`/device/${encodeURIComponent(selectedDeviceId)}/logs`, options)
 | 
					      .get(`/device/${encodeURIComponent(selectedDeviceId)}/logs${extraParams}`, options)
 | 
				
			||||||
      .then((response) => {
 | 
					      .then((response) => {
 | 
				
			||||||
        setLogs(response.data.values);
 | 
					        setLogs(response.data.values);
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
      .catch(() => {})
 | 
					      .catch(() => {})
 | 
				
			||||||
      .finally(() => {
 | 
					      .finally(() => {
 | 
				
			||||||
        setLoading(false);
 | 
					        setLoading(false);
 | 
				
			||||||
 | 
					        setLoadingMore(false);
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -99,6 +115,35 @@ const DeviceLogs = ({ selectedDeviceId }) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  useEffect(() => {
 | 
					  useEffect(() => {
 | 
				
			||||||
    if (selectedDeviceId) {
 | 
					    if (selectedDeviceId) {
 | 
				
			||||||
 | 
					      setLogLimit(25);
 | 
				
			||||||
 | 
					      setLoadingMore(false);
 | 
				
			||||||
 | 
					      setShowLoadingMore(true);
 | 
				
			||||||
 | 
					      setStart('');
 | 
				
			||||||
 | 
					      setEnd('');
 | 
				
			||||||
 | 
					      getLogs();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }, [selectedDeviceId]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    if (logLimit !== 25) {
 | 
				
			||||||
 | 
					      getLogs();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }, [logLimit]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    if (logs.length === 0 || (logs.length > 0 && logs.length < logLimit)) {
 | 
				
			||||||
 | 
					      setShowLoadingMore(false);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else {
 | 
				
			||||||
 | 
					      setShowLoadingMore(true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }, [logs]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    if (selectedDeviceId && start !== '' && end !== '') {
 | 
				
			||||||
 | 
					      getLogs();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if(selectedDeviceId && start === '' && end === ''){
 | 
				
			||||||
      getLogs();
 | 
					      getLogs();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }, [start, end, selectedDeviceId]);
 | 
					  }, [start, end, selectedDeviceId]);
 | 
				
			||||||
@@ -115,8 +160,6 @@ const DeviceLogs = ({ selectedDeviceId }) => {
 | 
				
			|||||||
              <CCol>
 | 
					              <CCol>
 | 
				
			||||||
                From:
 | 
					                From:
 | 
				
			||||||
                <DatePicker
 | 
					                <DatePicker
 | 
				
			||||||
                  selected={start === '' ? new Date() : new Date(start)}
 | 
					 | 
				
			||||||
                  value={start === '' ? new Date() : new Date(start)}
 | 
					 | 
				
			||||||
                  includeTime
 | 
					                  includeTime
 | 
				
			||||||
                  onChange={(date) => modifyStart(date)}
 | 
					                  onChange={(date) => modifyStart(date)}
 | 
				
			||||||
                />
 | 
					                />
 | 
				
			||||||
@@ -124,8 +167,6 @@ const DeviceLogs = ({ selectedDeviceId }) => {
 | 
				
			|||||||
              <CCol>
 | 
					              <CCol>
 | 
				
			||||||
                To:
 | 
					                To:
 | 
				
			||||||
                <DatePicker
 | 
					                <DatePicker
 | 
				
			||||||
                  selected={end === '' ? new Date() : new Date(end)}
 | 
					 | 
				
			||||||
                  value={end === '' ? new Date() : new Date(end)}
 | 
					 | 
				
			||||||
                  includeTime
 | 
					                  includeTime
 | 
				
			||||||
                  onChange={(date) => modifyEnd(date)}
 | 
					                  onChange={(date) => modifyEnd(date)}
 | 
				
			||||||
                />
 | 
					                />
 | 
				
			||||||
@@ -167,6 +208,17 @@ const DeviceLogs = ({ selectedDeviceId }) => {
 | 
				
			|||||||
                    ),
 | 
					                    ),
 | 
				
			||||||
                  }}
 | 
					                  }}
 | 
				
			||||||
                />
 | 
					                />
 | 
				
			||||||
 | 
					                <CRow  style={{marginBottom: '1%', marginRight: '1%'}}>
 | 
				
			||||||
 | 
					                    {showLoadingMore && 
 | 
				
			||||||
 | 
					                     <LoadingButton
 | 
				
			||||||
 | 
					                        label="View More"
 | 
				
			||||||
 | 
					                        isLoadingLabel="Loading More..."
 | 
				
			||||||
 | 
					                        isLoading={loadingMore}
 | 
				
			||||||
 | 
					                        action={showMoreLogs}
 | 
				
			||||||
 | 
					                        variant="outline"
 | 
				
			||||||
 | 
					                      />
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                </CRow>
 | 
				
			||||||
              </div>
 | 
					              </div>
 | 
				
			||||||
            </CCard>
 | 
					            </CCard>
 | 
				
			||||||
          </CCollapse>
 | 
					          </CCollapse>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user