فهرست منبع

first simple graph setup

Chris Mague 6 سال پیش
والد
کامیت
081542c85b
4فایلهای تغییر یافته به همراه65 افزوده شده و 0 حذف شده
  1. 4 0
      config.ru
  2. 1 0
      lib/neoinfra.rb
  3. 38 0
      lib/neoinfra/graph.rb
  4. 22 0
      web/controllers/graph.rb

+ 4 - 0
config.ru

@@ -23,3 +23,7 @@ end
 map '/search' do
   run Search
 end
+
+map '/graph' do
+  run Graph
+end

+ 1 - 0
lib/neoinfra.rb

@@ -17,4 +17,5 @@ module NeoInfra
   require 'neoinfra/vpcs'
   require 'neoinfra/nodes'
   require 'neoinfra/cloudwatch'
+  require 'neoinfra/graph'
 end

+ 38 - 0
lib/neoinfra/graph.rb

@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require 'neoinfra'
+require 'vpc'
+require 'accounts'
+require 'fog-aws'
+require 'neo4j'
+
+# NeoInfra Account information
+module NeoInfra
+  # Provide informations about the accounts available
+  class Graph
+    def initialize
+      @cfg = NeoInfra::Config.new
+      neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
+      Neo4j::Session.open(:server_db, neo4j_url)
+    end
+    def graph_vpcs
+      nodes = []
+      rels = []
+      i = 0
+      @cfg = NeoInfra::Config.new
+      @cfg.accounts.each do |account|
+        nodes << {title: account[:name], label: 'account'}
+        i +=1
+        Vpc.where(default: "false").each do |vpc|
+          if vpc.owned.name == account[:name]
+            source = i
+            nodes << {title: vpc.name, label: 'vpc'}
+            i +=1
+            rels << {source: source, target: i}
+          end
+        end
+      end
+      return {nodes: nodes, links: rels}
+    end
+  end
+end

+ 22 - 0
web/controllers/graph.rb

@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+lib_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', 'lib')
+$LOAD_PATH.unshift(lib_dir) unless
+  $LOAD_PATH.include?(lib_dir) || $LOAD_PATH.include?(lib_dir)
+
+require 'json'
+require 'neoinfra'
+require 'sinatra'
+require 'sinatra/base'
+require 'sinatra/contrib'
+
+# Handle loading data into the graph db
+class Graph < Sinatra::Base
+  register Sinatra::RespondWith
+  set :views, File.join(File.dirname(__FILE__), '..', '/views')
+
+  get '/vpcs' do
+    g = NeoInfra::Graph.new
+    g.graph_vpcs.to_json
+  end
+end