github.com/diadata-org/diadata@v1.4.593/pkg/model/scrapers.go (about) 1 package models 2 3 import ( 4 "context" 5 "database/sql" 6 "errors" 7 "fmt" 8 ) 9 10 // ScraperConfig is a JSON compatible struct to keep the configuration of a scraper 11 type ScraperConfig interface{} 12 13 // ScraperState is a JSON compatible struct to keep the state of a scraper 14 type ScraperState interface{} 15 16 func (rdb *RelDB) GetScraperState(ctx context.Context, scraperName string, state ScraperState) error { 17 return rdb.postgresClient.QueryRow(ctx, fmt.Sprintf("select state from %s where name=$1", scrapersTable), scraperName).Scan(state) 18 } 19 20 func (rdb *RelDB) SetScraperState(ctx context.Context, scraperName string, state ScraperState) error { 21 _, err := rdb.postgresClient.Exec(ctx, fmt.Sprintf("insert into %s(name, state) values($1, $2) on conflict(name) do update set state=excluded.state", scrapersTable), 22 scraperName, 23 state, 24 ) 25 26 return err 27 } 28 29 func (rdb *RelDB) GetScraperConfig(ctx context.Context, scraperName string, config ScraperConfig) error { 30 return rdb.postgresClient.QueryRow(ctx, fmt.Sprintf("select conf from %s where name=$1", scrapersTable), scraperName).Scan(config) 31 } 32 33 func (rdb *RelDB) SetScraperConfig(ctx context.Context, scraperName string, config ScraperConfig) error { 34 _, err := rdb.postgresClient.Exec(ctx, fmt.Sprintf("insert into %s(name, conf) values($1, $2) on conflict(name) do update set conf=excluded.conf", scrapersTable), 35 scraperName, 36 config, 37 ) 38 39 return err 40 } 41 42 func (rdb *RelDB) SetScraperIndex(exchange string, scraperType string, indexType string, index int64) error { 43 query := fmt.Sprintf( 44 `INSERT INTO %s (scraper,index_type,index_value) VALUES ($1,$2,$3) 45 ON CONFLICT (scraper,index_type) 46 DO UPDATE SET index_value=EXCLUDED.index_value`, 47 scraperCronjobStateTable, 48 ) 49 _, err := rdb.postgresClient.Exec( 50 context.Background(), 51 query, 52 exchange+"_"+scraperType, 53 indexType, 54 index, 55 ) 56 return err 57 } 58 59 func (rdb *RelDB) GetScraperIndex(exchange string, scraperType string) (string, int64, error) { 60 query := fmt.Sprintf( 61 "SELECT index_type,index_value FROM %s WHERE scraper=$1", 62 scraperCronjobStateTable, 63 ) 64 65 var indexType sql.NullString 66 var index sql.NullFloat64 67 err := rdb.postgresClient.QueryRow(context.Background(), query, exchange+"_"+scraperType).Scan(&indexType, &index) 68 if err != nil { 69 return "", 0, err 70 } 71 if index.Valid { 72 return indexType.String, int64(index.Float64), nil 73 } 74 return "", 0, errors.New("blocknumber not valid") 75 }