<commit_before>/*
 * Copyright 2016 EPAM Systems
 * 
 * 
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/commons-dao
 * 
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.epam.ta.reportportal.database.dao;

import com.epam.ta.reportportal.commons.DbUtils;
import com.epam.ta.reportportal.commons.MoreCollectors;
import com.epam.ta.reportportal.database.entity.*;
import com.epam.ta.reportportal.database.entity.history.status.FlakyHistory;
import com.epam.ta.reportportal.database.entity.history.status.MostFailedHistory;
import com.epam.ta.reportportal.database.entity.item.TestItem;
import com.epam.ta.reportportal.database.entity.item.TestItemType;
import com.epam.ta.reportportal.database.entity.item.issue.TestItemIssue;
import com.epam.ta.reportportal.database.entity.item.issue.TestItemIssueType;
import com.epam.ta.reportportal.database.entity.statistics.StatisticSubType;
import com.epam.ta.reportportal.database.search.ModifiableQueryBuilder;
import com.mongodb.BasicDBObject;
import org.apache.commons.collections.CollectionUtils;
import org.bson.types.ObjectId;
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.Fields;
import org.springframework.data.mongodb.core.aggregation.GroupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.time.Duration;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.StreamSupport;

import static com.epam.ta.reportportal.database.dao.aggregation.AddFieldsOperation.addFields;
import static com.epam.ta.reportportal.database.search.UpdateStatisticsQueryBuilder.*;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.data.domain.Sort.Direction.DESC;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

/**
 * Implementation of Test Items repository routines
 *
 * @author <NAME>
 * @author Andrei_Ramanchuk
 */
public class TestItemRepositoryCustomImpl implements TestItemRepositoryCustom {

	private static final String ID_REFERENCE = "id";
	private static final String ID = "_id";
	private static final String LAUNCH_REFERENCE = "launchRef";
	private static final String ITEM_REFERENCE = "testItemRef";
	private static final String ISSUE_TYPE = "issue.issueType";
	private static final String ISSUE_TICKET = "issue.externalSystemIssues";
	private static final String ISSUE_DESCRIPTION = "issue.issueDescription";
	private static final String ISSUE = "issue";
	private static final String HAS_CHILD = "has_childs";
	private static final String START_TIME = "start_time";
	private static final String TYPE = "type";
	private static final String NAME = "name";
	private static final String STATUS = "status";
	private static final String PARENT = "parent";
	private static final String UNIQUE_ID = "uniqueId";
	private static final String TOTAL = "total";
	private static final String FAILED = "failed";

	public static final int HISTORY_LIMIT = 2000;

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void updateExecutionStatistics(TestItem item) {
		mongoTemplate.updateMulti(getItemQuery(item), fromItemStatusAware(item.getStatus(), 1, 1), TestItem.class);
	}

	@Override
	public void updateIssueStatistics(TestItem item, Project.Configuration settings) {
		mongoTemplate.updateMulti(getItemQuery(item),
				fromIssueTypeAware(settings.getByLocator(item.getIssue().getIssueType()), 1),
				TestItem.class
		);
	}

	@Override
	public void dropIssueStatisticsType(String id, StatisticSubType type) {
		mongoTemplate.updateMulti(query(where(ID_REFERENCE).is(id)), dropIssueTypeAware(type), TestItem.class);
	}

	@Override
	public void updateHasChilds(String id, boolean hasChildren) {
		mongoTemplate.updateFirst(query(where(ID_REFERENCE).is(id)), Update.update("has_childs", hasChildren), TestItem.class);
	}

	@Override
	public void updateItemsIssues(Map<String, TestItemIssue> forUpdate) {
		Query query = query(where(ID).in(forUpdate.keySet()));
		Update update = new Update();
		mongoTemplate.stream(query, TestItem.class).forEachRemaining(dbo -> {
			String currentId = dbo.getId();
			TestItemIssue newValue = forUpdate.get(currentId);
			update.set(ISSUE_TYPE, newValue.getIssueType());
			update.set(ISSUE_DESCRIPTION, newValue.getIssueDescription());
			update.set(ISSUE_TICKET, newValue.getExternalSystemIssues());
			mongoTemplate.updateFirst(Query.query(Criteria.where(ID).is(currentId)),
					update,
					mongoTemplate.getCollectionName(TestItem.class)
			);
		});
	}

	@Override
	public boolean hasDescendants(Object... id) {
		return mongoTemplate.count(getItemDescendantsQuery(id), TestItem.class) > 0;
	}

