Skip to content

Commit

Permalink
add feed info endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
jppade committed Apr 29, 2022
1 parent 38ed091 commit 80ed90b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/http/restServer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/appleboy/gin-jwt/v2 v2.6.4
github.com/diadata-org/diadata v1.4.1-rc-131
github.com/diadata-org/diadata v1.4.1-rc-148
github.com/gin-contrib/cache v1.1.0
github.com/gin-gonic/contrib v0.0.0-20201101042839-6a891bf89f19
github.com/gin-gonic/gin v1.7.2
Expand Down
2 changes: 1 addition & 1 deletion cmd/http/restServer/restServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func main() {
diaGroup.GET("/NFT/:blockchain/:address/:id", cache.CachePageAtomic(memoryStore, cachingTimeLong, diaApiEnv.GetNFT))
diaGroup.GET("/NFTTrades/:blockchain/:address/:id", cache.CachePageAtomic(memoryStore, cachingTimeLong, diaApiEnv.GetNFTTrades))
diaGroup.GET("/NFTPrice30Days/:blockchain/:address", cache.CachePageAtomic(memoryStore, cachingTimeLong, diaApiEnv.GetNFTPrice30Days))
diaGroup.GET("/assetMarkets/:blockchain/:address", cache.CachePageAtomic(memoryStore, cachingTimeLong, diaApiEnv.GetVolumePerExchange))
diaGroup.GET("/feedStats/:blockchain/:address", cache.CachePageAtomic(memoryStore, cachingTimeLong, diaApiEnv.GetFeedStats))

}

Expand Down
97 changes: 93 additions & 4 deletions pkg/http/restServer/diaApi/diaApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -1966,13 +1966,102 @@ func (env *Env) GetNFTPrice30Days(c *gin.Context) {
c.JSON(http.StatusOK, avgPrice)
}

