From 708787925707ec63da3b98084732cd4601d91105 Mon Sep 17 00:00:00 2001 From: Ryan Doherty Date: Wed, 25 Oct 2023 23:50:57 -0400 Subject: [PATCH] Rework to deliver desired output --- .../wdk/model/dbms/TableUtilization.java | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/Model/src/main/java/org/gusdb/wdk/model/dbms/TableUtilization.java b/Model/src/main/java/org/gusdb/wdk/model/dbms/TableUtilization.java index 40c9dc452..a4f448df5 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/dbms/TableUtilization.java +++ b/Model/src/main/java/org/gusdb/wdk/model/dbms/TableUtilization.java @@ -1,23 +1,26 @@ package org.gusdb.wdk.model.dbms; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Stream; +import org.gusdb.fgputil.Tuples.TwoTuple; import org.gusdb.fgputil.iterator.IteratorUtil; import org.gusdb.fgputil.runtime.GusHome; import org.gusdb.wdk.model.WdkModel; import org.gusdb.wdk.model.query.SqlQuery; +import org.json.JSONArray; import org.json.JSONObject; public class TableUtilization { @@ -40,10 +43,11 @@ public static void main(String[] args) throws Exception { List tableNames = readTablesFile(dbTableFile); System.out.println("\nGathered " + tableNames.size() + " table names from tables file.\n"); - // initialize data structures outsize the try so we can close the model before dumping output - Map> queryToTablesMap = new LinkedHashMap<>(); // ordered query map (query -> tableName[]) - Map queryToJoinedStringMap = new LinkedHashMap<>(); // another ordered query map (query -> join(tableName[]) - //Map> joinedStringToQueriesMap = new LinkedHashMap<>(); + // initialize data structures outside the try so we can close the model before dumping output + // query -> tableName[] + Map> queryToTablesMap = new LinkedHashMap<>(); + // joinedTableString -> { tables, queries } + Map, List>> uniqueTableComboMap = new LinkedHashMap<>(); // build a model to get all queries try (WdkModel model = WdkModel.construct(projectId, GusHome.getGusHome())) { @@ -58,6 +62,7 @@ public static void main(String[] args) throws Exception { int numSqlQueries = 0; for (SqlQuery query : IteratorUtil.toIterable(queries.iterator())) { numSqlQueries++; + String queryName = query.getFullName(); String sql = query.getSql().toLowerCase(); List queryTables = new ArrayList<>(); for (String tableName : tableNames) { @@ -65,39 +70,41 @@ public static void main(String[] args) throws Exception { queryTables.add(tableName); } } - queryToTablesMap.put(query.getFullName(), queryTables); - queryToJoinedStringMap.put(query.getFullName(), String.join("|", queryTables)); + queryToTablesMap.put(queryName, queryTables); + + String joinedString = String.join("|", queryTables); + uniqueTableComboMap.computeIfAbsent(joinedString, s -> new TwoTuple<>(queryTables, new ArrayList<>())); + uniqueTableComboMap.get(joinedString).getSecond().add(queryName); } System.out.println("\nProcessed " + numSqlQueries + " SQL queries in model for " + projectId + "\n)"); } // dump out the map from queryName -> tableName[] - System.out.println("Map from query name to tables it uses: " + new JSONObject(queryToTablesMap).toString(2)); + System.out.println("Dumping map from query name to tables it uses."); + dumpOutputFile(Paths.get("tableUsageMap.json"), new JSONObject(queryToTablesMap).toString(2)); + + // other output file should be: { /id/: { tables: string[], queries: string[] } // make a list of unique table combinations; index will be the "ID" for that combo - List joinedStringList = new ArrayList<>(new HashSet<>(queryToJoinedStringMap.values())); + List joinedStringList = new ArrayList<>(uniqueTableComboMap.keySet()); joinedStringList.sort((a,b) -> a.compareTo(b)); - // dump unique table combinations - System.out.println("Unique table combinations by index:"); + JSONArray uniqueTableCombosArray = new JSONArray(); for (int i = 0; i < joinedStringList.size(); i++) { - // find how many queries use each combination - int count = 0; - for (String joinedTableString : queryToJoinedStringMap.values()) { - if (joinedStringList.get(i).equals(joinedTableString)) { - count++; - } - } - System.out.println(i + " (" + count + "): " + joinedStringList.get(i)); + TwoTuple,List> data = uniqueTableComboMap.get(joinedStringList.get(i)); + uniqueTableCombosArray.put(new JSONObject() + .put("id", i) + .put("tables", data.getFirst()) + .put("queries", data.getSecond())); } + dumpOutputFile(Paths.get("uniqueTableGroups.json"), uniqueTableCombosArray.toString(2)); + } - // desired output: - // list of table collections found in queries, so - // 1. start with map of query -> table[] - // 2. use to build ID -> table[] (unique collection of tables) - // 3. produce map of query -> ID, sort by IDs - + private static void dumpOutputFile(Path file, String content) throws IOException { + try (BufferedWriter out = new BufferedWriter(new FileWriter(file.toFile()))) { + out.write(content); + } } private static List readTablesFile(Path dbTableFile) throws FileNotFoundException, IOException {