FileReporter implements Reporter { @Override public Report[] retrieve(ReportQuery query) { ICsvBeanReader csvReader = null; try { csvReader = new CsvBeanReader(new FileReader(filename), FORMAT); } catch (FileNotFoundException e) { logger.warn("Could not find " + filename, e); return new Report[0]; } List<Report> reports = new ArrayList<Report>(); try { DateRangeChecker dateChecker = new DateRangeChecker(query); AgentChecker agentChecker = new AgentChecker(query); int limit = query.getLimit(); if (limit == ReportQuery.NO_LIMIT) { limit = Integer.MAX_VALUE; } final String[] header = csvReader.getHeader(true); while (reports.size() < limit) { Report report = csvReader.read(Report.class, header, READ_PROCESSORS); if (report == null) { break; } if (!dateChecker.isInDateRange(report)) { continue; } if (!agentChecker.isAgent(report)) { continue; } reports.add(report); } } catch (IOException e) { logger.warn("Could not parse reports from " + filename, e); } try { csvReader.close(); } catch (IOException e) { logger.warn("Unable to close csv file " + filename, e); } return reports.toArray(new Report[reports.size()]); } FileReporter(String filename); @Override void store(Report report); @Override Report[] retrieve(ReportQuery query); static final String ISO_8601; }
@Test public void testSimpleReadingCSV() throws FileNotFoundException { File file = getTempFile(); PrintWriter out = new PrintWriter(file.toString()); out.println(header); DateFormat df = new SimpleDateFormat(FileReporter.ISO_8601); String line = df.format(new Date()) + ",abcdef,Why?,all,because,0,87,123456789"; out.println(line); out.close(); Reporter fr = new FileReporter(file.toString()); Report[] reports = fr.retrieve(new ReportQuery()); assertEquals(1, reports.length); assertEquals("abcdef", reports[0].getRequestId()); assertEquals("Why?", reports[0].getRequestText()); assertEquals("all", reports[0].getAgentName()); assertEquals("because", reports[0].getResponseText()); assertEquals(87, reports[0].getRouteTime()); assertEquals(123456789, reports[0].getAgentTime()); }
@Test public void testDateRangeRetrieve() throws FileNotFoundException, ParseException { File file = getTempFile(); PrintWriter out = new PrintWriter(file.toString()); out.println(header); out.println("2016-02-02T15:43:34UTC,req-1,Why?,all,because,0,87,123456789"); out.println("2016-02-03T15:43:34UTC,req-2,Why?,all,because,0,87,123456789"); out.println("2016-02-04T15:43:34UTC,req-3,Why?,all,because,0,87,123456789"); out.println("2016-02-05T15:43:34UTC,req-4,Why?,all,because,0,87,123456789"); out.println("2016-02-06T15:43:34UTC,req-5,Why?,all,because,0,87,123456789"); out.close(); DateFormat df = new SimpleDateFormat(FileReporter.ISO_8601); Reporter fr = new FileReporter(file.toString()); ReportQuery rq = new ReportQuery(); rq.setStartDate(df.parse("2016-02-04T12:00:00UTC")); rq.setEndDate(df.parse("2016-02-06T12:00:00UTC")); Report[] reports = fr.retrieve(rq); assertEquals(2, reports.length); assertEquals(df.parse("2016-02-05T15:43:34UTC"), reports[1].getTimestamp()); }
@Test public void testLimitRetrieve() throws FileNotFoundException, ParseException { File file = getTempFile(); PrintWriter out = new PrintWriter(file.toString()); out.println(header); out.println("2016-02-02T15:43:34UTC,req-1,Why?,all,because,0,87,123456789"); out.println("2016-02-03T15:43:34UTC,req-2,Why?,all,because,0,87,123456789"); out.println("2016-02-04T15:43:34UTC,req-3,Why?,all,because,0,87,123456789"); out.println("2016-02-05T15:43:34UTC,req-4,Why?,all,because,0,87,123456789"); out.println("2016-02-06T15:43:34UTC,req-5,Why?,all,because,0,87,123456789"); out.close(); DateFormat df = new SimpleDateFormat(FileReporter.ISO_8601); Reporter fr = new FileReporter(file.toString()); ReportQuery rq = new ReportQuery(); rq.setLimit(3); Report[] reports = fr.retrieve(rq); assertEquals(3, reports.length); assertEquals(df.parse("2016-02-02T15:43:34UTC"), reports[0].getTimestamp()); assertEquals(df.parse("2016-02-03T15:43:34UTC"), reports[1].getTimestamp()); assertEquals(df.parse("2016-02-04T15:43:34UTC"), reports[2].getTimestamp()); }
@Test public void testAgentFilterRetrieve() throws FileNotFoundException, ParseException { File file = getTempFile(); PrintWriter out = new PrintWriter(file.toString()); out.println(header); out.println("2016-02-02T15:43:34UTC,req-1,Why?,all,because,0,87,123456789"); out.println("2016-02-03T15:43:34UTC,req-2,Why?,time,because,0,87,123456789"); out.println("2016-02-04T15:43:34UTC,req-3,Why?,date,because,0,87,123456789"); out.println("2016-02-05T15:43:34UTC,req-4,Why?,all,because,0,87,123456789"); out.println("2016-02-06T15:43:34UTC,req-5,Why?,all,because,0,87,123456789"); out.close(); DateFormat df = new SimpleDateFormat(FileReporter.ISO_8601); Reporter fr = new FileReporter(file.toString()); ReportQuery rq = new ReportQuery(); rq.setAgentName("all"); Report[] reports = fr.retrieve(rq); assertEquals(3, reports.length); assertEquals(df.parse("2016-02-02T15:43:34UTC"), reports[0].getTimestamp()); assertEquals(df.parse("2016-02-05T15:43:34UTC"), reports[1].getTimestamp()); assertEquals(df.parse("2016-02-06T15:43:34UTC"), reports[2].getTimestamp()); }
@Test public void testAgentArrayFilterRetrieve() throws FileNotFoundException, ParseException { File file = getTempFile(); PrintWriter out = new PrintWriter(file.toString()); out.println(header); out.println("2016-02-02T15:43:34UTC,req-1,Why?,all,because,0,87,123456789"); out.println("2016-02-03T15:43:34UTC,req-2,Why?,time,because,0,87,123456789"); out.println("2016-02-04T15:43:34UTC,req-3,Why?,date,because,0,87,123456789"); out.println("2016-02-05T15:43:34UTC,req-4,Why?,all,because,0,87,123456789"); out.println("2016-02-06T15:43:34UTC,req-5,Why?,all,because,0,87,123456789"); out.close(); DateFormat df = new SimpleDateFormat(FileReporter.ISO_8601); Reporter fr = new FileReporter(file.toString()); ReportQuery rq = new ReportQuery(); rq.setAgentNames(new String[]{"time", "date"}); Report[] reports = fr.retrieve(rq); assertEquals(2, reports.length); assertEquals(df.parse("2016-02-03T15:43:34UTC"), reports[0].getTimestamp()); assertEquals(df.parse("2016-02-04T15:43:34UTC"), reports[1].getTimestamp()); }