<commit_before>package ui.pages

import api.{ConfigStorage, ConnectedStream}
import japgolly.scalajs.react.vdom.prefix_<^._
import japgolly.scalajs.react.{BackendScope, ReactComponentB}
import model._
import ui.WorkbenchRef
import ui.widgets.{Alert, ContainersCard, InfoCard, TableCard}
import util.chrome.api._
import util.logger._

import scala.concurrent.ExecutionContext.Implicits.global


case object HomePage extends Page {

  val id = "HomePage"

  case class State(info: Option[DockerMetadata] = None, events: Seq[DockerEvent] = Seq.empty, error: Option[String] = None, stream: Option[ConnectedStream] = None)

  case class Props(ref: WorkbenchRef) {
    def url = ref.connection.map(_.url).getOrElse("''")
  }

  case class Backend(t: BackendScope[Props, State]) {
    def willMount(): Unit = t.props.ref.client.map { client =>

      def loadInfo() =
        client.metadata().map { info =>
          t.modState(s => s.copy(info = Some(info), error = None))
        }.onFailure {
          case ex: Exception =>
            log.error("HomePage", "Unable to get Metadata", ex)
            ConfigStorage.isRunningBoot2Docker.map {
              case false => s"Unable to connect to ${t.props.url}, is Docker daemon running?"
              case true => s"Unable to connect to ${t.props.url}, is Boot2docker running?"
            }.map { msg =>
              t.modState(s => s.copy(error = Some(msg)))
            }
        }

      def loadEvents() = {
        val stream = client.events { events => //streaming
          t.modState(s => s.copy(events = events))
        }
        t.modState(s => s.copy(stream = Some(stream)))
      }

      loadInfo()
      loadEvents()
    }

    def willUnMount(): Unit = t.state.stream.map(_.abort())

    def refresh() = willMount()
  }

  def component(ref: WorkbenchRef) = {
    val props = Props(ref)
    HomePageRender.component(props)
  }

}

object HomePageRender {

  import ui.pages.HomePage._

  val component = ReactComponentB[Props]("HomePage")
    .initialState(State())
    .backend(new Backend(_))
    .render((P, S, B) => vdom(S, P, B))
    .componentWillMount(_.backend.willMount)
    .componentWillUnmount(_.backend.willUnMount)
    .build

  def vdom(S: State, P: Props, B: Backend) = <.div(
    S.error.map(Alert(_, Some(P.ref.link(SettingsPage)))),
    S.info.map(vdomInfo(_, P.ref, B)),
    vdomEvents(S.events)
  )

  def vdomInfo(docker: DockerMetadata, ref: WorkbenchRef, B: Backend) = {
    val info = Map(
      "Connected to" -> docker.connection.url,
      "Version" -> s"${docker.version.Version} (api: ${docker.version.ApiVersion})",
      "Docker UI" -> chrome.runtime.getManifest().version
    )
    <.div(
      ContainersCard(docker, ref)(() => B.refresh()),
      InfoCard(info, InfoCard.SMALL, Some("System"))
    )
  }

  def vdomEvents(events: Seq[DockerEvent]) = {
    val values = events.map(e => Map("Status" -> e.status, "Id" -> e.shortId, "From" -> e.from, "Time" -> e.since))
    values.nonEmpty ?= <.div(^.className := "container  col-sm-12",
      <.div(^.className := "panel panel-default  bootcards-summary",
        <.div(^.className := "panel-heading clearfix",
          <.h3(^.className := "panel-title pull-left",
            <.span(^.className := "glyphicon glyphicon-list"), " Events History"
          )
        ),
        TableCard(values)
      )
    )
  }
}

<commit_msg>Add 'this is a beta' message in HomePage.<commit_after>package ui.pages

import api.{ConfigStorage, ConnectedStream}
import japgolly.scalajs.react.vdom.prefix_<^._
import japgolly.scalajs.react.{BackendScope, ReactComponentB}
import model._
import ui.WorkbenchRef
import ui.widgets.{Alert, ContainersCard, InfoCard, TableCard}
import util.chrome.api._
import util.logger._

