<commit_before>package controllers

import javax.inject.Inject

import models.db
import models.join._
import models.query.{Period, SnapshotSearch}
import models.view.{CItem, CShip}
import org.json4s._
import org.json4s.native.Serialization.write
import play.api.mvc._
import ranking.common.{Ranking, RankingType}
import scalikejdbc._
import util.{MFGDateUtil, Ymdh}

import scala.concurrent.ExecutionContext
import scala.util.Try

/**
 * Date: 14/06/11.
 */
class ViewSta @Inject()(implicit val ec: ExecutionContext) extends Controller {
  import controllers.Common._
  import ViewSta._

  def activities = actionAsync { Ok(views.html.sta.activities()) }

  def statistics(from: String, to: String) = actionAsync {
    val fromTo = Period.fromStr(from, to)
    val sCounts = db.CreateShip.materialCount(fromTo.where(sqls"cs.created")).take(50).takeWhile(_._2 > 1)
    val iCounts = db.CreateItem.materialCount(fromTo.whereOpt(sqls"ci.created")).take(50).takeWhile(_._2 > 1)
    Ok(views.html.sta.statistics(sCounts, iCounts, fromTo))
  }

  def cship(fuel: Int, ammo: Int, steel: Int, bauxite: Int, develop: Int, from: String, to: String) = actionAsync {
    val mat = Mat(fuel, ammo, steel, bauxite, develop)
    val fromTo = Period.fromStr(from, to)
    val cs = CShip(mat, fromTo)
    val counts = db.CreateShip.countByMatWithMaster(mat, fromTo.where(sqls"cs.created"))
    val graphJson = cshipGraphJson(counts, cs.title)
    val sum = counts.map(_._2).sum.toDouble
    val withRate = counts.map { case (ship, count) => (ship.name, count, count/sum) }
    val cships = db.CreateShip.findAllByMatWithName(mat, limit = 100)
    Ok(views.html.sta.cship(cs, graphJson, withRate, cships))
  }

  private def cshipGraphJson(counts: List[(db.MasterShipBase, Long)], title: String): String = {
    implicit val formats: Formats = DefaultFormats
    val sum = counts.map(_._2).sum.toDouble
    val sTypeName = db.MasterStype.findAll().map(ms => ms.id -> ms.name).toMap
    val sTypeCounts = counts.groupBy(it => sTypeName(it._1.stype)).mapValues(_.map(_._2).sum)
    val data = sTypeCounts.map { case (sname, sCount) =>
      val countByShip = counts.filter { case (ship, _) => sTypeName(ship.stype) == sname }
      val children = countByShip.map { case (ship, count) =>
        Map("name" -> s"${ship.name} $count(${toP(count/sum)}%)", "count" -> count)
      }
      Map("name" -> s"${sname} $sCount(${toP(sCount/sum)}%)", "children" -> children)
    }
    write(Map("name" -> title, "children" -> data))
  }

  def citem(fuel: Int, ammo: Int, steel: Int, bauxite: Int, sType: String, from: String, to: String) = actionAsync {
    val fromTo = Period.fromStr(from, to)
    val mat = ItemMat(fuel, ammo, steel, bauxite, sType)
    val citem = CItem(mat, fromTo)
    val ci = db.CreateItem.ci
    val mst = db.CreateItem.mst
    val citems = db.CreateItem.findAllByWithName(
      sqls.eq(ci.fuel, fuel).and.eq(ci.ammo, ammo).and.eq(ci.steel, steel).and.eq(ci.bauxite, bauxite).and.eq(mst.name, sType),
      limit = 100
    )
    val counts = db.CreateItem.countItemByMat(mat, fromTo.where(ci.created))
    val sum = counts.map(_._2).sum.toDouble
    val withRate = counts.map { case (item, count) => (item.name, count, count/sum) }
    val countJsonRaw = counts.map { case (item, count) =>
      val url = routes.ViewSta.fromShip().toString + s"#query=${item.name}"
      Map("label" -> item.name, "data" -> count, "url" -> url)
    }
    Ok(views.html.sta.citem(citem, write(countJsonRaw), withRate, citems))
  }

  def fromShip() = actionAsync { Ok(views.html.sta.from_ship()) }

