This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rhbz831894 - Add csv style formatting for statistics from the Java cl…
…ient.
- Loading branch information
Carlos Munoz
committed
Aug 13, 2012
1 parent
89527b8
commit 201587b
Showing
4 changed files
with
313 additions
and
112 deletions.
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
...ient-commands/src/main/java/org/zanata/client/commands/stats/ConsoleStatisticsOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a> | ||
*/ | ||
public class ConsoleStatisticsOutput implements ContainerStatisticsCommandOutput | ||
{ | ||
@Override | ||
public void write(ContainerTranslationStatistics statistics) | ||
{ | ||
List<TranslationStatistics> stats = statistics.getStats(); | ||
|
||
if( stats == null ) | ||
{ | ||
stats = new ArrayList<TranslationStatistics>(); | ||
} | ||
|
||
// 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<TranslationStatistics>() | ||
{ | ||
@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<headers.length; i++) | ||
{ | ||
int maxWidth = headers[i].length() + 3; | ||
|
||
for( Object[] row : rows ) | ||
{ | ||
if( row[i].toString().length() + 3 > maxWidth ) | ||
{ | ||
maxWidth = row[i].toString().length() + 3; | ||
} | ||
} | ||
|
||
colWidths[i] = maxWidth; | ||
tableWidth += maxWidth; | ||
} | ||
|
||
System.out.println(); | ||
for( int i=0; i<tableWidth; i++ ) | ||
{ | ||
System.out.print("="); | ||
} | ||
System.out.println(); | ||
|
||
// Print the headers | ||
for(int i=0; i<headers.length; i++) | ||
{ | ||
System.out.printf("%1$" + colWidths[i] + "s", headers[i]); | ||
} | ||
|
||
System.out.println(); | ||
for( int i=0; i<tableWidth; i++ ) | ||
{ | ||
System.out.print("="); | ||
} | ||
System.out.println(); | ||
|
||
// Print the results | ||
for(Object[] row : rows) | ||
{ | ||
for (int i = 0, rowLength = row.length; i < rowLength; i++) | ||
{ | ||
Object column = row[i]; | ||
System.out.printf("%1$" + colWidths[i] + "s", column); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
// Horizontal line | ||
for( int i=0; i<tableWidth; i++ ) | ||
{ | ||
System.out.print("="); | ||
} | ||
System.out.println(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ands/src/main/java/org/zanata/client/commands/stats/ContainerStatisticsCommandOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 org.zanata.rest.dto.stats.ContainerTranslationStatistics; | ||
|
||
/** | ||
* Interface that provides different types of output for statistics. | ||
* | ||
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a> | ||
*/ | ||
public interface ContainerStatisticsCommandOutput | ||
{ | ||
/** | ||
* Writes a Statistics object. | ||
* | ||
* @param statistics Statistics object to output. | ||
*/ | ||
public void write(ContainerTranslationStatistics statistics); | ||
} |
105 changes: 105 additions & 0 deletions
105
...a-client-commands/src/main/java/org/zanata/client/commands/stats/CsvStatisticsOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a> | ||
*/ | ||
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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.