	@Override<commit_msg>add autoAnalyzed mark on item updating<commit_after>/*
 * Copyright 2016 EPAM Systems
 * 
 * 
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/commons-dao
 * 
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.epam.ta.reportportal.database.dao;

import com.epam.ta.reportportal.commons.DbUtils;
import com.epam.ta.reportportal.commons.MoreCollectors;
import com.epam.ta.reportportal.database.entity.*;
import com.epam.ta.reportportal.database.entity.history.status.FlakyHistory;
import com.epam.ta.reportportal.database.entity.history.status.MostFailedHistory;
import com.epam.ta.reportportal.database.entity.item.TestItem;
import com.epam.ta.reportportal.database.entity.item.TestItemType;
import com.epam.ta.reportportal.database.entity.item.issue.TestItemIssue;
import com.epam.ta.reportportal.database.entity.item.issue.TestItemIssueType;
import com.epam.ta.reportportal.database.entity.statistics.StatisticSubType;
import com.epam.ta.reportportal.database.search.ModifiableQueryBuilder;
import com.mongodb.BasicDBObject;
import org.apache.commons.collections.CollectionUtils;
import org.bson.types.ObjectId;
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.Fields;
import org.springframework.data.mongodb.core.aggregation.GroupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.time.Duration;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.StreamSupport;

import static com.epam.ta.reportportal.database.dao.aggregation.AddFieldsOperation.addFields;
import static com.epam.ta.reportportal.database.search.UpdateStatisticsQueryBuilder.*;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.data.domain.Sort.Direction.DESC;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

/**
 * Implementation of Test Items repository routines
 *
 * @author <NAME>
 * @author Andrei_Ramanchuk
 */
public class TestItemRepositoryCustomImpl implements TestItemRepositoryCustom {

	private static final String ID_REFERENCE = "id";
	private static final String ID = "_id";
	private static final String LAUNCH_REFERENCE = "launchRef";
	private static final String ITEM_REFERENCE = "testItemRef";
	private static final String ISSUE_TYPE = "issue.issueType";
	private static final String ISSUE_ANALYZED = "issue.autoAnalyzed";
	private static final String ISSUE_TICKET = "issue.externalSystemIssues";
	private static final String ISSUE_DESCRIPTION = "issue.issueDescription";
	private static final String ISSUE = "issue";
	private static final String HAS_CHILD = "has_childs";
	private static final String START_TIME = "start_time";
	private static final String TYPE = "type";
	private static final String NAME = "name";
	private static final String STATUS = "status";
	private static final String PARENT = "parent";
	private static final String UNIQUE_ID = "uniqueId";
	private static final String TOTAL = "total";
	private static final String FAILED = "failed";

	public static final int HISTORY_LIMIT = 2000;

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void updateExecutionStatistics(TestItem item) {
		mongoTemplate.updateMulti(getItemQuery(item), fromItemStatusAware(item.getStatus(), 1, 1), TestItem.class);
	}

	@Override
	public void updateIssueStatistics(TestItem item, Project.Configuration settings) {
		mongoTemplate.updateMulti(getItemQuery(item),
				fromIssueTypeAware(settings.getByLocator(item.getIssue().getIssueType()), 1),
				TestItem.class
		);
	}

	@Override
	public void dropIssueStatisticsType(String id, StatisticSubType type) {
		mongoTemplate.updateMulti(query(where(ID_REFERENCE).is(id)), dropIssueTypeAware(type), TestItem.class);
	}

	@Override
	public void updateHasChilds(String id, boolean hasChildren) {
		mongoTemplate.updateFirst(query(where(ID_REFERENCE).is(id)), Update.update("has_childs", hasChildren), TestItem.class);
	}

	@Override
	public void updateItemsIssues(Map<String, TestItemIssue> forUpdate) {
		Query query = query(where(ID).in(forUpdate.keySet()));
		Update update = new Update();
		mongoTemplate.stream(query, TestItem.class).forEachRemaining(dbo -> {
			String currentId = dbo.getId();
			TestItemIssue newValue = forUpdate.get(currentId);
			update.set(ISSUE_TYPE, newValue.getIssueType());
			update.set(ISSUE_DESCRIPTION, newValue.getIssueDescription());
			update.set(ISSUE_TICKET, newValue.getExternalSystemIssues());
			update.set(ISSUE_ANALYZED, newValue.isAutoAnalyzed());
			mongoTemplate.updateFirst(Query.query(Criteria.where(ID).is(currentId)),
					update,
					mongoTemplate.getCollectionName(TestItem.class)
			);
		});
	}

	@Override
	public boolean hasDescendants(Object... id) {
		return mongoTemplate.count(getItemDescendantsQuery(id), TestItem.class) > 0;
	}

	@Override
{'fraction_non_alphanumeric': 0.1007021891780256, 'fraction_numerical': 0.00214787277984304, 'mean_word_length': 5.688397790055249, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '7664331', 'n_tokens_mistral': 3693, 'n_tokens_neox': 3516, 'n_words': 826}