<commit_before>package com.wavesplatform.metrics

import com.wavesplatform.network.{HandshakeHandler, MicroBlockInv}
import com.wavesplatform.state2.ByteStr
import io.netty.channel.ChannelHandlerContext
import org.influxdb.dto.Point
import scorex.block.{Block, MicroBlock}

object BlockStats {

  private val StringIdLength = 6

  trait Named {
    private[BlockStats] val name: String = {
      val className = getClass.getName
      className.slice(className.lastIndexOf('$', className.length - 2) + 1, className.length - 1)
    }
  }

  private sealed abstract class Event extends Named
  private object Event {
    case object Inv extends Event
    case object Received extends Event
    case object Applied extends Event
    case object Declined extends Event
    case object Mined extends Event
  }

  private sealed abstract class Type extends Named
  private object Type {
    case object Block extends Type
    case object Micro extends Type
  }

  sealed abstract class Source extends Named
  object Source {
    case object Broadcast extends Source
    case object Ext extends Source
  }

  def received(b: Block, source: Source, ctx: ChannelHandlerContext): Unit = write(
    block(b, source)
      .addField("from", nodeName(ctx))
      .addField("prop-time", System.currentTimeMillis() - b.timestamp)
      .addField("score", b.blockScore)
      .addField("bt", b.consensusData.baseTarget),
    Event.Received,
    Seq.empty
  )

  def applied(b: Block, source: Source, baseHeight: Int): Unit = write(
    block(b, source)
      .addField("txs", b.transactionData.size)
      .addField("height", baseHeight),
    Event.Applied,
    Seq.empty
  )

  def declined(b: Block, source: Source): Unit = write(
    block(b, source),
    Event.Declined,
    Seq.empty
  )

  def mined(b: Block, baseHeight: Int): Unit = write(
    block(b, Source.Broadcast)
      .tag("parent-id", id(b.reference))
      .addField("txs", b.transactionData.size)
      .addField("score", b.blockScore)
      .addField("bt", b.consensusData.baseTarget)
      .addField("height", baseHeight),
    Event.Mined,
    Seq.empty
  )


  def inv(m: MicroBlockInv, ctx: ChannelHandlerContext): Unit = write(
    measurement(Type.Micro)
      .tag("id", id(m.totalBlockSig))
      .tag("parent-id", id(m.prevBlockSig))
      .addField("from", nodeName(ctx)),
    Event.Inv,
    Seq.empty
  )

  def received(m: MicroBlock, ctx: ChannelHandlerContext, propagationTime: Long): Unit = write(
    micro(m)
      .tag("parent-id", id(m.prevResBlockSig))
      .addField("from", nodeName(ctx))
      .addField("prop-time", propagationTime),
    Event.Received,
    Seq.empty
  )

  def received(m: MicroBlock, ctx: ChannelHandlerContext): Unit = write(
    micro(m)
      .tag("parent-id", id(m.prevResBlockSig))
      .addField("from", nodeName(ctx)),
    Event.Received,
    Seq.empty
  )

  def applied(m: MicroBlock): Unit = write(
    micro(m)
      .addField("txs", m.transactionData.size),
    Event.Applied,
    Seq.empty
  )

  def declined(m: MicroBlock): Unit = write(
    micro(m),
    Event.Declined,
    Seq.empty
  )

  def mined(m: MicroBlock): Unit = write(
    micro(m)
      .tag("parent-id", id(m.prevResBlockSig))
      .addField("txs", m.transactionData.size),
    Event.Mined,
    Seq.empty
  )

  private def block(b: Block, source: Source): Point.Builder = measurement(Type.Block)
    .tag("id", id(b.uniqueId))
    .tag("source", source.name)

  private def micro(m: MicroBlock): Point.Builder = measurement(Type.Micro)
    .tag("id", id(m.totalResBlockSig))

  private def measurement(t: Type): Point.Builder = Point.measurement("block").tag("type", t.toString)

  private def nodeName(ctx: ChannelHandlerContext): String = {
    Option(ctx.channel().attr(HandshakeHandler.NodeNameAttributeKey).get()).getOrElse("")
  }

  private def id(x: ByteStr): String = x.toString.take(StringIdLength)

  private def write(init: Point.Builder, event: Event, addFields: Seq[(String, String)]): Unit = {
    Metrics.write(addFields.foldLeft(init.tag("event", event.name)) { case (r, (k, v)) => r.addField(k, v) })
  }
}
<commit_msg>NODE-256 A block score has greater value than InfluxDB supports<commit_after>package com.wavesplatform.metrics

import com.wavesplatform.network.{HandshakeHandler, MicroBlockInv}
import com.wavesplatform.state2.ByteStr
import io.netty.channel.ChannelHandlerContext
import org.influxdb.dto.Point
import scorex.block.{Block, MicroBlock}

object BlockStats {

  private val StringIdLength = 6

  trait Named {
    private[BlockStats] val name: String = {
      val className = getClass.getName
      className.slice(className.lastIndexOf('$', className.length - 2) + 1, className.length - 1)
    }
  }

  private sealed abstract class Event extends Named
  private object Event {
    case object Inv extends Event
    case object Received extends Event
    case object Applied extends Event
    case object Declined extends Event
    case object Mined extends Event
  }

  private sealed abstract class Type extends Named
  private object Type {
    case object Block extends Type
    case object Micro extends Type
  }

  sealed abstract class Source extends Named
  object Source {
    case object Broadcast extends Source
    case object Ext extends Source
  }

  def received(b: Block, source: Source, ctx: ChannelHandlerContext): Unit = write(
    block(b, source)
      .addField("from", nodeName(ctx))
      .addField("prop-time", System.currentTimeMillis() - b.timestamp)
      .addField("score", b.blockScore.toString)
      .addField("bt", b.consensusData.baseTarget),
    Event.Received,
    Seq.empty
  )

  def applied(b: Block, source: Source, baseHeight: Int): Unit = write(
    block(b, source)
      .addField("txs", b.transactionData.size)
      .addField("height", baseHeight),
    Event.Applied,
    Seq.empty
  )

  def declined(b: Block, source: Source): Unit = write(
    block(b, source),
    Event.Declined,
    Seq.empty
  )

  def mined(b: Block, baseHeight: Int): Unit = write(
    block(b, Source.Broadcast)
      .tag("parent-id", id(b.reference))
      .addField("txs", b.transactionData.size)
      .addField("score", b.blockScore.toString)
      .addField("bt", b.consensusData.baseTarget)
      .addField("height", baseHeight),
    Event.Mined,
    Seq.empty
  )


  def inv(m: MicroBlockInv, ctx: ChannelHandlerContext): Unit = write(
    measurement(Type.Micro)
      .tag("id", id(m.totalBlockSig))
      .tag("parent-id", id(m.prevBlockSig))
      .addField("from", nodeName(ctx)),
    Event.Inv,
    Seq.empty
  )

  def received(m: MicroBlock, ctx: ChannelHandlerContext, propagationTime: Long): Unit = write(
    micro(m)
      .tag("parent-id", id(m.prevResBlockSig))
      .addField("from", nodeName(ctx))
      .addField("prop-time", propagationTime),
    Event.Received,
    Seq.empty
  )

  def received(m: MicroBlock, ctx: ChannelHandlerContext): Unit = write(
    micro(m)
      .tag("parent-id", id(m.prevResBlockSig))
      .addField("from", nodeName(ctx)),
    Event.Received,
    Seq.empty
  )

  def applied(m: MicroBlock): Unit = write(
    micro(m)
      .addField("txs", m.transactionData.size),
    Event.Applied,
    Seq.empty
  )

  def declined(m: MicroBlock): Unit = write(
    micro(m),
    Event.Declined,
    Seq.empty
  )

  def mined(m: MicroBlock): Unit = write(
    micro(m)
      .tag("parent-id", id(m.prevResBlockSig))
      .addField("txs", m.transactionData.size),
    Event.Mined,
    Seq.empty
  )

  private def block(b: Block, source: Source): Point.Builder = measurement(Type.Block)
    .tag("id", id(b.uniqueId))
    .tag("source", source.name)

  private def micro(m: MicroBlock): Point.Builder = measurement(Type.Micro)
    .tag("id", id(m.totalResBlockSig))

  private def measurement(t: Type): Point.Builder = Point.measurement("block").tag("type", t.toString)

  private def nodeName(ctx: ChannelHandlerContext): String = {
    Option(ctx.channel().attr(HandshakeHandler.NodeNameAttributeKey).get()).getOrElse("")
  }

  private def id(x: ByteStr): String = x.toString.take(StringIdLength)

  private def write(init: Point.Builder, event: Event, addFields: Seq[(String, String)]): Unit = {
    Metrics.write(addFields.foldLeft(init.tag("event", event.name)) { case (r, (k, v)) => r.addField(k, v) })
  }
}

{'fraction_non_alphanumeric': 0.13038767156272574, 'fraction_numerical': 0.0015651336383337346, 'mean_word_length': 3.4733441033925687, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3747354', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2595, 'n_words': 637}