  def dropStage() = actionAsync {
    val stages = db.BattleResult.countAllByStage()
    Ok(views.html.sta.drop_stage(stages))<commit_msg>Add cache to stage drop count<commit_after>package controllers

import javax.inject.Inject

import models.db
import models.join._
import models.query.{Period, SnapshotSearch}
import models.view.{CItem, CShip}
import org.json4s._
import org.json4s.native.Serialization.write
import play.api.mvc._
import ranking.common.{Ranking, RankingType}
import scalikejdbc._
import util.{MFGDateUtil, PeriodicalValue, Ymdh}

import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
import scala.util.Try

/**
 * Date: 14/06/11.
 */
class ViewSta @Inject()(implicit val ec: ExecutionContext) extends Controller {
  import ViewSta._
  import controllers.Common._

  def activities = actionAsync { Ok(views.html.sta.activities()) }

  def statistics(from: String, to: String) = actionAsync {
    val fromTo = Period.fromStr(from, to)
    val sCounts = db.CreateShip.materialCount(fromTo.where(sqls"cs.created")).take(50).takeWhile(_._2 > 1)
    val iCounts = db.CreateItem.materialCount(fromTo.whereOpt(sqls"ci.created")).take(50).takeWhile(_._2 > 1)
    Ok(views.html.sta.statistics(sCounts, iCounts, fromTo))
  }

  def cship(fuel: Int, ammo: Int, steel: Int, bauxite: Int, develop: Int, from: String, to: String) = actionAsync {
    val mat = Mat(fuel, ammo, steel, bauxite, develop)
    val fromTo = Period.fromStr(from, to)
    val cs = CShip(mat, fromTo)
    val counts = db.CreateShip.countByMatWithMaster(mat, fromTo.where(sqls"cs.created"))
    val graphJson = cshipGraphJson(counts, cs.title)
    val sum = counts.map(_._2).sum.toDouble
    val withRate = counts.map { case (ship, count) => (ship.name, count, count/sum) }
    val cships = db.CreateShip.findAllByMatWithName(mat, limit = 100)
    Ok(views.html.sta.cship(cs, graphJson, withRate, cships))
  }

  private def cshipGraphJson(counts: List[(db.MasterShipBase, Long)], title: String): String = {
    implicit val formats: Formats = DefaultFormats
    val sum = counts.map(_._2).sum.toDouble
    val sTypeName = db.MasterStype.findAll().map(ms => ms.id -> ms.name).toMap
    val sTypeCounts = counts.groupBy(it => sTypeName(it._1.stype)).mapValues(_.map(_._2).sum)
    val data = sTypeCounts.map { case (sname, sCount) =>
      val countByShip = counts.filter { case (ship, _) => sTypeName(ship.stype) == sname }
      val children = countByShip.map { case (ship, count) =>
        Map("name" -> s"${ship.name} $count(${toP(count/sum)}%)", "count" -> count)
      }
      Map("name" -> s"${sname} $sCount(${toP(sCount/sum)}%)", "children" -> children)
    }
    write(Map("name" -> title, "children" -> data))
  }

  def citem(fuel: Int, ammo: Int, steel: Int, bauxite: Int, sType: String, from: String, to: String) = actionAsync {
    val fromTo = Period.fromStr(from, to)
    val mat = ItemMat(fuel, ammo, steel, bauxite, sType)
    val citem = CItem(mat, fromTo)
    val ci = db.CreateItem.ci
    val mst = db.CreateItem.mst
    val citems = db.CreateItem.findAllByWithName(
      sqls.eq(ci.fuel, fuel).and.eq(ci.ammo, ammo).and.eq(ci.steel, steel).and.eq(ci.bauxite, bauxite).and.eq(mst.name, sType),
      limit = 100
    )
    val counts = db.CreateItem.countItemByMat(mat, fromTo.where(ci.created))
    val sum = counts.map(_._2).sum.toDouble
    val withRate = counts.map { case (item, count) => (item.name, count, count/sum) }
    val countJsonRaw = counts.map { case (item, count) =>
      val url = routes.ViewSta.fromShip().toString + s"#query=${item.name}"
      Map("label" -> item.name, "data" -> count, "url" -> url)
    }
    Ok(views.html.sta.citem(citem, write(countJsonRaw), withRate, citems))
  }

  def fromShip() = actionAsync { Ok(views.html.sta.from_ship()) }

  val countStageCache = new PeriodicalValue(1.hour, () => db.BattleResult.countAllByStage())

  def dropStage() = actionAsync {
    val stages = countStageCache.apply()
    Ok(views.html.sta.drop_stage(stages))
{'fraction_non_alphanumeric': 0.15102793885081708, 'fraction_numerical': 0.007248286768581971, 'mean_word_length': 3.8678640153944834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, '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': '913541', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2678, 'n_words': 580}