diff --git a/cmd/http/restServer/go.mod b/cmd/http/restServer/go.mod index 88c0e99a2..43633d364 100644 --- a/cmd/http/restServer/go.mod +++ b/cmd/http/restServer/go.mod @@ -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 diff --git a/cmd/http/restServer/restServer.go b/cmd/http/restServer/restServer.go index 5de31e398..b6d72bcd5 100644 --- a/cmd/http/restServer/restServer.go +++ b/cmd/http/restServer/restServer.go @@ -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)) } diff --git a/pkg/http/restServer/diaApi/diaApi.go b/pkg/http/restServer/diaApi/diaApi.go index 0a34bee42..28c09c787 100644 --- a/pkg/http/restServer/diaApi/diaApi.go +++ b/pkg/http/restServer/diaApi/diaApi.go @@ -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) + } } diff --git a/pkg/model/relDB.go b/pkg/model/relDB.go index 317a3822c..234f39d39 100644 --- a/pkg/model/relDB.go +++ b/pkg/model/relDB.go @@ -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) diff --git a/pkg/model/trades.go b/pkg/model/trades.go index 0d84a27f2..3e7b9df36 100644 --- a/pkg/model/trades.go +++ b/pkg/model/trades.go @@ -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)