Skip to content

Commit

Permalink
Move fx to main package
Browse files Browse the repository at this point in the history
  • Loading branch information
thinktwice13 committed Jun 10, 2022
1 parent d6e06ae commit 92442d2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 52 deletions.
45 changes: 44 additions & 1 deletion fx/hrk.go → fx.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fx
package main

import (
"encoding/json"
Expand All @@ -9,9 +9,52 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"time"
)

type Rates map[int]map[string]float64

func (r Rates) Rate(ccy string, yr int) float64 {
return r[yr][ccy] // TODO Check errors
}

func NewFxRates(currencies []string, years []int) (Rates, error) {
t := time.Now()
r := make(Rates, len(years))

var m sync.Mutex
var wg sync.WaitGroup
wg.Add(len(years))
for _, y := range years {
go func(r Rates, y int, m *sync.Mutex, wg *sync.WaitGroup) {
defer wg.Done()
rates, err := grabHRKRates(y, currencies, 3)
if err != nil {
fmt.Println(err)
fmt.Printf("cannot get rates for year %d", y)
return
}
m.Lock()
defer m.Unlock()
r[y] = rates
}(r, y, &m, &wg)
}
wg.Wait()

fmt.Println("Rates fetched in:", time.Since(t))
return r, nil
}

func (r Rates) Print() {
fmt.Println("Fx")
for y := range r {
for c := range r[y] {
fmt.Printf("%d %s %g\n", y, c, r[y][c])
}
}
}

// TODO Other currencies https://ec.europa.eu/info/funding-tenders/procedures-guidelines-tenders/information-contractors-and-beneficiaries/exchange-rate-inforeuro_en
type rateResponse struct {
Currency string `json:"Valuta"`
Expand Down
49 changes: 0 additions & 49 deletions fx/main.go

This file was deleted.

3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"ibkr-report/fx"
"log"
"os"
"time"
Expand All @@ -19,7 +18,7 @@ func main() {
}

// Fetch currency conversion rates per year
rates, err := fx.New(currencies, years)
rates, err := NewFxRates(currencies, years)
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 92442d2

Please sign in to comment.