<commit_before>/*
 * Copyright (C) 2017  LREN CHUV for Human Brain Project
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ch.chuv.lren.woken.api

import akka.http.scaladsl.server.Route
import akka.actor._
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.model.StatusCodes._
import akka.util.Timeout
import akka.pattern.ask
import ch.chuv.lren.woken.api.swagger.MetadataServiceApi
import ch.chuv.lren.woken.config.{ AppConfiguration, JobsConfiguration }
import ch.chuv.lren.woken.dao.FeaturesDAL
import ch.chuv.lren.woken.messages.datasets.{ DatasetsQuery, DatasetsResponse }
import com.typesafe.scalalogging.LazyLogging

import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }

class MetadataApiService(
    val masterRouter: ActorRef,
    val featuresDatabase: FeaturesDAL,
    override val appConfiguration: AppConfiguration,
    val jobsConf: JobsConfiguration
)(implicit system: ActorSystem)
    extends MetadataServiceApi
    with SprayJsonSupport
    with SecuredRouteHelper
    with WebsocketSupport
    with LazyLogging {

  implicit val executionContext: ExecutionContext = system.dispatcher
  implicit val timeout: Timeout                   = Timeout(180.seconds)

  val routes: Route = listDatasets

  import spray.json._
  import ch.chuv.lren.woken.messages.datasets.datasetsProtocol._

  override def listDatasets: Route =
    securePathWithWebSocket(
      "datasets",
      listDatasetsFlow,
      get {
        complete {
          (masterRouter ? DatasetsQuery)
            .mapTo[DatasetsResponse]
            .map { datasetResponse =>
              OK -> datasetResponse.datasets.toJson
            }
            .recoverWith {
              case e => Future(BadRequest -> JsObject("error" -> JsString(e.toString)))
            }
        }
      }
    )
}<commit_msg>Metadata services start with path /metadata/<commit_after>/*
 * Copyright (C) 2017  LREN CHUV for Human Brain Project
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ch.chuv.lren.woken.api

import akka.actor._
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.model.StatusCodes._
import akka.util.Timeout
import akka.pattern.ask
import ch.chuv.lren.woken.api.swagger.MetadataServiceApi
import ch.chuv.lren.woken.config.{ AppConfiguration, JobsConfiguration }
import ch.chuv.lren.woken.dao.FeaturesDAL
import ch.chuv.lren.woken.messages.datasets.{ DatasetsQuery, DatasetsResponse }
import com.typesafe.scalalogging.LazyLogging

import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }

class MetadataApiService(
    val masterRouter: ActorRef,
    val featuresDatabase: FeaturesDAL,
    override val appConfiguration: AppConfiguration,
    val jobsConf: JobsConfiguration
)(implicit system: ActorSystem)
    extends MetadataServiceApi
    with SprayJsonSupport
    with SecuredRouteHelper
    with WebsocketSupport
    with LazyLogging {

  implicit val executionContext: ExecutionContext = system.dispatcher
  implicit val timeout: Timeout                   = Timeout(180.seconds)

  val routes: Route = listDatasets

  import spray.json._
  import ch.chuv.lren.woken.messages.datasets.datasetsProtocol._

  override def listDatasets: Route =
    securePathWithWebSocket(
      "metadata" / "datasets",
      listDatasetsFlow,
      get {
        complete {
          (masterRouter ? DatasetsQuery)
            .mapTo[DatasetsResponse]
            .map { datasetResponse =>
              OK -> datasetResponse.datasets.toJson
            }
            .recoverWith {
              case e => Future(BadRequest -> JsObject("error" -> JsString(e.toString)))
            }
        }
      }
    )
}
{'fraction_non_alphanumeric': 0.07525814138204924, 'fraction_numerical': 0.003177124702144559, 'mean_word_length': 3.316195372750643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5216305', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1420, 'n_words': 434}