<commit_before>package ch.epfl.scala.index
package server
package routes

import model._
import release._

import akka.http.scaladsl._
import server.Directives._
import model.StatusCodes._

class Badges(dataRepository: DataRepository) {

  private val shields = parameters(('color.?, 'style.?, 'logo.?, 'logoWidth.as[Int].?))
  private val shieldsSubject = shields & parameters('subject)

  private def shieldsSvg(rawSubject: String, rawStatus: String, rawColor: Option[String],
    style: Option[String], logo: Option[String], logoWidth: Option[Int]) = {

    def shieldEscape(in: String): String = 
      in
        .replaceAllLiterally("-", "--")
        .replaceAllLiterally("_", "__")

    val subject = shieldEscape(rawSubject)
    val status = shieldEscape(rawStatus)

    val color = rawColor.getOrElse("green") 

    // we need a specific encoding
    val query = List(
      style.map(("style", _)),
      logo.map(l => ("logo", java.net.URLEncoder.encode(l, "ascii").replaceAllLiterally("+", "%2B") )),
      logoWidth.map(w => ("logoWidth", w.toString))
    ).flatten.map{case (k, v) => k + "=" + v }.mkString("?", "&", "")

    redirect(
      s"https://img.shields.io/badge/$subject-$status-$color.svg$query",
      TemporaryRedirect
    )
    
  }

  val routes =
    get {
      path(Segment / Segment / Segment / "latest.svg") { (organization, repository, artifact) =>
        shields { (color, style, logo, logoWidth) =>
          onSuccess(dataRepository.projectPage(Project.Reference(organization, repository), 
            ReleaseSelection(Some(artifact), None))){

              case Some((_, options)) =>
                shieldsSvg(artifact, options.release.reference.version.toString(), color, style, logo, logoWidth)
              case _ => 
                complete((NotFound, ""))

          }
        }
      } ~
      path("count.svg") {
        parameter('q) { query =>
          shieldsSubject { (color, style, logo, logoWidth, subject) =>
            onSuccess(dataRepository.total(query))(count => 
              shieldsSvg(subject, count.toString, color, style, logo, logoWidth)
            )
          }
        }
      }
    }
}<commit_msg>show an empty status badge rather then a broken image<commit_after>package ch.epfl.scala.index
package server
package routes

import model._
import release._

import akka.http.scaladsl._
import server.Directives._
import model.StatusCodes._

class Badges(dataRepository: DataRepository) {

  private val shields = parameters(('color.?, 'style.?, 'logo.?, 'logoWidth.as[Int].?))
  private val shieldsSubject = shields & parameters('subject)

  private def shieldsSvg(rawSubject: String, rawStatus: String, rawColor: Option[String],
    style: Option[String], logo: Option[String], logoWidth: Option[Int]) = {

    def shieldEscape(in: String): String = 
      in
        .replaceAllLiterally("-", "--")
        .replaceAllLiterally("_", "__")
        .replaceAllLiterally(" ", "_")

    val subject = shieldEscape(rawSubject)
    val status = shieldEscape(rawStatus)

    val color = rawColor.getOrElse("green") 

    // we need a specific encoding
    val query = List(
      style.map(("style", _)),
      logo.map(l => ("logo", java.net.URLEncoder.encode(l, "ascii").replaceAllLiterally("+", "%2B") )),
      logoWidth.map(w => ("logoWidth", w.toString))
    ).flatten.map{case (k, v) => k + "=" + v }.mkString("?", "&", "")

    redirect(
      s"https://img.shields.io/badge/$subject-$status-$color.svg$query",
      TemporaryRedirect
    )
    
  }

  val routes =
    get {
      path(Segment / Segment / Segment / "latest.svg") { (organization, repository, artifact) =>
        shields { (color, style, logo, logoWidth) =>
          onSuccess(dataRepository.projectPage(Project.Reference(organization, repository), 
            ReleaseSelection(Some(artifact), None))){

              case Some((_, options)) =>
                shieldsSvg(artifact, options.release.reference.version.toString(), color, style, logo, logoWidth)
              case _ => 
                shieldsSvg(artifact, "no published release", color orElse Some("lightgrey"), style, logo, logoWidth)

          }
        }
      } ~
      path("count.svg") {
        parameter('q) { query =>
          shieldsSubject { (color, style, logo, logoWidth, subject) =>
            onSuccess(dataRepository.total(query))(count => 
              shieldsSvg(subject, count.toString, color, style, logo, logoWidth)
            )
          }
        }
      }
    }
}
{'fraction_non_alphanumeric': 0.14688262702462834, 'fraction_numerical': 0.00044375416019525185, 'mean_word_length': 2.7629382303839733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '2405199', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1304, 'n_words': 285}