Author: [log in to unmask]
Date: Tue Sep 29 13:09:31 2015
New Revision: 3734
Log:
Apply checkstyle and make sure help is shown when no arguments are passed.
Modified:
java/trunk/run-database/src/main/java/org/hps/run/database/RunDatabaseCommandLine.java
Modified: java/trunk/run-database/src/main/java/org/hps/run/database/RunDatabaseCommandLine.java
=============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/RunDatabaseCommandLine.java (original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/RunDatabaseCommandLine.java Tue Sep 29 13:09:31 2015
@@ -27,8 +27,8 @@
import org.lcsim.util.log.LogUtil;
/**
- * Command line tool for updating the run database from the EVIO files in the data catalog.
- *
+ * Command line tool for updating the run database from EVIO files registered in the data catalog.
+ *
* @author Jeremy McCormick, SLAC
*/
public class RunDatabaseCommandLine {
@@ -37,37 +37,35 @@
* Set of features supported by the tool.
*/
static enum Feature {
+ /**
+ * Insert EPICS data.
+ */
+ EPICS,
+ /**
+ * Insert scaler data.
+ */
+ SCALERS,
+ /**
+ * Insert run summary.
+ */
SUMMARY,
- EPICS,
- SCALERS,
+ /**
+ * Insert trigger config.
+ */
TRIGGER_CONFIG
}
-
+
/**
* Setup the logger.
*/
- private static final Logger LOGGER = LogUtil.create(RunDatabaseCommandLine.class, new DefaultLogFormatter(), Level.ALL);
-
+ private static final Logger LOGGER = LogUtil.create(RunDatabaseCommandLine.class, new DefaultLogFormatter(),
+ Level.ALL);
+
/**
* Command line options for the crawler.
*/
private static final Options OPTIONS = new Options();
-
- /**
- * The run manager for interacting with the run db.
- */
- private RunManager runManager;
-
- /**
- * The set of enabled features.
- */
- private Set<Feature> features = new HashSet<Feature>();
-
- /**
- * Allow updating of the database for existing runs.
- */
- private boolean allowUpdates = false;
-
+
/**
* Statically define the command options.
*/
@@ -78,33 +76,130 @@
OPTIONS.addOption("r", "run", true, "run to update");
OPTIONS.addOption("u", "update", false, "allow updating existing run in the database");
}
-
+
/**
* Run the program from the command line.
- *
+ *
* @param args the command line arguments
*/
- public static void main(String args[]) {
+ public static void main(final String args[]) {
new RunDatabaseCommandLine().parse(args).run();
- }
-
+ }
+
+ /**
+ * Allow updating of the database for existing runs.
+ */
+ private boolean allowUpdates = false;
+
+ /**
+ * The set of enabled features.
+ */
+ private final Set<Feature> features = new HashSet<Feature>();
+
+ /**
+ * The run manager for interacting with the run db.
+ */
+ private RunManager runManager;
+
+ /**
+ * Create a run processor from the current configuration.
+ *
+ * @return the run processor
+ */
+ private RunProcessor createEvioRunProcessor(final RunSummaryImpl runSummary, final List<File> files) {
+
+ final RunProcessor runProcessor = new RunProcessor(runSummary, files);
+
+ if (features.contains(Feature.EPICS)) {
+ runProcessor.addEpicsProcessor();
+ }
+ if (features.contains(Feature.SCALERS)) {
+ runProcessor.addScalerProcessor();
+ }
+ if (features.contains(Feature.TRIGGER_CONFIG)) {
+ runProcessor.addTriggerTimeProcessor();
+ }
+
+ return runProcessor;
+ }
+
+ /**
+ * Get the list of EVIO files for the run.
+ *
+ * @param run the run number
+ * @return the list of EVIO files from the run
+ */
+ private Map<File, Dataset> getEvioFiles(final int run) {
+ final DatacatClient datacatClient = new DatacatClientFactory().createClient();
+ final Set<String> metadata = new HashSet<String>();
+ final Map<File, Dataset> files = new HashMap<File, Dataset>();
+ metadata.add("runMin");
+ metadata.add("eventCount");
+ metadata.add("fileNumber");
+ metadata.add("endTimestamp");
+ metadata.add("startTimestamp");
+ metadata.add("hasEnd");
+ metadata.add("hasPrestart");
+ final List<Dataset> datasets = datacatClient.findDatasets("data/raw",
+ "fileFormat eq 'EVIO' AND dataType eq 'RAW' AND runMin eq " + run, metadata);
+ if (datasets.isEmpty()) {
+ throw new IllegalStateException("No EVIO datasets for run " + run + " were found in the data catalog.");
+ }
+ for (final Dataset dataset : datasets) {
+ files.put(new File(dataset.getLocations().get(0).getResource()), dataset);
+ }
+ return files;
+ }
+
+ /**
+ * Insert information for a run into the database.
+ *
+ * @param runManager the run manager for interacting with the run db
+ * @param runSummary the run summary with information about the run
+ */
+ private void insertRun(final RunManager runManager, final RunSummary runSummary) {
+
+ final RunDatabaseDaoFactory runFactory = new RunDatabaseDaoFactory(runManager.getConnection());
+
+ // Add the run summary record.
+ if (this.features.contains(Feature.SUMMARY)) {
+ LOGGER.info("inserting run summary");
+ runFactory.createRunSummaryDao().insertRunSummary(runSummary);
+ }
+
+ if (this.features.contains(Feature.EPICS)) {
+ LOGGER.info("inserting EPICS data");
+ runFactory.createEpicsDataDao().insertEpicsData(runSummary.getEpicsData());
+ }
+
+ if (this.features.contains(Feature.SCALERS)) {
+ LOGGER.info("inserting scaler data");
+ runFactory.createScalerDataDao().insertScalerData(runSummary.getScalerData(), runManager.getRun());
+ }
+
+ if (this.features.contains(Feature.TRIGGER_CONFIG)) {
+ LOGGER.info("inserting trigger config");
+ runFactory.createTriggerConfigDao().insertTriggerConfig(runSummary.getTriggerConfig(), runManager.getRun());
+ }
+ }
+
/**
* Parse command line options and return reference to <code>this</code>.
- *
+ *
* @param args the command line arguments
* @return reference to this object
*/
- RunDatabaseCommandLine parse(String args[]) {
+ RunDatabaseCommandLine parse(final String args[]) {
try {
final CommandLine cl = new PosixParser().parse(OPTIONS, args);
-
+
// Print help and exit.
- if (cl.hasOption("h")) {
+ if (cl.hasOption("h") || args.length == 0) {
final HelpFormatter help = new HelpFormatter();
help.printHelp("RunDatabaseCommandLine [options]", "", OPTIONS, "");
System.exit(0);
}
-
+
// Database connection properties file.
if (cl.hasOption("p")) {
final String dbPropPath = cl.getOptionValue("p");
@@ -115,14 +210,14 @@
}
final ConnectionParameters connectionParameters = ConnectionParameters.fromProperties(dbPropFile);
LOGGER.config("using " + dbPropPath + " for db connection properties");
-
+
runManager = new RunManager(connectionParameters.createConnection());
-
+
} else {
// Database connection properties file is required.
throw new RuntimeException("Connection properties are required.");
}
-
+
Integer run = null;
if (cl.hasOption("r")) {
run = Integer.parseInt(cl.getOptionValue("r"));
@@ -130,17 +225,17 @@
throw new RuntimeException("The run number is required.");
}
runManager.setRun(run);
-
+
if (cl.hasOption("f")) {
// Enable individual features.
- for (String arg : cl.getOptionValues("f")) {
+ for (final String arg : cl.getOptionValues("f")) {
features.add(Feature.valueOf(arg));
}
} else {
// By default all features are enabled.
features.addAll(Arrays.asList(Feature.values()));
}
- for (Feature feature : features) {
+ for (final Feature feature : features) {
LOGGER.config("feature " + feature.name() + " is enabled.");
}
@@ -149,149 +244,99 @@
this.allowUpdates = true;
LOGGER.config("updating or replacing existing run data is enabled");
}
-
- } catch (ParseException e) {
+
+ } catch (final ParseException e) {
throw new RuntimeException(e);
}
-
+
return this;
}
-
+
/**
* Run the job to update the information in the run database.
*/
private void run() {
-
- boolean runExists = runManager.runExists();
-
+
+ LOGGER.info("starting");
+
+ final boolean runExists = runManager.runExists();
+
// Fail if run exists and updates are not allowed.
if (runExists && !allowUpdates) {
- throw new IllegalStateException("The run " + runManager.getRun() + " already exists and updates are not allowed.");
- }
-
+ throw new IllegalStateException("The run " + runManager.getRun()
+ + " already exists and updates are not allowed.");
+ }
+
// Get the run number configured from command line.
- int run = runManager.getRun();
-
+ final int run = runManager.getRun();
+
// Get the list of EVIO files for the run using a data catalog query.
- Map<File, Dataset> fileDatasets = getEvioFiles(run);
- List<File> files = new ArrayList<File>(fileDatasets.keySet());
+ final Map<File, Dataset> fileDatasets = this.getEvioFiles(run);
+ final List<File> files = new ArrayList<File>(fileDatasets.keySet());
EvioFileUtilities.sortBySequence(files);
-
+
// Process the run's files to get information.
- RunSummaryImpl runSummary = new RunSummaryImpl(run);
- RunProcessor runProcessor = this.createEvioRunProcessor(runSummary, files);
+ final RunSummaryImpl runSummary = new RunSummaryImpl(run);
+ final RunProcessor runProcessor = this.createEvioRunProcessor(runSummary, files);
try {
runProcessor.processRun();
- } catch (Exception e) {
+ } catch (final Exception e) {
throw new RuntimeException(e);
}
- // Set start date.
- Dataset firstDataset = fileDatasets.get(files.get(0));
- DatasetMetadata metadata = firstDataset.getMetadata();
- Date startDate = new Date(metadata.getInteger("startTimestamp"));
- runSummary.setStartDate(startDate);
-
- // Set end date.
- Dataset lastDataset = fileDatasets.get(files.get(files.size() - 1));
- metadata = lastDataset.getMetadata();
- Date endDate = new Date(metadata.getInteger("endTimestamp"));
- runSummary.setEndDate(endDate);
- runSummary.setEndOkay(metadata.getInteger("hasEnd") == 0 ? false : true);
+ // Set number of files from datacat query.
+ runSummary.setTotalFiles(files.size());
+
+ // Set run start date.
+ this.setStartDate(fileDatasets, files, runSummary);
+
+ // Set run end date.
+ this.setEndDate(fileDatasets, files, runSummary);
// Delete existing run.
if (runExists) {
runManager.deleteRun();
}
-
+
// Insert run into database.
- insertRun(runManager, runSummary);
-
+ this.insertRun(runManager, runSummary);
+
// Close the database connection.
runManager.closeConnection();
- }
-
- /**
- * Insert information for a run into the database.
- *
- * @param runManager the run manager for interacting with the run db
- * @param runSummary the run summary with information about the run
- */
- private void insertRun(RunManager runManager, RunSummary runSummary) {
-
- RunDatabaseDaoFactory runFactory = new RunDatabaseDaoFactory(runManager.getConnection());
-
- // Add the run summary record.
- if (this.features.contains(Feature.SUMMARY)) {
- runFactory.createRunSummaryDao().insertRunSummary(runSummary);
- }
-
- // Does run exist now?
- if (runManager.runExists()) {
-
- if (this.features.contains(Feature.EPICS)) {
- runFactory.createEpicsDataDao().insertEpicsData(runSummary.getEpicsData());
- }
-
- if (this.features.contains(Feature.SCALERS)) {
- runFactory.createScalerDataDao().insertScalerData(runSummary.getScalerData(), runManager.getRun());
- }
-
- if (this.features.contains(Feature.TRIGGER_CONFIG)) {
- runFactory.createTriggerConfigDao().insertTriggerConfig(runSummary.getTriggerConfig(), runManager.getRun());
- }
- } else {
- // The run summary must be present to update any of the other information in the db.
- throw new RuntimeException("Run " + runManager.getRun() + " does not exist in the database.");
- }
- }
-
- /**
- * Create a run processor from the current configuration.
- *
- * @return the run processor
- */
- private RunProcessor createEvioRunProcessor(final RunSummaryImpl runSummary, List<File> files) {
-
- final RunProcessor runProcessor = new RunProcessor(runSummary, files);
-
- if (features.contains(Feature.EPICS)) {
- runProcessor.addEpicsProcessor();
- }
- if (features.contains(Feature.SCALERS)) {
- runProcessor.addScalerProcessor();
- }
- if (features.contains(Feature.TRIGGER_CONFIG)) {
- runProcessor.addTriggerTimeProcessor();
- }
-
- return runProcessor;
- }
-
- /**
- * Get the list of EVIO files for the run.
- *
- * @param run the run number
- * @return the list of EVIO files from the run
- */
- private Map<File, Dataset> getEvioFiles(int run) {
- DatacatClient datacatClient = new DatacatClientFactory().createClient();
- Set<String> metadata = new HashSet<String>();
- Map<File, Dataset> files = new HashMap<File, Dataset>();
- metadata.add("runMin");
- metadata.add("eventCount");
- metadata.add("fileNumber");
- metadata.add("endTimestamp");
- metadata.add("startTimestamp");
- metadata.add("hasEnd");
- metadata.add("hasPrestart");
- List<Dataset> datasets = datacatClient.findDatasets("data/raw", "fileFormat eq 'EVIO' AND dataType eq 'RAW' AND runMin eq " + run, metadata);
- if (datasets.isEmpty()) {
- throw new IllegalStateException("No EVIO datasets for run " + run + " were found in the data catalog.");
- }
- for (Dataset dataset : datasets) {
- files.put(new File(dataset.getLocations().get(0).getResource()), dataset);
- }
- return files;
+
+ LOGGER.info("done");
+ }
+
+ /**
+ * Set the run end date.
+ *
+ * @param fileDatasets the run's datasets
+ * @param files the run's EVIO files
+ * @param runSummary the run summary
+ */
+ private void setEndDate(final Map<File, Dataset> fileDatasets, final List<File> files,
+ final RunSummaryImpl runSummary) {
+ final Dataset lastDataset = fileDatasets.get(files.get(files.size() - 1));
+ final DatasetMetadata metadata = lastDataset.getMetadata();
+ // System.out.println("endTimestamp: " + metadata.getLong("endTimestamp"));
+ final Date endDate = new Date(metadata.getLong("endTimestamp"));
+ // System.out.println("endDate: " + startDate);
+ runSummary.setEndDate(endDate);
+ runSummary.setEndOkay(metadata.getLong("hasEnd") == 0 ? false : true);
+ }
+
+ /**
+ * Set the run start date.
+ *
+ * @param fileDatasets the run's datasets
+ * @param files the run's EVIO files
+ * @param runSummary the run summary
+ */
+ private void setStartDate(final Map<File, Dataset> fileDatasets, final List<File> files,
+ final RunSummaryImpl runSummary) {
+ final Dataset firstDataset = fileDatasets.get(files.get(0));
+ final DatasetMetadata metadata = firstDataset.getMetadata();
+ final Date startDate = new Date(metadata.getLong("startTimestamp"));
+ runSummary.setStartDate(startDate);
}
}
|