<commit_before>#
# generates visual displays of current_user's social information
#

# requires:
# apt-get install graphviz
#

require 'graphviz'


class Me::InfovizController < Me::BaseController

  def visualize
    format = params[:format] || 'svg'

    g = GraphViz::new( "structs", :output => "svg" )

    g.node["shape"] = "plaintext"

    g.add_node( "HTML" )

    g.add_node( "struct1", "html" => '<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">  <TR><TD>left</TD><TD PORT="f1">mid dle</TD><TD PORT="f2">right</TD></TR> </TABLE>>]; struct2 [label=< <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">  <TR><TD PORT="f0">one</TD><TD>two</TD></TR> </TABLE>')

    g.add_node( "struct2", "html" => '<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">  <TR><TD PORT="f0">one</TD><TD>two</TD></TR> </TABLE>' )
    g.add_node( "struct3", "html" => '<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">  <TR>  <TD ROWSPAN="3">hello<BR/>world</TD>  <TD COLSPAN="3">b</TD>  <TD ROWSPAN="3">g</TD>  <TD ROWSPAN="3">h</TD>  </TR>  <TR>  <TD>c</TD><TD PORT="here">d</TD><TD>e</TD>  </TR>  <TR>  <TD COLSPAN="3">f</TD>  </TR> </TABLE>' )

    g.add_edge( "struct1:f1", "struct2:f0" )
    g.add_edge( "struct1:f2", "struct3:here" )

    g.add_edge( "HTML", "struct1" )

    send_data(g.output(:output => format),
              :type => Media::MimeType.mime_type_from_extension(format),
              :disposition => 'inline')

  end

  protected<commit_msg>infoviz controller is showing something cool.<commit_after>#
# generates visual displays of current_user's social information
#

# requires:
# apt-get install graphviz
#

require 'graphviz'


class Me::InfovizController < Me::BaseController

  def visualize
    format = params[:format] || 'svg'

    @pages = Page.find_by_path(params[:path], options_for_me)
    @activities = Activity.for_dashboard(current_user).newest.unique.find(:all, :limit => 12)

    g = GraphViz::new( "structs", :output => "svg" )
    g[:rankdir] = "LR"

    # set global node options
    #g.node[:color]    = "#ddaa66"
    g.node[:style]    = "filled"
    g.node["shape"] = "rect"
    #g.node[:shape]    = "box"
    #g.node[:penwidth] = "1"
    g.node[:fontname] = "Trebuchet MS"
    g.node[:fontsize] = "12"
    #g.node[:fillcolor]= "#ffeecc"
    #g.node[:fontcolor]= "#775500"
    #g.node[:margin]   = "0.0"

    # set global edge options
    g.edge[:dir] = "none"
    g.edge[:color]    = "#999999"
    g.edge[:weight]   = "1"
    g.edge[:fontsize] = "6"
    #g.edge[:fontcolor]= "#444444"
    #g.edge[:fontname] = "Verdana"
    #g.edge[:dir]      = "forward"
    #g.edge[:arrowsize]= "0.5"




    # add cluster for users
    uc = g.add_graph('users')
    uc.add_node('USERS', :label => 'users', :shape => 'diamond', :href => '/people')

    # add node for self
    @user = current_user
    uc.add_node @user.id.to_s,
      :label => @user.login, :href => url_for_user(@user),
      :tooltip => @user.display_name, :shape => "ellipse"
    uc.add_edge 'USERS', @user.id.to_s, :weight => "2", :color => 'blue'

    # add nodes for all contacts
    @contacts = @user.contacts

    @contacts.each do |c|
      uc.add_node c.id.to_s,
        :label => c.login, :href => url_for_user(c),
        :tooltip => 'Last seen: ' + c.last_seen_at.to_s,
        :shape => "ellipse"
      uc.add_edge 'USERS', c.id.to_s, :weight => "2", :color => 'blue'
    end

    # add edges from self to all contacts
    @contacts.each do |c|
      uc.add_edge @user.id.to_s, c.id.to_s
    end

    # add edges between all contacts
    @contacts.each do |c|
      c.contact_ids.each do |id|
        if c.id < id # and @user.contact_ids.include? id
          uc.add_edge c.id.to_s, id.to_s
        end
      end
    end


    # add cluster for groups
    gc = g.add_graph('groups')
    gc.add_node 'GROUPS', :label => 'groups', :href => '/groups', :shape => 'diamond', :color => 'red'

    # add nodes for all member groups
    @groups = @user.all_groups

    @groups.each do |gr|
      gc.add_node 'group_%d' / gr.id, :label => gr.display_name,
        :color => 'red', :shape => 'rect', :href => url_for_group(gr)
      gc.add_edge 'GROUPS', 'group_%d' / gr.id, :color => 'blue'

      # add edges to all committees
      gr.committees.each do |c|
        gc.add_edge 'group_%d' / gr.id, 'group_%d' / c.id, :weight => "200",
          :color => '#880000', :style => "dashed"
      end

      # add edges to all networks
      gr.networks.each do |n|
        gc.add_edge 'group_%d' / n.id, 'group_%d' / gr.id, :weight => "100",
          :color => '#AA4444', :style => "dashed"
      end
    end

    # add edges between @user and all member groups
    @user.group_ids.each do |id|
      g.add_edge @user.id.to_s, 'group_%d' / id
    end

    # add edges between all contacts and all member groups
    @contacts.each do |c|
      c.groups.each do |gr|
        if @groups.include? gr
          g.add_edge c.id.to_s, 'group_%d' / gr.id
        end
      end
    end


    #require 'ruby-debug'; debugger
    # :use can be %w(dot neato twopi circo fdp)
    out_str = g.output(:use => 'fdp', :output => format)

    send_data(out_str,
              :type => Media::MimeType.mime_type_from_extension(format),
              :disposition => 'inline')

  end

  protected
{'fraction_non_alphanumeric': 0.18362706083001706, 'fraction_numerical': 0.015728633693386392, 'mean_word_length': 2.8638360175695463, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 98, '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': '2125669', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1995, 'n_words': 443}