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