Skip to content

Commit

Permalink
todo: update environment variable to be exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Apr 23, 2024
1 parent c1a1dab commit 4dca0ed
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions node/pkg/por/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ func New(ctx context.Context) (*App, error) {
return nil, err
}

initValue := 0.0
initTime := time.Time{}

loaded, err := LoadSubmission(ctx, adapter.Name)
if err == nil {
initTime = loaded.Time
initValue = loaded.Value
}

return &App{
Name: adapter.Name,
Definition: definition,
Expand All @@ -122,8 +131,8 @@ func New(ctx context.Context) (*App, error) {
KlaytnHelper: chainHelper,
ContractAddress: aggregator.Address,

LastSubmissionValue: 0,
LastSubmissionTime: time.Time{},
LastSubmissionValue: initValue,
LastSubmissionTime: initTime,
}, nil
}

Expand All @@ -135,29 +144,30 @@ func (a *App) Run(ctx context.Context) error {
ticker.Stop()
return nil
case <-ticker.C:
value, err := fetcher.FetchSingle(ctx, a.Definition)
err := a.Execute(ctx)
if err != nil {
log.Error().Err(err).Msg("error in fetch")
log.Error().Err(err).Msg("error in execute")
}
}
}
}

if a.LastSubmissionTime.IsZero() && a.LastSubmissionValue == 0 {
loaded, err := LoadSubmission(ctx, a.Name)
if err == nil {
a.LastSubmissionTime = loaded.Time
a.LastSubmissionValue = loaded.Value
}
}
func (a *App) Execute(ctx context.Context) error {
value, err := fetcher.FetchSingle(ctx, a.Definition)
if err != nil {
log.Error().Err(err).Msg("error in fetch")
}

now := time.Now()
if a.ShouldReport(value, now) {
err := a.report(ctx, now, value)
if err != nil {
log.Error().Err(err).Msg("failed to report")
continue
}
}
now := time.Now()
if a.ShouldReport(value, now) {
err := a.report(ctx, now, value)
if err != nil {
log.Error().Err(err).Msg("failed to report")
return err
}
}

return nil
}

func (a *App) ShouldReport(value float64, fetchedTime time.Time) bool {
Expand Down

0 comments on commit 4dca0ed

Please sign in to comment.