<commit_before>package io.korobi.mongotoelastic.processor;

import com.mongodb.client.MongoCursor;
import io.korobi.mongotoelastic.exception.ImportException;
import io.korobi.mongotoelastic.logging.InjectLogger;
import io.korobi.mongotoelastic.mongo.IChannelBlacklist;
import org.apache.logging.log4j.Logger;
import org.bson.Document;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import javax.inject.Inject;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

public class ChannelDocumentProcessor implements IDocumentProcessor {

    @InjectLogger
    private Logger logger;
    private final Client esClient;
    private final IChannelBlacklist blacklist;

    @Inject
    public ChannelDocumentProcessor(Client esClient, IChannelBlacklist blacklist) {
        this.esClient = esClient;
        this.blacklist = blacklist;
    }

    @Override
    public void run(MongoCursor<Document> documents, int bulkSize) {
        throw new UnsupportedOperationException("No support for multithreaded channel import");
    }

    @Override
    public void run(MongoCursor<Document> documents) {
        // We don't have enough channels to necessitate processing in a multithreaded manner
        BulkRequestBuilder bulkRequest = this.esClient.prepareBulk();
        while (documents.hasNext()) {
            Document doc = documents.next();
            String network = doc.getString("network");
            String channel = doc.getString("channel");
            if (this.blacklist.isBlacklisted(network, channel)) {
                logger.info(String.format("Skipping channel %s on network %s due to blacklist.", channel, network));
                continue;
            }

            try {
                XContentBuilder builder = buildDocument(doc);
                if (builder != null) {
                    bulkRequest.add(this.esClient.prepareIndex("channels", "channel").setSource(builder));
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
        logger.info("Submitting BR for {} channels", bulkRequest.numberOfActions());
        processBulkRequest(bulkRequest);
    }

    private void processBulkRequest(BulkRequestBuilder bulkRequest) {
        if (bulkRequest.numberOfActions() == 0) {
            return;
        }

        BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            logger.error("Bulk response failure! (this is bad!) Message: {}", bulkResponse.getItems()[0].getFailureMessage());
            throw new ImportException(bulkResponse.getItems()[0].getFailureMessage());
        } else {
            logger.info("{} channels were processed by the bulk request.", bulkRequest.numberOfActions());
        }
    }

    private XContentBuilder buildDocument(Document doc) throws IOException {
        // The fun part about storing these two dates is that it means we
        // ideally need to reindex the channel object every time someone
        // joins/parts/quits.

        Date lastValidContentAt = doc.getDate("last_valid_content_at");
        Date lastActivity = doc.getDate("last_activity");
        if (lastActivity == null || lastValidContentAt == null) {
            logger.warn("Refusing to index channel ({} on {}) with no activity.", doc.get("channel"), doc.get("network"));
            return null;
        }
        String channel = doc.getString("channel");
        String network = doc.getString("network");
        String mongoId = doc.get("_id").toString();
        return XContentFactory.jsonBuilder()
                .startObject()
                    .field("mongoId", mongoId)
                    .field("channel", channel)
                    .field("network", network)
                    .field("last_valid_content_at", lastValidContentAt.getTime())<commit_msg>Update MongoToElastic to fill in new field, TODO: make Felix use ms since epoch instead of s<commit_after>package io.korobi.mongotoelastic.processor;

import com.mongodb.client.MongoCursor;
import io.korobi.mongotoelastic.exception.ImportException;
import io.korobi.mongotoelastic.logging.InjectLogger;
import io.korobi.mongotoelastic.mongo.IChannelBlacklist;
import org.apache.logging.log4j.Logger;
import org.bson.Document;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import javax.inject.Inject;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

public class ChannelDocumentProcessor implements IDocumentProcessor {

    @InjectLogger
    private Logger logger;
    private final Client esClient;
    private final IChannelBlacklist blacklist;

    @Inject
    public ChannelDocumentProcessor(Client esClient, IChannelBlacklist blacklist) {
        this.esClient = esClient;
        this.blacklist = blacklist;
    }

    @Override
    public void run(MongoCursor<Document> documents, int bulkSize) {
        throw new UnsupportedOperationException("No support for multithreaded channel import");
    }

    @Override
    public void run(MongoCursor<Document> documents) {
        // We don't have enough channels to necessitate processing in a multithreaded manner
        BulkRequestBuilder bulkRequest = this.esClient.prepareBulk();
        while (documents.hasNext()) {
            Document doc = documents.next();
            String network = doc.getString("network");
            String channel = doc.getString("channel");
            if (this.blacklist.isBlacklisted(network, channel)) {
                logger.info(String.format("Skipping channel %s on network %s due to blacklist.", channel, network));
                continue;
            }

            try {
                XContentBuilder builder = buildDocument(doc);
                if (builder != null) {
                    bulkRequest.add(this.esClient.prepareIndex("channels", "channel").setSource(builder));
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
        logger.info("Submitting BR for {} channels", bulkRequest.numberOfActions());
        processBulkRequest(bulkRequest);
    }

    private void processBulkRequest(BulkRequestBuilder bulkRequest) {
        if (bulkRequest.numberOfActions() == 0) {
            return;
        }

        BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            logger.error("Bulk response failure! (this is bad!) Message: {}", bulkResponse.getItems()[0].getFailureMessage());
            throw new ImportException(bulkResponse.getItems()[0].getFailureMessage());
        } else {
            logger.info("{} channels were processed by the bulk request.", bulkRequest.numberOfActions());
        }
    }

    private XContentBuilder buildDocument(Document doc) throws IOException {
        // The fun part about storing these two dates is that it means we
        // ideally need to reindex the channel object every time someone
        // joins/parts/quits.

        Date lastValidContentAt = doc.getDate("last_valid_content_at");
        Date lastActivity = doc.getDate("last_activity");
        if (lastActivity == null || lastValidContentAt == null) {
            logger.warn("Refusing to index channel ({} on {}) with no activity.", doc.get("channel"), doc.get("network"));
            return null;
        }
        String channel = doc.getString("channel");
        String network = doc.getString("network");
        String mongoId = doc.get("_id").toString();
        return XContentFactory.jsonBuilder()
                .startObject()
                    .field("mongoId", mongoId)
                    .field("channel", channel)
                    .field("network", network)
                    .field("added_at", doc.getObjectId("_id").getDate().getTime())
                    .field("last_valid_content_at", lastValidContentAt.getTime())
{'fraction_non_alphanumeric': 0.10159220532319392, 'fraction_numerical': 0.0009505703422053232, 'mean_word_length': 2.820699046754426, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '832907', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2133, 'n_words': 513}