<commit_before>package org.scalawiki.wlx.stat

import org.scalawiki.MwBot
import org.scalawiki.dto.Image
import org.scalawiki.wlx.dto.Contest
import org.scalawiki.wlx.{ImageDB, MonumentDB}

import scala.concurrent.ExecutionContext

object Output {

  def monumentsByType(/*imageDbs: Seq[ImageDB], totalImageDb: ImageDB,*/ monumentDb: MonumentDB) = {
    val regions = monumentDb.contest.country.regionById

    for ((typ, size) <- monumentDb._byType.mapValues(_.size).toSeq.sortBy(-_._2)) {
      val byRegion = monumentDb._byTypeAndRegion(typ)

      val regionStat = byRegion.toSeq.sortBy(-_._2.size).map {
        case (regionId, monuments) =>
          val byReg1 = s"${regions(regionId)}: ${monuments.size}"

          val byReg2 = if (byRegion.size == 1) {
            val byReg2Stat = monuments.groupBy(m => m.id.substring(0, 6))

            byReg2Stat.toSeq.sortBy(-_._2.size).map {
              case (regionId2, monuments2) =>
                s"$regionId2: ${monuments2.size}"
            }.mkString("(", ", ", ")")
          } else ""

          byReg1 + byReg2
      }.mkString(", ")
      println(s"$typ: ${monumentDb._byType(typ).size}, $regionStat")
    }
  }

  def galleryByRegionAndId(monumentDb: MonumentDB, authorImageDb: ImageDB, oldImageDb: ImageDB, rater: Rater): String = {
    val contest = monumentDb.contest
    val country = contest.country
    val regionIds = country.regionIds.filter(id => authorImageDb.idsByRegion(id).nonEmpty)
    val author = authorImageDb.authors.head

    val groupedTotal = authorImageDb.ids.map { id =>
      val rateParts = rater.asInstanceOf[RateSum].raters.map(_.rate(id, author))
      val rateId = s"${rater.rate(id, author)} || " + rateParts.mkString(" || ")
      (id, rater.rate(id, author), rateId)
    }.groupBy { case (_, rate, rateId) =>
      (rate, rateId)
    }.mapValues(_.map(_._1)).toSeq.sortBy(-_._1._1)

    val tableTotal = "\n== Summary ==\n{| class=\"wikitable\"\n! rate !! base !! authors <br> bonus !! images <br> bonus !! objects !! ids \n|-\n" +
      groupedTotal.map {
        case ((rate, rateId), ids) =>
          s"| $rateId || ${ids.size} || ${ids.toSeq.sorted.mkString(", ")}"
      }.mkString("\n|-\n") + "\n|}\n"

    tableTotal + regionIds.map {<commit_msg>fix test<commit_after>package org.scalawiki.wlx.stat

import org.scalawiki.MwBot
import org.scalawiki.dto.Image
import org.scalawiki.wlx.dto.Contest
import org.scalawiki.wlx.{ImageDB, MonumentDB}

import scala.concurrent.ExecutionContext

object Output {

  def monumentsByType(/*imageDbs: Seq[ImageDB], totalImageDb: ImageDB,*/ monumentDb: MonumentDB) = {
    val regions = monumentDb.contest.country.regionById

    for ((typ, size) <- monumentDb._byType.mapValues(_.size).toSeq.sortBy(-_._2)) {
      val byRegion = monumentDb._byTypeAndRegion(typ)

      val regionStat = byRegion.toSeq.sortBy(-_._2.size).map {
        case (regionId, monuments) =>
          val byReg1 = s"${regions(regionId)}: ${monuments.size}"

          val byReg2 = if (byRegion.size == 1) {
            val byReg2Stat = monuments.groupBy(m => m.id.substring(0, 6))

            byReg2Stat.toSeq.sortBy(-_._2.size).map {
              case (regionId2, monuments2) =>
                s"$regionId2: ${monuments2.size}"
            }.mkString("(", ", ", ")")
          } else ""

          byReg1 + byReg2
      }.mkString(", ")
      println(s"$typ: ${monumentDb._byType(typ).size}, $regionStat")
    }
  }

  def galleryByRegionAndId(monumentDb: MonumentDB, authorImageDb: ImageDB, oldImageDb: ImageDB, rater: Rater): String = {
    val contest = monumentDb.contest
    val country = contest.country
    val regionIds = country.regionIds.filter(id => authorImageDb.idsByRegion(id).nonEmpty)
    val author = authorImageDb.authors.head

    val tableTotal = rater match {
      case rateSum: RateSum =>
        val groupedTotal = authorImageDb.ids.map { id =>
          val rateParts = rateSum.raters.map(_.rate(id, author))
          val rateId = s"${rater.rate(id, author)} || " + rateParts.mkString(" || ")
          (id, rater.rate(id, author), rateId)
        }.groupBy { case (_, rate, rateId) =>
          (rate, rateId)
        }.mapValues(_.map(_._1)).toSeq.sortBy(-_._1._1)

        "\n== Summary ==\n{| class=\"wikitable\"\n! rate !! base !! authors <br> bonus !! images <br> bonus !! objects !! ids \n|-\n" +
          groupedTotal.map {
            case ((rate, rateId), ids) =>
              s"| $rateId || ${ids.size} || ${ids.toSeq.sorted.mkString(", ")}"
          }.mkString("\n|-\n") + "\n|}\n"
      case _ => ""
    }

    tableTotal + regionIds.map {
{'fraction_non_alphanumeric': 0.16220917590780604, 'fraction_numerical': 0.008262665796912372, 'mean_word_length': 2.8174273858921164, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, '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': '15289', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1542, 'n_words': 293}