<commit_before>require 'sequel'
require 'digest'
require 'psych'
require 'json'
require 'sequelizer'
require 'conceptql/query'
require 'conceptql/graph'
require 'conceptql/fake_grapher'
require_relative 'hashable'

Sequel::Model.db = Sequel.connect(Psych.load_file('config/database.yml')['site_data'])
class Example < Sequel::Model
  include Sequelizer
  include Hashable

  def hash_id
    hash_it(id)
  end

  def sql
    ConceptQL::Query.new(data_db, parsed_statement).sql
  end

  def image_path
    output_file = Pathname.new('public') + image_hash
    file = Pathname.new(output_file.to_s + '.png')
    graph_it(parsed_statement, output_file.to_s) unless file.exist?
    file.basename.to_s
  end

  def parsed_statement
    @parsed_statement ||= JSON.parse(statement)
  end

  def yaml_statement
    @yaml_statement ||= parsed_statement.to_yaml
  end

  private
  def data_db
    @data_db ||= new_db(Psych.load_file('config/database.yml')['data'])
  end

  def graph_it(statement, file)
    ConceptQL::Graph.new(statement,
                         dangler: true,
                         suffix: 'png',
                         db: db
                        ).graph_it(file)
  rescue LoadError
    ConceptQL::FakeGrapher.new(suffix: 'png').graph_it(statement, file)
  end

  def image_hash
    @image_hash ||= Digest::SHA256.hexdigest([ statement, sql, database_config ].join)
  end

  def database_config
    Psych.load_file('config/database.yml')['data']
  end
end<commit_msg>Example: fix bug so graph_it uses data_db<commit_after>require 'sequel'
require 'digest'
require 'psych'
require 'json'
require 'sequelizer'
require 'conceptql/query'
require 'conceptql/graph'
require 'conceptql/fake_grapher'
require_relative 'hashable'

Sequel::Model.db = Sequel.connect(Psych.load_file('config/database.yml')['site_data'])
class Example < Sequel::Model
  include Sequelizer
  include Hashable

  def hash_id
    hash_it(id)
  end

  def sql
    ConceptQL::Query.new(data_db, parsed_statement).sql
  end

  def image_path
    output_file = Pathname.new('public') + image_hash
    file = Pathname.new(output_file.to_s + '.png')
    graph_it(parsed_statement, output_file.to_s) unless file.exist?
    file.basename.to_s
  end

  def parsed_statement
    @parsed_statement ||= JSON.parse(statement)
  end

  def yaml_statement
    @yaml_statement ||= parsed_statement.to_yaml
  end

  private
  def data_db
    @data_db ||= new_db(Psych.load_file('config/database.yml')['data'])
  end

  def graph_it(statement, file)
    ConceptQL::Graph.new(statement,
                         dangler: true,
                         suffix: 'png',
                         db: data_db
                        ).graph_it(file)
  rescue LoadError
    ConceptQL::FakeGrapher.new(suffix: 'png').graph_it(statement, file)
  end

  def image_hash
    @image_hash ||= Digest::SHA256.hexdigest([ statement, sql, database_config ].join)
  end

  def database_config
    Psych.load_file('config/database.yml')['data']
  end
end
{'fraction_non_alphanumeric': 0.1088911088911089, 'fraction_numerical': 0.001998001998001998, 'mean_word_length': 3.334776334776335, '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': '2578708', 'n_tokens_mistral': 1034, 'n_tokens_neox': 979, 'n_words': 199}