/*******************************************************************************
* Copyright 2014 Miami-Dade County
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package cirmlive;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import mjson.Json;
import org.sharegov.cirm.stats.CirmClusterStatistics;
import org.sharegov.cirm.stats.CirmClusterStatisticsStore;
import org.sharegov.cirm.stats.CirmClusterStatisticsTask;
import util.AllowAnySSL;
/**
* Simple standalone program that updates the CirmLive store with fresh server stats data every full hour.
* Run this for a while to collect data if hour by hour comparison is needed.
* Afer collecting data, run CirmLive, it will load all data from the store.
*
* @author Thomas Hilpold
*/
public class CirmLiveStoreUpdater {
private CirmClusterStatistics clusterStats;
private CirmClusterStatisticsStore clusterStatsStore;
private CirmClusterStatisticsTask clusterStatsTask;
private String storeLocation;
public static void main(String[] argv) {
CirmLiveStoreUpdater s = new CirmLiveStoreUpdater();
s.initClusterStats();
s.initTimer();
for(;;) {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(CirmLiveStoreUpdater.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void initTimer() {
clusterStatsTask = CirmClusterStatisticsTask.getInstance(clusterStats, clusterStatsStore);
clusterStatsTask.start();
}
/**
* Sets up SSL, ClusterStats and the store - needs to be done before UI.
* Exits on failure.
*/
public void initClusterStats() {
AllowAnySSL allow = new AllowAnySSL();
allow.installPermissiveTrustmanager();
try {
clusterStats = new CirmClusterStatistics();
initialize(CiRMLive.readConfig());
clusterStatsStore = CirmClusterStatisticsStore.create(new File(storeLocation));
} catch (Exception e ) {
e.printStackTrace();
System.exit(-1);
}
}
/**
* Configures CiRMLIve basics based on a json configuration.
*
* @param configuration
*/
final void initialize(Json configuration) {
if (!configuration.has("storeLocation")) throw new IllegalArgumentException("no storeLocation in " + configuration.toString());
storeLocation = configuration.at("storeLocation").asString();
File f = new File(storeLocation);
if (!f.exists()) {
f.mkdirs();
System.out.println("Directories " + storeLocation + " was created.");
}
if (!new File(storeLocation).exists()) throw new IllegalArgumentException("please create store directory: " + storeLocation);
}
}