func (env *Env) GetVolumePerExchange(c *gin.Context) {
func (env *Env) GetFeedStats(c *gin.Context) {

blockchain := c.Param("blockchain")
address := common.HexToAddress(c.Param("address")).Hex()
exchangeVolume, err := env.DataStore.Get24HVolumePerExchange(dia.Asset{Blockchain: blockchain, Address: address})
address := c.Param("address")
starttimeStr := c.Query("starttime")
endtimeStr := c.Query("endtime")
var starttime time.Time
var endtime time.Time

if endtimeStr == "" {
endtime = time.Now()
} else {
endtimeInt, err := strconv.ParseInt(endtimeStr, 10, 64)
if err != nil {
restApi.SendError(c, http.StatusInternalServerError, err)
return
}
endtime = time.Unix(endtimeInt, 0)
}
if starttimeStr == "" {
starttime = endtime.AddDate(0, 0, -1)
} else {
starttimeInt, err := strconv.ParseInt(starttimeStr, 10, 64)
if err != nil {
restApi.SendError(c, http.StatusInternalServerError, err)
return
}
starttime = time.Unix(starttimeInt, 0)
}

asset, err := env.RelDB.GetAsset(address, blockchain)
if err != nil {
restApi.SendError(c, http.StatusInternalServerError, nil)
}

c.JSON(http.StatusOK, exchangeVolume)
exchVolumes, err := env.RelDB.GetAggVolumesByExchange(asset, starttime, endtime)
if err != nil {
restApi.SendError(c, http.StatusInternalServerError, nil)
}

pairVolumes, err := env.RelDB.GetAggVolumesByPair(asset, starttime, endtime)
if err != nil {
restApi.SendError(c, http.StatusInternalServerError, nil)
}

tradesDist, err := env.RelDB.GetTradesDistribution(asset, starttime, endtime)
if err != nil {
restApi.SendError(c, http.StatusInternalServerError, nil)
}

type localDistType struct {
NumTradesTotal int `json:"NumTradesTotal"`
NumLowBins int `json:"NumberLowBins"`
Threshold int `json:"Threshold"`
SizeBinSeconds int64 `json:"SizeBin"`
AvgNumPerBin float64 `json:"AverageNumberPerBin"`
StdDeviation float64 `json:"StandardDeviation"`
TimeRangeSeconds int64 `json:"TimeRangeSeconds"`
}
var tradesDistReduced []localDistType
for _, val := range tradesDist {
tradesDistReduced = append(tradesDistReduced, localDistType{
NumTradesTotal: val.NumTradesTotal,
NumLowBins: val.NumLowBins,
Threshold: val.Threshold,
SizeBinSeconds: val.SizeBinSeconds,
AvgNumPerBin: val.AvgNumPerBin,
StdDeviation: val.StdDeviation,
TimeRangeSeconds: val.TimeRangeSeconds,
})
}

type localReturn struct {
ExchangeVolumes []dia.ExchangeVolume
PairVolumes []dia.PairVolume
TradesDistribution localDistType
Timestamp time.Time
}

var retVal []localReturn

for i := range exchVolumes {
var l localReturn
l.ExchangeVolumes = exchVolumes[i].Volumes
l.PairVolumes = pairVolumes[i].Volumes
l.Timestamp = exchVolumes[i].Timestamp
if len(tradesDistReduced) > i {
l.TradesDistribution = tradesDistReduced[i]
}
retVal = append(retVal, l)
}
if endtimeStr == "" && starttimeStr == "" {
if len(retVal) > 0 {
c.JSON(http.StatusOK, retVal[0])
}
} else {
c.JSON(http.StatusOK, retVal)
}
}
3 changes: 2 additions & 1 deletion pkg/model/relDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type RelDatastore interface {
GetAssetsWithVOL(numAssets int64, substring string) ([]dia.Asset, error)
SetAggregatedVolume(aggVol dia.AggregatedVolume) error
GetAggregatedVolumes(asset dia.Asset, starttime time.Time, endtime time.Time) ([]dia.AggregatedVolume, error)
GetAggVolumesByExchange(asset dia.Asset, starttime time.Time, endtime time.Time) ([]dia.ExchangeVolume, error)
GetAggVolumesByExchange(asset dia.Asset, starttime time.Time, endtime time.Time) ([]dia.ExchangeVolumesList, error)
GetAggVolumesByPair(asset dia.Asset, starttime time.Time, endtime time.Time) ([]dia.PairVolumesList, error)
SetTradesDistribution(tradesDist dia.TradesDistribution) error
GetTradesDistribution(asset dia.Asset, starttime time.Time, endtime time.Time) ([]dia.TradesDistribution, error)

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/trades.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (datastore *DB) GetTradesByExchangesBatchedFull(asset dia.Asset, exchanges
}
subQuery = "and exchange =~ /" + strings.TrimRight(subQuery, "|") + "/"
}
query = query + fmt.Sprintf("SELECT time,estimatedUSDPrice,exchange,foreignTradeID,pair,price,symbol,volume,verified,basetokenblockchain,basetokenaddress FROM %s WHERE quotetokenaddress='%s' AND quotetokenblockchain='%s' %s AND estimatedUSDPrice > 0 AND time > %d AND time <= %d ;", influxDbTradesTable, asset.Address, asset.Blockchain, subQuery, startTimes[i].UnixNano(), endTimes[i].UnixNano())
query = query + fmt.Sprintf("SELECT time,estimatedUSDPrice,exchange,foreignTradeID,pair,price,symbol,volume,verified,basetokenblockchain,basetokenaddress FROM %s WHERE quotetokenaddress='%s' AND quotetokenblockchain='%s' %s AND estimatedUSDPrice > 0 AND time > %d AND time <= %d ;", influxDbTradesTable, asset.Address, asset.Blockchain, subQuery, startTimes[i].UnixNano(), endTimes[i].UnixNano())
}

res, err := queryInfluxDB(datastore.influxClient, query)
Expand Down

0 comments on commit 80ed90b

Please sign in to comment.