arnaucube
arnaucube/goBlockchainDataAnalysisPublic Self-hosted repository
sankey in frontend works, added logs system, added getLastTx in backend and frontend
arnaucode idoctnef@openmailbox.org
Mon, 07 Aug 2017 22:00:07 +0200
commit

fe164456fb90b8ec9235339f58060a72b3ea6670

parent

2c67f57b7b1f62062b629cd5548c58e0275845a1

M .gitignore → .gitignore

@@ -17,3 +17,4 @@

/*.conf /*.json *.mp4 +logs
M README.md → README.md

@@ -1,6 +1,18 @@

# goBlockchainDataAnalysis blockchain data analysis, written in Go + +![goBlockchainDataAnalysis](https://raw.githubusercontent.com/arnaucode/goBlockchainDataAnalysis/master/goBlockchainDataAnalysis01.png "goBlockchainDataAnalysis") + + +![goBlockchainDataAnalysis](https://raw.githubusercontent.com/arnaucode/goBlockchainDataAnalysis/master/goBlockchainDataAnalysis02.png "goBlockchainDataAnalysis") + + +![goBlockchainDataAnalysis](https://raw.githubusercontent.com/arnaucode/goBlockchainDataAnalysis/master/goBlockchainDataAnalysis05.png "goBlockchainDataAnalysis") + + +![goBlockchainDataAnalysis](https://raw.githubusercontent.com/arnaucode/goBlockchainDataAnalysis/master/goBlockchainDataAnalysis04.png "goBlockchainDataAnalysis") + ### Install 1. Nodejs & NPM https://nodejs.org/ 2. MongoDB https://www.mongodb.com/
M errors.go → errors.go

@@ -1,9 +1,9 @@

package main -import "fmt" +import "log" func check(err error) { if err != nil { - fmt.Println(err) + log.Println(err) } }
A log.go

@@ -0,0 +1,19 @@

+package main + +import ( + "io" + "log" + "os" + "time" +) + +func savelog() { + timeS := time.Now().String() + _ = os.Mkdir("logs", os.ModePerm) + logFile, err := os.OpenFile("logs/log-"+timeS+".log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) + if err != nil { + panic(err) + } + mw := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(mw) +}
M main.go → main.go

@@ -1,7 +1,6 @@

package main import ( - "fmt" "log" "net/http" "os"

@@ -20,6 +19,7 @@ var dateCountCollection *mgo.Collection

var hourCountCollection *mgo.Collection func main() { + savelog() //read goBlockchainDataAbalysis config readConfig("config.json")

@@ -33,50 +33,46 @@ edgeCollection = getCollection(session, "edges")

dateCountCollection = getCollection(session, "dateCounts") hourCountCollection = getCollection(session, "hourCounts") - // create new client instance - client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{ - HTTPPostMode: true, - DisableTLS: true, - Host: config.Host + ":" + config.Port, - User: config.User, - Pass: config.Pass, - }, nil) - if err != nil { - log.Fatalf("error creating new btc client: %v", err) - } - - // list accounts - accounts, err := client.ListAccounts() - if err != nil { - log.Fatalf("error listing accounts: %v", err) - } - // iterate over accounts (map[string]btcutil.Amount) and write to stdout - for label, amount := range accounts { - log.Printf("%s: %s", label, amount) - } if len(os.Args) > 1 { if os.Args[1] == "-explore" { + // create new client instance + client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{ + HTTPPostMode: true, + DisableTLS: true, + Host: config.Host + ":" + config.Port, + User: config.User, + Pass: config.Pass, + }, nil) + if err != nil { + log.Fatalf("error creating new btc client: %v", err) + } + + // list accounts + accounts, err := client.ListAccounts() + if err != nil { + log.Fatalf("error listing accounts: %v", err) + } + // iterate over accounts (map[string]btcutil.Amount) and write to stdout + for label, amount := range accounts { + log.Printf("%s: %s", label, amount) + } color.Blue("starting to explore blockchain") explore(client, config.GenesisBlock) - } - /*if os.Args[1] == "-tree" { - color.Blue("starting to make tree") - addressTree(client, "fY3HZxu7HFKRcYzVSTXRZpAJMP4qba2oR6") - }*/ - } - // Get the current block count. - blockCount, err := client.GetBlockCount() - if err != nil { - log.Fatal(err) + // Get the current block count. + blockCount, err := client.GetBlockCount() + if err != nil { + log.Fatal(err) + } + log.Printf("Block count: %d", blockCount) + } } - log.Printf("Block count: %d", blockCount) //http server start readServerConfig("./serverConfig.json") - color.Green("server running") - fmt.Print("port: ") - color.Green(serverConfig.ServerPort) + log.Println("server running") + log.Print("port: ") + log.Println(serverConfig.ServerPort) router := NewRouter() headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
M serverRoutes.go → serverRoutes.go

@@ -27,6 +27,12 @@ "/alladdresses",

AllAddresses, }, Route{ + "GetLastTx", + "Get", + "/lasttx", + GetLastTx, + }, + Route{ "AddressNetwork", "GET", "/address/network/{address}",

@@ -50,14 +56,6 @@ "Get",

"/houranalysis", GetHourAnalysis, }, - /* - Route{ - "SelectItem", - "GET", - "/selectItem/{userid}/{itemid}", - SelectItem, - }, - */ } //ROUTES

@@ -94,6 +92,19 @@ jsonNodes, err := json.Marshal(nodes)

check(err) fmt.Fprintln(w, string(jsonNodes)) +} +func GetLastTx(w http.ResponseWriter, r *http.Request) { + ipFilter(w, r) + + nodes := []NodeModel{} + err := nodeCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&nodes) + check(err) + + //convert []resp struct to json + jNodes, err := json.Marshal(nodes) + check(err) + + fmt.Fprintln(w, string(jNodes)) } func AddressNetwork(w http.ResponseWriter, r *http.Request) { ipFilter(w, r)
M web/views/main/main.html → web/views/main/main.html

@@ -3,10 +3,14 @@ <div class="row">

<div class="col-sm-4"> <div class="panel"> <div class="panel-heading c_blueGrey300"> - <h3 class="panel-title">Last blocks</h3> + <h3 class="panel-title">Last addresses</h3> </div> <div class="panel-body" style="max-height: 500px;overflow-y: scroll;"> - + <div class="list-group-item" ng-repeat="node in addresses"> + <div class="row-content"> + <p class="list-group-item-text">{{node.id}}</p> + </div> + </div> </div> </div> </div>
M web/views/main/main.js → web/views/main/main.js

@@ -10,5 +10,14 @@ });

}]) .controller('MainCtrl', function($scope, $http) { - + $scope.addresses = []; + $http.get(urlapi + 'lasttx') + .then(function(data, status, headers, config) { + console.log('data success'); + console.log(data); + + $scope.addresses = data.data; + }, function(data, status, headers, config) { + console.log('data error'); + }); });
M web/views/sankey/sankey.html → web/views/sankey/sankey.html

@@ -19,7 +19,7 @@ </div>

</div> <div class="col-sm-10"> <div class="panel-heading c_blueGrey300"> - <h3 class="panel-title">Sankey</h3> + <h3 class="panel-title">Sankey - address {{selectedAddress}}</h3> </div> <div class="panel-body"> <!--<ng-sankey id="sankeyChart" options="options" data="data"></ng-sankey>
M web/views/sankey/sankey.js → web/views/sankey/sankey.js

@@ -10,7 +10,7 @@ });

}]) .controller('SankeyCtrl', function($scope, $http, $routeParams) { - + $scope.selectedAddress = ""; $scope.options = { chart: '#sankeyChart', width: 960,

@@ -36,6 +36,9 @@ console.log('data error');

}); $scope.getAddressSankey = function(address) { console.log(address); + $scope.selectedAddress = address.id; + $scope.data.nodes = []; + $scope.data.links = []; $http.get(urlapi + 'address/sankey/' + address.id) .then(function(data, status, headers, config) { console.log('data success');

@@ -43,6 +46,7 @@ console.log(data);

$scope.data.nodes = data.data.nodes; $scope.data.links = data.data.links; console.log($scope.data); + d3.selectAll("svg > *").remove(); let chart = new d3.sankeyChart(data.data, $scope.options); //$scope.data = data.data; }, function(data, status, headers, config) {