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