<commit_before>package se.chimps.bitziness.core.services.healthcheck.service

import akka.actor.ActorRef
import se.chimps.bitziness.core.endpoints.rest.spray.unrouting.Action
import se.chimps.bitziness.core.endpoints.rest.spray.unrouting.Framework.Controller
import se.chimps.bitziness.core.endpoints.rest.spray.unrouting.Model.Responses.{Error, Ok}
import se.chimps.bitziness.core.endpoints.rest.{EndpointDefinition, RestEndpointBuilder, RESTEndpoint}
import se.chimps.bitziness.core.generic.Serializers.JSONSerializer
import se.chimps.bitziness.core.services.healthcheck.HealthChecks

/**
 *
 */
class HealthCheckEndpoint(val service:ActorRef) extends RESTEndpoint {
  override def rest: Receive = {
    case msg:Any => println(s"Unhandled message to HealthCheckEndpoint: ${msg}.")
  }

  override def configureRestEndpoint(builder: RestEndpointBuilder): EndpointDefinition = {
    builder.mountController("/admin", new HealthCheckController)
    builder.build()
  }
}

class HealthCheckController extends Controller with JSONSerializer {
  override def apply(endpoint: ActorRef): Unit = {
    get("/health", Action { req =>
      val health = HealthChecks.execute()
        .map {
          case (key, value) => if (value) { (key -> "healthy") } else { (key -> "unhealthy") }
        }

      Ok().sendEntity(toJSON(health), "application/javascript").build()
    })
  }
}<commit_msg>Adds an admin/ping url with the health check service.<commit_after>package se.chimps.bitziness.core.services.healthcheck.service

import akka.actor.ActorRef
import se.chimps.bitziness.core.endpoints.rest.spray.unrouting.Action
import se.chimps.bitziness.core.endpoints.rest.spray.unrouting.Framework.Controller
import se.chimps.bitziness.core.endpoints.rest.spray.unrouting.Model.Responses.{Error, Ok}
import se.chimps.bitziness.core.endpoints.rest.{EndpointDefinition, RestEndpointBuilder, RESTEndpoint}
import se.chimps.bitziness.core.generic.Serializers.JSONSerializer
import se.chimps.bitziness.core.services.healthcheck.HealthChecks

/**
 *
 */
class HealthCheckEndpoint(val service:ActorRef) extends RESTEndpoint {
  override def rest: Receive = {
    case msg:Any => println(s"Unhandled message to HealthCheckEndpoint: ${msg}.")
  }

  override def configureRestEndpoint(builder: RestEndpointBuilder): EndpointDefinition = {
    builder.mountController("/admin", new HealthCheckController)
    builder.build()
  }
}

class HealthCheckController extends Controller with JSONSerializer {
  override def apply(endpoint: ActorRef): Unit = {
    get("/health", Action { req =>
      val health = HealthChecks.execute()
        .map {
          case (key, value) => if (value) { (key -> "healthy") } else { (key -> "unhealthy") }
        }

      Ok().sendEntity(toJSON(health), "application/javascript").build()
    })
    get("/ping", Action { req =>
      Ok().sendEntity("pong", "text/plain").build()
    })
  }
}
{'fraction_non_alphanumeric': 0.12603305785123967, 'fraction_numerical': 0.0, 'mean_word_length': 4.470809792843691, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '4560503', 'n_tokens_mistral': 892, 'n_tokens_neox': 862, 'n_words': 161}