MongoStats extends BaleenTask { @Override protected void execute(JobSettings settings) throws AnalysisEngineProcessException { MongoCollection<Document> entityCollection = mongoResource.getDB().getCollection(entitiesCollectionName); MongoCollection<Document> documentCollection = mongoResource.getDB().getCollection(documentsCollectionName); MongoCollection<Document> relationCollection = mongoResource.getDB().getCollection(relationsCollectionName); File file = new File(filename); boolean newFile = !file.exists() || file.length() == 0; try (Writer writer = new OutputStreamWriter(new FileOutputStream(filename, true), StandardCharsets.UTF_8)) { if (newFile) { writer.write("timestamp,documents,entities,relations\n"); } writer.write( String.format( "%s,%d,%d,%d%n", FORMATTER.format(LocalDateTime.now()), documentCollection.count(), entityCollection.count(), relationCollection.count())); } catch (IOException e) { LOGGER.warn("Unable to write stats to file {} ", filename, e); } }   static final String KEY_MONGO; static final String PARAM_ENTITIES_COLLECTION; static final String PARAM_RELATIONS_COLLECTION; static final String PARAM_DOCUMENTS_COLLECTION; static final String PARAM_FILE; }
@Test public void testNewFile() throws ResourceInitializationException, AnalysisEngineProcessException, IOException { ExternalResourceDescription erd = ExternalResourceFactory.createNamedResourceDescription( "mongo", SharedFongoResource.class, SharedFongoResource.PARAM_FONGO_COLLECTION, "documents", SharedFongoResource.PARAM_FONGO_DATA, objectMapper.writeValueAsString(DATA)); File tempFile = File.createTempFile("test", "mongostats"); tempFile.delete(); try { AnalysisEngine task = create(MongoStats.class, "mongo", erd, "file", tempFile.getAbsolutePath()); execute(task); task.destroy(); List<String> lines = Files.readAllLines(tempFile.toPath()); assertEquals(2, lines.size()); assertEquals("timestamp,documents,entities,relations", lines.get(0)); String[] split = lines.get(1).split(","); assertEquals("3", split[1]); assertEquals("0", split[2]); assertEquals("0", split[3]); } finally { tempFile.delete(); } }
@Test public void testEmptyFile() throws ResourceInitializationException, AnalysisEngineProcessException, IOException { ExternalResourceDescription erd = ExternalResourceFactory.createNamedResourceDescription( "mongo", SharedFongoResource.class, SharedFongoResource.PARAM_FONGO_COLLECTION, "documents", SharedFongoResource.PARAM_FONGO_DATA, objectMapper.writeValueAsString(DATA)); File tempFile = File.createTempFile("test", "mongostats"); try { AnalysisEngine task = create(MongoStats.class, "mongo", erd, "file", tempFile.getAbsolutePath()); execute(task); task.destroy(); List<String> lines = Files.readAllLines(tempFile.toPath()); assertEquals(2, lines.size()); assertEquals("timestamp,documents,entities,relations", lines.get(0)); String[] split = lines.get(1).split(","); assertEquals("3", split[1]); assertEquals("0", split[2]); assertEquals("0", split[3]); } finally { tempFile.delete(); } }
@Test public void testWritingToExistingFile() throws ResourceInitializationException, AnalysisEngineProcessException, IOException { ExternalResourceDescription erd = ExternalResourceFactory.createNamedResourceDescription( "mongo", SharedFongoResource.class, SharedFongoResource.PARAM_FONGO_COLLECTION, "documents", SharedFongoResource.PARAM_FONGO_DATA, objectMapper.writeValueAsString(DATA)); File tempFile = File.createTempFile("test", "mongostats-existing"); try (FileWriter fileWriter = new FileWriter(tempFile)) { fileWriter.write("hello\n"); } try { AnalysisEngine task = create(MongoStats.class, "mongo", erd, "file", tempFile.getAbsolutePath()); execute(task); task.destroy(); List<String> lines = Files.readAllLines(tempFile.toPath()); assertEquals(2, lines.size()); assertEquals("hello", lines.get(0)); String[] split = lines.get(1).split(","); assertEquals("3", split[1]); assertEquals("0", split[2]); assertEquals("0", split[3]); } finally { tempFile.delete(); } }