implicit def read()

in core/scheduler/src/main/scala/org/apache/openwhisk/core/scheduler/queue/ElasticSearchDurationChecker.scala [190:237]


  implicit def read(json: JsValue) = {
    val jsObject = json.asJsObject

    jsObject.getFields("aggregations", "took", "hits") match {
      case Seq(aggregations, took, hits) =>
        val hitCount = hits.asJsObject.getFields("total").headOption
        val filterAggregations = aggregations.asJsObject.getFields(FilterAggregationName)
        val averageAggregations = aggregations.asJsObject.getFields(AverageAggregationName)

        (filterAggregations, averageAggregations, hitCount) match {
          case (filterAggregations, _, Some(count)) if filterAggregations.nonEmpty =>
            val averageDuration =
              filterAggregations.headOption.flatMap(
                _.asJsObject
                  .getFields(AverageAggregationName)
                  .headOption
                  .flatMap(_.asJsObject.getFields("value").headOption))

            averageDuration match {
              case Some(JsNull) =>
                DurationCheckResult(None, count.convertTo[Long], took.convertTo[Long])

              case Some(duration) =>
                DurationCheckResult(Some(duration.convertTo[Double]), count.convertTo[Long], took.convertTo[Long])

              case _ => deserializationError("Cannot deserialize ProductItem: invalid input. Raw input: ")
            }

          case (_, averageAggregations, Some(count)) if averageAggregations.nonEmpty =>
            val averageDuration = averageAggregations.headOption.flatMap(_.asJsObject.getFields("value").headOption)

            averageDuration match {
              case Some(JsNull) =>
                DurationCheckResult(None, count.convertTo[Long], took.convertTo[Long])

              case Some(duration) =>
                DurationCheckResult(Some(duration.convertTo[Double]), count.convertTo[Long], took.convertTo[Long])

              case t => deserializationError(s"Cannot deserialize DurationCheckResult: invalid input. Raw input: $t")
            }

          case t => deserializationError(s"Cannot deserialize DurationCheckResult: invalid input. Raw input: $t")
        }

      case other => deserializationError(s"Cannot deserialize DurationCheckResult: invalid input. Raw input: $other")
    }

  }