Skip to content

Commit

Permalink
fix sorting of insights metric values
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 28, 2015
1 parent 603b9dc commit 8f56d1f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
6 changes: 1 addition & 5 deletions modules/insight/src/main/AggregationClusters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ object AggregationClusters {

private def postSort[X](q: Question[X])(clusters: List[Cluster[X]]): List[Cluster[X]] = q.dimension match {
case Dimension.Opening => clusters
case _ => sortLike[Cluster[X], X](clusters, Dimension.valuesOf(q.dimension), _.x)
}

private def sortLike[A, B](la: List[A], lb: List[B], f: A => B): List[A] = la.sortWith {
case (x, y) => lb.indexOf(f(x)) < lb.indexOf(f(y))
case _ => Util.sortLike[Cluster[X], X](clusters, Dimension.valuesOf(q.dimension), _.x)
}
}
9 changes: 8 additions & 1 deletion modules/insight/src/main/Chart.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ object Chart {
case (_, serie) => serie.copy(data = serie.data.reverse)
}.toList

def sortedSeries = answer.clusters.headOption.fold(series) {
_.insight match {
case Insight.Single(_) => series
case Insight.Stacked(points) => Util.sortLike[Serie, String](series, points.map(_._1.name), _.name)
}
}

Chart(
xAxis = xAxis,
valueYaxis = Yaxis(metric.name, metric.dataType.name),
sizeYaxis = Yaxis(metric.position.tellNumber, Metric.DataType.Count.name),
series = series,
series = sortedSeries,
sizeSerie = sizeSerie)
}
}
2 changes: 1 addition & 1 deletion modules/insight/src/main/Dimension.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object Dimension {
case Color => chess.Color.all
case Opening => EcopeningDB.all
case OpponentStrength => RelativeStrength.all
case PieceRole => chess.Role.all
case PieceRole => chess.Role.all.reverse
}

def valueByKey[X](d: Dimension[X], key: String): Option[X] = d match {
Expand Down
2 changes: 1 addition & 1 deletion modules/insight/src/main/Metric.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object Metric {
case Result => lila.insight.Result.all.map { r =>
MetricValue(BSONInteger(r.id), MetricValueName(r.name))
}
case PieceRole => chess.Role.all.map { r =>
case PieceRole => chess.Role.all.reverse.map { r =>
MetricValue(BSONString(r.forsyth.toString), MetricValueName(r.toString))
}
case _ => Nil
Expand Down
8 changes: 8 additions & 0 deletions modules/insight/src/main/Util.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package lila.insight

private object Util {

def sortLike[A, B](la: List[A], lb: List[B], f: A => B): List[A] = la.sortWith {
case (x, y) => lb.indexOf(f(x)) < lb.indexOf(f(y))
}
}

0 comments on commit 8f56d1f

Please sign in to comment.