<commit_before>package org.opengraph.data

import play.api._
import play.api.mvc._
import javax.inject.Singleton
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.libs.json._
import com.typesafe.scalalogging.slf4j.Logging

case class Success(success: String)

object DataFormatter {
  implicit val successFormatter = Json.format[Success]
  implicit val graphFormatter = Json.format[Graph]
  implicit val newFormatter = Json.format[NewGraph]
  implicit val dataPointFormatter = Json.format[DataPoint]
  implicit val createGraphFormatter = Json.format[CreateGraph]
  implicit val seriesFormatter = Json.format[Series]
  implicit val plotFormatter = Json.format[Plot]
}

// @Singleton
class DataController extends Controller {

  val dataStore: DataStore = new DataStore
  import DataFormatter._

  def Response(message: String) = Ok(Json.toJson(Success(message)))

  
  /**
   * serves all the graphs for a given user
   * The return object is a sequence
   * of plots in JSON
   */
  def get(userId: Long) = Action {
    val graphs = dataStore.getGraphs(userId)
    val data = graphs.map{ g =>
      dataStore.getDataPoints(g.id).groupBy(_.series)
    }
    val dataSeries = data.map { series =>
      series.map { case(k,v) =>
        Series(k.getOrElse(""), v)
      }
    }
    val plots = graphs.zip(dataSeries).map {
      case(g, d) => Plot(g, d toList)
    }
    Ok(Json.toJson(plots))
  }

  def post(userId: Long) = Action(parse.json) { request =>
    request.body.validate[CreateGraph].map {
      case create => {
        val id = dataStore.getOrCreate(userId, create.graph)
        create.data.map{ data =>
          dataStore.createDataPoints(id, data, create.series)
        }
        Response("graph recieved")
      }
    }.recoverTotal{
        e => BadRequest("Detected error:"+ JsError.toFlatJson(e))
    }
  }
}

// {<commit_msg>sort graphs from backend<commit_after>package org.opengraph.data

import play.api._
import play.api.mvc._
import javax.inject.Singleton
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.libs.json._
import com.typesafe.scalalogging.slf4j.Logging

case class Success(success: String)

object DataFormatter {
  implicit val successFormatter = Json.format[Success]
  implicit val graphFormatter = Json.format[Graph]
  implicit val newFormatter = Json.format[NewGraph]
  implicit val dataPointFormatter = Json.format[DataPoint]
  implicit val createGraphFormatter = Json.format[CreateGraph]
  implicit val seriesFormatter = Json.format[Series]
  implicit val plotFormatter = Json.format[Plot]
}

// @Singleton
class DataController extends Controller {

  val dataStore: DataStore = new DataStore
  import DataFormatter._

  def Response(message: String) = Ok(Json.toJson(Success(message)))

  
  def sortPlot(plot: Plot): Plot = {
    val series = plot.series.sortBy{ ser =>
      ser.data.map( _.y ).sum
    }
    Plot(plot.graph, series.reverse take 10)
  }
  /**
   * serves all the graphs for a given user
   * The return object is a sequence
   * of plots in JSON
   */
  def get(userId: Long) = Action {
    val graphs = dataStore.getGraphs(userId)
    val data = graphs.map{ g =>
      dataStore.getDataPoints(g.id).groupBy(_.series)
    }
    val dataSeries = data.map { series =>
      series.map { case(k,v) =>
        Series(k.getOrElse(""), v)
      }
    }
    val plots = graphs.zip(dataSeries).map {
      case(g, d) => Plot(g, d toList)
    }
    val sorted = plots.map(sortPlot)

    Ok(Json.toJson(sorted))
  }

  def post(userId: Long) = Action(parse.json) { request =>
    request.body.validate[CreateGraph].map {
      case create => {
        val id = dataStore.getOrCreate(userId, create.graph)
        create.data.map{ data =>
          dataStore.createDataPoints(id, data, create.series)
        }
        Response("graph recieved")
      }
    }.recoverTotal{
        e => BadRequest("Detected error:"+ JsError.toFlatJson(e))
    }
  }
}

// {
{'fraction_non_alphanumeric': 0.1148989898989899, 'fraction_numerical': 0.00101010101010101, 'mean_word_length': 3.173867228661749, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '6708977', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1257, 'n_words': 295}