Browse Source

added get7day tx/day analysis

master
arnaucode 7 years ago
parent
commit
5baca8bc81
6 changed files with 108 additions and 36 deletions
  1. +3
    -9
      DevelopmentNotes.md
  2. +13
    -6
      dateAnalysis.go
  3. +4
    -4
      mongoModels.go
  4. +58
    -10
      serverRoutes.go
  5. +10
    -3
      web/views/main/main.html
  6. +20
    -4
      web/views/main/main.js

+ 3
- 9
DevelopmentNotes.md

@ -2,24 +2,20 @@
## ToDo list ## ToDo list
- Sankey generation without loops
- Backend - Backend
- Network Address generation avoiding infinite relation loops - Network Address generation avoiding infinite relation loops
- Sankey Address generation without loops - Sankey Address generation without loops
- Frontend - Frontend
- After Sankey visualization, go to Network Address visualization and render without Sankey dots - After Sankey visualization, go to Network Address visualization and render without Sankey dots
- Both - Both
- Tx/day
- Tx volume - Tx volume
- Block size - Block size
- Blockchain size - Blockchain size
other 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 - Add counter with total blocks, total tx, total address
- store date hour, day, etc: - store date hour, day, etc:
@ -35,5 +31,3 @@ other
``` ```
- mantain connection with wallet using websockets - mantain connection with wallet using websockets
- add 24h to hour analysis, to show also hours with 0 transactions

+ 13
- 6
dateAnalysis.go

@ -3,12 +3,18 @@ package main
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
"gopkg.in/mgo.v2/bson" "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) { func decomposeDate(blockTime int64) (int, int, int, int) {
/*i, err := strconv.ParseInt(blockTime, 10, 64) /*i, err := strconv.ParseInt(blockTime, 10, 64)
if err != nil { if err != nil {
@ -38,16 +44,17 @@ func timeToDate(blockTime int64) string {
} }
func hourAnalysis(e EdgeModel, blockTime int64) { func hourAnalysis(e EdgeModel, blockTime int64) {
//fmt.Println(blockTime) //fmt.Println(blockTime)
date := timeToDate(blockTime)
/*date := timeToDate(blockTime)
dateHour := strings.Split(date, " ")[1] 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) err := hourCountCollection.Find(bson.M{"hour": hour}).One(&hourCount)
if err != nil { if err != nil {
//date not yet in DB //date not yet in DB
var hourCount HourCountModel
hourCount.Hour = hour
var hourCount ChartCountModel
hourCount.Elem = hour
hourCount.Count = 1 hourCount.Count = 1
err = hourCountCollection.Insert(hourCount) err = hourCountCollection.Insert(hourCount)
check(err) check(err)

+ 4
- 4
mongoModels.go

@ -84,11 +84,11 @@ type SankeyModel struct {
Links []SankeyLinkModel `json:"links"` 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"` Labels []string `json:"labels"`
Data []int `json:"data"` Data []int `json:"data"`
} }

+ 58
- 10
serverRoutes.go

@ -70,6 +70,12 @@ var routes = Routes{
"/last24hour", "/last24hour",
GetLast24HourAnalysis, GetLast24HourAnalysis,
}, },
Route{
"GetLast7DayAnalysis",
"Get",
"/last7day",
GetLast7DayAnalysis,
},
} }
//ROUTES //ROUTES
@ -210,18 +216,18 @@ func NetworkMap(w http.ResponseWriter, r *http.Request) {
func GetTotalHourAnalysis(w http.ResponseWriter, r *http.Request) { func GetTotalHourAnalysis(w http.ResponseWriter, r *http.Request) {
ipFilter(w, r) ipFilter(w, r)
hourAnalysis := []HourCountModel{}
hourAnalysis := []ChartCountModel{}
iter := hourCountCollection.Find(bson.M{}).Limit(10000).Iter() iter := hourCountCollection.Find(bson.M{}).Limit(10000).Iter()
err := iter.All(&hourAnalysis) err := iter.All(&hourAnalysis)
//sort by hour //sort by hour
sort.Slice(hourAnalysis, func(i, j int) bool { 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 { 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) resp.Data = append(resp.Data, d.Count)
} }
@ -246,22 +252,64 @@ func GetLast24HourAnalysis(w http.ResponseWriter, r *http.Request) {
}).Sort("-$natural").All(&txs) }).Sort("-$natural").All(&txs)
check(err) check(err)
hourFrequencies := make(map[int]int)
//generate map with 24 hours
hourFrequencies := map24hours()
for _, tx := range txs { for _, tx := range txs {
hourFrequencies[tx.Date.Hour]++ hourFrequencies[tx.Date.Hour]++
} }
var hourCount []HourCountModel
var hourCount []ChartCountModel
for hour, frequency := range hourFrequencies { for hour, frequency := range hourFrequencies {
hourCount = append(hourCount, HourCountModel{strconv.Itoa(hour), frequency})
hourCount = append(hourCount, ChartCountModel{hour, frequency})
} }
//sort by hour //sort by hour
sort.Slice(hourCount, func(i, j int) bool { 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 { 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) resp.Data = append(resp.Data, d.Count)
} }

+ 10
- 3
web/views/main/main.html

@ -19,8 +19,15 @@
<h3 class="panel-title">Last 24 hours Tx/Hour</h3> <h3 class="panel-title">Last 24 hours Tx/Hour</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<canvas id="line" class="chart chart-line" chart-data="data" chart-labels="labels">
</canvas>
<canvas id="line" class="chart chart-line" chart-data="last24hour.data" chart-labels="last24hour.labels">
</canvas>
</div>
<div class="panel-heading c_blueGrey300">
<h3 class="panel-title">Last 7 days Tx/Day</h3>
</div>
<div class="panel-body">
<canvas id="line" class="chart chart-line" chart-data="last7day.data" chart-labels="last7day.labels">
</canvas>
</div> </div>
</div> </div>
<div class="col-sm-4"> <div class="col-sm-4">
@ -29,7 +36,7 @@
<h3 class="panel-title">Hours</h3> <h3 class="panel-title">Hours</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<canvas id="doughnut" class="chart chart-doughnut" chart-data="data" chart-labels="labels">
<canvas id="doughnut" class="chart chart-doughnut" chart-data="last24hour.data" chart-labels="last24hour.labels">
</canvas> </canvas>
</div> </div>
</div> </div>

+ 20
- 4
web/views/main/main.js

@ -35,15 +35,31 @@ angular.module('app.main', ['ngRoute'])
}); });
//date analysis //date analysis
$scope.data = [];
$scope.labels = [];
$scope.last24hour= {
data:[],
labels: []
};
$http.get(urlapi + 'last24hour') $http.get(urlapi + 'last24hour')
.then(function(data, status, headers, config) { .then(function(data, status, headers, config) {
console.log('data success'); console.log('data success');
console.log(data); 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) { }, function(data, status, headers, config) {
console.log('data error'); console.log('data error');
}); });

Loading…
Cancel
Save