<commit_before>package selfservice.cache;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.commons.lang3.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import selfservice.dao.LmngIdentifierDao;
import selfservice.domain.IdentityProvider;
import selfservice.domain.License;
import selfservice.domain.Service;
import selfservice.domain.csa.Article;
import selfservice.domain.csa.MappingEntry;
import selfservice.service.CrmService;

@Component
public class CrmCache extends AbstractCache {

  private static final Logger LOG = LoggerFactory.getLogger(CrmCache.class);

  private final CrmService crmService;
  private final LmngIdentifierDao lmngIdentifierDao;

  /**
   * Cache of Licenses, keyed by Entry of Idp institutionId and spEntityId
   */
  private AtomicReference<Map<MappingEntry, License>> licenseCache = new AtomicReference<>(new HashMap<>());

  /**
   * Cache of Articles, keyed by spEntityId
   */
  private AtomicReference<Map<String, Article>> articleCache = new AtomicReference<>(new HashMap<>());

  private List<MappingEntry> idpToLmngId;
  private List<MappingEntry> spToLmngId;

  @Autowired
  public CrmCache(CrmService crmService, LmngIdentifierDao lmngIdentifierDao,
                  @Value("${cache.crm.initialDelay}") long initialDelay,
                  @Value("${cache.crm.delay}") long delay) {
    super(initialDelay, delay);
    this.crmService = crmService;
    this.lmngIdentifierDao = lmngIdentifierDao;
  }

  @Override
  protected synchronized void doPopulateCache() {
    populateMappings();
    licenseCache.set(createNewLicensesCache());
    articleCache.set(createNewArticleCache());
  }

  private void populateMappings() {
    idpToLmngId = lmngIdentifierDao.findAllIdentityProviders();
    spToLmngId = lmngIdentifierDao.findAllServiceProviders();
  }

  private Map<String, Article> createNewArticleCache() {
    Map<String, Article> newCache = new HashMap<>();

    for (MappingEntry spAndLmngId : spToLmngId) {
      String spEntityId = spAndLmngId.getKey();
      String lmngId = spAndLmngId.getValue();

      List<Article> articlesForServiceProviders = crmService.getArticlesForServiceProviders(Collections.singletonList(spEntityId));

      if (articlesForServiceProviders.size() > 1) {
        LOG.info("Unexpected: list of articles for SP ({}) is larger than 1: {}", spEntityId, articlesForServiceProviders);
        articlesForServiceProviders.forEach(a -> LOG.info("Article found: {}", a));
      }

      if (articlesForServiceProviders.size() >= 1) {
        newCache.put(spEntityId, articlesForServiceProviders.get(0));
      } else {
        LOG.info("No article found for SP {}, with lmng id: {}", spEntityId, lmngId);
      }
    }
    return newCache;
  }

