From 5baca8bc8150b8c11542f53a8a7b2e00de527eb2 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sat, 19 Aug 2017 21:15:03 +0200 Subject: [PATCH] added get7day tx/day analysis --- DevelopmentNotes.md | 12 ++----- dateAnalysis.go | 19 +++++++---- mongoModels.go | 8 ++--- serverRoutes.go | 68 ++++++++++++++++++++++++++++++++++------ web/views/main/main.html | 13 ++++++-- web/views/main/main.js | 24 +++++++++++--- 6 files changed, 108 insertions(+), 36 deletions(-) diff --git a/DevelopmentNotes.md b/DevelopmentNotes.md index dbab99b..97c8912 100644 --- a/DevelopmentNotes.md +++ b/DevelopmentNotes.md @@ -2,24 +2,20 @@ ## ToDo list +- Sankey generation without loops + - Backend - Network Address generation avoiding infinite relation loops - Sankey Address generation without loops - Frontend - After Sankey visualization, go to Network Address visualization and render without Sankey dots - Both - - Tx/day - Tx volume - Block size - Blockchain size other -- To get tx/hours of last 24 hours - Search for TxModel with DateF > last24h - Count for each hour -- To get tx/day of last month - Search TxModel with DateF > last month - Count each day + - Add counter with total blocks, total tx, total address - store date hour, day, etc: @@ -35,5 +31,3 @@ other ``` - mantain connection with wallet using websockets - -- add 24h to hour analysis, to show also hours with 0 transactions diff --git a/dateAnalysis.go b/dateAnalysis.go index 2a0da60..c54ebdf 100644 --- a/dateAnalysis.go +++ b/dateAnalysis.go @@ -3,12 +3,18 @@ package main import ( "fmt" "strconv" - "strings" "time" "gopkg.in/mgo.v2/bson" ) +func map24hours() map[int]int { + h := make(map[int]int) + for i := 0; i < 24; i++ { + h[i] = 0 + } + return h +} func decomposeDate(blockTime int64) (int, int, int, int) { /*i, err := strconv.ParseInt(blockTime, 10, 64) if err != nil { @@ -38,16 +44,17 @@ func timeToDate(blockTime int64) string { } func hourAnalysis(e EdgeModel, blockTime int64) { //fmt.Println(blockTime) - date := timeToDate(blockTime) + /*date := timeToDate(blockTime) dateHour := strings.Split(date, " ")[1] - hour := strings.Split(dateHour, ":")[0] + hourString := strings.Split(dateHour, ":")[0]*/ + _, _, _, hour := decomposeDate(blockTime) - hourCount := HourCountModel{} + hourCount := ChartCountModel{} err := hourCountCollection.Find(bson.M{"hour": hour}).One(&hourCount) if err != nil { //date not yet in DB - var hourCount HourCountModel - hourCount.Hour = hour + var hourCount ChartCountModel + hourCount.Elem = hour hourCount.Count = 1 err = hourCountCollection.Insert(hourCount) check(err) diff --git a/mongoModels.go b/mongoModels.go index 4ae567e..8536350 100644 --- a/mongoModels.go +++ b/mongoModels.go @@ -84,11 +84,11 @@ type SankeyModel struct { Links []SankeyLinkModel `json:"links"` } -type HourCountModel struct { - Hour string `json:"hour"` - Count int `json:"count"` +type ChartCountModel struct { + Elem int `json:"elem"` + Count int `json:"count"` } -type HourAnalysisResp struct { +type ChartAnalysisResp struct { Labels []string `json:"labels"` Data []int `json:"data"` } diff --git a/serverRoutes.go b/serverRoutes.go index 1a04631..c0529c4 100644 --- a/serverRoutes.go +++ b/serverRoutes.go @@ -70,6 +70,12 @@ var routes = Routes{ "/last24hour", GetLast24HourAnalysis, }, + Route{ + "GetLast7DayAnalysis", + "Get", + "/last7day", + GetLast7DayAnalysis, + }, } //ROUTES @@ -210,18 +216,18 @@ func NetworkMap(w http.ResponseWriter, r *http.Request) { func GetTotalHourAnalysis(w http.ResponseWriter, r *http.Request) { ipFilter(w, r) - hourAnalysis := []HourCountModel{} + hourAnalysis := []ChartCountModel{} iter := hourCountCollection.Find(bson.M{}).Limit(10000).Iter() err := iter.All(&hourAnalysis) //sort by hour sort.Slice(hourAnalysis, func(i, j int) bool { - return hourAnalysis[i].Hour < hourAnalysis[j].Hour + return hourAnalysis[i].Elem < hourAnalysis[j].Elem }) - var resp HourAnalysisResp + var resp ChartAnalysisResp for _, d := range hourAnalysis { - resp.Labels = append(resp.Labels, d.Hour) + resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem)) resp.Data = append(resp.Data, d.Count) } @@ -246,22 +252,64 @@ func GetLast24HourAnalysis(w http.ResponseWriter, r *http.Request) { }).Sort("-$natural").All(&txs) check(err) - hourFrequencies := make(map[int]int) + //generate map with 24 hours + hourFrequencies := map24hours() for _, tx := range txs { hourFrequencies[tx.Date.Hour]++ } - var hourCount []HourCountModel + var hourCount []ChartCountModel for hour, frequency := range hourFrequencies { - hourCount = append(hourCount, HourCountModel{strconv.Itoa(hour), frequency}) + hourCount = append(hourCount, ChartCountModel{hour, frequency}) } //sort by hour sort.Slice(hourCount, func(i, j int) bool { - return hourCount[i].Hour < hourCount[j].Hour + return hourCount[i].Elem < hourCount[j].Elem }) - var resp HourAnalysisResp + var resp ChartAnalysisResp for _, d := range hourCount { - resp.Labels = append(resp.Labels, d.Hour) + resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem)) + resp.Data = append(resp.Data, d.Count) + } + + //convert []resp struct to json + jsonResp, err := json.Marshal(resp) + check(err) + fmt.Fprintln(w, string(jsonResp)) +} +func GetLast7DayAnalysis(w http.ResponseWriter, r *http.Request) { + ipFilter(w, r) + + fromDate := time.Now().AddDate(0, 0, -7) + toDate := time.Now() + + txs := []TxModel{} + err := txCollection.Find(bson.M{ + "datet": bson.M{ + "$gt": fromDate, + "$lt": toDate, + }, + }).Sort("-$natural").All(&txs) + check(err) + + //generate map with 24 hours + //hourFrequencies := map24hours() + dayFrequencies := make(map[int]int) + for _, tx := range txs { + dayFrequencies[tx.Date.Day]++ + } + var dayCount []ChartCountModel + for day, frequency := range dayFrequencies { + dayCount = append(dayCount, ChartCountModel{day, frequency}) + } + //sort by hour + sort.Slice(dayCount, func(i, j int) bool { + return dayCount[i].Elem < dayCount[j].Elem + }) + + var resp ChartAnalysisResp + for _, d := range dayCount { + resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem)) resp.Data = append(resp.Data, d.Count) } diff --git a/web/views/main/main.html b/web/views/main/main.html index 1f1c748..59905cd 100644 --- a/web/views/main/main.html +++ b/web/views/main/main.html @@ -19,8 +19,15 @@

Last 24 hours Tx/Hour

- - + + +
+
+

Last 7 days Tx/Day

+
+
+ +
@@ -29,7 +36,7 @@

Hours

- +
diff --git a/web/views/main/main.js b/web/views/main/main.js index c28ac46..1186ba4 100644 --- a/web/views/main/main.js +++ b/web/views/main/main.js @@ -35,15 +35,31 @@ angular.module('app.main', ['ngRoute']) }); //date analysis - $scope.data = []; - $scope.labels = []; + $scope.last24hour= { + data:[], + labels: [] + }; $http.get(urlapi + 'last24hour') .then(function(data, status, headers, config) { console.log('data success'); console.log(data); - $scope.data = data.data.data; - $scope.labels = data.data.labels; + $scope.last24hour.data = data.data.data; + $scope.last24hour.labels = data.data.labels; + }, function(data, status, headers, config) { + console.log('data error'); + }); + $scope.last7day= { + data:[], + labels: [] + }; + $http.get(urlapi + 'last7day') + .then(function(data, status, headers, config) { + console.log('data success'); + console.log(data); + + $scope.last7day.data = data.data.data; + $scope.last7day.labels = data.data.labels; }, function(data, status, headers, config) { console.log('data error'); });