<commit_before>require 'digest/md5'

module Crashbreak
  class DatabaseDumper < BasicFormatter
    include Crashbreak::AWS

    def dump
      prepare_aws

      if dump_not_exist_yet?
        dump_database
        upload_dump
        remove_locally_dump
      end

      { file_name: aws_file_name }
    end

    private

    def dump_database
      system(dump_command)
    end

    def upload_dump
      client.put_object(bucket: bucket_name, key: aws_file_name, body: File.read(dump_location))
    end

    def remove_locally_dump
      File.delete(dump_location)
    end

    def aws_file_name
      "Crashbreak - database dump #{Digest::MD5.hexdigest(ENV['RACK_ENV'] + exception.class.to_s + exception.backtrace[0])}"
    end

    def dump_not_exist_yet?
      !aws_resource_bucket.object(aws_file_name).exists?
    end

    def dump_location
      Crashbreak.configure.dumper_options[:dump_location]
    end

    def dump_command
      Crashbreak.configure.dumper_options[:dump_command]<commit_msg>Database dumper - change dump file name (add timestamp)<commit_after>require 'digest/md5'

module Crashbreak
  class DatabaseDumper < BasicFormatter
    include Crashbreak::AWS

    def dump
      prepare_aws
      dump_database
      upload_dump
      remove_locally_dump

      { file_name: aws_file_name }
    end

    private

    def dump_database
      system(dump_command)
    end

    def upload_dump
      client.put_object(bucket: bucket_name, key: aws_file_name, body: File.read(dump_location))
    end

    def remove_locally_dump
      File.delete(dump_location)
    end

    def aws_file_name
      @aws_file_name ||= "Crashbreak - database dump #{Time.now} (#{Time.now.to_f})"
    end

    def dump_location
      Crashbreak.configure.dumper_options[:dump_location]
    end

    def dump_command
      Crashbreak.configure.dumper_options[:dump_command]
{'fraction_non_alphanumeric': 0.06784188034188034, 'fraction_numerical': 0.002136752136752137, 'mean_word_length': 3.0107066381156318, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5084201', 'n_tokens_mistral': 609, 'n_tokens_neox': 589, 'n_words': 117}