2019-08-06 21:57:08 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package logs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/chunker"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
|
|
|
)
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
var (
|
|
|
|
ErrNoLogs = errors.New("no logs available for transforming")
|
|
|
|
ErrNoTransformers = errors.New("no event transformers configured in the log delegator")
|
2019-08-14 22:29:04 +00:00
|
|
|
)
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
type ILogDelegator interface {
|
|
|
|
AddTransformer(t transformer.EventTransformer)
|
2019-09-19 01:55:15 +00:00
|
|
|
DelegateLogs() error
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LogDelegator struct {
|
|
|
|
Chunker chunker.Chunker
|
|
|
|
LogRepository datastore.HeaderSyncLogRepository
|
|
|
|
Transformers []transformer.EventTransformer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (delegator *LogDelegator) AddTransformer(t transformer.EventTransformer) {
|
|
|
|
delegator.Transformers = append(delegator.Transformers, t)
|
|
|
|
delegator.Chunker.AddConfig(t.GetConfig())
|
|
|
|
}
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
func (delegator *LogDelegator) DelegateLogs() error {
|
2019-08-06 21:57:08 +00:00
|
|
|
if len(delegator.Transformers) < 1 {
|
2019-09-19 01:55:15 +00:00
|
|
|
return ErrNoTransformers
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
persistedLogs, fetchErr := delegator.LogRepository.GetUntransformedHeaderSyncLogs()
|
|
|
|
if fetchErr != nil {
|
|
|
|
logrus.Errorf("error loading logs from db: %s", fetchErr.Error())
|
2019-09-19 01:55:15 +00:00
|
|
|
return fetchErr
|
2019-08-12 17:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(persistedLogs) < 1 {
|
2019-09-19 01:55:15 +00:00
|
|
|
return ErrNoLogs
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
transformErr := delegator.delegateLogs(persistedLogs)
|
|
|
|
if transformErr != nil {
|
|
|
|
logrus.Errorf("error transforming logs: %s", transformErr)
|
2019-09-19 01:55:15 +00:00
|
|
|
return transformErr
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
return nil
|
2019-08-06 21:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (delegator *LogDelegator) delegateLogs(logs []core.HeaderSyncLog) error {
|
|
|
|
chunkedLogs := delegator.Chunker.ChunkLogs(logs)
|
|
|
|
for _, t := range delegator.Transformers {
|
|
|
|
transformerName := t.GetConfig().TransformerName
|
|
|
|
logChunk := chunkedLogs[transformerName]
|
|
|
|
err := t.Execute(logChunk)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("%v transformer failed to execute in watcher: %v", transformerName, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|