1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.cpdp.eval;
|
---|
16 |
|
---|
17 | import java.io.FileInputStream;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.sql.Connection;
|
---|
20 | import java.sql.DriverManager;
|
---|
21 | import java.sql.ResultSet;
|
---|
22 | import java.sql.SQLException;
|
---|
23 | import java.sql.Statement;
|
---|
24 | import java.util.Properties;
|
---|
25 |
|
---|
26 | import org.apache.commons.dbcp2.BasicDataSource;
|
---|
27 |
|
---|
28 | import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
|
---|
29 |
|
---|
30 | import de.lmu.ifi.dbs.elki.logging.Logging.Level;
|
---|
31 | import de.ugoe.cs.util.console.Console;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * <p>
|
---|
35 | * Implements a storage of experiment results in a MySQL database.
|
---|
36 | * </p>
|
---|
37 | *
|
---|
38 | * @author Steffen Herbold
|
---|
39 | */
|
---|
40 | public class MySQLResultStorage implements IResultStorage {
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Connection to the database
|
---|
44 | */
|
---|
45 | //private Connection con = null;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Connection pool for the data base.
|
---|
49 | */
|
---|
50 | private MysqlDataSource connectionPool = null;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * <p>
|
---|
54 | * Creates a new results storage. Tries to read a properties file mysql.cred located in the
|
---|
55 | * working directory. If this file is not found, the default database configuration is used:
|
---|
56 | * <ul>
|
---|
57 | * <li>dbHost = localhost</li>
|
---|
58 | * <li>dbPort = 3306</li>
|
---|
59 | * <li>dbName = crosspare</li>
|
---|
60 | * <li>dbUser = crosspare</li>
|
---|
61 | * <li>dbPass = benchmark</li>
|
---|
62 | * </p>
|
---|
63 | */
|
---|
64 | public MySQLResultStorage() {
|
---|
65 | Properties dbProperties = new Properties();
|
---|
66 | try {
|
---|
67 | dbProperties.load(new FileInputStream("mysql.cred"));
|
---|
68 | }
|
---|
69 | catch (IOException e) {
|
---|
70 | Console.printerr("Could not load mysql.cred file: " + e.getMessage());
|
---|
71 | Console.printerr("Must be a properties file located in working directory.");
|
---|
72 | Console
|
---|
73 | .traceln(Level.WARNING,
|
---|
74 | "Using default DB configuration since mysql.cred file could not be loaded");
|
---|
75 | }
|
---|
76 | String dbHost = dbProperties.getProperty("db.host", "localhost");
|
---|
77 | String dbPort = dbProperties.getProperty("db.port", "3306");
|
---|
78 | String dbName = dbProperties.getProperty("db.name", "crosspare");
|
---|
79 | String dbUser = dbProperties.getProperty("db.user", "crosspare");
|
---|
80 | String dbPass = dbProperties.getProperty("db.pass", "benchmark");
|
---|
81 | connectToDB(dbHost, dbPort, dbName, dbUser, dbPass);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * <p>
|
---|
86 | * Sets up the database connection
|
---|
87 | * </p>
|
---|
88 | *
|
---|
89 | * @param dbHost
|
---|
90 | * host of the database
|
---|
91 | * @param dbPort
|
---|
92 | * port of the database
|
---|
93 | * @param dbName
|
---|
94 | * name of the database
|
---|
95 | * @param dbUser
|
---|
96 | * user of the database
|
---|
97 | * @param dbPass
|
---|
98 | * password of the user
|
---|
99 | */
|
---|
100 | private void connectToDB(String dbHost,
|
---|
101 | String dbPort,
|
---|
102 | String dbName,
|
---|
103 | String dbUser,
|
---|
104 | String dbPass)
|
---|
105 | {
|
---|
106 | connectionPool = new MysqlDataSource();
|
---|
107 | connectionPool.setUser(dbUser);
|
---|
108 | connectionPool.setPassword(dbPass);
|
---|
109 | connectionPool.setUrl("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName);
|
---|
110 | /*
|
---|
111 | try {
|
---|
112 | Properties connectionProperties = new Properties();
|
---|
113 | connectionProperties.put("user", dbUser);
|
---|
114 | connectionProperties.put("password", dbPass);
|
---|
115 | connectionProperties.put("autoReconnect", "true");
|
---|
116 | connectionProperties.put("maxReconnects", "10000");
|
---|
117 | Class.forName("com.mysql.jdbc.Driver");
|
---|
118 |
|
---|
119 | con = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":" + dbPort + "/" +
|
---|
120 | dbName, connectionProperties);
|
---|
121 | }
|
---|
122 | catch (ClassNotFoundException e) {
|
---|
123 | Console.printerr("JDBC driver not found");
|
---|
124 | }
|
---|
125 | catch (SQLException e) {
|
---|
126 | Console.printerr("Problem with MySQL connection: ");
|
---|
127 | Console.printerr("SQLException: " + e.getMessage());
|
---|
128 | Console.printerr("SQLState: " + e.getSQLState());
|
---|
129 | Console.printerr("VendorError: " + e.getErrorCode());
|
---|
130 | }
|
---|
131 | */
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * (non-Javadoc)
|
---|
136 | *
|
---|
137 | * @see de.ugoe.cs.cpdp.eval.IResultStorage#addResult(de.ugoe.cs.cpdp.eval.ExperimentResult)
|
---|
138 | */
|
---|
139 | @Override
|
---|
140 | public void addResult(ExperimentResult result) {
|
---|
141 | StringBuilder sql = new StringBuilder();
|
---|
142 | sql.append("INSERT INTO crosspare.results VALUES (NULL,");
|
---|
143 | sql.append("\'" + result.getConfigurationName() + "\',");
|
---|
144 | sql.append("\'" + result.getProductName() + "\',");
|
---|
145 | sql.append("\'" + result.getClassifier() + "\',");
|
---|
146 | sql.append(result.getSizeTestData() + ",");
|
---|
147 | sql.append(result.getSizeTrainingData() + ",");
|
---|
148 | sql.append(result.getSuccHe() + ",");
|
---|
149 | sql.append(result.getSuccZi() + ",");
|
---|
150 | sql.append(result.getSuccG75() + ",");
|
---|
151 | sql.append(result.getSuccG60() + ",");
|
---|
152 | sql.append(result.getError() + ",");
|
---|
153 | sql.append(result.getRecall() + ",");
|
---|
154 | sql.append(result.getPrecision() + ",");
|
---|
155 | sql.append(result.getFscore() + ",");
|
---|
156 | sql.append(result.getGscore() + ",");
|
---|
157 | sql.append(result.getMcc() + ",");
|
---|
158 | sql.append(result.getAuc() + ",");
|
---|
159 | sql.append(result.getAucec() + ",");
|
---|
160 | sql.append(result.getTpr() + ",");
|
---|
161 | sql.append(result.getTnr() + ",");
|
---|
162 | sql.append(result.getFpr() + ",");
|
---|
163 | sql.append(result.getFnr() + ",");
|
---|
164 | sql.append(result.getTp() + ",");
|
---|
165 | sql.append(result.getFn() + ",");
|
---|
166 | sql.append(result.getTn() + ",");
|
---|
167 | sql.append(result.getFp() + ");");
|
---|
168 |
|
---|
169 | Statement stmt;
|
---|
170 | try {
|
---|
171 | stmt = connectionPool.getConnection().createStatement();
|
---|
172 | stmt.executeUpdate(sql.toString().replace("NaN", "NULL"));
|
---|
173 | }
|
---|
174 | catch (SQLException e) {
|
---|
175 | Console.printerr("Problem with MySQL connection: ");
|
---|
176 | Console.printerr("SQLException: " + e.getMessage());
|
---|
177 | Console.printerr("SQLState: " + e.getSQLState());
|
---|
178 | Console.printerr("VendorError: " + e.getErrorCode());
|
---|
179 | return;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * (non-Javadoc)
|
---|
185 | *
|
---|
186 | * @see de.ugoe.cs.cpdp.eval.IResultStorage#containsResult(java.lang.String, java.lang.String)
|
---|
187 | */
|
---|
188 | @Override
|
---|
189 | public boolean containsResult(String experimentName, String productName) {
|
---|
190 | String sql = "SELECT COUNT(*) as cnt FROM crosspare.results WHERE configurationName=\'" +
|
---|
191 | experimentName + "\' AND productName=\'" + productName + "\';";
|
---|
192 | Statement stmt;
|
---|
193 | boolean contained = false;
|
---|
194 | try {
|
---|
195 | stmt = connectionPool.getConnection().createStatement();
|
---|
196 | ResultSet results = stmt.executeQuery(sql);
|
---|
197 | results.next();
|
---|
198 | int count = results.getInt("cnt");
|
---|
199 | contained = count > 0;
|
---|
200 | }
|
---|
201 | catch (SQLException e) {
|
---|
202 | Console.printerr("Problem with MySQL connection: \n");
|
---|
203 | Console.printerr("SQLException: " + e.getMessage() + "\n");
|
---|
204 | Console.printerr("SQLState: " + e.getSQLState() + "\n");
|
---|
205 | Console.printerr("VendorError: " + e.getErrorCode() + "\n");
|
---|
206 | Console.printerr("\nskipping product since we could not check if the results is available");
|
---|
207 | contained = true;
|
---|
208 | }
|
---|
209 | return contained;
|
---|
210 | }
|
---|
211 | }
|
---|