<commit_before>/*
 * Copyright 2017 <NAME>, S.A..
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bbva.arq.devops.ae.mirrorgate.repository;

import static org.springframework.data.domain.Sort.Direction.DESC;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

import com.bbva.arq.devops.ae.mirrorgate.core.dto.ApplicationDTO;
import com.bbva.arq.devops.ae.mirrorgate.core.dto.ApplicationReviewsDTO;
import com.bbva.arq.devops.ae.mirrorgate.model.Review;
import com.mongodb.BasicDBObject;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;

/**
 * Reviews Repository
 */
public class ReviewRepositoryImpl implements ReviewRepositoryCustom {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<ApplicationDTO> getAverageRateByAppNames(List<String> names) {

        Aggregation aggregation = newAggregation(
            match(Criteria.where("appname").in(names)),
            sort(new Sort(DESC, "timestamp")),
            project("appname", "platform", "starrating",
                        "amount", "timestamp", "comment", "authorName")
                .and("starrating").multiply("amount").as("starrating_accumulated"),
            group("appname", "platform")
                .sum("amount").as("total_amount")
                .sum("starrating_accumulated").as("total_starrating")
                .push(new BasicDBObject("author", "$authorName")
                    .append("rate", "$starrating" )
                    .append("timestamp", "$timestamp")
                    .append("comment", "$comment")
                ).as("reviews"),
            project("appname", "platform")
                .and("total_starrating").divide("total_amount").as("rate")
                .and("reviews").slice(3, 0)
        );

        //Convert the aggregation result into a List
        AggregationResults<ApplicationDTO> groupResults
                = mongoTemplate.aggregate(aggregation, Review.class, ApplicationDTO.class);<commit_msg>refactor(reviews): ensure lack of amount property compatibilty to get average<commit_after>/*
 * Copyright 2017 <NAME>entaria, S.A..
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bbva.arq.devops.ae.mirrorgate.repository;

import static org.springframework.data.domain.Sort.Direction.DESC;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

import com.bbva.arq.devops.ae.mirrorgate.core.dto.ApplicationDTO;
import com.bbva.arq.devops.ae.mirrorgate.core.dto.ApplicationReviewsDTO;
import com.bbva.arq.devops.ae.mirrorgate.model.Review;
import com.mongodb.BasicDBObject;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.IfNull;
import org.springframework.data.mongodb.core.query.Criteria;

/**
 * Reviews Repository
 */
public class ReviewRepositoryImpl implements ReviewRepositoryCustom {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<ApplicationDTO> getAverageRateByAppNames(List<String> names) {

        Aggregation aggregation = newAggregation(
            match(Criteria.where("appname").in(names)),
            sort(new Sort(DESC, "timestamp")),
            project("appname", "platform", "starrating",
                        "timestamp", "comment", "authorName")
                .and("amount").applyCondition(IfNull.ifNull("amount").then(1))
                .and("starrating").multiply(IfNull.ifNull("amount").then(1)).as("starrating_accumulated"),
            group("appname", "platform")
                .sum("amount").as("total_amount")
                .sum("starrating_accumulated").as("total_starrating")
                .push(new BasicDBObject("author", "$authorName")
                    .append("rate", "$starrating" )
                    .append("timestamp", "$timestamp")
                    .append("comment", "$comment")
                ).as("reviews"),
            project("appname", "platform")
                .and("total_starrating").divide("total_amount").as("rate")
                .and("reviews").slice(3, 0)
        );

        //Convert the aggregation result into a List
        AggregationResults<ApplicationDTO> groupResults
                = mongoTemplate.aggregate(aggregation, Review.class, ApplicationDTO.class);
{'fraction_non_alphanumeric': 0.11745022586581898, 'fraction_numerical': 0.00368077630918521, 'mean_word_length': 3.353969410050983, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2659307', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1604, 'n_words': 360}