MCPcopy
hub / github.com/ananthakumaran/paisa / SyncCommodities

Function SyncCommodities

internal/model/model.go:70–103  ·  view source on GitHub ↗
(db *gorm.DB)

Source from the content-addressed store, hash-verified

68}
69
70func SyncCommodities(db *gorm.DB) error {
71 AutoMigrate(db)
72 log.Info("Fetching commodities price history")
73 commodities := lo.Shuffle(commodity.All())
74
75 var errors []error
76 for _, commodity := range commodities {
77 name := commodity.Name
78 log.Info("Fetching commodity ", name)
79 code := commodity.Price.Code
80 var prices []*price.Price
81 var err error
82
83 provider := scraper.GetProviderByCode(commodity.Price.Provider)
84 prices, err = provider.GetPrices(code, name)
85
86 if err != nil {
87 log.Error(err)
88 errors = append(errors, fmt.Errorf("Failed to fetch price for %s: %w", name, err))
89 continue
90 }
91
92 price.UpsertAllByTypeNameAndID(db, commodity.Type, name, code, prices)
93 }
94
95 if len(errors) > 0 {
96 var message string
97 for _, error := range errors {
98 message += error.Error() + "\n"
99 }
100 return fmt.Errorf("%s", strings.Trim(message, "\n"))
101 }
102 return nil
103}
104
105func SyncCII(db *gorm.DB) error {
106 AutoMigrate(db)

Callers 2

SyncFunction · 0.92
update.goFile · 0.92

Calls 7

AllFunction · 0.92
GetProviderByCodeFunction · 0.92
UpsertAllByTypeNameAndIDFunction · 0.92
AutoMigrateFunction · 0.85
InfoMethod · 0.80
ErrorMethod · 0.80
GetPricesMethod · 0.65

Tested by

no test coverage detected