Author: [log in to unmask] Date: Mon Apr 27 15:58:36 2015 New Revision: 2831 Log: Add run-summary command to conditions command line tool for printing collection info for a run. Added: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/RunSummaryCommand.java Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java ============================================================================= --- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java (original) +++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java Mon Apr 27 15:58:36 2015 @@ -25,12 +25,24 @@ * * @author <a href="mailto:[log in to unmask]">Jeremy McCormick</a> */ -public class CommandLineTool { +public final class CommandLineTool { /** * Setup logging. */ private static final Logger LOGGER = LogUtil.create(CommandLineTool.class); + + private static Options OPTIONS = new Options(); + + static { + OPTIONS.addOption(new Option("h", false, "print help")); + OPTIONS.addOption(new Option("d", true, "detector name")); + OPTIONS.addOption(new Option("r", true, "run number")); + OPTIONS.addOption(new Option("p", true, "database connection properties file")); + OPTIONS.addOption(new Option("x", true, "conditions XML configuration file")); + OPTIONS.addOption(new Option("t", true, "conditions tag to use for filtering records")); + OPTIONS.addOption(new Option("l", true, "log level of the conditions manager (INFO, FINE, etc.)")); + } /** * Create a basic instance of this class. @@ -39,16 +51,11 @@ */ private static CommandLineTool create() { final CommandLineTool cli = new CommandLineTool(); - cli.options.addOption(new Option("h", false, "Print help and exit")); - cli.options.addOption(new Option("d", true, "Set the detector name")); - cli.options.addOption(new Option("r", true, "Set the run number")); - cli.options.addOption(new Option("v", false, "Enable verbose print output")); - cli.options.addOption(new Option("p", true, "Set the database connection properties file")); - cli.options.addOption(new Option("x", true, "Set the conditions XML configuration file")); cli.registerCommand(new LoadCommand()); cli.registerCommand(new PrintCommand()); cli.registerCommand(new AddCommand()); cli.registerCommand(new TagCommand()); + cli.registerCommand(new RunSummaryCommand()); return cli; } @@ -72,19 +79,9 @@ private DatabaseConditionsManager conditionsManager; /** - * The top level options (does not include sub-command options). - */ - private final Options options = new Options(); - - /** * The options parser. */ private final PosixParser parser = new PosixParser(); - - /** - * The verbose setting. - */ - private boolean verbose = false; /** * Exit with the given status. @@ -104,7 +101,7 @@ for (final String command : this.commands.keySet()) { s.append(command + '\n'); } - help.printHelp("CommandLineTool", "Commands:\n" + s.toString(), this.options, ""); + help.printHelp("CommandLineTool", "Commands:\n" + s.toString(), OPTIONS, ""); } /** @@ -133,9 +130,9 @@ CommandLine commandLine = null; try { - commandLine = this.parser.parse(this.options, arguments, true); + commandLine = this.parser.parse(OPTIONS, arguments, true); } catch (final ParseException e) { - LOGGER.log(Level.SEVERE, "error parsing the options", e); + LOGGER.log(Level.SEVERE, "Error parsing the options.", e); printUsage(); exit(1); } @@ -144,15 +141,7 @@ printUsage(); exit(0); } - - // Set verbosity. - if (commandLine.hasOption("v")) { - LOGGER.setLevel(Level.ALL); - LOGGER.getHandlers()[0].setLevel(Level.ALL); - this.verbose = true; - LOGGER.config("verbose mode enabled"); - } - + // Setup conditions manager from command line options. setupConditionsManager(commandLine); @@ -167,8 +156,7 @@ final String[] commandArguments = new String[commandLine.getArgs().length - 1]; System.arraycopy(commandLine.getArgs(), 1, commandArguments, 0, commandArguments.length); - // Excecute the sub-command. - command.setVerbose(this.verbose); + // Execute the sub-command. command.execute(commandArguments); } catch (final Exception e) { e.printStackTrace(); @@ -185,62 +173,80 @@ */ private void setupConditionsManager(final CommandLine commandLine) { - LOGGER.info("setting up conditions manager"); + LOGGER.info("Setting up the conditions manager ..."); // Create new manager. this.conditionsManager = DatabaseConditionsManager.getInstance(); - - // Set log level. - this.conditionsManager.setLogLevel(LOGGER.getLevel()); - + + // Set the conditions manager log level (does not affect logger of this class or sub-commands). + if (commandLine.hasOption("l")) { + Level level = Level.parse(commandLine.getOptionValue("l")); + conditionsManager.setLogLevel(level); + LOGGER.config("conditions manager log level will be set to " + level.toString()); + } + // Connection properties. if (commandLine.hasOption("p")) { final File connectionPropertiesFile = new File(commandLine.getOptionValue("p")); this.conditionsManager.setConnectionProperties(connectionPropertiesFile); - LOGGER.config("connection properties -p " + connectionPropertiesFile); - } - - // XML config. + LOGGER.config("using connection properties " + connectionPropertiesFile.getPath()); + } + + // Conditions system XML configuration file. if (commandLine.hasOption("x")) { final File xmlConfigFile = new File(commandLine.getOptionValue("x")); this.conditionsManager.setXmlConfig(xmlConfigFile); - LOGGER.config("XML config -x " + xmlConfigFile); + LOGGER.config("using XML config " + xmlConfigFile.getPath()); + } + + // User specified tag of conditions records. + if (commandLine.hasOption("t")) { + String tag = commandLine.getOptionValue("t"); + conditionsManager.setTag(tag); + LOGGER.config("using tag " + tag); } // If there is a run number or detector number then attempt to initialize the conditions system. if (commandLine.hasOption("r") || commandLine.hasOption("d")) { - - LOGGER.config("detector name or run number supplied so manager will be initialized"); + + if (!commandLine.hasOption("r")) { + // Missing run number. + throw new RuntimeException("Missing -r option with run number which must be given when detector name is used."); + } + + if (!commandLine.hasOption("d")) { + // Missing detector name. + throw new RuntimeException("Missing -d option with detector name which must be given when run number is used."); + } // Set detector name. String detectorName = null; if (commandLine.hasOption("d")) { detectorName = commandLine.getOptionValue("d"); - LOGGER.config("detector -d " + detectorName); + LOGGER.config("using detector " + detectorName); } else { detectorName = "HPS-ECalCommissioning-v2"; - LOGGER.config("default detector " + detectorName + " is being used"); } // Get run number. Integer run = null; if (commandLine.hasOption("r")) { run = Integer.parseInt(commandLine.getOptionValue("r")); - LOGGER.config("run -r " + run); + LOGGER.config("using run " + run); } else { run = 0; - LOGGER.config("default run number " + run + " is being used"); } // Setup the conditions manager with user detector name and run number. try { - LOGGER.config("initializing conditions manager with detector " + detectorName + " and run " + run); + LOGGER.config("Initializing conditions manager with detector " + detectorName + " and run " + run + " ..."); DatabaseConditionsManager.getInstance().setDetector(detectorName, run); - LOGGER.config("conditions manager initialized successfully"); + LOGGER.config("Conditions manager was initialized successfully!"); LOGGER.getHandlers()[0].flush(); } catch (final ConditionsNotFoundException e) { - throw new RuntimeException(e); - } - } + throw new RuntimeException("Error setting up the conditions manager.", e); + } + } + LOGGER.info("Done setting up the conditions manager!"); } } Added: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/RunSummaryCommand.java ============================================================================= --- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/RunSummaryCommand.java (added) +++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/RunSummaryCommand.java Mon Apr 27 15:58:36 2015 @@ -0,0 +1,115 @@ +package org.hps.conditions.cli; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.hps.conditions.api.ConditionsObjectCollection; +import org.hps.conditions.api.ConditionsRecord; +import org.hps.conditions.api.ConditionsRecord.ConditionsRecordCollection; +import org.hps.conditions.api.TableMetaData; +import org.hps.conditions.database.DatabaseConditionsManager; +import org.lcsim.util.log.LogUtil; +import org.lcsim.util.log.MessageOnlyLogFormatter; + +/** + * This is a sub-command to print out collection meta data for the current conditions configuration + * of tag, detector model and run number, which are given as arguments to the conditions command line + * front-end. It does not print out any conditions objects, only the collection information. + * By default it will print information about the single collection found for a given type, which is + * by convention the last one updated. The <code>-a</code> option can be used to print out all collection information. + * + * @author @author <a href="mailto:[log in to unmask]">Jeremy McCormick</a> + * + */ +final class RunSummaryCommand extends AbstractCommand { + + /** + * Setup logger. + */ + private static final Logger LOGGER = LogUtil.create(RunSummaryCommand.class, new MessageOnlyLogFormatter(), Level.INFO); + + /** + * Define command options. + */ + static Options options = new Options(); + static { + options.addOption(new Option("h", false, "Show help for run-summary command")); + options.addOption(new Option("a", false, "Print all collections found for the run")); + } + + /** + * Class constructor. + */ + RunSummaryCommand() { + super("run-summary", "Print the run summary", options); + } + + /** + * Print out the run summary information. + * + * @param arguments the command line arguments + */ + @Override + final void execute(final String[] arguments) { + + final CommandLine commandLine = parse(arguments); + + final DatabaseConditionsManager conditionsManager = DatabaseConditionsManager.getInstance(); + + if (!conditionsManager.isInitialized()) { + throw new RuntimeException("The conditions system is not initialized."); + } + + final int run = conditionsManager.getRun(); + + boolean printAll = commandLine.hasOption("a"); + if (printAll) { + LOGGER.info("All collections will be printed."); + } + + // Get all the conditions records from the manager including those that overlap in time validity. + ConditionsRecordCollection conditionsRecords = conditionsManager.getConditionsRecords(); + LOGGER.info('\n' + "Run " + run + " has " + conditionsRecords.size() + " conditions records."); + + // Get the list of unique conditions keys and sort them. + List<String> conditionsKeys = new ArrayList<String>(conditionsRecords.getConditionsKeys()); + Collections.sort(conditionsKeys); + LOGGER.info('\n' + "Found these unique conditions keys for the run ..."); + for (String key : conditionsKeys) { + LOGGER.info(key); + } + LOGGER.info(""); + + // Loop over all the conditions keys that apply to this run. + for (String key : conditionsKeys) { + + // Get the table meta data for the key. + TableMetaData tableMetaData = conditionsManager.findTableMetaData(key); + + // Get all the conditions records that match this key. + ConditionsRecordCollection collectionRecords = conditionsRecords.findByKey(key); + + // Get the table name. + final String tableName = tableMetaData.getTableName(); + + if (!printAll) { + // Print out the single collection that will be used if retrieved through the converter. + ConditionsObjectCollection<?> collection = conditionsManager.getCachedConditions(tableMetaData.getCollectionClass(), key).getCachedData(); + LOGGER.info(tableMetaData.getObjectClass().getSimpleName() + " collection " + collection.getCollectionId() + " in " + tableName + " with " + collection.size() + " rows."); + } else { + // Print out information about all applicable collections for this key, without figuring out which would be used. + LOGGER.info(tableMetaData.getObjectClass().getSimpleName() + " has " + collectionRecords.size() + " collection(s) in " + tableName + " for run."); + for (ConditionsRecord record : collectionRecords) { + LOGGER.info(" collection " + record.getCollectionId().toString() + " created on " + record.getCreated().toString()); + } + } + } + LOGGER.info('\n' + "Done printing run summary!"); + } +}