<commit_before>package com.alvin.niagara.sparkservice

import akka.http.scaladsl.model.HttpHeader.ParsingResult
import akka.http.scaladsl.model.{HttpEntity, _}
import akka.http.scaladsl.model.headers.HttpCookie
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.MethodRejection
import akka.stream.ActorMaterializer
import com.alvin.niagara.common.{Post, Response, Tags}
import spray.json.DefaultJsonProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

import scala.concurrent.ExecutionContext.Implicits.global

/**
  * Created by JINC4 on 6/4/2016.
  *
  * RouteService contains a bunch of directives calling Spark SQL queries
  *
  */

trait AkkaJSONProtocol extends DefaultJsonProtocol {
  implicit val postFormat = jsonFormat4(Post.apply)
  implicit val responseFormat = jsonFormat3(Response.apply)
  implicit val tagFormat = jsonFormat1(Tags.apply)
}

trait Routes extends AkkaJSONProtocol {


  val route =
    path("postid" / LongNumber) { id =>
      get {
        onSuccess(CassandraService.queryPostById(id)) {
          case result: List[Response] =>
            complete(result)
        }

      } ~
        (post & entity(as[Tags])) { t =>
          onSuccess(CassandraService.updatePost(id, t.tags)) {
            case result: String =>
              complete(HttpEntity(ContentTypes.`application/json`, result))
          }
        } ~ {
        reject(MethodRejection(HttpMethods.GET))
      }
    } ~
      path("createdate" / Segment) { (date: String) =>
        get {
          complete(SparkService.searchPostsByDate(date))
        }
      } ~
      path("tag" / Segment) { (tag: String) =>
        get {
          onSuccess(CassandraService.queryPostByTag(tag)) {
            case result: List[Response] =>
              complete(result)
          }
        }
      } ~
      path("post") {
        (post & entity(as[Post])) { p =>
          onSuccess(CassandraService.insertPost(p)) {
            case result: String =>
              complete(HttpEntity(ContentTypes.`application/json`, result))
          }


        }<commit_msg>Supported Query by list column element<commit_after>package com.alvin.niagara.sparkservice

import akka.http.scaladsl.model.HttpHeader.ParsingResult
import akka.http.scaladsl.model.{HttpEntity, _}
import akka.http.scaladsl.model.headers.HttpCookie
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.MethodRejection
import akka.stream.ActorMaterializer
import com.alvin.niagara.common.{Post, Response, Tags}
import spray.json.DefaultJsonProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

import scala.concurrent.ExecutionContext.Implicits.global

/**
  * Created by JINC4 on 6/4/2016.
  *
  * RouteService contains a bunch of directives calling Spark SQL queries
  *
  */

trait AkkaJSONProtocol extends DefaultJsonProtocol {
  implicit val postFormat = jsonFormat4(Post.apply)
  implicit val responseFormat = jsonFormat3(Response.apply)
  implicit val tagFormat = jsonFormat1(Tags.apply)
}

trait Routes extends AkkaJSONProtocol {


  val route =
    path("postid" / LongNumber) { id =>
      //Query posts by specific postid
      get {
        onSuccess(CassandraService.queryPostById(id)) {
          case result: List[Response] =>
            complete(result)
        }
      } ~
      //Update the tag list by looking up the postid
        (post & entity(as[Tags])) { t =>
          onSuccess(CassandraService.updatePost(id, t.tags)) {
            case result: String =>
              complete(HttpEntity(ContentTypes.`application/json`, result))
          }
        } ~ {
        reject(MethodRejection(HttpMethods.GET))
      }
    } ~
  //Query posts containing a specific element in its tags
      path("tag" / Segment) { (tag: String) =>
        get {
          onSuccess(CassandraService.queryPostByTag(tag)) {
            case result: List[Response] =>
              complete(result)
          }
        }
      } ~
      //Create a new post by the payload
      path("post") {
        (post & entity(as[Post])) { p =>
          onSuccess(CassandraService.insertPost(p)) {
            case result: String =>
              complete(HttpEntity(ContentTypes.`application/json`, result))
          }


        }
{'fraction_non_alphanumeric': 0.10614916997895721, 'fraction_numerical': 0.004676174888940846, 'mean_word_length': 2.694300518134715, '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': '5626529', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1250, 'n_words': 243}