  private Map<MappingEntry, License> createNewLicensesCache() {
    Map<MappingEntry, License> newLicenseCache = new HashMap<>();

    for (MappingEntry idpLmngEntry : idpToLmngId) {
      String idpInstitutionId = idpLmngEntry.getKey();
      for (MappingEntry spLmngEntry : spToLmngId) {
        String spEntityId = spLmngEntry.getKey();
        String spLmngId = spLmngEntry.getValue();

        IdentityProvider idp = new IdentityProvider(idpInstitutionId, idpInstitutionId, "dummy");

        List<License> licensesForIdpAndSp = crmService.getLicensesForIdpAndSp(idp, spLmngId);
        if (licensesForIdpAndSp.isEmpty()) {
          continue;
        }

        if (licensesForIdpAndSp.size() > 1) {
          LOG.warn("Unexpected: list of licenses by IdP and SP ({} and {}) is larger than 1: {}", idpInstitutionId, spEntityId, licensesForIdpAndSp.size());
        }
        License license = licensesForIdpAndSp.get(0);
        LOG.trace("License found by IdP and SP ({} and {}): {}", idpInstitutionId, spEntityId, license);
        newLicenseCache.put(new MappingEntry(idpInstitutionId, spEntityId), license);<commit_msg>Reduce log level to info about more than 1 license for sp and idp<commit_after>package selfservice.cache;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.commons.lang3.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import selfservice.dao.LmngIdentifierDao;
import selfservice.domain.IdentityProvider;
import selfservice.domain.License;
import selfservice.domain.Service;
import selfservice.domain.csa.Article;
import selfservice.domain.csa.MappingEntry;
import selfservice.service.CrmService;

@Component
public class CrmCache extends AbstractCache {

  private static final Logger LOG = LoggerFactory.getLogger(CrmCache.class);

  private final CrmService crmService;
  private final LmngIdentifierDao lmngIdentifierDao;

  /**
   * Cache of Licenses, keyed by Entry of Idp institutionId and spEntityId
   */
  private AtomicReference<Map<MappingEntry, License>> licenseCache = new AtomicReference<>(new HashMap<>());

  /**
   * Cache of Articles, keyed by spEntityId
   */
  private AtomicReference<Map<String, Article>> articleCache = new AtomicReference<>(new HashMap<>());

  private List<MappingEntry> idpToLmngId;
  private List<MappingEntry> spToLmngId;

  @Autowired
  public CrmCache(CrmService crmService, LmngIdentifierDao lmngIdentifierDao,
                  @Value("${cache.crm.initialDelay}") long initialDelay,
                  @Value("${cache.crm.delay}") long delay) {
    super(initialDelay, delay);
    this.crmService = crmService;
    this.lmngIdentifierDao = lmngIdentifierDao;
  }

  @Override
  protected synchronized void doPopulateCache() {
    populateMappings();
    licenseCache.set(createNewLicensesCache());
    articleCache.set(createNewArticleCache());
  }

  private void populateMappings() {
    idpToLmngId = lmngIdentifierDao.findAllIdentityProviders();
    spToLmngId = lmngIdentifierDao.findAllServiceProviders();
  }

  private Map<String, Article> createNewArticleCache() {
    Map<String, Article> newCache = new HashMap<>();

    for (MappingEntry spAndLmngId : spToLmngId) {
      String spEntityId = spAndLmngId.getKey();
      String lmngId = spAndLmngId.getValue();

      List<Article> articlesForServiceProviders = crmService.getArticlesForServiceProviders(Collections.singletonList(spEntityId));

      if (articlesForServiceProviders.size() > 1) {
        LOG.info("Unexpected: list of articles for SP ({}) is larger than 1: {}", spEntityId, articlesForServiceProviders);
        articlesForServiceProviders.forEach(a -> LOG.info("Article found: {}", a));
      }

      if (articlesForServiceProviders.size() >= 1) {
        newCache.put(spEntityId, articlesForServiceProviders.get(0));
      } else {
        LOG.info("No article found for SP {}, with lmng id: {}", spEntityId, lmngId);
      }
    }
    return newCache;
  }

  private Map<MappingEntry, License> createNewLicensesCache() {
    Map<MappingEntry, License> newLicenseCache = new HashMap<>();

    for (MappingEntry idpLmngEntry : idpToLmngId) {
      String idpInstitutionId = idpLmngEntry.getKey();
      for (MappingEntry spLmngEntry : spToLmngId) {
        String spEntityId = spLmngEntry.getKey();
        String spLmngId = spLmngEntry.getValue();

        IdentityProvider idp = new IdentityProvider(idpInstitutionId, idpInstitutionId, "dummy");

        List<License> licensesForIdpAndSp = crmService.getLicensesForIdpAndSp(idp, spLmngId);
        if (licensesForIdpAndSp.isEmpty()) {
          continue;
        }

        if (licensesForIdpAndSp.size() > 1) {
          LOG.info("Unexpected: list of licenses by IdP and SP ({} and {}) is larger than 1: {}", idpInstitutionId, spEntityId, licensesForIdpAndSp.size());
        }

        License license = licensesForIdpAndSp.get(0);
        newLicenseCache.put(new MappingEntry(idpInstitutionId, spEntityId), license);
{'fraction_non_alphanumeric': 0.10416666666666667, 'fraction_numerical': 0.002514367816091954, 'mean_word_length': 4.330567964262923, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 47, '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': '6199661', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2511, 'n_words': 530}