Skip to content

Commit

Permalink
Rework to deliver desired output
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrdoherty committed Oct 26, 2023
1 parent a393b6c commit 7087879
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions Model/src/main/java/org/gusdb/wdk/model/dbms/TableUtilization.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -40,10 +43,11 @@ public static void main(String[] args) throws Exception {
List<String> 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<String, List<String>> queryToTablesMap = new LinkedHashMap<>(); // ordered query map (query -> tableName[])
Map<String, String> queryToJoinedStringMap = new LinkedHashMap<>(); // another ordered query map (query -> join(tableName[])
//Map<String, List<String>> joinedStringToQueriesMap = new LinkedHashMap<>();
// initialize data structures outside the try so we can close the model before dumping output
// query -> tableName[]
Map<String, List<String>> queryToTablesMap = new LinkedHashMap<>();
// joinedTableString -> { tables, queries }
Map<String, TwoTuple<List<String>, List<String>>> uniqueTableComboMap = new LinkedHashMap<>();

// build a model to get all queries
try (WdkModel model = WdkModel.construct(projectId, GusHome.getGusHome())) {
Expand All @@ -58,46 +62,49 @@ 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<String> queryTables = new ArrayList<>();
for (String tableName : tableNames) {
if (sql.contains(tableName)) {
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<String> joinedStringList = new ArrayList<>(new HashSet<>(queryToJoinedStringMap.values()));
List<String> 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<String>,List<String>> 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<String> readTablesFile(Path dbTableFile) throws FileNotFoundException, IOException {
Expand Down

0 comments on commit 7087879

Please sign in to comment.