|
| 1 | +package org.phoebus.applications.uxanalytics.monitor.backend.database; |
| 2 | + |
| 3 | +import javafx.application.Platform; |
| 4 | +import org.phoebus.applications.uxanalytics.monitor.backend.image.FilesystemImageClient; |
| 5 | +import org.phoebus.applications.uxanalytics.monitor.backend.image.ImageClient; |
| 6 | +import org.phoebus.applications.uxanalytics.monitor.representation.ActiveTab; |
| 7 | +import org.phoebus.applications.uxanalytics.monitor.util.FileUtils; |
| 8 | +import org.phoebus.framework.preferences.PhoebusPreferenceService; |
| 9 | +import org.phoebus.security.tokens.AuthenticationScope; |
| 10 | + |
| 11 | +import java.net.URI; |
| 12 | +import java.sql.*; |
| 13 | +import java.util.logging.Level; |
| 14 | +import java.util.logging.Logger; |
| 15 | + |
| 16 | +public class SQLConnection implements BackendConnection{ |
| 17 | + |
| 18 | + Logger logger = Logger.getLogger(SQLConnection.class.getName()); |
| 19 | + Connection conn; |
| 20 | + String userHost = null; |
| 21 | + Integer userPort = null; |
| 22 | + String userName = null; |
| 23 | + String password = null; |
| 24 | + String database = null; |
| 25 | + String table = null; |
| 26 | + private ImageClient imageClient; |
| 27 | + private SQLConnection() { |
| 28 | + tryAutoConnect(AuthenticationScope.MARIADB); |
| 29 | + } |
| 30 | + private static SQLConnection instance; |
| 31 | + public static SQLConnection getInstance(){ |
| 32 | + if(instance == null){ |
| 33 | + instance = new SQLConnection(); |
| 34 | + } |
| 35 | + return instance; |
| 36 | + } |
| 37 | + |
| 38 | + static class JDBCConnectionBuilder { |
| 39 | + private String protocol = "jdbc:mysql://"; |
| 40 | + private String host = null; |
| 41 | + private String port = null; |
| 42 | + private String username = null; |
| 43 | + private String password = null; |
| 44 | + private String database = null; |
| 45 | + |
| 46 | + |
| 47 | + public JDBCConnectionBuilder() { |
| 48 | + } |
| 49 | + |
| 50 | + public JDBCConnectionBuilder protocol(String protocol) { |
| 51 | + this.protocol = protocol; |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + public JDBCConnectionBuilder host(String host) { |
| 56 | + this.host = host; |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + public JDBCConnectionBuilder port(Integer port) { |
| 61 | + if(port != null) |
| 62 | + this.port = port.toString(); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + public JDBCConnectionBuilder username(String username) { |
| 67 | + this.username = username; |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + public JDBCConnectionBuilder password(String password) { |
| 72 | + this.password = password; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public JDBCConnectionBuilder database(String database) { |
| 77 | + this.database = database; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public Connection build() { |
| 82 | + String url = protocol; |
| 83 | + if(host != null) |
| 84 | + url += host; |
| 85 | + else |
| 86 | + url += PhoebusPreferenceService.userNodeForClass(SQLConnection.class).get("host", "localhost"); |
| 87 | + if(port != null) |
| 88 | + url += ":" + port; |
| 89 | + else |
| 90 | + url += ":" + PhoebusPreferenceService.userNodeForClass(SQLConnection.class).get("port", "3306"); |
| 91 | + if(username == null) |
| 92 | + username = PhoebusPreferenceService.userNodeForClass(SQLConnection.class).get("username", "root"); |
| 93 | + if(database != null) |
| 94 | + url += "/" + database; |
| 95 | + else |
| 96 | + url += "/" + PhoebusPreferenceService.userNodeForClass(SQLConnection.class).get("database", "phoebus_analytics"); |
| 97 | + try{ |
| 98 | + Logger.getLogger(SQLConnection.class.getName()).info("Connecting to JDBC: " + url); |
| 99 | + return DriverManager.getConnection(url, username, password); |
| 100 | + } catch(SQLException e){ |
| 101 | + Logger.getLogger(JDBCConnectionBuilder.class.getName()).info("UX Analytics Failed to connect to JDBC: " + e.getMessage()); |
| 102 | + return null; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Boolean connect(String hostOrRegion, Integer port, String usernameOrAccessKey, String passwordOrSecretKey) { |
| 109 | + if(conn != null) { |
| 110 | + try { |
| 111 | + conn.close(); |
| 112 | + } catch (SQLException e) { |
| 113 | + logger.log(Level.WARNING, "Failed to close existing JDBC connection: " + e.getMessage()); |
| 114 | + } |
| 115 | + } |
| 116 | + conn = null; |
| 117 | + userHost = hostOrRegion; |
| 118 | + userPort = port; |
| 119 | + userName = usernameOrAccessKey; |
| 120 | + database = PhoebusPreferenceService.userNodeForClass(SQLConnection.class).get("database", "phoebus_analytics"); |
| 121 | + conn = new JDBCConnectionBuilder() |
| 122 | + .host(userHost) |
| 123 | + .port(userPort) |
| 124 | + .username(userName) |
| 125 | + .password(passwordOrSecretKey) |
| 126 | + .database(database) |
| 127 | + .build(); |
| 128 | + return (conn != null); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String getProtocol() { |
| 133 | + return "jdbc:mysql://"; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public String getHost() { |
| 138 | + if(userHost != null) |
| 139 | + return userHost; |
| 140 | + return PhoebusPreferenceService.userNodeForClass(SQLConnection.class).get("host", "localhost"); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String getPort() { |
| 145 | + if(userPort != null) |
| 146 | + return userPort.toString(); |
| 147 | + return PhoebusPreferenceService.userNodeForClass(SQLConnection.class).get("port", "3306"); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String getDefaultUsername() { |
| 152 | + return "root"; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public Integer tearDown() { |
| 157 | + try{ |
| 158 | + conn.close(); |
| 159 | + return 0; |
| 160 | + } catch(SQLException e){ |
| 161 | + logger.warning("Failed to close JDBC connection: " + e.getMessage()); |
| 162 | + return -1; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void setImageClient(ImageClient imageClient) { |
| 168 | + this.imageClient = imageClient; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void handleClick(ActiveTab who, Integer x, Integer y) { |
| 173 | + if(conn!=null){ |
| 174 | + Platform.runLater(()->{ |
| 175 | + String path = FileUtils.analyticsPathForTab(who); |
| 176 | + String tableName = path.substring(path.lastIndexOf('_') + 1); |
| 177 | + if(imageClient==null){ |
| 178 | + logger.warning("No ImageClient set for SQLConnection, defaulting to filesystem"); |
| 179 | + imageClient = FilesystemImageClient.getInstance(); |
| 180 | + } |
| 181 | + if(!imageClient.imageExists(URI.create(path))){ |
| 182 | + imageClient.uploadImage(URI.create(path), FileUtils.getSnapshot(who)); |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + //create table if it doesn't exist |
| 187 | + try{ |
| 188 | + Statement stmt = conn.createStatement(); |
| 189 | + stmt.execute("CREATE TABLE IF NOT EXISTS " + tableName + " (x INT, y INT, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); |
| 190 | + stmt.close(); |
| 191 | + } catch(SQLException e){ |
| 192 | + logger.warning("Failed to create table for " + tableName + ": " + e.getMessage()); |
| 193 | + } |
| 194 | + |
| 195 | + //insert click into table |
| 196 | + try{ |
| 197 | + PreparedStatement stmt = conn.prepareStatement("INSERT INTO " + tableName + " (x, y) VALUES (?, ?)"); |
| 198 | + stmt.setInt(1, x); |
| 199 | + stmt.setInt(2, y); |
| 200 | + stmt.execute(); |
| 201 | + stmt.close(); |
| 202 | + logger.fine("Inserted click into table " + tableName); |
| 203 | + } catch(SQLException e){ |
| 204 | + logger.warning("Failed to insert click into table for " + tableName + ": " + e.getMessage()); |
| 205 | + } |
| 206 | + }); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments