<commit_before>class GDash
  class Dashboard
    attr_accessor :properties

    def initialize(short_name, graph_templates, category, options={}, graphite_render="")

      @properties = {:graph_width => nil,
                     :graph_height => nil,
                     :graph_from => nil,
                     :graph_until => nil}

      @properties[:short_name] = short_name
      @properties[:graph_templates] = graph_templates
      @properties[:category] = category
      @properties[:directory] = File.join(graph_templates, category, short_name)
      @properties[:graphite_render] = graphite_render
      
      raise "Cannot find dashboard directory #{directory}" unless File.directory?(directory)

      @properties[:yaml] = File.join(directory, "dash.yaml")
      
      raise "Cannot find YAML file #{yaml}" unless File.exists?(yaml)


      @properties.merge!(YAML.load_file(yaml))

      if @properties[:include_properties] == nil || @properties[:include_properties].empty?
        property_includes = []
      elsif @properties[:include_properties].is_a? Array
        property_includes = @properties[:include_properties]
      elsif @properties[:include_properties].is_a? String
        property_includes = [@properties[:include_properties]]
      else
        raise "Invalid value for include_properties in #{File.join(directory, 'dash.yaml')}"
      end

      for property_file in property_includes
        yaml_file = File.join(graph_templates, property_file)
        if File.exist?(yaml_file)
          @properties.rmerge!(YAML.load_file(yaml_file))
        end
      end

      # Properties defined in dashboard config file are overridden when given on initialization
      @properties[:graph_width] = options.delete(:width) || graph_width
      @properties[:graph_height] = options.delete(:height) || graph_height
      @properties[:graph_from] = options.delete(:from) || graph_from
      @properties[:graph_until] = options.delete(:until) || graph_until

      #Graphite defined in gdash.yaml is overwritten if set in dash.yaml
      if !(@properties[:graphite] == nil || @properties[:graphite].empty?)
        @properties[:graphite_render] = @properties[:graphite]+"/render"
      end
    end

    def list_graphs(directories)
      graphs = {}
      directories.each { |directory|
        current_graphs = Dir.entries(directory).select {|f| f.match(/\.graph$/)}
        current_graphs.each { |graph_filename|  
          graph_name = File.basename(graph_filename, ".graph")
          graphs[graph_name] = File.join(directory, graph_filename) 
        }
      }
      graphs
    end

    def graphs(options={})
      options[:width] ||= graph_width
      options[:height] ||= graph_height
      options[:from] ||= graph_from
      options[:until] ||= graph_until

      overrides = options.reject { |k,v| v.nil? }
      overrides = overrides.merge!(@properties[:graph_properties]) if @properties[:graph_properties]

      if @properties[:include_graphs] == nil || @properties[:include_graphs].empty?
        graph_includes = []
      elsif @properties[:include_graphs].is_a? Array
        graph_includes = @properties[:include_graphs]
      elsif @properties[:include_graphs].is_a? String
        graph_includes = [@properties[:include_graphs]]
      else
        raise "Invalid value for include in #{File.join(directory, 'graph.yaml')}"
      end

      directories = graph_includes.map { |d|
        File.join(graph_templates, d)
      }
      directories << directory

      graphs = list_graphs(directories)

      graphs.keys.sort.map do |graph_name|
        {:name => graph_name, 
         :graphite => GraphiteGraph.new(graphs[graph_name], overrides)}
      end
    end

    def graph_by_name(name, options={})
      options[:width] ||= graph_width
      options[:height] ||= graph_height
      options[:from] ||= graph_from
      options[:until] ||= graph_until

      graph = "#{name}.graph"

      overrides = options.reject { |k,v| v.nil? }

      {:name => File.basename(graph, ".graph"), :graphite => GraphiteGraph.new(File.join(directory, graph), overrides)}
    end

    def method_missing(method, *args)
      if properties.include?(method)
        properties[method]
      else
        super
      end
    end
  end
end<commit_msg>First pass at fixing graphy_by_name<commit_after>class GDash
  class Dashboard
    attr_accessor :properties

    def initialize(short_name, graph_templates, category, options={}, graphite_render="")

      @properties = {:graph_width => nil,
                     :graph_height => nil,
                     :graph_from => nil,
                     :graph_until => nil}

      @properties[:short_name] = short_name
      @properties[:graph_templates] = graph_templates
      @properties[:category] = category
      @properties[:directory] = File.join(graph_templates, category, short_name)
      @properties[:graphite_render] = graphite_render

      raise "Cannot find dashboard directory #{directory}" unless File.directory?(directory)

      @properties[:yaml] = File.join(directory, "dash.yaml")

      raise "Cannot find YAML file #{yaml}" unless File.exists?(yaml)


      @properties.merge!(YAML.load_file(yaml))

      if @properties[:include_properties] == nil || @properties[:include_properties].empty?
        property_includes = []
      elsif @properties[:include_properties].is_a? Array
        property_includes = @properties[:include_properties]
      elsif @properties[:include_properties].is_a? String
        property_includes = [@properties[:include_properties]]
      else
        raise "Invalid value for include_properties in #{File.join(directory, 'dash.yaml')}"
      end

      for property_file in property_includes
        yaml_file = File.join(graph_templates, property_file)
        if File.exist?(yaml_file)
          @properties.rmerge!(YAML.load_file(yaml_file))
        end
      end

      # Properties defined in dashboard config file are overridden when given on initialization
      @properties[:graph_width] = options.delete(:width) || graph_width
      @properties[:graph_height] = options.delete(:height) || graph_height
      @properties[:graph_from] = options.delete(:from) || graph_from
      @properties[:graph_until] = options.delete(:until) || graph_until

      if @properties[:include_graphs] == nil || @properties[:include_graphs].empty?
        graph_includes = []
      elsif @properties[:include_graphs].is_a? Array
        graph_includes = @properties[:include_graphs]
      elsif @properties[:include_graphs].is_a? String
        graph_includes = [@properties[:include_graphs]]
      else
        raise "Invalid value for include in #{File.join(directory, 'dash.yaml')}"
      end

      @directories = graph_includes.map { |d|
        File.join(graph_templates, d)
      }
      @directories << directory

      #Graphite defined in gdash.yaml is overwritten if set in dash.yaml
      if !(@properties[:graphite] == nil || @properties[:graphite].empty?)
        @properties[:graphite_render] = @properties[:graphite]+"/render"
      end
    end

    def list_graphs
      graphs = {}
      @directories.each { |directory|
        current_graphs = Dir.entries(directory).select {|f| f.match(/\.graph$/)}
        current_graphs.each { |graph_filename|  
          graph_name = File.basename(graph_filename, ".graph")
          graphs[graph_name] = File.join(directory, graph_filename) 
        }
      }
      graphs
    end

    def graphs(options={})
      options[:width] ||= graph_width
      options[:height] ||= graph_height
      options[:from] ||= graph_from
      options[:until] ||= graph_until

      overrides = options.reject { |k,v| v.nil? }
      overrides = overrides.merge!(@properties[:graph_properties]) if @properties[:graph_properties]

      graphs = list_graphs

      graphs.keys.sort.map do |graph_name|
        {:name => graph_name, 
          :graphite => GraphiteGraph.new(graphs[graph_name], overrides)}
      end
    end

    def graph_by_name(name, options={})
      options[:width] ||= graph_width
      options[:height] ||= graph_height
      options[:from] ||= graph_from
      options[:until] ||= graph_until

      overrides = options.reject { |k,v| v.nil? }
      overrides = overrides.merge!(@properties[:graph_properties]) if @properties[:graph_properties]

      graphs = list_graphs

      {:name => name, :graphite => GraphiteGraph.new(graphs[name], overrides)}
    end

    def method_missing(method, *args)
      if properties.include?(method)
        properties[method]
      else
        super
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11993950674732434, 'fraction_numerical': 0.0, 'mean_word_length': 3.0744075829383886, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '7020998', 'n_tokens_mistral': 2461, 'n_tokens_neox': 2371, 'n_words': 509}