|
| 1 | +import ContentPasteSearchIcon from "@mui/icons-material/ContentPasteSearch"; |
| 2 | +import DatasetLinkedIcon from "@mui/icons-material/DatasetLinked"; |
| 3 | +import PeopleAltIcon from "@mui/icons-material/PeopleAlt"; |
| 4 | +import StorageIcon from "@mui/icons-material/Storage"; |
| 5 | +import TopicIcon from "@mui/icons-material/Topic"; |
| 6 | +import { Box, Typography } from "@mui/material"; |
| 7 | +import { Colors } from "design/theme"; |
| 8 | +import { useAppDispatch } from "hooks/useAppDispatch"; |
| 9 | +import { useAppSelector } from "hooks/useAppSelector"; |
| 10 | +import React, { useEffect } from "react"; |
| 11 | +import { fetchDbStats } from "redux/neurojson/neurojson.action"; |
| 12 | +import { DbStatsItem } from "redux/neurojson/types/neurojson.interface"; |
| 13 | +import { RootState } from "redux/store"; |
| 14 | + |
| 15 | +// function for calculate links and size |
| 16 | +const calculateLinksAndSize = (dbStats: DbStatsItem[] | null) => { |
| 17 | + if (!dbStats) return { totalLinks: 0, totalSizeTB: "0.00" }; |
| 18 | + |
| 19 | + const filtered = dbStats.filter( |
| 20 | + (item) => item.view !== "dbinfo" && item.view !== "subjects" |
| 21 | + ); |
| 22 | + |
| 23 | + const totalLinks = filtered.reduce((acc, item) => acc + item.num, 0); |
| 24 | + const totalSizeBytes = filtered.reduce((acc, item) => acc + item.size, 0); |
| 25 | + const totalSizeTB = Math.floor(totalSizeBytes / 1024 ** 4); |
| 26 | + return { totalLinks, totalSizeTB }; |
| 27 | +}; |
| 28 | + |
| 29 | +const StatisticsBanner: React.FC = () => { |
| 30 | + const dispatch = useAppDispatch(); |
| 31 | + const dbstats = useAppSelector((state: RootState) => state.neurojson.dbStats); |
| 32 | + const registry = useAppSelector( |
| 33 | + (state: RootState) => state.neurojson.registry |
| 34 | + ); |
| 35 | + |
| 36 | + const databaseCount = registry?.length ?? "-"; |
| 37 | + const datasetStat = dbstats?.find((item) => item.view === "dbinfo"); |
| 38 | + const subjectStat = dbstats?.find((item) => item.view === "subjects"); |
| 39 | + const { totalLinks, totalSizeTB } = calculateLinksAndSize(dbstats); |
| 40 | + |
| 41 | + // format numbers with commas |
| 42 | + const formatNumber = (num: number | undefined) => |
| 43 | + num?.toLocaleString() ?? "—"; |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + dispatch(fetchDbStats()); |
| 47 | + }, [dispatch]); |
| 48 | + |
| 49 | + return ( |
| 50 | + <Box |
| 51 | + sx={{ |
| 52 | + backgroundColor: "rgba(0, 0, 0, 0.5)", |
| 53 | + backdropFilter: "blur(15px)", |
| 54 | + borderRadius: "8px", |
| 55 | + zIndex: 100, |
| 56 | + padding: "1rem", |
| 57 | + position: "absolute", |
| 58 | + // top: "2%", |
| 59 | + // top: "20%", |
| 60 | + // left: "8%", |
| 61 | + bottom: "13%", |
| 62 | + left: "50%", |
| 63 | + transform: "translateX(-50%)", |
| 64 | + display: "flex", |
| 65 | + // flexDirection: "column", |
| 66 | + // flexWrap: "wrap", |
| 67 | + gap: "2rem", |
| 68 | + }} |
| 69 | + > |
| 70 | + {/* Databases */} |
| 71 | + <Box |
| 72 | + sx={{ |
| 73 | + display: "flex", |
| 74 | + flexDirection: "row", |
| 75 | + alignItems: "center", |
| 76 | + }} |
| 77 | + > |
| 78 | + <StorageIcon |
| 79 | + sx={{ |
| 80 | + marginRight: 1, |
| 81 | + verticalAlign: "middle", |
| 82 | + color: Colors.lightGray, |
| 83 | + // color: Colors.darkPurple, |
| 84 | + fontSize: "2.5rem", |
| 85 | + }} |
| 86 | + /> |
| 87 | + <Box> |
| 88 | + <Typography |
| 89 | + sx={{ |
| 90 | + // color: Colors.darkPurple, |
| 91 | + color: Colors.green, |
| 92 | + fontWeight: "bold", |
| 93 | + textAlign: "center", |
| 94 | + fontSize: "1.4rem", |
| 95 | + }} |
| 96 | + > |
| 97 | + {databaseCount.toLocaleString()} |
| 98 | + </Typography> |
| 99 | + <Typography |
| 100 | + sx={{ |
| 101 | + // color: Colors.darkPurple, |
| 102 | + color: Colors.lightGray, |
| 103 | + fontWeight: "medium", |
| 104 | + fontSize: "0.9rem", |
| 105 | + textAlign: "center", |
| 106 | + }} |
| 107 | + > |
| 108 | + Databases |
| 109 | + </Typography> |
| 110 | + </Box> |
| 111 | + </Box> |
| 112 | + |
| 113 | + {/* Datasets */} |
| 114 | + <Box sx={{ display: "flex", flexDirection: "row", alignItems: "center" }}> |
| 115 | + <ContentPasteSearchIcon |
| 116 | + sx={{ |
| 117 | + marginRight: 1, |
| 118 | + verticalAlign: "middle", |
| 119 | + // color: Colors.darkPurple, |
| 120 | + color: Colors.lightGray, |
| 121 | + fontSize: "2.5rem", |
| 122 | + }} |
| 123 | + /> |
| 124 | + <Box> |
| 125 | + <Typography |
| 126 | + sx={{ |
| 127 | + // color: Colors.darkPurple, |
| 128 | + color: Colors.green, |
| 129 | + fontWeight: "bold", |
| 130 | + textAlign: "center", |
| 131 | + fontSize: "1.4rem", |
| 132 | + }} |
| 133 | + > |
| 134 | + {formatNumber(datasetStat?.num)} |
| 135 | + </Typography> |
| 136 | + <Typography |
| 137 | + sx={{ |
| 138 | + // color: Colors.darkPurple, |
| 139 | + color: Colors.lightGray, |
| 140 | + fontWeight: "medium", |
| 141 | + fontSize: "0.9rem", |
| 142 | + textAlign: "center", |
| 143 | + }} |
| 144 | + > |
| 145 | + Datasets |
| 146 | + </Typography> |
| 147 | + </Box> |
| 148 | + </Box> |
| 149 | + {/* Subjects */} |
| 150 | + <Box sx={{ display: "flex", flexDirection: "row", alignItems: "center" }}> |
| 151 | + <PeopleAltIcon |
| 152 | + sx={{ |
| 153 | + marginRight: 1, |
| 154 | + verticalAlign: "middle", |
| 155 | + // color: Colors.darkPurple, |
| 156 | + color: Colors.lightGray, |
| 157 | + fontSize: "2.5rem", |
| 158 | + }} |
| 159 | + /> |
| 160 | + <Box> |
| 161 | + <Typography |
| 162 | + sx={{ |
| 163 | + // color: Colors.darkPurple, |
| 164 | + color: Colors.green, |
| 165 | + fontWeight: "bold", |
| 166 | + textAlign: "center", |
| 167 | + fontSize: "1.4rem", |
| 168 | + }} |
| 169 | + > |
| 170 | + {formatNumber(subjectStat?.num)} |
| 171 | + </Typography> |
| 172 | + <Typography |
| 173 | + sx={{ |
| 174 | + // color: Colors.darkPurple, |
| 175 | + color: Colors.lightGray, |
| 176 | + fontWeight: "medium", |
| 177 | + fontSize: "0.9rem", |
| 178 | + textAlign: "center", |
| 179 | + }} |
| 180 | + > |
| 181 | + Subjects |
| 182 | + </Typography> |
| 183 | + </Box> |
| 184 | + </Box> |
| 185 | + {/* Links */} |
| 186 | + <Box sx={{ display: "flex", flexDirection: "row", alignItems: "center" }}> |
| 187 | + <DatasetLinkedIcon |
| 188 | + sx={{ |
| 189 | + marginRight: 1, |
| 190 | + verticalAlign: "middle", |
| 191 | + // color: Colors.darkPurple, |
| 192 | + color: Colors.lightGray, |
| 193 | + fontSize: "2.5rem", |
| 194 | + }} |
| 195 | + /> |
| 196 | + <Box> |
| 197 | + <Typography |
| 198 | + sx={{ |
| 199 | + // color: Colors.darkPurple, |
| 200 | + color: Colors.green, |
| 201 | + fontWeight: "bold", |
| 202 | + textAlign: "center", |
| 203 | + fontSize: "1.4rem", |
| 204 | + }} |
| 205 | + > |
| 206 | + {totalLinks.toLocaleString() ?? "-"} |
| 207 | + </Typography> |
| 208 | + <Typography |
| 209 | + sx={{ |
| 210 | + // color: Colors.darkPurple, |
| 211 | + color: Colors.lightGray, |
| 212 | + fontWeight: "medium", |
| 213 | + fontSize: "0.9rem", |
| 214 | + textAlign: "center", |
| 215 | + }} |
| 216 | + > |
| 217 | + Links |
| 218 | + </Typography> |
| 219 | + </Box> |
| 220 | + </Box> |
| 221 | + {/* Size */} |
| 222 | + <Box sx={{ display: "flex", flexDirection: "row", alignItems: "center" }}> |
| 223 | + <TopicIcon |
| 224 | + sx={{ |
| 225 | + marginRight: 1, |
| 226 | + verticalAlign: "middle", |
| 227 | + // color: Colors.darkPurple, |
| 228 | + color: Colors.lightGray, |
| 229 | + fontSize: "2.5rem", |
| 230 | + }} |
| 231 | + /> |
| 232 | + <Box> |
| 233 | + <Typography |
| 234 | + sx={{ |
| 235 | + // color: Colors.darkPurple, |
| 236 | + color: Colors.green, |
| 237 | + fontWeight: "bold", |
| 238 | + textAlign: "center", |
| 239 | + fontSize: "1.4rem", |
| 240 | + }} |
| 241 | + > |
| 242 | + {totalSizeTB ?? "-"} TB |
| 243 | + </Typography> |
| 244 | + <Typography |
| 245 | + sx={{ |
| 246 | + // color: Colors.darkPurple, |
| 247 | + color: Colors.lightGray, |
| 248 | + fontWeight: "medium", |
| 249 | + fontSize: "0.9rem", |
| 250 | + textAlign: "center", |
| 251 | + }} |
| 252 | + > |
| 253 | + Size |
| 254 | + </Typography> |
| 255 | + </Box> |
| 256 | + </Box> |
| 257 | + </Box> |
| 258 | + ); |
| 259 | +}; |
| 260 | + |
| 261 | +export default StatisticsBanner; |
0 commit comments