Skip to content

Commit

Permalink
Started to implement alternative example
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Mueller committed Sep 10, 2017
1 parent 9df28a2 commit d4aaebe
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 437 deletions.
95 changes: 0 additions & 95 deletions example/behaviors/coin.go

This file was deleted.

80 changes: 0 additions & 80 deletions example/behaviors/coins.go

This file was deleted.

62 changes: 12 additions & 50 deletions example/behaviors/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package behaviors
//--------------------

import (
"strconv"
"sync"
"time"

"github.com/tideland/gocells/behaviors"
Expand All @@ -28,55 +28,17 @@ func MakeLogger() cells.Behavior {
return behaviors.NewLoggerBehavior()
}

// MakeRawCoinsConverter returns a behavior converting raw coins
// into correct typed ones.
func MakeRawCoinsConverter() cells.Behavior {
processor := func(cell cells.Cell, event cells.Event) error {
var rawCoins RawCoins
err := event.Payload().Unmarshal(&rawCoins)
if err != nil {
return err
}
// Two helpers for trusted conversions.
atof := func(a string) float64 {
f, err := strconv.ParseFloat(a, 64)
if err != nil {
return 0.0
}
return f
}
atoi := func(a string) int {
return int(atof(a))
}
// Convert raw received coins and emit more
// usable ones.
var coins Coins
for _, rawCoin := range rawCoins {
if rawCoin.MarketCapUSD == "" {
continue
}
coin := Coin{
ID: rawCoin.ID,
Name: rawCoin.Name,
Symbol: rawCoin.Symbol,
Rank: atoi(rawCoin.Rank),
PriceUSD: atof(rawCoin.PriceUSD),
PriceBTC: atof(rawCoin.PriceBTC),
Volume24hUSD: atof(rawCoin.Volume24hUSD),
MarketCapUSD: atof(rawCoin.MarketCapUSD),
AvailableSupply: atoi(rawCoin.AvailableSupply),
TotalSupply: atoi(rawCoin.TotalSupply),
PercentChange1h: atof(rawCoin.PercentChange1h),
PercentChange24h: atof(rawCoin.PercentChange24h),
PercentChange7d: atof(rawCoin.PercentChange7d),
LastUpdated: time.Unix(int64(atoi(rawCoin.LastUpdated)), 0),
}
coins = append(coins, coin)
}
cell.EmitNew("coins", coins)
return nil
}
return behaviors.NewSimpleProcessorBehavior(processor)
// MakeTicker returns a ticker for the clocking of the
// emitting of the data.
func MakeTicker() cells.Behavior {
return behaviors.NewTickerBehavior(100 * time.Millisecond)
}

// MakeEndOFData returns a behavior reacting when all
// data is done.
func MakeEndOfData(wg sync.WaitGroup) cells.Behavior {
wg.Add(1)
return nil
}

// EOF
54 changes: 10 additions & 44 deletions example/behaviors/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,20 @@ package behaviors
// IMPORTS
//--------------------

import (
"time"
)

//--------------------
// COIN
// POPULATION
//--------------------

// RawCoin contains one raw coin object of the API.
type RawCoin struct {
ID string `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Rank string `json:"rank"`
PriceUSD string `json:"price_usd"`
PriceBTC string `json:"price_btc"`
Volume24hUSD string `json:"24h_volume_usd"`
MarketCapUSD string `json:"market_cap_usd"`
AvailableSupply string `json:"available_supply"`
TotalSupply string `json:"total_supply"`
PercentChange1h string `json:"percent_change_1h"`
PercentChange24h string `json:"percent_change_24h"`
PercentChange7d string `json:"percent_change_7d"`
LastUpdated string `json:"last_updated"`
}

// RawCoins contains a list of raw coins
type RawCoins []RawCoin

// Coin is one converted raw coin reduced to the minimum.
type Coin struct {
ID string
Name string
Symbol string
Rank int
PriceUSD float64
PriceBTC float64
Volume24hUSD float64
MarketCapUSD float64
AvailableSupply int
TotalSupply int
PercentChange1h float64
PercentChange24h float64
PercentChange7d float64
LastUpdated time.Time
// Population contains the population of a country or region
// in one year.
type Population struct {
CountryName string
CountryCode string
Year string
Value int
}

// Coins contains a list of coins
type Coins []Coin
// Populations is the set of all populations.
type Popoulations []Population

// EOF
Loading

0 comments on commit d4aaebe

Please sign in to comment.