|
| 1 | +import React, { useState, useEffect, useContext } from "react" |
| 2 | +import { Message } from "primereact/message" |
| 3 | +import { DataTable } from "primereact/datatable" |
| 4 | +import { Column } from "primereact/column" |
| 5 | +import { Button } from "primereact/button" |
| 6 | +import { toast } from "react-toastify" |
| 7 | +import { connectToMongoDB } from "../../mongoDB/mongoDBUtils" |
| 8 | +import { DataContext } from "../../workspace/dataContext" |
| 9 | + |
| 10 | +const DropDuplicatesToolsDB = ({ currentCollection }) => { |
| 11 | + const { globalData } = useContext(DataContext) |
| 12 | + const [data, setData] = useState([]) |
| 13 | + const [columns, setColumns] = useState([]) |
| 14 | + const [duplicateColumns, setDuplicateColumns] = useState([]) |
| 15 | + const [selectedColumn, setSelectedColumn] = useState(null) |
| 16 | + const [loadingData, setLoadingData] = useState(false) |
| 17 | + |
| 18 | + const fetchData = async () => { |
| 19 | + setLoadingData(true) |
| 20 | + if (!currentCollection) { |
| 21 | + toast.warn("No collection selected.") |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + const db = await connectToMongoDB() |
| 27 | + const collection = db.collection(globalData[currentCollection].id) |
| 28 | + |
| 29 | + const documents = await collection.find({}).limit(10).toArray() |
| 30 | + setData(documents) |
| 31 | + |
| 32 | + const sampleDocument = documents[0] |
| 33 | + if (sampleDocument) { |
| 34 | + const allKeys = Object.keys(sampleDocument).filter((key) => key !== "_id") |
| 35 | + const columnStructure = allKeys.map((key) => ({ |
| 36 | + field: key, |
| 37 | + header: key.charAt(0).toUpperCase() + key.slice(1) |
| 38 | + })) |
| 39 | + setColumns(columnStructure) |
| 40 | + |
| 41 | + findDuplicateColumns(allKeys, collection) |
| 42 | + } |
| 43 | + } catch (error) { |
| 44 | + console.error("Error fetching data:", error) |
| 45 | + toast.error("An error occurred while fetching data.") |
| 46 | + } finally { |
| 47 | + setLoadingData(false) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const findDuplicateColumns = async (allKeys, collection) => { |
| 52 | + let duplicatePairs = [] |
| 53 | + |
| 54 | + try { |
| 55 | + for (let i = 0; i < allKeys.length; i++) { |
| 56 | + for (let j = i + 1; j < allKeys.length; j++) { |
| 57 | + const column1 = allKeys[i] |
| 58 | + const column2 = allKeys[j] |
| 59 | + |
| 60 | + const pipeline = [{ $project: { areEqual: { $eq: [`$${column1}`, `$${column2}`] } } }, { $match: { areEqual: false } }, { $count: "mismatchedDocuments" }] |
| 61 | + |
| 62 | + const result = await collection.aggregate(pipeline).toArray() |
| 63 | + if (result.length === 0 || (result[0]?.mismatchedDocuments || 0) === 0) { |
| 64 | + duplicatePairs.push({ column1, column2 }) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + setDuplicateColumns(duplicatePairs) |
| 70 | + } catch (error) { |
| 71 | + console.error("Error finding duplicate columns:", error) |
| 72 | + toast.error("An error occurred while finding duplicate columns.") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const handleDeleteColumn = async () => { |
| 77 | + if (!selectedColumn) { |
| 78 | + toast.warn("Please select a column to delete.") |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + const db = await connectToMongoDB() |
| 84 | + const collection = db.collection(globalData[currentCollection].id) |
| 85 | + |
| 86 | + await collection.updateMany({}, { $unset: { [selectedColumn]: "" } }) |
| 87 | + |
| 88 | + toast.success(`Column "${selectedColumn}" has been deleted.`) |
| 89 | + setSelectedColumn(null) |
| 90 | + await fetchData() |
| 91 | + } catch (error) { |
| 92 | + console.error("Error deleting column:", error) |
| 93 | + toast.error("An error occurred while deleting the column.") |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + useEffect(() => { |
| 98 | + fetchData() |
| 99 | + }, [currentCollection]) |
| 100 | + |
| 101 | + return ( |
| 102 | + <div |
| 103 | + style={{ |
| 104 | + display: "flex", |
| 105 | + flexDirection: "column", |
| 106 | + justifyContent: "center", |
| 107 | + alignItems: "center", |
| 108 | + padding: "5px" |
| 109 | + }} |
| 110 | + > |
| 111 | + {loadingData && <Message severity="info" text="Loading..." style={{ marginBottom: "15px" }} />} |
| 112 | + <Message severity="info" text="This tool identifies duplicate columns in your dataset and allows you to choose one for deletion." style={{ marginBottom: "15px" }} /> |
| 113 | + <Message severity="success" text={`Current Collection: ${globalData[currentCollection]?.name || "None"}`} style={{ marginBottom: "15px" }} /> |
| 114 | + {data.length > 0 && ( |
| 115 | + <DataTable value={data} paginator rows={5} rowsPerPageOptions={[5, 10, 15]} className="p-datatable-gridlines"> |
| 116 | + {columns.map((col) => ( |
| 117 | + <Column |
| 118 | + key={col.field} |
| 119 | + field={col.field} |
| 120 | + header={col.header} |
| 121 | + sortable |
| 122 | + style={{ |
| 123 | + backgroundColor: col.field === selectedColumn ? "#ee6b6e" : "transparent" |
| 124 | + }} // Highlight the selected column |
| 125 | + /> |
| 126 | + ))} |
| 127 | + </DataTable> |
| 128 | + )} |
| 129 | + {duplicateColumns.length > 0 && ( |
| 130 | + <div style={{ marginTop: "20px" }}> |
| 131 | + <h4>Duplicate Columns</h4> |
| 132 | + <ul style={{ listStyleType: "none", padding: 0 }}> |
| 133 | + {duplicateColumns.map((pair, index) => ( |
| 134 | + <li key={index} style={{ marginBottom: "10px" }}> |
| 135 | + {pair.column1} and {pair.column2}{" "} |
| 136 | + <Button |
| 137 | + label={`Delete ${pair.column2}`} |
| 138 | + className="p-button-danger" |
| 139 | + onClick={() => setSelectedColumn(pair.column2)} |
| 140 | + style={{ |
| 141 | + marginLeft: "10px", |
| 142 | + marginTop: "5px", |
| 143 | + display: "inline-block" |
| 144 | + }} |
| 145 | + /> |
| 146 | + </li> |
| 147 | + ))} |
| 148 | + </ul> |
| 149 | + </div> |
| 150 | + )} |
| 151 | + |
| 152 | + {selectedColumn && ( |
| 153 | + <div style={{ marginTop: "20px", textAlign: "center" }}> |
| 154 | + <h4>Selected Column: {selectedColumn}</h4> |
| 155 | + <Button label="Confirm Delete" icon="pi pi-trash" className="p-button-danger" onClick={handleDeleteColumn} /> |
| 156 | + </div> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + ) |
| 160 | +} |
| 161 | + |
| 162 | +export default DropDuplicatesToolsDB |
0 commit comments