Commit 641b818d authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add simple statistics module.

Turned on with --stats option, a new static tab appears that
shows various statistics per class. For now there's just a table
that lists:

- nr of members
- nr of non-inherited members
- fan-in
- fan-out
parent 33f7f9d2
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ require 'jsduck/class_formatter'
require 'jsduck/class'
require 'jsduck/icons'
require 'jsduck/search_data'
require 'jsduck/stats'
require 'jsduck/relations'
require 'jsduck/inherit_doc'
require 'jsduck/exporter'
@@ -179,6 +180,7 @@ module JsDuck
        :videos => @videos.to_array,
        :examples => @examples.to_array,
        :search => SearchData.new.create(@relations.classes),
        :stats => @opts.stats ? Stats.new.create(@relations.classes) : [],
      }) + ";\n"
      File.open(@opts.output_dir+"/data.js", 'w') {|f| f.write(js) }
    end
+6 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ module JsDuck
    attr_accessor :guides
    attr_accessor :videos
    attr_accessor :examples
    attr_accessor :stats
    attr_accessor :categories_path
    attr_accessor :pretty_json
    attr_accessor :images
@@ -84,6 +85,7 @@ module JsDuck
      @guides = nil
      @videos = nil
      @examples = nil
      @stats = false
      @categories_path = nil
      @pretty_json = false
      @images = []
@@ -209,6 +211,10 @@ module JsDuck
          @examples = canonical(path)
        end

        opts.on('--stats', "Creates page with all kinds of statistics.", " ") do
          @stats = true
        end

        opts.on('--categories=PATH',
          "Path to JSON file which defines categories for classes.", " ") do |path|
          @categories_path = canonical(path)

lib/jsduck/stats.rb

0 → 100644
+56 −0
Original line number Diff line number Diff line

module JsDuck

  # Calculates all kinds of statistics for classes
  class Stats
    # Maps array of classes into array of stats per class
    def create(classes)
      @classes = classes

      classes.map do |cls|
        {
          :name => cls[:name],
          :members => cls.all_members.length,
          :localMembers => cls.all_local_members.length,
          :fanIn => fan_in(cls),
          :fanOut => fan_out(cls),
        }
      end
    end

    # How many classes depend on this class
    def fan_in(cls)
      fan_in_table[cls[:name]] || 0
    end

    # On how many classes this class depends on
    def fan_out(cls)
      dependencies(cls).length
    end

    # list of class names the class depends on
    def dependencies(cls)
      [
        cls[:extends],
        cls[:mixins],
        cls[:requires],
        cls[:uses],
      ].compact.flatten.sort.uniq
    end

    # Returns map of class names to its fan-in number.
    def fan_in_table
      return @fi_table if @fi_table

      @fi_table = {}
      @classes.each do |cls|
        dependencies(cls).each do |d|
          @fi_table[d] = (@fi_table[d] || 0) + 1
        end
      end
      @fi_table
    end

  end

end
+1 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ Ext.define('Docs.Application', {
        'Examples',
        'Guides',
        'Videos',
        'Stats',
        'Tabs'
    ],

+4 −1
Original line number Diff line number Diff line
@@ -64,6 +64,9 @@ Ext.define("Docs.History", {
        else if (url.type === "example") {
            Docs.App.getController('Examples').loadExample(url.url, true);
        }
        else if (url.url === "#!/stats") {
            Docs.App.getController('Stats').loadIndex();
        }
        else if (url.url === "#!/comment") {
            Docs.App.getController('Comments').loadIndex();
        }
@@ -80,7 +83,7 @@ Ext.define("Docs.History", {

    // Parses current browser location
    parseToken: function(token) {
        var matches = token && token.match(/!?(\/(api|guide|example|video|comment)(\/(.*))?)/);
        var matches = token && token.match(/!?(\/(api|guide|example|video|stats|comment)(\/(.*))?)/);
        return matches ? {type: matches[2], url: "#!"+matches[1]} : {};
    },

Loading