All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gatling.charts.template.PageTemplate.scala Maven / Gradle / Ivy

There is a newer version: 3.13.1
Show newest version
/*
 * Copyright 2011-2021 GatlingCorp (https://gatling.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.gatling.charts.template

import java.nio.charset.Charset

import io.gatling.charts.FileNamingConventions
import io.gatling.charts.component.Component
import io.gatling.charts.config.ChartsFiles._
import io.gatling.charts.util.HtmlHelper._
import io.gatling.commons.shared.unstable.model.stats.Group
import io.gatling.commons.util.StringHelper._
import io.gatling.core.stats.writer.RunMessage

private[charts] object PageTemplate {

  private var runMessage: RunMessage = _
  private var runStart: Long = _
  private var runEnd: Long = _

  def setRunInfo(runMessage: RunMessage, runEnd: Long): Unit = {
    this.runMessage = runMessage
    this.runStart = runMessage.start
    this.runEnd = runEnd
  }
}

private[charts] abstract class PageTemplate(title: String, isDetails: Boolean, requestName: Option[String], group: Option[Group], components: Component*) {

  def jsFiles: Seq[String] = (CommonJsFiles ++ components.flatMap(_.jsFiles)).distinct

  @SuppressWarnings(Array("org.wartremover.warts.ListAppend"))
  def getOutput(charset: Charset): String = {
    val runMessage = PageTemplate.runMessage
    val runStart = PageTemplate.runStart
    val runEnd = PageTemplate.runEnd
    val duration = (runEnd - runStart) / 1000

    val pageStats =
      if (isDetails) {
        val groupHierarchy = group.map(_.hierarchy).getOrElse(Nil).map(_.toGroupFileName(charset))

        val groupAndRequestHierarchy = requestName match {
          case Some(req) => groupHierarchy :+ req.toRequestFileName(charset)
          case _         => groupHierarchy
        }

        s"""var pageStats = stats.contents['${groupAndRequestHierarchy.mkString("'].contents['")}'].stats;"""
      } else {
        "var pageStats = stats.stats;"
      }

    s"""







${jsFiles.map(jsFile => s"""""").mkString(Eol)}
Gatling Stats - $title


${runMessage.simulationId}

> $title

${components.map(_.html).mkString}
""" } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy