File storage.rb changed (mode: 100644) (index 0e13f2a..52359d5) |
1 |
|
require 'ostruct'
|
|
2 |
|
require 'redis'
|
|
3 |
|
require 'json'
|
|
4 |
|
require 'will_paginate'
|
|
5 |
|
require 'will_paginate/array'
|
|
6 |
|
|
|
7 |
|
require './color_helper'
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
module Storage
|
|
12 |
|
@@rdb = Redis.new(:db => CONFIG.redis_db_index)
|
|
13 |
|
|
|
14 |
|
# this is unnecessary, as we can directly access it through CONFIG.all_graphs
|
|
15 |
|
# def self.all_graphs
|
|
16 |
|
# @@all_graphs = JSON.parse(@@rdb.smembers(CONFIG.graph_index_key)[0]) unless defined? @@all_graphs
|
|
17 |
|
# return @@all_graphs
|
|
18 |
|
# end
|
|
19 |
|
def self.get_info()
|
|
20 |
|
info = OpenStruct.new
|
|
21 |
|
info.nodes = {}
|
|
22 |
|
info.edges = {}
|
|
23 |
|
CONFIG.all_graphs.each do |graph|
|
|
24 |
|
info.nodes[graph] = @@rdb.hget(CONFIG.info_index_key[graph], 'number_of_nodes')
|
|
25 |
|
info.edges[graph] = @@rdb.hget(CONFIG.info_index_key[graph], 'number_of_edges')
|
|
26 |
|
end
|
|
27 |
|
return info
|
|
28 |
|
end
|
|
29 |
|
|
|
30 |
|
def self.all_nodes(graph)
|
|
31 |
|
@@all_nodes = JSON.parse(@@rdb.smembers(CONFIG.node_index_key[graph]).to_s) unless defined? @@all_nodes
|
|
32 |
|
return @@all_nodes.map{|id| id.to_s}
|
|
33 |
|
end
|
|
34 |
|
|
|
35 |
|
def self.metric_names(graph)
|
|
36 |
|
@@metric_names = JSON.parse(@@rdb.smembers(CONFIG.metric_index_key[graph])[0]) unless defined? @@metric_names
|
|
37 |
|
return @@metric_names
|
|
38 |
|
end
|
|
39 |
|
|
|
40 |
|
def self.score_names(graph)
|
|
41 |
|
@@score_names = JSON.parse(@@rdb.smembers(CONFIG.score_index_key[graph])[0]) unless defined? @@score_names
|
|
42 |
|
return @@score_names
|
|
43 |
|
end
|
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
def self.get_node(graph, nodeid)
|
|
48 |
|
node = OpenStruct.new
|
|
49 |
|
node.id = nodeid
|
|
50 |
|
node.metrics = {}
|
|
51 |
|
node.scores = {}
|
|
52 |
|
|
|
53 |
|
#get raw data from redis
|
|
54 |
|
all_values = @@rdb.hgetall(CONFIG.node_prefix[graph]+nodeid.to_s)
|
|
55 |
|
|
|
56 |
|
#build structured data
|
|
57 |
|
CONFIG.metric_names[graph].each do |metric,name|
|
|
58 |
|
node.metrics[metric] = {}
|
|
59 |
|
node.metrics[metric][:name] = name
|
|
60 |
|
node.metrics[metric][:absolute] = all_values[metric].to_f
|
|
61 |
|
node.metrics[metric][:normalized] = all_values[metric+CONFIG.normalization_suffix].to_f
|
|
62 |
|
node.metrics[metric][:color_class] = ColorHelper.color_class_by_value(all_values[metric+CONFIG.normalization_suffix].to_f)
|
|
63 |
|
node.metrics[metric][:color_code] = ColorHelper.color_code_by_value(all_values[metric+CONFIG.normalization_suffix].to_f)
|
|
64 |
|
end
|
|
65 |
|
CONFIG.score_names[graph].each do |score,name|
|
|
66 |
|
node.scores[score] = {}
|
|
67 |
|
node.scores[score][:name] = name
|
|
68 |
|
node.scores[score][:absolute] = all_values[score].to_f
|
|
69 |
|
node.scores[score][:color_class] = ColorHelper.color_class_by_value(all_values[score].to_f)
|
|
70 |
|
node.scores[score][:color_code] = ColorHelper.color_code_by_value(all_values[score].to_f)
|
|
71 |
|
end
|
|
72 |
|
|
|
73 |
|
node.neighbors = JSON.parse(@@rdb.smembers(CONFIG.node_neighbors_prefix[graph]+nodeid.to_s).to_s)
|
|
74 |
|
|
|
75 |
|
return node
|
|
76 |
|
end
|
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
|
def self.get_metric_nodes(graph, metric_name, page=1)
|
|
81 |
|
nodes = {}
|
|
82 |
|
@@rdb.zrevrange(CONFIG.metric_prefix[graph]+metric_name, 0, -1, {withscores: true}).each do |value|
|
|
83 |
|
nodes[value[0]] = {:id => value[0]}
|
|
84 |
|
nodes[value[0]][:absolute] = value[1].to_f
|
|
85 |
|
end
|
|
86 |
|
|
|
87 |
|
@@rdb.zrevrange(CONFIG.metric_prefix[graph]+metric_name+CONFIG.normalization_suffix, 0, -1, {withscores: true}).each do |value|
|
|
88 |
|
nodes[value[0]][:normalized] = value[1].to_f
|
|
89 |
|
nodes[value[0]][:color_class] = ColorHelper.color_class_by_value(value[1].to_f)
|
|
90 |
|
end
|
|
91 |
|
return nodes.to_a.paginate(:page => page, :per_page => CONFIG.nodes_per_page)
|
|
92 |
|
end
|
|
93 |
|
|
|
94 |
|
def self.get_all_metric_values_normalized(graph, metric_name)
|
|
95 |
|
return @@rdb.zrevrange(CONFIG.metric_prefix[graph]+metric_name+CONFIG.normalization_suffix, 0, -1, {withscores: true}).map{|score| score[1]}
|
|
96 |
|
end
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
def self.get_score_nodes(graph, score_name, page=1)
|
|
100 |
|
nodes = {}
|
|
101 |
|
@@rdb.zrevrange(CONFIG.score_prefix[graph]+score_name, 0, -1, {withscores: true}).each do |value|
|
|
102 |
|
nodes[value[0]] = {:id => value[0]}
|
|
103 |
|
nodes[value[0]][:absolute] = value[1].to_f
|
|
104 |
|
nodes[value[0]][:color_class] = ColorHelper.color_class_by_value(value[1].to_f)
|
|
105 |
|
end
|
|
106 |
|
return nodes.to_a.paginate(:page => page, :per_page => CONFIG.nodes_per_page)
|
|
107 |
|
end
|
|
108 |
|
|
|
109 |
|
def self.get_all_score_values(graph, score_name)
|
|
110 |
|
return @@rdb.zrevrange(CONFIG.score_prefix[graph]+score_name, 0, -1, {withscores: true}).map{|score| score[1]}
|
|
111 |
|
end
|
|
112 |
|
|
|
113 |
|
def self.get_absolute_metric_statistics(graph)
|
|
114 |
|
data_metrics_absolute = {}
|
|
115 |
|
CONFIG.metric_names[graph].each do |metric,mname|
|
|
116 |
|
metric_data = {}
|
|
117 |
|
CONFIG.statistical_indicators.each do |indicator,iname|
|
|
118 |
|
value = @@rdb.hget(CONFIG.statistics_prefix[graph]+metric, indicator)
|
|
119 |
|
metric_data[indicator] = value
|
|
120 |
|
end
|
|
121 |
|
metric_data['display_name'] = mname
|
|
122 |
|
data_metrics_absolute[metric] = metric_data
|
|
123 |
|
end
|
|
124 |
|
return data_metrics_absolute
|
|
125 |
|
end
|
|
126 |
|
|
|
127 |
|
def self.get_normalized_metric_statistics(graph)
|
|
128 |
|
data_metrics_normalized = {}
|
|
129 |
|
CONFIG.metric_names[graph].each do |metric,mname|
|
|
130 |
|
metric_data = {}
|
|
131 |
|
CONFIG.statistical_indicators.each do |indicator,iname|
|
|
132 |
|
value = @@rdb.hget(CONFIG.statistics_prefix[graph]+metric+CONFIG.normalization_suffix, indicator)
|
|
133 |
|
metric_data[indicator] = value
|
|
134 |
|
end
|
|
135 |
|
metric_data['display_name'] = mname
|
|
136 |
|
data_metrics_normalized[metric] = metric_data
|
|
137 |
|
end
|
|
138 |
|
return data_metrics_normalized
|
|
139 |
|
end
|
|
140 |
|
|
|
141 |
|
def self.get_score_statistics(graph)
|
|
142 |
|
data_scores = {}
|
|
143 |
|
CONFIG.score_names[graph].each do |score,sname|
|
|
144 |
|
score_data = {}
|
|
145 |
|
CONFIG.statistical_indicators.each do |indicator,iname|
|
|
146 |
|
value = @@rdb.hget(CONFIG.statistics_prefix[graph]+score, indicator)
|
|
147 |
|
score_data[indicator] = value
|
|
148 |
|
end
|
|
149 |
|
score_data['display_name'] = sname
|
|
150 |
|
data_scores[score] = score_data
|
|
151 |
|
end
|
|
152 |
|
return data_scores
|
|
153 |
|
end
|
|
154 |
|
|
|
155 |
|
def self.get_metric_correlations(graph)
|
|
156 |
|
correlation_data = {}
|
|
157 |
|
CONFIG.metric_names[graph].each do |metric1,m1name|
|
|
158 |
|
correlation_data[metric1] = {:from => m1name, :correlation => {}}
|
|
159 |
|
CONFIG.metric_names[graph].each do |metric2,m2name|
|
|
160 |
|
corr = @@rdb.hget(CONFIG.statistics_prefix[graph]+'correlations:'+metric1+':'+metric2, 'correlation')
|
|
161 |
|
conf = @@rdb.hget(CONFIG.statistics_prefix[graph]+'correlations:'+metric1+':'+metric2, 'confidence')
|
|
162 |
|
color_code = ColorHelper.color_code_by_value(corr.to_f.abs)
|
|
163 |
|
correlation_data[metric1][:correlation][metric2] = {:to => m2name, :correlation => corr, :confidence => conf, :color_code => color_code}
|
|
164 |
|
end
|
|
165 |
|
end
|
|
166 |
|
return correlation_data
|
|
167 |
|
end
|
|
168 |
|
|
|
169 |
|
def self.get_percolation_methods_ordered(graph)
|
|
170 |
|
percolation_methods = {}
|
|
171 |
|
@@rdb.zrevrange(CONFIG.percolation_prefix[graph]+'10%', 0, -1, {withscores: true}).each do |name,score|
|
|
172 |
|
data = {}
|
|
173 |
|
data['display_name'] = CONFIG.percolation_methods[graph][name]
|
|
174 |
|
data['value'] = score.to_f
|
|
175 |
|
#percolation_methods[value[0]] = {:display_name => value[0]}
|
|
176 |
|
#percolation_methods[value[0]][:value] = value[1].to_f
|
|
177 |
|
percolation_methods[name] = data
|
|
178 |
|
end
|
|
179 |
|
return percolation_methods
|
|
180 |
|
end
|
|
181 |
|
|
|
182 |
|
def self.get_percolation_statistics(graph)
|
|
183 |
|
percolation_statistics = {}
|
|
184 |
|
CONFIG.percolation_methods[graph].each do |method,name|
|
|
185 |
|
data = {}
|
|
186 |
|
data['display_name'] = name
|
|
187 |
|
data['value'] = {}
|
|
188 |
|
CONFIG.percolation_pctages[graph].each do |pct|
|
|
189 |
|
data['value'][pct] = @@rdb.hget(CONFIG.percolation_prefix[graph]+method,pct)
|
|
190 |
|
end
|
|
191 |
|
percolation_statistics[method] = data
|
|
192 |
|
end
|
|
193 |
|
return percolation_statistics
|
|
194 |
|
end
|
|
195 |
|
|
|
196 |
|
|
|
197 |
|
end
|
|
|
1 |
|
require 'ostruct' |
|
2 |
|
require 'redis' |
|
3 |
|
require 'json' |
|
4 |
|
require 'will_paginate' |
|
5 |
|
require 'will_paginate/array' |
|
6 |
|
|
|
7 |
|
require './color_helper' |
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
module Storage |
|
12 |
|
@@rdb = Redis.new( |
|
13 |
|
:host => CONFIG.redis_host, |
|
14 |
|
:port => CONFIG.redis_port, |
|
15 |
|
:db => CONFIG.redis_db_index |
|
16 |
|
) |
|
17 |
|
# this is unnecessary, as we can directly access it through CONFIG.all_graphs |
|
18 |
|
# def self.all_graphs |
|
19 |
|
# @@all_graphs = JSON.parse(@@rdb.smembers(CONFIG.graph_index_key)[0]) unless defined? @@all_graphs |
|
20 |
|
# return @@all_graphs |
|
21 |
|
# end |
|
22 |
|
def self.get_info() |
|
23 |
|
info = OpenStruct.new |
|
24 |
|
info.nodes = {} |
|
25 |
|
info.edges = {} |
|
26 |
|
CONFIG.all_graphs.each do |graph| |
|
27 |
|
info.nodes[graph] = @@rdb.hget(CONFIG.info_index_key[graph], 'number_of_nodes') |
|
28 |
|
info.edges[graph] = @@rdb.hget(CONFIG.info_index_key[graph], 'number_of_edges') |
|
29 |
|
end |
|
30 |
|
return info |
|
31 |
|
end |
|
32 |
|
|
|
33 |
|
def self.all_nodes(graph) |
|
34 |
|
@@all_nodes = JSON.parse(@@rdb.smembers(CONFIG.node_index_key[graph]).to_s) unless defined? @@all_nodes |
|
35 |
|
return @@all_nodes.map{|id| id.to_s} |
|
36 |
|
end |
|
37 |
|
|
|
38 |
|
def self.metric_names(graph) |
|
39 |
|
@@metric_names = JSON.parse(@@rdb.smembers(CONFIG.metric_index_key[graph])[0]) unless defined? @@metric_names |
|
40 |
|
return @@metric_names |
|
41 |
|
end |
|
42 |
|
|
|
43 |
|
def self.score_names(graph) |
|
44 |
|
@@score_names = JSON.parse(@@rdb.smembers(CONFIG.score_index_key[graph])[0]) unless defined? @@score_names |
|
45 |
|
return @@score_names |
|
46 |
|
end |
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
def self.get_node(graph, nodeid) |
|
51 |
|
node = OpenStruct.new |
|
52 |
|
node.id = nodeid |
|
53 |
|
node.metrics = {} |
|
54 |
|
node.scores = {} |
|
55 |
|
|
|
56 |
|
#get raw data from redis |
|
57 |
|
all_values = @@rdb.hgetall(CONFIG.node_prefix[graph]+nodeid.to_s) |
|
58 |
|
|
|
59 |
|
#build structured data |
|
60 |
|
CONFIG.metric_names[graph].each do |metric,name| |
|
61 |
|
node.metrics[metric] = {} |
|
62 |
|
node.metrics[metric][:name] = name |
|
63 |
|
node.metrics[metric][:absolute] = all_values[metric].to_f |
|
64 |
|
node.metrics[metric][:normalized] = all_values[metric+CONFIG.normalization_suffix].to_f |
|
65 |
|
node.metrics[metric][:color_class] = ColorHelper.color_class_by_value(all_values[metric+CONFIG.normalization_suffix].to_f) |
|
66 |
|
node.metrics[metric][:color_code] = ColorHelper.color_code_by_value(all_values[metric+CONFIG.normalization_suffix].to_f) |
|
67 |
|
end |
|
68 |
|
CONFIG.score_names[graph].each do |score,name| |
|
69 |
|
node.scores[score] = {} |
|
70 |
|
node.scores[score][:name] = name |
|
71 |
|
node.scores[score][:absolute] = all_values[score].to_f |
|
72 |
|
node.scores[score][:color_class] = ColorHelper.color_class_by_value(all_values[score].to_f) |
|
73 |
|
node.scores[score][:color_code] = ColorHelper.color_code_by_value(all_values[score].to_f) |
|
74 |
|
end |
|
75 |
|
|
|
76 |
|
node.neighbors = JSON.parse(@@rdb.smembers(CONFIG.node_neighbors_prefix[graph]+nodeid.to_s).to_s) |
|
77 |
|
|
|
78 |
|
return node |
|
79 |
|
end |
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
|
def self.get_metric_nodes(graph, metric_name, page=1) |
|
84 |
|
nodes = {} |
|
85 |
|
@@rdb.zrevrange(CONFIG.metric_prefix[graph]+metric_name, 0, -1, {withscores: true}).each do |value| |
|
86 |
|
nodes[value[0]] = {:id => value[0]} |
|
87 |
|
nodes[value[0]][:absolute] = value[1].to_f |
|
88 |
|
end |
|
89 |
|
|
|
90 |
|
@@rdb.zrevrange(CONFIG.metric_prefix[graph]+metric_name+CONFIG.normalization_suffix, 0, -1, {withscores: true}).each do |value| |
|
91 |
|
nodes[value[0]][:normalized] = value[1].to_f |
|
92 |
|
nodes[value[0]][:color_class] = ColorHelper.color_class_by_value(value[1].to_f) |
|
93 |
|
end |
|
94 |
|
return nodes.to_a.paginate(:page => page, :per_page => CONFIG.nodes_per_page) |
|
95 |
|
end |
|
96 |
|
|
|
97 |
|
def self.get_all_metric_values_normalized(graph, metric_name) |
|
98 |
|
return @@rdb.zrevrange(CONFIG.metric_prefix[graph]+metric_name+CONFIG.normalization_suffix, 0, -1, {withscores: true}).map{|score| score[1]} |
|
99 |
|
end |
|
100 |
|
|
|
101 |
|
|
|
102 |
|
def self.get_score_nodes(graph, score_name, page=1) |
|
103 |
|
nodes = {} |
|
104 |
|
@@rdb.zrevrange(CONFIG.score_prefix[graph]+score_name, 0, -1, {withscores: true}).each do |value| |
|
105 |
|
nodes[value[0]] = {:id => value[0]} |
|
106 |
|
nodes[value[0]][:absolute] = value[1].to_f |
|
107 |
|
nodes[value[0]][:color_class] = ColorHelper.color_class_by_value(value[1].to_f) |
|
108 |
|
end |
|
109 |
|
return nodes.to_a.paginate(:page => page, :per_page => CONFIG.nodes_per_page) |
|
110 |
|
end |
|
111 |
|
|
|
112 |
|
def self.get_all_score_values(graph, score_name) |
|
113 |
|
return @@rdb.zrevrange(CONFIG.score_prefix[graph]+score_name, 0, -1, {withscores: true}).map{|score| score[1]} |
|
114 |
|
end |
|
115 |
|
|
|
116 |
|
def self.get_absolute_metric_statistics(graph) |
|
117 |
|
data_metrics_absolute = {} |
|
118 |
|
CONFIG.metric_names[graph].each do |metric,mname| |
|
119 |
|
metric_data = {} |
|
120 |
|
CONFIG.statistical_indicators.each do |indicator,iname| |
|
121 |
|
value = @@rdb.hget(CONFIG.statistics_prefix[graph]+metric, indicator) |
|
122 |
|
metric_data[indicator] = value |
|
123 |
|
end |
|
124 |
|
metric_data['display_name'] = mname |
|
125 |
|
data_metrics_absolute[metric] = metric_data |
|
126 |
|
end |
|
127 |
|
return data_metrics_absolute |
|
128 |
|
end |
|
129 |
|
|
|
130 |
|
def self.get_normalized_metric_statistics(graph) |
|
131 |
|
data_metrics_normalized = {} |
|
132 |
|
CONFIG.metric_names[graph].each do |metric,mname| |
|
133 |
|
metric_data = {} |
|
134 |
|
CONFIG.statistical_indicators.each do |indicator,iname| |
|
135 |
|
value = @@rdb.hget(CONFIG.statistics_prefix[graph]+metric+CONFIG.normalization_suffix, indicator) |
|
136 |
|
metric_data[indicator] = value |
|
137 |
|
end |
|
138 |
|
metric_data['display_name'] = mname |
|
139 |
|
data_metrics_normalized[metric] = metric_data |
|
140 |
|
end |
|
141 |
|
return data_metrics_normalized |
|
142 |
|
end |
|
143 |
|
|
|
144 |
|
def self.get_score_statistics(graph) |
|
145 |
|
data_scores = {} |
|
146 |
|
CONFIG.score_names[graph].each do |score,sname| |
|
147 |
|
score_data = {} |
|
148 |
|
CONFIG.statistical_indicators.each do |indicator,iname| |
|
149 |
|
value = @@rdb.hget(CONFIG.statistics_prefix[graph]+score, indicator) |
|
150 |
|
score_data[indicator] = value |
|
151 |
|
end |
|
152 |
|
score_data['display_name'] = sname |
|
153 |
|
data_scores[score] = score_data |
|
154 |
|
end |
|
155 |
|
return data_scores |
|
156 |
|
end |
|
157 |
|
|
|
158 |
|
def self.get_metric_correlations(graph) |
|
159 |
|
correlation_data = {} |
|
160 |
|
CONFIG.metric_names[graph].each do |metric1,m1name| |
|
161 |
|
correlation_data[metric1] = {:from => m1name, :correlation => {}} |
|
162 |
|
CONFIG.metric_names[graph].each do |metric2,m2name| |
|
163 |
|
corr = @@rdb.hget(CONFIG.statistics_prefix[graph]+'correlations:'+metric1+':'+metric2, 'correlation') |
|
164 |
|
conf = @@rdb.hget(CONFIG.statistics_prefix[graph]+'correlations:'+metric1+':'+metric2, 'confidence') |
|
165 |
|
color_code = ColorHelper.color_code_by_value(corr.to_f.abs) |
|
166 |
|
correlation_data[metric1][:correlation][metric2] = {:to => m2name, :correlation => corr, :confidence => conf, :color_code => color_code} |
|
167 |
|
end |
|
168 |
|
end |
|
169 |
|
return correlation_data |
|
170 |
|
end |
|
171 |
|
|
|
172 |
|
def self.get_percolation_methods_ordered(graph) |
|
173 |
|
percolation_methods = {} |
|
174 |
|
@@rdb.zrevrange(CONFIG.percolation_prefix[graph]+'10%', 0, -1, {withscores: true}).each do |name,score| |
|
175 |
|
data = {} |
|
176 |
|
data['display_name'] = CONFIG.percolation_methods[graph][name] |
|
177 |
|
data['value'] = score.to_f |
|
178 |
|
#percolation_methods[value[0]] = {:display_name => value[0]} |
|
179 |
|
#percolation_methods[value[0]][:value] = value[1].to_f |
|
180 |
|
percolation_methods[name] = data |
|
181 |
|
end |
|
182 |
|
return percolation_methods |
|
183 |
|
end |
|
184 |
|
|
|
185 |
|
def self.get_percolation_statistics(graph) |
|
186 |
|
percolation_statistics = {} |
|
187 |
|
CONFIG.percolation_methods[graph].each do |method,name| |
|
188 |
|
data = {} |
|
189 |
|
data['display_name'] = name |
|
190 |
|
data['value'] = {} |
|
191 |
|
CONFIG.percolation_pctages[graph].each do |pct| |
|
192 |
|
data['value'][pct] = @@rdb.hget(CONFIG.percolation_prefix[graph]+method,pct) |
|
193 |
|
end |
|
194 |
|
percolation_statistics[method] = data |
|
195 |
|
end |
|
196 |
|
return percolation_statistics |
|
197 |
|
end |
|
198 |
|
|
|
199 |
|
|
|
200 |
|
end |