Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh data in goroutine #168

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions api/handler/circulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ func Circulation(c echo.Context) error {
func CirculationRefresh(c echo.Context) error {
cc := c.(*ApiContext)

circulation, err := cc.StorageClient.GetCirculation(cc.Storage)
if err != nil {
log.Warning("failed to get circulation: %v", err)
return c.NoContent(http.StatusInternalServerError)
}

if err = cc.Cache.Set(context.Background(), "circulation", circulation); err != nil {
log.Warning("failed to cache circulation: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
go func() {
circulation, err := cc.StorageClient.GetCirculation(cc.Storage)
if err != nil {
log.Warning("failed to get circulation: %v", err)
return
}

if err = cc.Cache.Set(context.Background(), "circulation", circulation); err != nil {
log.Warning("failed to cache circulation: %v", err)
return
}

log.Info("circulation refreshed")
}()

return c.NoContent(http.StatusOK)
}
44 changes: 26 additions & 18 deletions api/handler/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,20 @@ func EpochRefresh(c echo.Context) error {
return c.NoContent(http.StatusBadRequest)
}

epochStats, err := cc.StorageClient.GetEpochStats(cc.Storage, int64(id), cc.LayersPerEpoch)
if err != nil {
log.Warning("failed to get epoch stats: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
go func() {
epochStats, err := cc.StorageClient.GetEpochStats(cc.Storage, int64(id), cc.LayersPerEpoch)
if err != nil {
log.Warning("failed to get epoch stats: %v", err)
return
}

if err = cc.Cache.Set(context.Background(), "epochStats"+c.Param("id"), epochStats); err != nil {
log.Warning("failed to cache epoch stats: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
if err = cc.Cache.Set(context.Background(), "epochStats"+c.Param("id"), epochStats); err != nil {
log.Warning("failed to cache epoch stats: %v", err)
return
}

log.Info("epoch %d refreshed", id)
}()

return c.NoContent(http.StatusOK)
}
Expand Down Expand Up @@ -91,16 +95,20 @@ func EpochDecentralRefresh(c echo.Context) error {
return c.NoContent(http.StatusBadRequest)
}

epochStats, err := cc.StorageClient.GetEpochDecentralRatio(cc.Storage, int64(id))
if err != nil {
log.Warning("failed to get epoch stats: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
go func() {
epochStats, err := cc.StorageClient.GetEpochDecentralRatio(cc.Storage, int64(id))
if err != nil {
log.Warning("failed to get epoch stats: %v", err)
return
}

if err = cc.Cache.Set(context.Background(), "epochStatsDecentral"+c.Param("id"), epochStats); err != nil {
log.Warning("failed to cache epoch stats: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
if err = cc.Cache.Set(context.Background(), "epochStatsDecentral"+c.Param("id"), epochStats); err != nil {
log.Warning("failed to cache epoch stats: %v", err)
return
}

log.Info("epoch decentral refreshed")
}()

return c.NoContent(http.StatusOK)
}
24 changes: 14 additions & 10 deletions api/handler/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ func Overview(c echo.Context) error {
func OverviewRefresh(c echo.Context) error {
cc := c.(*ApiContext)

overview, err := cc.StorageClient.Overview(cc.Storage)
if err != nil {
log.Warning("failed to get overview: %v", err)
return c.NoContent(http.StatusInternalServerError)
}

if err = cc.Cache.Set(context.Background(), "overview", overview); err != nil {
log.Warning("failed to cache overview: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
go func() {
overview, err := cc.StorageClient.Overview(cc.Storage)
if err != nil {
log.Warning("failed to get overview: %v", err)
return
}

if err = cc.Cache.Set(context.Background(), "overview", overview); err != nil {
log.Warning("failed to cache overview: %v", err)
return
}

log.Info("overview refreshed")
}()

return c.NoContent(http.StatusOK)
}
60 changes: 34 additions & 26 deletions api/handler/smesher.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@ func Smeshers(c echo.Context) error {
func SmeshersRefresh(c echo.Context) error {
cc := c.(*ApiContext)

smeshers, err := cc.StorageClient.GetSmeshers(cc.Storage, 1000, 0)
if err != nil {
log.Warning("failed to get smeshers: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
go func() {
smeshers, err := cc.StorageClient.GetSmeshers(cc.Storage, 1000, 0)
if err != nil {
log.Warning("failed to get smeshers: %v", err)
return
}

for i := 0; i < len(smeshers.Smeshers); i += 20 {
if err = cc.Cache.Set(context.Background(), fmt.Sprintf("smeshers-%d-%d", 20, i), &storage.SmesherList{
Smeshers: smeshers.Smeshers[i : i+20],
}); err != nil {
log.Warning("failed to cache smeshers: %v", err)
return c.NoContent(http.StatusInternalServerError)
for i := 0; i < len(smeshers.Smeshers); i += 20 {
if err = cc.Cache.Set(context.Background(), fmt.Sprintf("smeshers-%d-%d", 20, i), &storage.SmesherList{
Smeshers: smeshers.Smeshers[i : i+20],
}); err != nil {
log.Warning("failed to cache smeshers: %v", err)
return
}
}
}

log.Info("smeshers refreshed")
}()

return c.NoContent(http.StatusOK)
}
Expand Down Expand Up @@ -97,23 +101,27 @@ func SmeshersByEpochRefresh(c echo.Context) error {
return c.NoContent(http.StatusBadRequest)
}

smeshers, err := cc.StorageClient.GetSmeshersByEpoch(cc.Storage, 1000, 0, uint64(epochId))
if err != nil {
log.Warning("failed to get smeshers: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
go func() {
smeshers, err := cc.StorageClient.GetSmeshersByEpoch(cc.Storage, 1000, 0, uint64(epochId))
if err != nil {
log.Warning("failed to get smeshers: %v", err)
return
}

for i := 0; i < len(smeshers.Smeshers); i += 20 {
if err = cc.Cache.Set(context.Background(),
fmt.Sprintf("smeshers-epoch-%d-%d-%d", epochId, 20, i), &storage.SmesherList{
Smeshers: smeshers.Smeshers[i : i+20],
}); err != nil {
log.Warning("failed to cache smeshers: %v", err)
return c.NoContent(http.StatusInternalServerError)
for i := 0; i < len(smeshers.Smeshers); i += 20 {
if err = cc.Cache.Set(context.Background(),
fmt.Sprintf("smeshers-epoch-%d-%d-%d", epochId, 20, i), &storage.SmesherList{
Smeshers: smeshers.Smeshers[i : i+20],
}); err != nil {
log.Warning("failed to cache smeshers: %v", err)
return
}
}
}

return c.JSON(http.StatusOK, smeshers)
log.Info("smeshers by epoch %d refreshed", epochId)
}()

return c.NoContent(http.StatusOK)
}

func Smesher(c echo.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func RefreshRouter(e *echo.Echo) {
e.Use(echoprometheus.NewMiddleware("spacemesh_explorer_stats_api_refresh"))
g := e.Group("/refresh")
g.GET("/epoch/:id", handler.EpochRefresh)
e.GET("/epoch/:id/decentral", handler.EpochDecentralRefresh)
g.GET("/epoch/:id/decentral", handler.EpochDecentralRefresh)
g.GET("/overview", handler.OverviewRefresh)
g.GET("/smeshers/:epoch", handler.SmeshersByEpochRefresh)
g.GET("/smeshers", handler.SmeshersRefresh)
Expand Down
Loading