<commit_before>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.internal.statistic.collectors.fus.ui

import com.intellij.internal.statistic.eventLog.EventLogGroup
import com.intellij.internal.statistic.eventLog.events.EventFields
import com.intellij.internal.statistic.eventLog.validator.ValidationResultType
import com.intellij.internal.statistic.eventLog.validator.rules.EventContext
import com.intellij.internal.statistic.eventLog.validator.rules.impl.CustomValidationRule
import com.intellij.internal.statistic.service.fus.collectors.CounterUsagesCollector
import com.intellij.internal.statistic.utils.getPluginInfoByDescriptor
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.extensions.ExtensionPointListener
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.extensions.PluginDescriptor
import com.intellij.ui.GotItTooltip
import com.intellij.util.xmlb.annotations.Attribute


class GotItTooltipAllowlistEP {
  companion object {
    @JvmField
    val EP_NAME : ExtensionPointName<GotItTooltipAllowlistEP> = ExtensionPointName.create("com.intellij.gotItTooltipAllowlist")

  }

  @Attribute("prefix")
  var prefix: String = ""
}

class GotItUsageCollector private constructor() {
  companion object {
    @JvmStatic
    val instance: GotItUsageCollector
      get() = ApplicationManager.getApplication().getService(GotItUsageCollector::class.java)
  }


  private val allowedPrefixes : MutableSet<String> = HashSet()

  init {
    GotItTooltipAllowlistEP.EP_NAME.processWithPluginDescriptor(this::addToWhileList)
    GotItTooltipAllowlistEP.EP_NAME.addExtensionPointListener(object: ExtensionPointListener<GotItTooltipAllowlistEP> {
      override fun extensionAdded(extension: GotItTooltipAllowlistEP, pluginDescriptor: PluginDescriptor) {
        addToWhileList(extension, pluginDescriptor)
      }

      override fun extensionRemoved(extension: GotItTooltipAllowlistEP, pluginDescriptor: PluginDescriptor) {
        allowedPrefixes.remove(extension.prefix)
      }
    }, null)

  }

  private fun addToWhileList(extension : GotItTooltipAllowlistEP, plugin : PluginDescriptor) {
    if (getPluginInfoByDescriptor(plugin).isDevelopedByJetBrains() && extension.prefix.isNotEmpty()) {
      allowedPrefixes.add(extension.prefix)
    }
  }

  fun logOpen(id: String, count: Int) {
    toPrefix(id)?.let { GotItUsageCollectorGroup.showEvent.log(it, count) }
  }

  fun logClose(id: String, closeType: GotItUsageCollectorGroup.CloseType) {
    toPrefix(id)?.let { GotItUsageCollectorGroup.closeEvent.log(id, closeType) }
  }

  private fun toPrefix(id: String): String? {
    if (allowedPrefixes.contains(id)) {
      return id
    }

    var prefix = ""
    for (fragment in id.split(".")) {
      prefix = if (prefix.isEmpty()) fragment else "$prefix.$fragment"
      if (allowedPrefixes.contains(prefix)) {
        return prefix
      }
    }
    return null
  }
}

class GotItUsageCollectorGroup : CounterUsagesCollector() {
  override fun getGroup(): EventLogGroup = GROUP

  enum class CloseType(private val text: String) {
    ButtonClick("click.button"),
    LinkClick("click.link"),
    OutsideClick("click.outside"),
    AncestorRemoved("ancestor.removed"),
    EscapeShortcutPressed("escape.shortcut.pressed"),
    Timeout("timeout");

    override fun toString(): String = text
  }

  companion object {
    private val GROUP = EventLogGroup("gotit.usage.collector", 1)

    internal val showEvent = GROUP.registerEvent("show",
                                                 EventFields.StringValidatedByCustomRule("id.prefix", GotItTooltip.PROPERTY_PREFIX),
                                                 EventFields.Int("count"))

    internal val closeEvent = GROUP.registerEvent("close",
                                                  EventFields.StringValidatedByCustomRule("id.prefix", GotItTooltip.PROPERTY_PREFIX),
                                                  EventFields.Enum<CloseType>("type"))
  }
}

class GotItIDValidator : CustomValidationRule() {
  override fun doValidate(data: String, context: EventContext): ValidationResultType {
    return acceptWhenReportedByJetBrainsPlugin(context)
  }

  override fun acceptRuleId(ruleId: String?): Boolean {
    return GotItTooltip.PROPERTY_PREFIX == ruleId
  }
}<commit_msg>IDEA-252493 FUS: modify field name<commit_after>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.internal.statistic.collectors.fus.ui

