|
| 1 | +package io.hyperfoil.tools.h5m.cli; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import io.agroal.api.AgroalDataSource; |
| 6 | +import io.agroal.api.configuration.supplier.AgroalPropertiesReader; |
| 7 | +import io.hyperfoil.tools.h5m.api.Folder; |
| 8 | +import io.hyperfoil.tools.h5m.entity.FolderEntity; |
| 9 | +import io.hyperfoil.tools.h5m.svc.FolderService; |
| 10 | +import jakarta.inject.Inject; |
| 11 | +import picocli.CommandLine; |
| 12 | + |
| 13 | +import java.sql.Connection; |
| 14 | +import java.sql.PreparedStatement; |
| 15 | +import java.sql.ResultSet; |
| 16 | +import java.sql.Statement; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.Callable; |
| 20 | + |
| 21 | +@CommandLine.Command(name="load-legacy-runs") |
| 22 | +public class LoadLegacyRuns implements Callable<Integer> { |
| 23 | + |
| 24 | + @Inject |
| 25 | + FolderService folderService; |
| 26 | + |
| 27 | + @CommandLine.Option(names = {"username"}, description = "legacy db username", defaultValue = "quarkus") String username; |
| 28 | + @CommandLine.Option(names = {"password"}, description = "legacy db password", defaultValue = "quarkus") String password; |
| 29 | + @CommandLine.Option(names = {"url"}, description = "legacy connection url",defaultValue = "jdbc:postgresql://0.0.0.0:") String url; |
| 30 | + @CommandLine.Option(names = {"testId"}, description = "specify which test to load. Loads all if unspecified" ) Long testId; |
| 31 | + @CommandLine.Option(names = {"limit"}, description = "max runs to load", defaultValue = "-1") int limit; |
| 32 | + |
| 33 | + @Override |
| 34 | + public Integer call() throws Exception { |
| 35 | + Map<String, String> props = new HashMap<>(); |
| 36 | + props.put(AgroalPropertiesReader.MAX_SIZE, "1"); |
| 37 | + props.put(AgroalPropertiesReader.MIN_SIZE, "1"); |
| 38 | + props.put(AgroalPropertiesReader.INITIAL_SIZE, "1"); |
| 39 | + props.put(AgroalPropertiesReader.MAX_LIFETIME_S, "57"); |
| 40 | + props.put(AgroalPropertiesReader.ACQUISITION_TIMEOUT_S, "54"); |
| 41 | + props.put(AgroalPropertiesReader.PRINCIPAL,username); //username |
| 42 | + props.put(AgroalPropertiesReader.CREDENTIAL,password);//password |
| 43 | + props.put(AgroalPropertiesReader.PROVIDER_CLASS_NAME , "org.postgresql.Driver"); |
| 44 | + props.put(AgroalPropertiesReader.JDBC_URL, url ); |
| 45 | + AgroalDataSource ds = AgroalDataSource.from(new AgroalPropertiesReader() |
| 46 | + .readProperties(props) |
| 47 | + .get()); |
| 48 | + |
| 49 | + Map<Long,String> tests = new HashMap<>(); |
| 50 | + ObjectMapper mapper = new ObjectMapper(); |
| 51 | + try(Connection connection = ds.getConnection()){ |
| 52 | + if(testId!=null && testId > -1){ |
| 53 | + try(PreparedStatement statement = connection.prepareStatement("select name from test where id = ?")){ |
| 54 | + statement.setLong(1, testId); |
| 55 | + try (ResultSet rs = statement.executeQuery()){ |
| 56 | + while(rs.next()){ |
| 57 | + tests.put(testId, rs.getString("name")); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + }else { |
| 62 | + try (Statement statement = connection.createStatement()) { |
| 63 | + try (ResultSet rs = statement.executeQuery("select id,name from test")) { |
| 64 | + while (rs.next()) { |
| 65 | + tests.put(rs.getLong(1), rs.getString(2)); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + System.out.println("loaded "+tests.size()+" legacy tests"); |
| 71 | + for(Long testId : tests.keySet()){ |
| 72 | + String name = tests.get(testId); |
| 73 | + Folder folder = folderService.byName(name); |
| 74 | + if(folder == null){ |
| 75 | + System.out.println("Failed to find Folder for test "+name+" id="+testId); |
| 76 | + continue; |
| 77 | + } |
| 78 | + try (PreparedStatement ps = connection.prepareStatement("select count(id) from run where testid = ? and trashed = false")) { |
| 79 | + ps.setLong(1, testId); |
| 80 | + try (ResultSet rs = ps.executeQuery()){ |
| 81 | + while(rs.next()){ |
| 82 | + System.out.println("loading "+rs.getLong(1)+" uploads to "+name); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + String runQuery = limit > 0 |
| 87 | + ? "select id,data from run where testid = ? and trashed = false order by id desc limit ?" |
| 88 | + : "select id,data from run where testid = ? and trashed = false order by id desc"; |
| 89 | + connection.setAutoCommit(false); |
| 90 | + try (PreparedStatement ps = connection.prepareStatement(runQuery)) { |
| 91 | + ps.setFetchSize(5); |
| 92 | + ps.setLong(1, testId); |
| 93 | + if (limit > 0) ps.setInt(2, limit); |
| 94 | + int count = 0; |
| 95 | + try (ResultSet rs = ps.executeQuery()) { |
| 96 | + while(rs.next()){ |
| 97 | + Long id = rs.getLong(1); |
| 98 | + System.out.println(name+" "+id); |
| 99 | + JsonNode data = mapper.readTree(rs.getCharacterStream("data")); |
| 100 | + folderService.upload(folder.name(),null,data); |
| 101 | + count++; |
| 102 | + } |
| 103 | + } |
| 104 | + System.out.println("loaded " + count + " runs"); |
| 105 | + } finally { |
| 106 | + connection.setAutoCommit(true); |
| 107 | + } |
| 108 | + } |
| 109 | + } finally { |
| 110 | + ds.close(); |
| 111 | + } |
| 112 | + return 0; |
| 113 | + } |
| 114 | +} |
0 commit comments