Skip to content

Commit

Permalink
ENH: System alerts ui
Browse files Browse the repository at this point in the history
  • Loading branch information
mseng10 committed Jul 16, 2024
1 parent 8a03b73 commit 7ba5a9c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
44 changes: 44 additions & 0 deletions client/react/src/modals/system/SystemAlerts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useEffect, useState } from 'react';
import Modal from '@mui/material/Modal';
import Box from '@mui/material/Box';
import Alerts from '../../pages/Alerts';

const SystemAlerts = ({ isOpen, system, onRequestClose }) => {
const [alerts, setAlerts] = useState([]);

useEffect(() => {
if (isOpen && system && system.system) {
const url = 'http://127.0.0.1:5000/system/' + system.system.id.toString() + '/alerts'
// Fetch plant data from the server
fetch(url)
.then((response) => response.json())
.then((data) => setAlerts(data))
.catch((error) => console.error('Error fetching plant data:', error));
}
}, [isOpen, system]);

if (!system) {
return (<div></div>);
}

return (
<Modal
open={isOpen}
onClose={onRequestClose}
disableAutoFocus={true}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'inherit',
border: 'none',
}}
>
<Box sx={{ width: 756, height: 512, bgcolor: 'background.paper', borderRadius: 2 }}>
<Alerts alerts={alerts}></Alerts>
</Box>
</Modal>
);
};

export default SystemAlerts;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import Modal from '@mui/material/Modal';
import Box from '@mui/material/Box';
import Plants from '../pages/Plants';
import Plants from '../../pages/Plants';

const SystemPlants = ({ isOpen, system, onRequestClose }) => {
const [plants, setPlants] = useState([]);
Expand All @@ -18,7 +18,6 @@ const SystemPlants = ({ isOpen, system, onRequestClose }) => {
}, [isOpen, system]);

if (!system) {

return (<div></div>);
}

Expand Down
13 changes: 11 additions & 2 deletions client/react/src/models/System.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import IconButton from '@mui/material/IconButton';
import ReportGmailerrorredSharpIcon from '@mui/icons-material/ReportGmailerrorredSharp';
import CardActions from '@mui/material/CardActions';
import PointOfSaleIcon from '@mui/icons-material/PointOfSale';
import SystemPlants from '../modals/SystemPlants';
import SystemPlants from '../modals/system/SystemPlants';
import SystemAlerts from '../modals/system/SystemAlerts';

const System = ({ system, full }) => {

const [isSystemsPlanetsOpen, setIsSystemsPlanetsOpen] = useState(false);

const [isSystemAlertsOpen, setIsSystemsAlertsOpen] = useState(false);

if (!system) {
// Handle case when system data is not available
return <div>No system data available</div>;
Expand Down Expand Up @@ -118,6 +120,13 @@ const System = ({ system, full }) => {
system={{system}}
/>
)}
{isSystemAlertsOpen && (
<SystemAlerts
isOpen={isSystemAlertsOpen}
onRequestClose={() => setIsSystemsAlertsOpen(false)}
system={{system}}
/>
)}
</>
);
};
Expand Down
4 changes: 2 additions & 2 deletions client/react/src/pages/Alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import ReportGmailerrorredSharpIcon from '@mui/icons-material/ReportGmailerrorre
// import Typography from '@mui/material/Typography';
import CheckCircleOutlineIcon from '@mui/icons-material/CheckCircleOutline';

const Alerts = () => {
const Alerts = (alerts) => {

// Passable Data
const [plantAlerts, setPlantAlerts] = useState([]);
const [plantAlerts, setPlantAlerts] = useState(alerts && alerts.alerts ? alerts.alerts : []);

const [resolve, setResolve] = useState(null);

Expand Down
18 changes: 18 additions & 0 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,24 @@ def get_systems_plants(system_id):
# Return JSON response
return jsonify(plants_json)

@app.route("/system/<int:system_id>/plants", methods=["GET"])
def get_systems_alerts(system_id):
"""
Get system's alerts.
"""
# Log the request
logger.info("Received request to get a system's alerts")

session = Session()
plant_alerts = session.query(PlantAlerts).filter(Plant.system_id == system_id).all()
session.close()

# Transform plant alerts to JSON format
plant_alerts_json = [plant_alert.to_json() for plant_alert in plant_alerts]

# Return JSON response
return jsonify(plant_alerts_json)

if __name__ == "__main__":
# Run the Flask app
app.run(debug=True)

0 comments on commit 7ba5a9c

Please sign in to comment.