import scala.concurrent.ExecutionContext.Implicits.global


case object HomePage extends Page {

  val id = "HomePage"

  case class State(info: Option[DockerMetadata] = None, events: Seq[DockerEvent] = Seq.empty, error: Option[String] = None, stream: Option[ConnectedStream] = None)

  case class Props(ref: WorkbenchRef) {
    def url = ref.connection.map(_.url).getOrElse("''")
  }

  case class Backend(t: BackendScope[Props, State]) {
    def willMount(): Unit = t.props.ref.client.map { client =>

      def loadInfo() =
        client.metadata().map { info =>
          t.modState(s => s.copy(info = Some(info), error = None))
        }.onFailure {
          case ex: Exception =>
            log.error("HomePage", "Unable to get Metadata", ex)
            ConfigStorage.isRunningBoot2Docker.map {
              case false => s"Unable to connect to ${t.props.url}, is Docker daemon running?"
              case true => s"Unable to connect to ${t.props.url}, is Boot2docker running?"
            }.map { msg =>
              t.modState(s => s.copy(error = Some(msg)))
            }
        }

      def loadEvents() = {
        val stream = client.events { events => //streaming
          t.modState(s => s.copy(events = events))
        }
        t.modState(s => s.copy(stream = Some(stream)))
      }

      loadInfo()
      loadEvents()
    }

    def willUnMount(): Unit = t.state.stream.map(_.abort())

    def refresh() = willMount()
  }

  def component(ref: WorkbenchRef) = {
    val props = Props(ref)
    HomePageRender.component(props)
  }

}

object HomePageRender {

  import ui.pages.HomePage._

  val component = ReactComponentB[Props]("HomePage")
    .initialState(State())
    .backend(new Backend(_))
    .render((P, S, B) => vdom(S, P, B))
    .componentWillMount(_.backend.willMount)
    .componentWillUnmount(_.backend.willUnMount)
    .build

  def vdom(S: State, P: Props, B: Backend) = <.div(
    S.error.map(Alert(_, Some(P.ref.link(SettingsPage)))),
    S.info.map(vdomInfo(_, P.ref, B)),
    vdomEvents(S.events)
  )

  def vdomInfo(docker: DockerMetadata, ref: WorkbenchRef, B: Backend) = {
    val info = Map(
      "Connected to" -> docker.connection.url,
      "Version" -> s"${docker.version.Version} (api: ${docker.version.ApiVersion})",
      "Docker UI" -> chrome.runtime.getManifest().version
    )
    <.div(
      ContainersCard(docker, ref)(() => B.refresh()),
      InfoCard(info, InfoCard.SMALL, Some("System"), footer = infoFooter)
    )
  }

  val infoFooter = Some(<.div(^.className:="panel-footer alert-warning"," Please note this is a beta version, ",
    <.a(^.href := "https://github.com/felixgborrego/docker-ui-chrome-app/issues",^.target:="blank", "any feedback is more than welcome!"
    )))

  def vdomEvents(events: Seq[DockerEvent]) = {
    val values = events.map(e => Map("Status" -> e.status, "Id" -> e.shortId, "From" -> e.from, "Time" -> e.since))
    values.nonEmpty ?= <.div(^.className := "container  col-sm-12",
      <.div(^.className := "panel panel-default  bootcards-summary",
        <.div(^.className := "panel-heading clearfix",
          <.h3(^.className := "panel-title pull-left",
            <.span(^.className := "glyphicon glyphicon-list"), " Events History"
          )
        ),
        TableCard(values)
      )
    )
  }
}


{'fraction_non_alphanumeric': 0.1554800339847069, 'fraction_numerical': 0.001416029453412631, 'mean_word_length': 2.846949891067538, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 45, 'https://': 1, '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': '5310365', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2289, 'n_words': 494}