Skip to content
Snippets Groups Projects
Commit 28233092 authored by Gomez Goiri, Aitor's avatar Gomez Goiri, Aitor
Browse files

Removing unnecessary global stats in chaincode and some visual enhancements

parent 58fea059
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,7 @@ const bidAsset = async ( ...@@ -19,7 +19,7 @@ const bidAsset = async (
{ {
type: "number", type: "number",
name: "inputQuantity", name: "inputQuantity",
message: `Choose an ammount of slag (in ${assetToBid.units}, max ${assetToBid.quantity}):` message: `Choose an ammount of slag (max: ${assetToBid.quantity} ${assetToBid.units}):`
} }
]) ])
chosenQuantity = inputQuantity chosenQuantity = inputQuantity
......
...@@ -39,12 +39,13 @@ const humanizePercentage = value => { ...@@ -39,12 +39,13 @@ const humanizePercentage = value => {
if (value.percentage === undefined) { if (value.percentage === undefined) {
return value.total return value.total
} }
return `${value.total} [%${value.percentage.toFixed(2)}]` return `${value.total.toFixed(3)} [%${(value.percentage * 100).toFixed(2)}]`
} }
const humanizePrice = value => `${value.avg} [${value.min}, ${value.max}]` const humanizePrice = value =>
`${value.avg.toFixed(2)} [${value.min.toFixed(2)}, ${value.max.toFixed(2)}]`
const toLine = stat => [ const toLine = stat => [
stat.total, stat.total.toFixed(3),
humanizePercentage(stat.reused), humanizePercentage(stat.reused),
humanizePercentage(stat.discarded), humanizePercentage(stat.discarded),
humanizePrice(stat.price) humanizePrice(stat.price)
...@@ -61,11 +62,10 @@ const getStats = async apiClient => { ...@@ -61,11 +62,10 @@ const getStats = async apiClient => {
"Discarded\nt [%]", "Discarded\nt [%]",
"Price (€)\n Avg [Min, Max]" "Price (€)\n Avg [Min, Max]"
], ],
...Object.entries(stats.perCompany || []).map(([org, val]) => [ ...Object.entries(stats.perSteelCompany || []).map(([org, val]) => [
org, org,
...toLine(val) ...toLine(val)
]), ])
["Total", ...toLine(stats.global)]
] ]
console.log(table(tableValues, TABLE_CONFIG)) console.log(table(tableValues, TABLE_CONFIG))
......
...@@ -199,7 +199,7 @@ const bidResponse = ...@@ -199,7 +199,7 @@ const bidResponse =
name: "inputBidder", name: "inputBidder",
message: `Select the bidder whose bid will be ${action}ed`, message: `Select the bidder whose bid will be ${action}ed`,
choices: assetSelected.fields.bids.map(b => ({ choices: assetSelected.fields.bids.map(b => ({
name: `${b.bidder.id} (quantity: ${b.quantity}, price: ${b.price})`, name: `${b.bidder.id} (quantity: ${b.quantity} ${assetSelected.units}, price: ${b.price})`,
value: b.bidder value: b.bidder
})) }))
} }
......
...@@ -15,8 +15,7 @@ import ( ...@@ -15,8 +15,7 @@ import (
type StatsResult struct { type StatsResult struct {
GlobalStats model.Stats `json:"global"` // in tons PerSteelCompany map[string]model.Stats `json:"perSteelCompany,omitempty"`
PerCompany map[string]model.Stats `json:"perCompany,omitempty"`
} }
func GetStatsOperation(stub shared.LedgerBuildrStubInterface, request shared.LedgerBuildrAsset) protos.Response { func GetStatsOperation(stub shared.LedgerBuildrStubInterface, request shared.LedgerBuildrAsset) protos.Response {
...@@ -29,14 +28,8 @@ func GetStatsOperation(stub shared.LedgerBuildrStubInterface, request shared.Led ...@@ -29,14 +28,8 @@ func GetStatsOperation(stub shared.LedgerBuildrStubInterface, request shared.Led
return api.NewApiResponsePtr(fnName, err, nil).SendResponse() return api.NewApiResponsePtr(fnName, err, nil).SendResponse()
} }
globalStat, err := getStats(stub, GLOBAL_KEY)
if err != nil {
return api.NewApiResponsePtr(fnName, err, nil).SendResponse()
}
ret.GlobalStats = *globalStat.RefineStats()
if len(*orgs) > 0 { if len(*orgs) > 0 {
ret.PerCompany = make(map[string]model.Stats) ret.PerSteelCompany = make(map[string]model.Stats)
} }
for _, org := range *orgs { for _, org := range *orgs {
...@@ -44,7 +37,7 @@ func GetStatsOperation(stub shared.LedgerBuildrStubInterface, request shared.Led ...@@ -44,7 +37,7 @@ func GetStatsOperation(stub shared.LedgerBuildrStubInterface, request shared.Led
if err != nil { if err != nil {
return api.NewApiResponsePtr(fnName, err, nil).SendResponse() return api.NewApiResponsePtr(fnName, err, nil).SendResponse()
} }
ret.PerCompany[org] = *orgStat.RefineStats() ret.PerSteelCompany[org] = *orgStat.RefineStats()
} }
return api.NewAPIGenericResponsePtr(fnName, nil, ret).SendResponse() return api.NewAPIGenericResponsePtr(fnName, nil, ret).SendResponse()
......
...@@ -9,14 +9,14 @@ package stats ...@@ -9,14 +9,14 @@ package stats
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"git.code.tecnalia.com/blockchain/hypercog/model" "git.code.tecnalia.com/blockchain/hypercog/model"
"git.code.tecnalia.com/ledgerbuilder/sdk/core/util/logging" "git.code.tecnalia.com/ledgerbuilder/sdk/core/util/logging"
"git.code.tecnalia.com/ledgerbuilder/sdk/shared" "git.code.tecnalia.com/ledgerbuilder/sdk/shared"
) )
const GLOBAL_KEY = "global" const STEEL_ORG_LIST_KEY = "steel_orgs"
const ORG_LIST_KEY = "org_list"
type OrgList []string type OrgList []string
...@@ -26,6 +26,7 @@ func getStats(stub shared.LedgerBuildrStubInterface, key string) (st *model.RawS ...@@ -26,6 +26,7 @@ func getStats(stub shared.LedgerBuildrStubInterface, key string) (st *model.RawS
value, err := stub.GetState(key) value, err := stub.GetState(key)
if err != nil || len(value) == 0 { if err != nil || len(value) == 0 {
st = new(model.RawStats) st = new(model.RawStats)
st.MinPrice = math.MaxFloat64
} else { } else {
err = json.Unmarshal(value, &st) err = json.Unmarshal(value, &st)
if err != nil { if err != nil {
...@@ -51,7 +52,7 @@ func putStats(stub shared.LedgerBuildrStubInterface, key string, stats *model.Ra ...@@ -51,7 +52,7 @@ func putStats(stub shared.LedgerBuildrStubInterface, key string, stats *model.Ra
} }
func getOrgList(stub shared.LedgerBuildrStubInterface) (st *OrgList, err error) { func getOrgList(stub shared.LedgerBuildrStubInterface) (st *OrgList, err error) {
value, err := stub.GetState(ORG_LIST_KEY) value, err := stub.GetState(STEEL_ORG_LIST_KEY)
if err != nil || len(value) == 0 { if err != nil || len(value) == 0 {
st = new(OrgList) st = new(OrgList)
} else { } else {
...@@ -65,11 +66,6 @@ func getOrgList(stub shared.LedgerBuildrStubInterface) (st *OrgList, err error) ...@@ -65,11 +66,6 @@ func getOrgList(stub shared.LedgerBuildrStubInterface) (st *OrgList, err error)
} }
func resetStats(stub shared.LedgerBuildrStubInterface) (error) { func resetStats(stub shared.LedgerBuildrStubInterface) (error) {
err := stub.DelState(GLOBAL_KEY)
if err != nil {
return err
}
orgs, err := getOrgList(stub) orgs, err := getOrgList(stub)
if len(*orgs) > 0 { if len(*orgs) > 0 {
for _, org := range *orgs { for _, org := range *orgs {
...@@ -80,7 +76,7 @@ func resetStats(stub shared.LedgerBuildrStubInterface) (error) { ...@@ -80,7 +76,7 @@ func resetStats(stub shared.LedgerBuildrStubInterface) (error) {
} }
} }
err = stub.DelState(ORG_LIST_KEY) err = stub.DelState(STEEL_ORG_LIST_KEY)
return err return err
} }
...@@ -107,7 +103,7 @@ func addOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err er ...@@ -107,7 +103,7 @@ func addOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err er
return err return err
} }
err = stub.PutState(ORG_LIST_KEY, serialized) err = stub.PutState(STEEL_ORG_LIST_KEY, serialized)
if err != nil { if err != nil {
return err return err
} }
...@@ -119,26 +115,21 @@ func addOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err er ...@@ -119,26 +115,21 @@ func addOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err er
} }
func retrieveExistingStats(stub shared.LedgerBuildrStubInterface) (*model.RawStats, *model.RawStats, error) { func retrieveExistingStats(stub shared.LedgerBuildrStubInterface) (*model.RawStats, error) {
org, err := stub.GetOrganization() org, err := stub.GetOrganization()
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
customStats, err := getStats(stub, org) customStats, err := getStats(stub, org)
if err != nil { if err != nil {
return nil, nil, err return nil, err
}
globalStats, err := getStats(stub , GLOBAL_KEY)
if err != nil {
return nil, nil, err
} }
return globalStats, customStats, nil return customStats, nil
} }
func upgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawStats, customStats *model.RawStats) (error) { func upgradeStats(stub shared.LedgerBuildrStubInterface, customStats *model.RawStats) (error) {
org, err := stub.GetOrganization() org, err := stub.GetOrganization()
if err != nil { if err != nil {
return err return err
...@@ -149,11 +140,6 @@ func upgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawS ...@@ -149,11 +140,6 @@ func upgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawS
return err return err
} }
err = putStats(stub, GLOBAL_KEY, globalStats)
if err != nil {
return err
}
err = addOrgIfNotExist(stub, org) err = addOrgIfNotExist(stub, org)
if err != nil { if err != nil {
return err return err
...@@ -165,15 +151,14 @@ func upgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawS ...@@ -165,15 +151,14 @@ func upgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawS
func RegisterSlag(stub shared.LedgerBuildrStubInterface, quantity uint32, units string) (error) { func RegisterSlag(stub shared.LedgerBuildrStubInterface, quantity uint32, units string) (error) {
globalStats, customStats, err := retrieveExistingStats(stub) orgStats, err := retrieveExistingStats(stub)
if err != nil { if err != nil {
return err return err
} }
customStats.RegisterSlag(quantity, units) orgStats.RegisterSlag(quantity, units)
globalStats.RegisterSlag(quantity, units)
err = upgradeStats(stub, globalStats, customStats) err = upgradeStats(stub, orgStats)
if err != nil { if err != nil {
return err return err
} }
...@@ -182,17 +167,16 @@ func RegisterSlag(stub shared.LedgerBuildrStubInterface, quantity uint32, units ...@@ -182,17 +167,16 @@ func RegisterSlag(stub shared.LedgerBuildrStubInterface, quantity uint32, units
} }
func RegisterSale(stub shared.LedgerBuildrStubInterface, quantity uint32, price float32, units string) (error) { func RegisterSale(stub shared.LedgerBuildrStubInterface, quantity uint32, price float32, units string) (error) {
globalStats, orgStats, err := retrieveExistingStats(stub) orgStats, err := retrieveExistingStats(stub)
if err != nil { if err != nil {
return err return err
} }
orgStats.UpgradeSell(quantity, price, units) orgStats.UpgradeSale(quantity, price, units)
orgStats.UpgradeSell(quantity, price, units)
log.Debug(fmt.Sprintf("Sale registered in stats (quantity: %d %s, price: %.2f €)", quantity, units, price)) log.Debug(fmt.Sprintf("Sale registered in stats (quantity: %d %s, price: %.2f €)", quantity, units, price))
err = upgradeStats(stub, globalStats, orgStats) err = upgradeStats(stub, orgStats)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -50,7 +50,7 @@ func (raw *RawStats) RegisterSlag(quantity uint32, units string) { ...@@ -50,7 +50,7 @@ func (raw *RawStats) RegisterSlag(quantity uint32, units string) {
raw.TotalSlag += toTons(quantity, units) raw.TotalSlag += toTons(quantity, units)
} }
func (raw *RawStats) UpgradeSell(quantity uint32, price float32, units string) { func (raw *RawStats) UpgradeSale(quantity uint32, price float32, units string) {
raw.SlagReused += toTons(quantity, units) raw.SlagReused += toTons(quantity, units)
raw.TotalPrice += float64(price) raw.TotalPrice += float64(price)
raw.BidAmount += 1 raw.BidAmount += 1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment