diff --git a/dateAnalysis.go b/dateAnalysis.go index c54ebdf..4aea917 100644 --- a/dateAnalysis.go +++ b/dateAnalysis.go @@ -8,6 +8,9 @@ import ( "gopkg.in/mgo.v2/bson" ) +func dateBeforeThan(dateA time.Time, dateB time.Time) bool { + return dateA.Before(dateB) +} func map24hours() map[int]int { h := make(map[int]int) for i := 0; i < 24; i++ { diff --git a/mongoModels.go b/mongoModels.go index 652a93f..a733064 100644 --- a/mongoModels.go +++ b/mongoModels.go @@ -107,6 +107,10 @@ type ChartAnalysisResp struct { Labels []string `json:"labels"` Data []int `json:"data"` } +type ChartAnalysisRespFloat64 struct { + Labels []string `json:"labels"` + Data []float64 `json:"data"` +} type ChartSeriesAnalysisResp struct { Labels []string `json:"labels"` Data [][]int `json:"data"` diff --git a/serverRoutes.go b/serverRoutes.go index f874e76..03d6063 100644 --- a/serverRoutes.go +++ b/serverRoutes.go @@ -52,6 +52,12 @@ var routes = Routes{ "/block/{height}", Block, }, + Route{ + "Tx", + "GET", + "/tx/{txid}", + Tx, + }, Route{ "Address", "GET", @@ -100,6 +106,12 @@ var routes = Routes{ "/last7dayhour", GetLast7DayHourAnalysis, }, + Route{ + "GetAddressTimeChart", + "GET", + "/addresstimechart/{hash}", + GetAddressTimeChart, + }, } //ROUTES @@ -190,14 +202,6 @@ func Block(w http.ResponseWriter, r *http.Request) { err = txCollection.Find(bson.M{"blockheight": heightString}).All(&txs) block.Txs = txs - /*for _, tx := range address.Txs { - blocks := []BlockModel{} - err = blockCollection.Find(bson.M{"blockheight": tx.BlockHash}).All(&blocks) - for _, block := range blocks { - address.Blocks = append(address.Blocks, block) - } - }*/ - //convert []resp struct to json jsonResp, err := json.Marshal(block) check(err) @@ -205,6 +209,24 @@ func Block(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, string(jsonResp)) } } +func Tx(w http.ResponseWriter, r *http.Request) { + ipFilter(w, r) + + vars := mux.Vars(r) + txid := vars["txid"] + if txid == "undefined" { + fmt.Fprintln(w, "not valid txid") + } else { + tx := TxModel{} + err := txCollection.Find(bson.M{"txid": txid}).One(&tx) + + //convert []resp struct to json + jsonResp, err := json.Marshal(tx) + check(err) + + fmt.Fprintln(w, string(jsonResp)) + } +} func Address(w http.ResponseWriter, r *http.Request) { ipFilter(w, r) @@ -217,7 +239,7 @@ func Address(w http.ResponseWriter, r *http.Request) { err := addressCollection.Find(bson.M{"hash": hash}).One(&address) txs := []TxModel{} - err = txCollection.Find(bson.M{"$or": []bson.M{bson.M{"from": hash}, bson.M{"to": hash}}}).All(&txs) + err = txCollection.Find(bson.M{"$or": []bson.M{bson.M{"vin.address": hash}, bson.M{"vout.address": hash}}}).All(&txs) address.Txs = txs for _, tx := range address.Txs { @@ -463,3 +485,56 @@ func GetLast7DayHourAnalysis(w http.ResponseWriter, r *http.Request) { check(err) fmt.Fprintln(w, string(jsonResp)) } +func GetAddressTimeChart(w http.ResponseWriter, r *http.Request) { + ipFilter(w, r) + + vars := mux.Vars(r) + hash := vars["hash"] + if hash == "undefined" { + fmt.Fprintln(w, "not valid hash") + } else { + address := AddressModel{} + err := addressCollection.Find(bson.M{"hash": hash}).One(&address) + + txs := []TxModel{} + err = txCollection.Find(bson.M{"$or": []bson.M{bson.M{"vin.address": hash}, bson.M{"vout.address": hash}}}).All(&txs) + address.Txs = txs + + for _, tx := range address.Txs { + blocks := []BlockModel{} + err = blockCollection.Find(bson.M{"hash": tx.BlockHash}).All(&blocks) + for _, block := range blocks { + address.Blocks = append(address.Blocks, block) + } + } + + count := make(map[time.Time]float64) + for _, tx := range txs { + var val float64 + for _, vin := range tx.Vin { + val = val + vin.Amount + } + count[tx.DateT] = val + } + var dateSorted []time.Time + for t, _ := range count { + dateSorted = append(dateSorted, t) + } + sort.Slice(dateSorted, func(i, j int) bool { + //return dateSorted[i] < dateSorted[j] + return dateBeforeThan(dateSorted[i], dateSorted[j]) + }) + + var resp ChartAnalysisRespFloat64 + for _, t := range dateSorted { + resp.Labels = append(resp.Labels, t.String()) + resp.Data = append(resp.Data, count[t]) + } + + //convert []resp struct to json + jsonResp, err := json.Marshal(resp) + check(err) + + fmt.Fprintln(w, string(jsonResp)) + } +} diff --git a/web/app.js b/web/app.js index 258f655..34f9f7f 100644 --- a/web/app.js +++ b/web/app.js @@ -12,6 +12,7 @@ angular.module('webApp', [ 'app.navbar', 'app.main', 'app.block', + 'app.tx', 'app.address', 'app.network', 'app.addressNetwork', diff --git a/web/index.html b/web/index.html index dd19dde..9e0b767 100644 --- a/web/index.html +++ b/web/index.html @@ -76,8 +76,9 @@ - + + diff --git a/web/views/address/address.html b/web/views/address/address.html index f53502e..a92825e 100644 --- a/web/views/address/address.html +++ b/web/views/address/address.html @@ -8,20 +8,29 @@ {{address.amount}} +
+
+

Address uses +

+
+ + +
+

Blocks where address appears

-
+ @@ -39,32 +48,36 @@ - - - - - + + + - -
BlockHeightFromToAmountTxidInputOutput
- - {{tx.blockheight}} + + {{tx.txid}} +
+ {{tx.datet}}
- - {{tx.from}} - +
+ + {{vin.address}} + + :{{vin.amount}} +
- - {{tx.to}} - +
+ + {{vout.address}} + + :{{vout.value}} +
{{tx.amount}}View
diff --git a/web/views/address/address.js b/web/views/address/address.js index 9d6934d..663f9f5 100644 --- a/web/views/address/address.js +++ b/web/views/address/address.js @@ -18,4 +18,16 @@ angular.module('app.address', ['ngRoute']) }, function(data, status, headers, config) { console.log('data error'); }); + $scope.addresstimechart= { + data:[], + labels: [] + }; + $http.get(urlapi + 'addresstimechart/' + $routeParams.hash) + .then(function(data, status, headers, config) { + console.log(data); + $scope.addresstimechart.data = data.data.data; + $scope.addresstimechart.labels = data.data.labels; + }, function(data, status, headers, config) { + console.log('data error'); + }); }); diff --git a/web/views/block/block.html b/web/views/block/block.html index b3d5334..fea5f41 100644 --- a/web/views/block/block.html +++ b/web/views/block/block.html @@ -43,7 +43,7 @@ - + {{tx.txid}} diff --git a/web/views/tx/tx.html b/web/views/tx/tx.html new file mode 100644 index 0000000..76fa3a4 --- /dev/null +++ b/web/views/tx/tx.html @@ -0,0 +1,47 @@ +
+
+
+
+

Tx {{tx.txid}}

+
+
+
+ + + + + + + + + + + + + + + +
TxidInputOutput
+ + {{tx.txid}} + + +
+ + {{vin.address}} + + :{{vin.amount}} +
+
+
+ + {{vout.address}} + + :{{vout.value}} +
+
+
+
+
+
+
diff --git a/web/views/tx/tx.js b/web/views/tx/tx.js new file mode 100644 index 0000000..c66e791 --- /dev/null +++ b/web/views/tx/tx.js @@ -0,0 +1,21 @@ +'use strict'; + +angular.module('app.tx', ['ngRoute']) + + .config(['$routeProvider', function($routeProvider) { + $routeProvider.when('/tx/:txid', { + templateUrl: 'views/tx/tx.html', + controller: 'TxCtrl' + }); + }]) + + .controller('TxCtrl', function($scope, $http, $routeParams) { + $scope.tx = {}; + $http.get(urlapi + 'tx/' + $routeParams.txid) + .then(function(data, status, headers, config) { + console.log(data); + $scope.tx = data.data; + }, function(data, status, headers, config) { + console.log('data error'); + }); + });