import com.intellij.internal.statistic.eventLog.EventLogGroup
import com.intellij.internal.statistic.eventLog.events.EventFields
import com.intellij.internal.statistic.eventLog.validator.ValidationResultType
import com.intellij.internal.statistic.eventLog.validator.rules.EventContext
import com.intellij.internal.statistic.eventLog.validator.rules.impl.CustomValidationRule
import com.intellij.internal.statistic.service.fus.collectors.CounterUsagesCollector
import com.intellij.internal.statistic.utils.getPluginInfoByDescriptor
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.extensions.ExtensionPointListener
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.extensions.PluginDescriptor
import com.intellij.ui.GotItTooltip
import com.intellij.util.xmlb.annotations.Attribute


class GotItTooltipAllowlistEP {
  companion object {
    @JvmField
    val EP_NAME : ExtensionPointName<GotItTooltipAllowlistEP> = ExtensionPointName.create("com.intellij.gotItTooltipAllowlist")

  }

  @Attribute("prefix")
  var prefix: String = ""
}

class GotItUsageCollector private constructor() {
  companion object {
    @JvmStatic
    val instance: GotItUsageCollector
      get() = ApplicationManager.getApplication().getService(GotItUsageCollector::class.java)
  }


  private val allowedPrefixes : MutableSet<String> = HashSet()

  init {
    GotItTooltipAllowlistEP.EP_NAME.processWithPluginDescriptor(this::addToWhileList)
    GotItTooltipAllowlistEP.EP_NAME.addExtensionPointListener(object: ExtensionPointListener<GotItTooltipAllowlistEP> {
      override fun extensionAdded(extension: GotItTooltipAllowlistEP, pluginDescriptor: PluginDescriptor) {
        addToWhileList(extension, pluginDescriptor)
      }

      override fun extensionRemoved(extension: GotItTooltipAllowlistEP, pluginDescriptor: PluginDescriptor) {
        allowedPrefixes.remove(extension.prefix)
      }
    }, null)

  }

  private fun addToWhileList(extension : GotItTooltipAllowlistEP, plugin : PluginDescriptor) {
    if (getPluginInfoByDescriptor(plugin).isDevelopedByJetBrains() && extension.prefix.isNotEmpty()) {
      allowedPrefixes.add(extension.prefix)
    }
  }

  fun logOpen(id: String, count: Int) {
    toPrefix(id)?.let { GotItUsageCollectorGroup.showEvent.log(it, count) }
  }

  fun logClose(id: String, closeType: GotItUsageCollectorGroup.CloseType) {
    toPrefix(id)?.let { GotItUsageCollectorGroup.closeEvent.log(id, closeType) }
  }

  private fun toPrefix(id: String): String? {
    if (allowedPrefixes.contains(id)) {
      return id
    }

    var prefix = ""
    for (fragment in id.split(".")) {
      prefix = if (prefix.isEmpty()) fragment else "$prefix.$fragment"
      if (allowedPrefixes.contains(prefix)) {
        return prefix
      }
    }
    return null
  }
}

class GotItUsageCollectorGroup : CounterUsagesCollector() {
  override fun getGroup(): EventLogGroup = GROUP

  enum class CloseType(private val text: String) {
    ButtonClick("click.button"),
    LinkClick("click.link"),
    OutsideClick("click.outside"),
    AncestorRemoved("ancestor.removed"),
    EscapeShortcutPressed("escape.shortcut.pressed"),
    Timeout("timeout");

    override fun toString(): String = text
  }

  companion object {
    private val GROUP = EventLogGroup("gotit.usage.collector", 1)

    internal val showEvent = GROUP.registerEvent("show",
                                                 EventFields.StringValidatedByCustomRule("id_prefix", GotItTooltip.PROPERTY_PREFIX),
                                                 EventFields.Int("count"))

    internal val closeEvent = GROUP.registerEvent("close",
                                                  EventFields.StringValidatedByCustomRule("id_prefix", GotItTooltip.PROPERTY_PREFIX),
                                                  EventFields.Enum<CloseType>("type"))
  }
}

class GotItIDValidator : CustomValidationRule() {
  override fun doValidate(data: String, context: EventContext): ValidationResultType {
    return acceptWhenReportedByJetBrainsPlugin(context)
  }

  override fun acceptRuleId(ruleId: String?): Boolean {
    return GotItTooltip.PROPERTY_PREFIX == ruleId
  }
}
{'fraction_non_alphanumeric': 0.09328400044848077, 'fraction_numerical': 0.0031393653997084875, 'mean_word_length': 3.972129319955407, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5330789', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2488, 'n_words': 448}