<commit_before>require "json"
require "logstash/time"
require "logstash/namespace"
require "uri"

# General event type. Will expand this in the future.
class LogStash::Event
  public
  def initialize(data=Hash.new)
    @cancelled = false
    @data = {
      "@source" => "unknown",
      "@type" => nil,
      "@tags" => [],
      "@fields" => {},
    }.merge(data)

    if !@data.include?("@timestamp")
      @data["@timestamp"] = LogStash::Time.now.utc.to_iso8601
    end
  end # def initialize

  public
  def self.from_json(json)
    return LogStash::Event.new(JSON.parse(json))
  end # def self.from_json

  public
  def cancel
    @cancelled = true
  end

  public
  def cancelled?
    return @cancelled
  end

  public
  def to_s
    return "#{timestamp} #{source}: #{message}"
  end # def to_s

  public
  def timestamp; @data["@timestamp"]; end # def timestamp
  def timestamp=(val); @data["@timestamp"] = val; end # def timestamp=

  public
  def source; @data["@source"]; end # def source
  def source=(val) 
    if val.is_a?(URI)
      @data["@source"] = val.to_s
      @data["@source_host"] = val.host
      @data["@source_path"] = val.path
    else
      @data["@source"] = val
    end
  end # def source=

  public
  def message; @data["@message"]; end # def message
  def message=(val); @data["@message"] = val; end # def message=

  public
  def type; @data["@type"]; end # def type<commit_msg>- Add LogStash::Event#format(str) for sprintf-like functionality for an   event. - Updated #to_s to use the new format method.<commit_after>require "json"
require "logstash/time"
require "logstash/namespace"
require "uri"

# General event type. Will expand this in the future.
class LogStash::Event
  public
  def initialize(data=Hash.new)
    @cancelled = false
    @data = {
      "@source" => "unknown",
      "@type" => nil,
      "@tags" => [],
      "@fields" => {},
    }.merge(data)

    if !@data.include?("@timestamp")
      @data["@timestamp"] = LogStash::Time.now.utc.to_iso8601
    end
  end # def initialize

  public
  def self.from_json(json)
    return LogStash::Event.new(JSON.parse(json))
  end # def self.from_json

  public
  def cancel
    @cancelled = true
  end

  public
  def cancelled?
    return @cancelled
  end

  public
  def to_s
    return self.format("%{@timestamp} %{@source}: %{@message}"
  end # def to_s

  # Return a string with this event's data formatted into it
  # Format strings are
  # %{key} where 'key' is a field in the event. @source and friends are valid,
  # too.
  public
  def format(formatstring)
    return formatstring.gsub(/%\{[^}]+\}/) do |tok|
      key = tok[2 ... -1]
      value = self[key]
      if value.nil?
        tok # leave the %{foo} if this field does not exist in this event.
      elsif value.is_a?(Array)
        value.join(",") # Join by ',' if value is an rray
      else
        value # otherwise return the value
      end
    end
  end # def format

  public
  def timestamp; @data["@timestamp"]; end # def timestamp
  def timestamp=(val); @data["@timestamp"] = val; end # def timestamp=

  public
  def source; @data["@source"]; end # def source
  def source=(val) 
    if val.is_a?(URI)
      @data["@source"] = val.to_s
      @data["@source_host"] = val.host
      @data["@source_path"] = val.path
    else
      @data["@source"] = val
    end
  end # def source=

  public
  def message; @data["@message"]; end # def message
  def message=(val); @data["@message"] = val; end # def message=

  public
  def type; @data["@type"]; end # def type
{'fraction_non_alphanumeric': 0.14592760180995476, 'fraction_numerical': 0.002828054298642534, 'mean_word_length': 2.9563758389261743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '7209685', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1175, 'n_words': 349}