Class: Birdwatcher::Modules::Users::InfluenceGraph

Inherits:
Birdwatcher::Module show all
Defined in:
lib/birdwatcher/modules/users/influence_graph.rb

Constant Summary

Constants inherited from Birdwatcher::Module

Birdwatcher::Module::MODULE_PATH

Constants included from Concerns::Concurrency

Concerns::Concurrency::DEFAULT_THREAD_POOL_SIZE

Constants included from Concerns::Core

Concerns::Core::DATA_DIRECTORY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Birdwatcher::Module

_file_path, _file_path=, descendants, #execute, inherited, meta, meta=, module_by_path, module_paths, modules, path

Methods included from Concerns::Concurrency

included, #thread_pool

Methods included from Concerns::Persistence

included, #save_status, #save_user

Methods included from Concerns::Presentation

included, #make_status_summary_output, #make_url_summary_output, #make_user_details_output, #make_user_summary_output, #output_status_summary, #output_user_details, #output_user_summary, #page_text

Methods included from Concerns::Outputting

#confirm, #error, #fatal, included, #info, #line_separator, #newline, #output, #output_formatted, #task, #warn

Methods included from Concerns::Util

#escape_html, #excerpt, included, #parse_time, #pluralize, #strip_control_characters, #strip_html, #suppress_output, #suppress_warnings, #time_ago_in_words, #unescape_html

Methods included from Concerns::Core

#console, #current_workspace, #current_workspace=, #database, included, #klout_client, #read_data_file, #twitter_client

Class Method Details

.infoObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/birdwatcher/modules/users/influence_graph.rb', line 28

def self.info
<<-INFO
The Influence Graph module generates an influence graph between users in the
currently active workspace. The graph can be used to identify who each user is
being influenced by as well as who each user influences.

The influence information is retrieved by the #{'users/klout_influence'.bold} so
be sure to run that module before running this one.

The generated graph is in PNG format.
INFO
end

Instance Method Details

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/birdwatcher/modules/users/influence_graph.rb', line 41

def run
  if !GraphViz::Constants::FORMATS.include?(option_setting("FORMAT"))
    error("Unsupported format: #{option_setting('FORMAT').bold}")
    return false
  end
  if screen_names = option_setting("USERS")
    users = current_workspace.users_dataset
      .where("screen_name IN ?", screen_names.split(" ").map(&:strip))
      .order(:screen_name)
      .eager(:influencers, :influencees)
  else
    users = current_workspace.users_dataset.order(:screen_name).eager(:influencers, :influencees)
  end
  if users.empty?
    error("There are no users to process")
    return false
  end
  graph = GraphViz.new(:G, :type => :digraph)
  users_in_workspace = current_workspace.users.map(&:screen_name)
  nodes = {}
  influences = {}
  users.each do |user|
    influences[user.screen_name] ||= []
    influences[user.screen_name] += user.influencees.select { |i| users_in_workspace.include?(i.screen_name) }.map(&:screen_name)
    user.influencers.select { |i| users_in_workspace.include?(i.screen_name) }.map(&:screen_name).each do |influencer|
      influences[influencer] ||= []
      influences[influencer] << user.screen_name unless influences[influencer].include?(user.screen_name)
    end
  end
  influences.each_pair do |user, influence|
    influence.uniq!
    next if influence.empty?
    nodes[user] ||= graph.add_nodes(user)
    influence.each do |influencee|
      if influences[influencee] && influences[influencee].include?(user)
        direction = "both"
        influences[influencee].delete(user)
      else
        direction = "forward"
      end
      nodes[influencee] ||= graph.add_nodes(influencee)
      graph.add_edges(nodes[user], nodes[influencee], :color => "lightblue", :fontcolor => "cornflowerblue", :dir => direction, :arrowhead => "normal")
    end
  end
  task("Outputting graph...") do
    graph.output(option_setting("FORMAT") => option_setting("DEST"))
  end
  info("Graph written to #{option_setting('DEST').bold}")
end