<commit_before>package com.jraska.gradle.buildtime.report

import com.jraska.gradle.buildtime.BuildData
import com.jraska.gradle.buildtime.BuildReporter
import com.mixpanel.mixpanelapi.ClientDelivery
import com.mixpanel.mixpanelapi.MessageBuilder
import com.mixpanel.mixpanelapi.MixpanelAPI
import org.json.JSONObject
import java.util.concurrent.TimeUnit

class MixpanelReporter(
  private val apiKey: String,
  private val api: MixpanelAPI
) : BuildReporter {
  override fun report(buildData: BuildData) {
    val start = nowMillis()

    reportInternal(buildData)

    val reportingOverhead = nowMillis() - start
    println("$STOPWATCH_ICON Build time '${buildData.buildTime} ms' reported to Mixpanel in $reportingOverhead ms.$STOPWATCH_ICON")
  }

  private fun reportInternal(buildData: BuildData) {
    val delivery = ClientDelivery()

    val properties = convertBuildData(buildData)
    val mixpanelEvent = MessageBuilder(apiKey)
      .event(SINGLE_NAME_FOR_ONE_USER, "Android Build", JSONObject(properties))

    delivery.addMessage(mixpanelEvent)

    api.deliver(delivery)
  }

  private fun nowMillis() = TimeUnit.NANOSECONDS.toMillis(System.nanoTime())

  private fun convertBuildData(buildData: BuildData): Map<String, Any?> {<commit_msg>Catch exceptions during build time reporting (#304)<commit_after>package com.jraska.gradle.buildtime.report

import com.jraska.gradle.buildtime.BuildData
import com.jraska.gradle.buildtime.BuildReporter
import com.mixpanel.mixpanelapi.ClientDelivery
import com.mixpanel.mixpanelapi.MessageBuilder
import com.mixpanel.mixpanelapi.MixpanelAPI
import org.json.JSONObject
import java.util.concurrent.TimeUnit

class MixpanelReporter(
  private val apiKey: String,
  private val api: MixpanelAPI
) : BuildReporter {
  override fun report(buildData: BuildData) {
    try {
      reportToMixpanel(buildData)
    } catch (ex: Exception) {
      println("Build time reporting failed: $ex")
    }
  }

  private fun reportToMixpanel(buildData: BuildData) {
    val start = nowMillis()

    reportInternal(buildData)

    val reportingOverhead = nowMillis() - start
    println("$STOPWATCH_ICON Build time '${buildData.buildTime} ms' reported to Mixpanel in $reportingOverhead ms.$STOPWATCH_ICON")
  }

  private fun reportInternal(buildData: BuildData) {
    val delivery = ClientDelivery()

    val properties = convertBuildData(buildData)
    val mixpanelEvent = MessageBuilder(apiKey)
      .event(SINGLE_NAME_FOR_ONE_USER, "Android Build", JSONObject(properties))

    delivery.addMessage(mixpanelEvent)

    api.deliver(delivery)
  }

  private fun nowMillis() = TimeUnit.NANOSECONDS.toMillis(System.nanoTime())

  private fun convertBuildData(buildData: BuildData): Map<String, Any?> {
{'fraction_non_alphanumeric': 0.09034381858083394, 'fraction_numerical': 0.0010972933430870519, 'mean_word_length': 5.104910714285714, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3636060', 'n_tokens_mistral': 847, 'n_tokens_neox': 824, 'n_words': 178}