Created
August 10, 2012 17:21
-
-
Save clr/3315893 to your computer and use it in GitHub Desktop.
ubigraph thingie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'bundler' | |
require 'bundler' | |
require 'riak' | |
require 'xmlrpc/client' | |
require 'rake' | |
begin | |
Bundler.setup(:default, :development) | |
rescue Bundler::BundlerError => e | |
$stderr.puts e.message | |
$stderr.puts "Run `bundle install` to install missing gems" | |
exit e.status_code | |
end | |
class Time | |
def to_ms | |
self.to_f * 1000.0 | |
end | |
end | |
# Connect to UbiGraph and Riak | |
@server = XMLRPC::Client.new2("http://127.0.0.1:20738/RPC2") | |
@client = [] | |
@chars = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten | |
@chars_l = @chars.length | |
(1..7).each do |id| | |
@client[id] = Riak::Client.new(:nodes => [:host => '127.0.0.1', :http_port => "809#{id}"]) | |
end | |
namespace(:ubi) do | |
task :start do | |
(1..7).each do |id| | |
@server.call("ubigraph.new_vertex_w_id", id) | |
@server.call("ubigraph.set_vertex_attribute", id, "shape", "sphere") | |
@server.call("ubigraph.set_vertex_attribute", id, "color", "#808080") | |
end | |
(1..7).each do |id| | |
@server.call("ubigraph.new_edge", id, (id % 7 + 1)) | |
end | |
end | |
task :for_3_minute do | |
stop_time = Time.now + 180 | |
while Time.now < stop_time do | |
(1..7).each do |id| | |
begin | |
timer = Time.now.to_ms | |
new = @client[id].bucket('cascadia').get_or_new((0..8).map{@chars[rand(@chars_l)]}.join) | |
new.content_type = 'text/plain' | |
new.raw_data = (0..(rand(10000))).map{@chars[rand(@chars_l)]}.join | |
new.store | |
time = (Time.now.to_ms - timer) / 30 * 128 | |
red = 128 - time | |
blue = 128 + (time > 128 ? 128 : time) | |
gren = 128 + (time % 128) | |
red = red.to_i.to_s(16) | |
blue = blue.to_i.to_s(16) | |
gren = gren.to_i.to_s(16) | |
red = (red.length < 2 ? "0#{red}" : red) | |
blue = (blue.length < 2 ? "0#{blue}" : blue) | |
gren = (gren.length < 2 ? "0#{gren}" : gren) | |
@server.call("ubigraph.set_vertex_attribute", id, "color", "##{red}#{gren}#{blue}") | |
rescue => e | |
@server.call("ubigraph.set_vertex_attribute", id, "color", "#ff0000") | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment