<commit_before>/**
 *
 */
package org.devgateway.ocds.web.rest.controller;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.ArrayUtils;
import org.devgateway.ocds.persistence.mongo.Award;
import org.devgateway.ocds.persistence.mongo.Tender;
import org.devgateway.ocds.persistence.mongo.constants.MongoConstants;
import org.devgateway.ocds.web.rest.controller.request.DefaultFilterPagingRequest;
import org.devgateway.ocds.web.rest.controller.request.GroupingFilterPagingRequest;
import org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest;
import org.devgateway.toolkit.persistence.mongo.aggregate.CustomSortingOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.Fields;
import org.springframework.data.mongodb.core.aggregation.GroupOperation;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.TextCriteria;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.devgateway.ocds.persistence.mongo.constants.MongoConstants.FieldNames.FLAGS_TOTAL_FLAGGED;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * @author mpostelnicu
 */
public abstract class GenericOCDSController {

    private static final int LAST_MONTH_ZERO = 11;
    public static final int BIGDECIMAL_SCALE = 15;
    public static final int DAY_MS = 86400000;

    public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);


    protected Map<String, Object> filterProjectMap;

    protected final Logger logger = LoggerFactory.getLogger(GenericOCDSController.class);

    @Autowired
    protected MongoTemplate mongoTemplate;

    /**
     * Gets the date of the first day of the year (01.01.year)
     *
     * @param year
     * @return
     */
    protected Date getStartDate(final int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.DAY_OF_YEAR, 1);
        Date start = cal.getTime();
        return start;
    }

    protected List<DBObject> releaseAgg(Aggregation agg) {
        return releaseAgg(agg, DBObject.class);
    }

    protected <Z> List<Z> releaseAgg(Aggregation agg, Class<Z> clazz) {
        return mongoTemplate.aggregate(agg, "release", clazz).getMappedResults();
    }

    /**
     * Creates a mongo $cond that calculates percentage of expression1/expression2.
     * Checks for division by zero.
     *
     * @param expression1
     * @param expression2
     * @return
     */
    protected DBObject getPercentageMongoOp(String expression1, String expression2) {
        return new BasicDBObject(
                "$cond",
                Arrays.asList(new BasicDBObject("$eq", Arrays.asList(
                        ref(expression2),
                        0
                        )), new BasicDBObject("$literal", 0),
                        new BasicDBObject(
                                "$multiply",
                                Arrays.asList(new BasicDBObject(
                                        "$divide",
                                        Arrays.asList(
                                                ref(expression1),
                                                ref(expression2)
                                        )
                                ), 100)
                        )
                )
        );<commit_msg>OCE-453 better control of releaseAgg method<commit_after>/**
 *
 */
package org.devgateway.ocds.web.rest.controller;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.ArrayUtils;
import org.devgateway.ocds.persistence.mongo.Award;
import org.devgateway.ocds.persistence.mongo.Tender;
import org.devgateway.ocds.persistence.mongo.constants.MongoConstants;
import org.devgateway.ocds.web.rest.controller.request.DefaultFilterPagingRequest;
import org.devgateway.ocds.web.rest.controller.request.GroupingFilterPagingRequest;
import org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest;
import org.devgateway.toolkit.persistence.mongo.aggregate.CustomSortingOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
import org.springframework.data.mongodb.core.aggregation.Fields;
import org.springframework.data.mongodb.core.aggregation.GroupOperation;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.TextCriteria;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.devgateway.ocds.persistence.mongo.constants.MongoConstants.FieldNames.FLAGS_TOTAL_FLAGGED;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * @author mpostelnicu
 */
public abstract class GenericOCDSController {

    private static final int LAST_MONTH_ZERO = 11;
    public static final int BIGDECIMAL_SCALE = 15;
    public static final int DAY_MS = 86400000;

    public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);


    protected Map<String, Object> filterProjectMap;

    protected final Logger logger = LoggerFactory.getLogger(GenericOCDSController.class);

    @Autowired
    protected MongoTemplate mongoTemplate;

    /**
     * Gets the date of the first day of the year (01.01.year)
     *
     * @param year
     * @return
     */
    protected Date getStartDate(final int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.DAY_OF_YEAR, 1);
        Date start = cal.getTime();
        return start;
    }

    protected <Z> List<Z> releaseAgg(Aggregation agg, AggregationOptions options, Class<Z> clazz) {
        return mongoTemplate.aggregate(agg.withOptions(options), "release", clazz)
                .getMappedResults();
    }

    protected List<DBObject> releaseAgg(Aggregation agg) {
        return releaseAgg(agg, DBObject.class);
    }

    protected List<DBObject> releaseAgg(Aggregation agg, AggregationOptions options) {
        return releaseAgg(agg, options, DBObject.class);
    }

    protected <Z> List<Z> releaseAgg(Aggregation agg, Class<Z> clazz) {
        return releaseAgg(agg, Aggregation.newAggregationOptions().allowDiskUse(true).build(), clazz);
    }

    /**
     * Creates a mongo $cond that calculates percentage of expression1/expression2.
     * Checks for division by zero.
     *
     * @param expression1
     * @param expression2
     * @return
     */
    protected DBObject getPercentageMongoOp(String expression1, String expression2) {
        return new BasicDBObject(
                "$cond",
                Arrays.asList(new BasicDBObject("$eq", Arrays.asList(
                        ref(expression2),
                        0
                        )), new BasicDBObject("$literal", 0),
                        new BasicDBObject(
                                "$multiply",
                                Arrays.asList(new BasicDBObject(
                                        "$divide",
                                        Arrays.asList(
                                                ref(expression1),
                                                ref(expression2)
                                        )
                                ), 100)
                        )
                )
        );
{'fraction_non_alphanumeric': 0.09271948608137046, 'fraction_numerical': 0.008244111349036402, 'mean_word_length': 2.8345648604269296, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5399920', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2532, 'n_words': 483}