Commit 9978f961 authored by Michael Ritter's avatar Michael Ritter
Browse files

Submit a formatter for use from either the csv or bean output

parent 4b45e9d0
package edu.umiacs.ace.stats; package edu.umiacs.ace.stats;
import edu.umiacs.ace.util.EntityManagerServlet; import edu.umiacs.ace.util.EntityManagerServlet;
import edu.umiacs.ace.util.FileSizeFormatter;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
...@@ -28,6 +29,7 @@ public class StatisticsServlet extends EntityManagerServlet { ...@@ -28,6 +29,7 @@ public class StatisticsServlet extends EntityManagerServlet {
private static final String AFTER = "after"; private static final String AFTER = "after";
private static final String BEFORE = "before"; private static final String BEFORE = "before";
private static final String GROUP = "group"; private static final String GROUP = "group";
private static final String TRUNCATE = "truncate";
private static final String COLLECTION = "collection"; private static final String COLLECTION = "collection";
private static final String CSV = "csv"; private static final String CSV = "csv";
...@@ -53,14 +55,20 @@ public class StatisticsServlet extends EntityManagerServlet { ...@@ -53,14 +55,20 @@ public class StatisticsServlet extends EntityManagerServlet {
String group = getParameter(request, GROUP, null); String group = getParameter(request, GROUP, null);
String after = getParameter(request, AFTER, null); String after = getParameter(request, AFTER, null);
String before = getParameter(request, BEFORE, null); String before = getParameter(request, BEFORE, null);
String truncate = getParameter(request, TRUNCATE, null);
String collection = getParameter(request, COLLECTION, null); String collection = getParameter(request, COLLECTION, null);
FileSizeFormatter formatter = new FileSizeFormatter(truncate);
SummaryQuery q = new SummaryQuery(group, after, before, collection); SummaryQuery q = new SummaryQuery(group, after, before, collection);
// We only output to csv for now, so only check true/false // We only output to csv for now, so only check true/false
// Might be better to have a radio w/ json/csv/none or smth // Might be better to have a radio w/ json/csv/none or smth
if (csv == null) { if (csv == null) {
List<IngestSummary> summary = q.getSummary(); List<IngestSummary> summary = q.getSummary();
// bleghhh
for (IngestSummary ingestSummary : summary) {
ingestSummary.setFormatter(formatter);
}
request.setAttribute("summary", summary); request.setAttribute("summary", summary);
RequestDispatcher dispatcher = request.getRequestDispatcher(SERVLET); RequestDispatcher dispatcher = request.getRequestDispatcher(SERVLET);
...@@ -70,7 +78,7 @@ public class StatisticsServlet extends EntityManagerServlet { ...@@ -70,7 +78,7 @@ public class StatisticsServlet extends EntityManagerServlet {
response.setContentType("text/plain"); response.setContentType("text/plain");
// Download instead of load a page // Download instead of load a page
response.setHeader("Content-Disposition", "attachment; filename=ingest-summary.csv"); response.setHeader("Content-Disposition", "attachment; filename=ingest-summary.csv");
q.writeToCsv(response.getOutputStream()); q.writeToCsv(response.getOutputStream(), formatter);
} catch (SQLException e) { } catch (SQLException e) {
LOG.error("Error with statistics query", e); LOG.error("Error with statistics query", e);
} }
......
package edu.umiacs.ace.stats; package edu.umiacs.ace.stats;
import edu.umiacs.ace.util.FileSizeFormatter;
import edu.umiacs.ace.util.PersistUtil; import edu.umiacs.ace.util.PersistUtil;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
...@@ -85,6 +86,11 @@ public class SummaryQuery { ...@@ -85,6 +86,11 @@ public class SummaryQuery {
this.collection = collection; this.collection = collection;
} }
/**
* Get the IngestSummary for the SummaryQuery
*
* @return the IngestSummary
*/
public List<IngestSummary> getSummary() { public List<IngestSummary> getSummary() {
List<String> params = new ArrayList<>(); List<String> params = new ArrayList<>();
EntityManager em = PersistUtil.getEntityManager(); EntityManager em = PersistUtil.getEntityManager();
...@@ -102,7 +108,17 @@ public class SummaryQuery { ...@@ -102,7 +108,17 @@ public class SummaryQuery {
return (List<IngestSummary>)nq.getResultList(); return (List<IngestSummary>)nq.getResultList();
} }
public void writeToCsv(ServletOutputStream os) throws SQLException, IOException { /**
* Write the SummaryQuery to an OutputStream as comma separated values
*
* values are as follows:
* date,collection,group,file_count,total_size
*
* @param os the OutputStream to write to
* @throws SQLException if there's an exception with the sql statement
* @throws IOException if there's an exception writing data
*/
public void writeToCsv(ServletOutputStream os, FileSizeFormatter formatter) throws SQLException, IOException {
List<String> params = new ArrayList<>(); List<String> params = new ArrayList<>();
DataSource db = PersistUtil.getDataSource(); DataSource db = PersistUtil.getDataSource();
Connection conn = db.getConnection(); Connection conn = db.getConnection();
...@@ -136,7 +152,7 @@ public class SummaryQuery { ...@@ -136,7 +152,7 @@ public class SummaryQuery {
bldr.append(collection).append(","); bldr.append(collection).append(",");
bldr.append(group).append(","); bldr.append(group).append(",");
bldr.append(count).append(","); bldr.append(count).append(",");
bldr.append(size); bldr.append(formatter.format(size));
os.write(bldr.toString().getBytes(charset)); os.write(bldr.toString().getBytes(charset));
os.println(); os.println();
......
...@@ -18,6 +18,7 @@ public class FileSizeFormatter { ...@@ -18,6 +18,7 @@ public class FileSizeFormatter {
} }
public String format(BigDecimal decimal) { public String format(BigDecimal decimal) {
// todo: not sure about the scale, it'll probably be something we want to investigate more
BigDecimal result = decimal.divide(unit.divisor, 5, RoundingMode.HALF_UP); BigDecimal result = decimal.divide(unit.divisor, 5, RoundingMode.HALF_UP);
String displayUnit = unit == Unit.B ? "" : unit.name(); String displayUnit = unit == Unit.B ? "" : unit.name();
return result.stripTrailingZeros().toPlainString() + " " + displayUnit; return result.stripTrailingZeros().toPlainString() + " " + displayUnit;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment