diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/ConsoleStatisticsOutput.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/ConsoleStatisticsOutput.java new file mode 100644 index 00000000..b0160660 --- /dev/null +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/ConsoleStatisticsOutput.java @@ -0,0 +1,159 @@ +/* + * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the + * @author tags. See the copyright.txt file in the distribution for a full + * listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.zanata.client.commands.stats; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.zanata.rest.dto.Link; +import org.zanata.rest.dto.stats.ContainerTranslationStatistics; +import org.zanata.rest.dto.stats.TranslationStatistics; + +/** + * Statistics output that prints stats to the java console. + * + * @author Carlos Munoz camunoz@redhat.com + */ +public class ConsoleStatisticsOutput implements ContainerStatisticsCommandOutput +{ + @Override + public void write(ContainerTranslationStatistics statistics) + { + List stats = statistics.getStats(); + + if( stats == null ) + { + stats = new ArrayList(); + } + + // Display headers + Link sourceRef = statistics.getRefs().findLinkByRel("statSource"); + if( sourceRef.getType().equals("PROJ_ITER") ) + { + System.out.println("Project Version: " + statistics.getId() ); + } + else if( sourceRef.getType().equals("DOC") ) + { + System.out.println(); + System.out.println("Document: " + statistics.getId()); + } + + Collections.sort(stats, new Comparator() + { + @Override + public int compare(TranslationStatistics o1, TranslationStatistics o2) + { + int localeComparisson = o1.getLocale().compareTo(o2.getLocale()); + if (localeComparisson == 0) + { + return o1.getUnit().toString().compareTo(o2.getUnit().toString()); + } else + { + return localeComparisson; + } + } + }); + + String[] headers = new String[]{"Locale", "Unit", "Total", "Translated", "Need Review", "Untranslated"}; + Object[][] data = new Object[stats.size()][headers.length]; + + for (int i = 0, statsSize = stats.size(); i < statsSize; i++) + { + TranslationStatistics s = stats.get(i); + data[i] = new Object[]{s.getLocale(), s.getUnit(), s.getTotal(), s.getTranslated(), + s.getNeedReview(), s.getUntranslated()}; + } + + printTable(headers, data); + + // Print detailed stats + if( statistics.getDetailedStats() != null ) + { + for( ContainerTranslationStatistics detailedStats : statistics.getDetailedStats() ) + { + write(detailedStats); + } + } + } + + private static void printTable( String[] headers, Object[][] rows ) + { + // Calculate the column widths (max column content + 1) + int[] colWidths = new int[ headers.length ]; + int tableWidth = 0; + + for(int i=0; i maxWidth ) + { + maxWidth = row[i].toString().length() + 3; + } + } + + colWidths[i] = maxWidth; + tableWidth += maxWidth; + } + + System.out.println(); + for( int i=0; icamunoz@redhat.com + */ +public interface ContainerStatisticsCommandOutput +{ + /** + * Writes a Statistics object. + * + * @param statistics Statistics object to output. + */ + public void write(ContainerTranslationStatistics statistics); +} diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/CsvStatisticsOutput.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/CsvStatisticsOutput.java new file mode 100644 index 00000000..92114a87 --- /dev/null +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/CsvStatisticsOutput.java @@ -0,0 +1,105 @@ +/* + * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the + * @author tags. See the copyright.txt file in the distribution for a full + * listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this software; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF + * site: http://www.fsf.org. + */ +package org.zanata.client.commands.stats; + +import java.io.IOException; +import java.io.OutputStreamWriter; + +import org.zanata.rest.dto.Link; +import org.zanata.rest.dto.stats.ContainerTranslationStatistics; +import org.zanata.rest.dto.stats.TranslationStatistics; + +import au.com.bytecode.opencsv.CSVWriter; + +/** + * Outputs statistics in CSV format to the console. + * + * @author Carlos Munoz camunoz@redhat.com + */ +public class CsvStatisticsOutput implements ContainerStatisticsCommandOutput +{ + @Override + public void write(ContainerTranslationStatistics statistics) + { + CSVWriter csvWriter = new CSVWriter( new OutputStreamWriter( System.out )); + writeToCsv(statistics, csvWriter); + try + { + csvWriter.flush(); + csvWriter.close(); + } + catch (IOException e) + { + throw new RuntimeException(e); + } + } + + private void writeToCsv(ContainerTranslationStatistics statistics, CSVWriter writer) + { + writer.writeNext(new String[]{}); + + // Display headers + Link sourceRef = statistics.getRefs().findLinkByRel("statSource"); + if( sourceRef.getType().equals("PROJ_ITER") ) + { + writer.writeNext(new String[]{"Project Version: ", statistics.getId()} ); + } + else if( sourceRef.getType().equals("DOC") ) + { + writer.writeNext(new String[]{"Document: ", statistics.getId()} ); + } + + // Write headers + writer.writeNext( new String[]{"Locale", "Unit", "Total", "Translated", "Need Review", "Untranslated"} ); + + // Write stats + if( statistics.getStats() != null ) + { + for(TranslationStatistics transStats : statistics.getStats()) + { + writer.writeNext( new String[]{ transStats.getLocale(), transStats.getUnit().toString(), + Long.toString(transStats.getTotal()), + Long.toString(transStats.getTranslated()), Long.toString(transStats.getNeedReview()), + Long.toString(transStats.getUntranslated())} ); + } + } + + writer.writeNext(new String[]{}); + + try + { + writer.flush(); + } + catch (IOException e) + { + throw new RuntimeException(e); + } + + // Detailed stats + if( statistics.getDetailedStats() != null ) + { + for( ContainerTranslationStatistics detailedStats : statistics.getDetailedStats() ) + { + writeToCsv(detailedStats, writer); + } + } + } +} diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/GetStatisticsCommand.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/GetStatisticsCommand.java index 9c0fc069..383e75ae 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/GetStatisticsCommand.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/stats/GetStatisticsCommand.java @@ -84,127 +84,26 @@ public void run() throws Exception getOpts().getIncludeWordLevelStats(), localeListArg); } - printContainerStats(containerStats); - } - - private static void printContainerStats( ContainerTranslationStatistics containerStats ) - { - List stats = containerStats.getStats(); - - if( stats == null ) + if( getOpts().getFormat() == null ) { - stats = new ArrayList(); + log.warn("Output format not specified; defaulting to Console output."); } - // Display headers - Link sourceRef = containerStats.getRefs().findLinkByRel("statSource"); - if( sourceRef.getType().equals("PROJ_ITER") ) - { - System.out.println("Project Version: " + containerStats.getId() ); - } - else if( sourceRef.getType().equals("DOC") ) + // Select the format (output) + ContainerStatisticsCommandOutput statsOutput; + // csv + if( "csv".equalsIgnoreCase( getOpts().getFormat() ) ) { - System.out.println(); - System.out.println("Document: " + containerStats.getId()); + statsOutput = new CsvStatisticsOutput(); } - - Collections.sort(stats, new Comparator() - { - @Override - public int compare(TranslationStatistics o1, TranslationStatistics o2) - { - int localeComparisson = o1.getLocale().compareTo(o2.getLocale()); - if( localeComparisson == 0 ) - { - return o1.getUnit().toString().compareTo( o2.getUnit().toString() ); - } - else - { - return localeComparisson; - } - } - }); - - String[] headers = new String[]{"Locale", "Unit", "Total", "Translated", "Need Review", "Untranslated"}; - Object[][] data = new Object[stats.size()][headers.length]; - - for (int i = 0, statsSize = stats.size(); i < statsSize; i++) + // Default: console + else { - TranslationStatistics s = stats.get(i); - data[i] = new Object[]{s.getLocale(), s.getUnit(), s.getTotal(), s.getTranslated(), - s.getNeedReview(), s.getUntranslated()}; + statsOutput = new ConsoleStatisticsOutput(); } - printTable(headers, data); - - // Print detailed stats - if( containerStats.getDetailedStats() != null ) - { - for( ContainerTranslationStatistics detailedStats : containerStats.getDetailedStats() ) - { - printContainerStats(detailedStats); - } - } + statsOutput.write( containerStats ); } - private static void printTable( String[] headers, Object[][] rows ) - { - // Calculate the column widths (max column content + 1) - int[] colWidths = new int[ headers.length ]; - int tableWidth = 0; - for(int i=0; i maxWidth ) - { - maxWidth = row[i].toString().length() + 3; - } - } - - colWidths[i] = maxWidth; - tableWidth += maxWidth; - } - - System.out.println(); - for( int i=0; i