<commit_before># Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/

class StatsStore < ApplicationModel
  store :data

=begin

  count = StatsStore.count_by_search(
    object: 'User',
    o_id: ticket.owner_id,
    key: 'ticket:reopen',
    start:  Time.zone.now - 7.days,
    end:    Time.zone.now,
  )

=end

  def self.count_by_search(data)

    # lookups
    if data[:object]
      object_id = ObjectLookup.by_name( data[:object] )
    end

    StatsStore.where(stats_store_object_id: object_id, o_id: data[:o_id], key: data[:key])
              .where('created_at > ? AND created_at < ?', data[:start], data[:end]).count
  end

=begin

  item = StatsStore.search(
    object: 'User',
    o_id: current_user.id,
    key: 'dashboard',
  )

=end

  def self.search(data)

    # lookups
    if data[:object]
      data[:stats_store_object_id] = ObjectLookup.by_name( data[:object] )
      data.delete(:object)
    end

    find_by(data)
  end

=begin

  item = StatsStore.snyc(
    object: 'User',
    o_id: current_user.id,
    key: 'dashboard',
    data: {some data},
  )

=end

  def self.sync(params)

    data = params[:data]
    params.delete(:data)

    item = search(params)

    if item
      item.data = data
      item.save
      return item
    end

    # lookups
    if data[:object]
      data[:stats_store_object_id] = ObjectLookup.by_name( data[:object] )
      data.delete(:object)<commit_msg>Fixed typo.<commit_after># Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/

class StatsStore < ApplicationModel
  store :data

=begin

  count = StatsStore.count_by_search(
    object: 'User',
    o_id: ticket.owner_id,
    key: 'ticket:reopen',
    start:  Time.zone.now - 7.days,
    end:    Time.zone.now,
  )

=end

  def self.count_by_search(data)

    # lookups
    if data[:object]
      object_id = ObjectLookup.by_name( data[:object] )
    end

    StatsStore.where(stats_store_object_id: object_id, o_id: data[:o_id], key: data[:key])
              .where('created_at > ? AND created_at < ?', data[:start], data[:end]).count
  end

=begin

  item = StatsStore.search(
    object: 'User',
    o_id: current_user.id,
    key: 'dashboard',
  )

=end

  def self.search(data)

    # lookups
    if data[:object]
      data[:stats_store_object_id] = ObjectLookup.by_name( data[:object] )
      data.delete(:object)
    end

    find_by(data)
  end

=begin

  item = StatsStore.sync(
    object: 'User',
    o_id: current_user.id,
    key: 'dashboard',
    data: {some data},
  )

=end

  def self.sync(params)

    data = params[:data]
    params.delete(:data)

    item = search(params)

    if item
      item.data = data
      item.save
      return item
    end

    # lookups
    if data[:object]
      data[:stats_store_object_id] = ObjectLookup.by_name( data[:object] )
      data.delete(:object)
{'fraction_non_alphanumeric': 0.1300557880055788, 'fraction_numerical': 0.006276150627615063, 'mean_word_length': 2.7700394218134035, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1846130', 'n_tokens_mistral': 1067, 'n_tokens_neox': 1009, 'n_words': 210}