<commit_before>package com.softwaremill.codebrag.finders.browsingcontext

import com.softwaremill.codebrag.dao.browsingcontext.UserBrowsingContextDAO
import com.softwaremill.codebrag.cache.RepositoriesCache
import com.softwaremill.codebrag.domain.UserBrowsingContext
import org.bson.types.ObjectId
import com.typesafe.scalalogging.slf4j.Logging

class UserBrowsingContextFinder(val userBrowsingContextDao: UserBrowsingContextDAO, val repositoriesCache: RepositoriesCache) extends Logging {

  def findUserDefaultContext(userId: ObjectId) = {
    logger.debug(s"Searching for default browsing context for user $userId")
    val context = userBrowsingContextDao.findDefault(userId)
      .filter(context => repositoriesCache.hasRepo(context.repoName))
      .getOrElse(findSystemDefaultContext(userId))
    logger.debug(s"User context is $context")
    context
  }

  private def findSystemDefaultContext(userId: ObjectId) = {
    logger.debug(s"Default context not found, getting system default")
    val repoName = repositoriesCache.repoNames.head
    val branchName = repositoriesCache.getRepo(repoName).getCheckedOutBranchShortName
    logger.debug(s"Building system default context from $repoName and $branchName")
    UserBrowsingContext(userId, repoName, branchName)
  }
}
<commit_msg>Find user browsing context for given repo or fallback to currently checked out branch for this repo if not found.<commit_after>package com.softwaremill.codebrag.finders.browsingcontext

import com.softwaremill.codebrag.dao.browsingcontext.UserBrowsingContextDAO
import com.softwaremill.codebrag.cache.RepositoriesCache
import com.softwaremill.codebrag.domain.UserBrowsingContext
import org.bson.types.ObjectId
import com.typesafe.scalalogging.slf4j.Logging

class UserBrowsingContextFinder(val userBrowsingContextDao: UserBrowsingContextDAO, val repositoriesCache: RepositoriesCache) extends Logging {

  def find(userId: ObjectId, repoName: String): Option[UserBrowsingContext] = {
    userBrowsingContextDao.find(userId, repoName) match {
      case Some(context) => Some(context)
      case None => Some(UserBrowsingContext(userId, repoName, repositoriesCache.getCheckedOutBranchShortName(repoName)))
    }
  }

  def findUserDefaultContext(userId: ObjectId) = {
    logger.debug(s"Searching for default browsing context for user $userId")
    val context = userBrowsingContextDao.findDefault(userId)
      .filter(context => repositoriesCache.hasRepo(context.repoName))
      .getOrElse(findSystemDefaultContext(userId))
    logger.debug(s"User context is $context")
    context
  }

  private def findSystemDefaultContext(userId: ObjectId) = {
    logger.debug(s"Default context not found, getting system default")
    val repoName = repositoriesCache.repoNames.head
    val branchName = repositoriesCache.getRepo(repoName).getCheckedOutBranchShortName
    logger.debug(s"Building system default context from $repoName and $branchName")
    UserBrowsingContext(userId, repoName, branchName)
  }
}

{'fraction_non_alphanumeric': 0.07921122994652406, 'fraction_numerical': 0.0006684491978609625, 'mean_word_length': 5.9282407407407405, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5716695', 'n_tokens_mistral': 889, 'n_tokens_neox': 875, 'n_words': 178}