| 45 | } |
| 46 | |
| 47 | public void init(Database sql) { |
| 48 | this.sql = sql; |
| 49 | |
| 50 | sql.update("CREATE TABLE IF NOT EXISTS network_stats (key VARCHAR(20), value VARCHAR(20))"); |
| 51 | |
| 52 | List<NetworkStatsEntity> entityBaseList = sql.get(NetworkStatsEntity.class).query("SELECT * FROM network_stats").getAll(); |
| 53 | if (entityBaseList.isEmpty()) { |
| 54 | sql.update("INSERT INTO network_stats (key, value) VALUES ('packetsSent', '0'), ('packetsReceived', '0'), ('bytesSent', '0'), ('bytesReceived', '0'), ('since', ?)", |
| 55 | System.currentTimeMillis()); |
| 56 | since = System.currentTimeMillis(); |
| 57 | } else { |
| 58 | for (NetworkStatsEntity entity : entityBaseList) { |
| 59 | String key = entity.getKey(); |
| 60 | String value = entity.getValue(); |
| 61 | |
| 62 | if (key.equals("packetsSent")) |
| 63 | totalPacketsSent = Integer.parseInt(value); |
| 64 | if (key.equals("packetsReceived")) |
| 65 | totalPacketsReceived = Integer.parseInt(value); |
| 66 | if (key.equals("bytesSent")) |
| 67 | totalBytesSent = Long.parseLong(value); |
| 68 | if (key.equals("bytesReceived")) |
| 69 | totalBytesReceived = Long.parseLong(value); |
| 70 | if (key.equals("since")) |
| 71 | since = Long.parseLong(value); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public void tick() { |
| 77 | if (System.currentTimeMillis() - lastSaveTime > 1000 * 60) { |