<commit_before>module Lita
  # Executes a block of code when a window of time includes only
  # times that haven't been executed before.
  #
  # Use this when you have some code that requires a start time and and end time, and
  # you want to execute it roughly every half hour.
  #
  # Redis is used to persist the end of the last window that executed and ensure the
  # block doesn't execute again until that window is passed.
  #
  #     window = SlidingWindow.new("my-sliding-window", redis)
  #     window.advance(duration_minutes: 30) do |window_start, window_end|
  #       puts "#{window_start} -> #{window_end}"
  #     end
  #
  # Call this as often as you like, and the block passed to advance() will
  # only execute if it's been 30 minutes since the last time it executed.
  #
  class SlidingWindow
    def initialize(name, redis)
      @name, @redis = name, redis

      initialise_last_time_if_not_set
    end

    def advance(duration_minutes: 30, buffer_minutes: 0, &block)
      start_time = Time.now - mins_to_seconds(duration_minutes) - mins_to_seconds(buffer_minutes)
      advance_to = start_time + mins_to_seconds(duration_minutes)

      puts "start_time: #{start_time} advance_to: #{advance_to} last_time: #{last_time}"
      return unless start_time > last_time

      yield last_time, advance_to 

      @redis.set(@name, advance_to.to_i)
    end

    private

    def mins_to_seconds(mins)<commit_msg>Revert "add some debugging to sliding window"<commit_after>module Lita
  # Executes a block of code when a window of time includes only
  # times that haven't been executed before.
  #
  # Use this when you have some code that requires a start time and and end time, and
  # you want to execute it roughly every half hour.
  #
  # Redis is used to persist the end of the last window that executed and ensure the
  # block doesn't execute again until that window is passed.
  #
  #     window = SlidingWindow.new("my-sliding-window", redis)
  #     window.advance(duration_minutes: 30) do |window_start, window_end|
  #       puts "#{window_start} -> #{window_end}"
  #     end
  #
  # Call this as often as you like, and the block passed to advance() will
  # only execute if it's been 30 minutes since the last time it executed.
  #
  class SlidingWindow
    def initialize(name, redis)
      @name, @redis = name, redis

      initialise_last_time_if_not_set
    end

    def advance(duration_minutes: 30, buffer_minutes: 0, &block)
      start_time = Time.now - mins_to_seconds(duration_minutes) - mins_to_seconds(buffer_minutes)
      advance_to = start_time + mins_to_seconds(duration_minutes)

      return unless start_time > last_time

      yield last_time, advance_to 

      @redis.set(@name, advance_to.to_i)
    end

    private

    def mins_to_seconds(mins)
{'fraction_non_alphanumeric': 0.073545162441985, 'fraction_numerical': 0.004998214923241699, 'mean_word_length': 3.324074074074074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4595186', 'n_tokens_mistral': 904, 'n_tokens_neox': 848, 'n_words': 306}