|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2020 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.stat.impl; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.sql.Connection; |
| 25 | +import java.sql.DriverManager; |
| 26 | +import java.sql.PreparedStatement; |
| 27 | +import java.sql.SQLException; |
| 28 | +import java.sql.Timestamp; |
| 29 | + |
| 30 | +import org.springframework.core.env.Environment; |
| 31 | + |
| 32 | +import eu.openanalytics.containerproxy.service.EventService.Event; |
| 33 | +import eu.openanalytics.containerproxy.stat.IStatCollector; |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + * # MonetDB, Postgresql, MySQL/MariaDB usage-stats-url: |
| 38 | + * jdbc:monetdb://localhost:50000/usage_stats usage-stats-url: |
| 39 | + * jdbc:postgresql://localhost/postgres usage-stats-url: |
| 40 | + * jdbc:mysql://localhost/shinyproxy |
| 41 | + * |
| 42 | + * Assumed table layout: |
| 43 | + * |
| 44 | + * create table event( event_time timestamp, username varchar(128), type |
| 45 | + * varchar(128), data text ); |
| 46 | + * |
| 47 | + * |
| 48 | + * # MS SQL Server usage-stats-url: |
| 49 | + * jdbc:sqlserver://localhost;databaseName=shinyproxy |
| 50 | + * |
| 51 | + * Assumed table layout: |
| 52 | + * |
| 53 | + * create table event( event_time datetime, username varchar(128), type |
| 54 | + * varchar(128), data text ); |
| 55 | + * |
| 56 | + */ |
| 57 | +public class JDBCCollector implements IStatCollector { |
| 58 | + |
| 59 | + private Connection conn; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void accept(Event event, Environment env) throws IOException { |
| 63 | + synchronized (this) { |
| 64 | + String baseURL = env.getProperty("proxy.usage-stats-url"); |
| 65 | + String username = env.getProperty("proxy.usage-stats-username", "monetdb"); |
| 66 | + String password = env.getProperty("proxy.usage-stats-password", "monetdb"); |
| 67 | + try { |
| 68 | + if (conn == null || conn.isClosed()) { |
| 69 | + conn = DriverManager.getConnection(baseURL, username, password); |
| 70 | + } |
| 71 | + } catch (SQLException e) { |
| 72 | + throw new IOException("Failed to connect to " + baseURL, e); |
| 73 | + } |
| 74 | + } |
| 75 | + String sql = "INSERT INTO event(event_time, username, type, data) VALUES (?,?,?,?)"; |
| 76 | + try (PreparedStatement stmt = conn.prepareStatement(sql)) { |
| 77 | + stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis())); |
| 78 | + stmt.setString(2, event.user); |
| 79 | + stmt.setString(3, event.type); |
| 80 | + stmt.setString(4, event.data); |
| 81 | + stmt.executeUpdate(); |
| 82 | + } catch (SQLException e) { |
| 83 | + throw new IOException("Exception while loggin stats", e); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments