parallelize the calls
This commit is contained in:
67
main.go
67
main.go
@ -10,6 +10,7 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -93,29 +94,53 @@ func getCategories() ([]Transaction, error) {
|
||||
return []Transaction{}, fmt.Errorf("error reading csv: %w", err)
|
||||
}
|
||||
|
||||
waitGroup := sync.WaitGroup{}
|
||||
transactionChan := make(chan Transaction)
|
||||
|
||||
var finalTransactions []Transaction
|
||||
count := 0
|
||||
for _, line := range transactions {
|
||||
//if count > 100 {
|
||||
// break
|
||||
//}
|
||||
fmt.Println(line)
|
||||
transaction, err := parseTransaction(line.TransactionName)
|
||||
if err != nil {
|
||||
// continue if parsing error
|
||||
fmt.Println(err)
|
||||
fmt.Printf("Transaction Count: %v\n", len(transactions))
|
||||
for _, transaction := range transactions {
|
||||
if count != 0 && count%100 == 0 {
|
||||
fmt.Printf("At %v waiting...\n", count)
|
||||
time.Sleep(60 * time.Second)
|
||||
fmt.Println("starting... next 100")
|
||||
}
|
||||
//fmt.Printf("%+v\n", transaction)
|
||||
combinedTransaction := Transaction{
|
||||
Category: transaction.Category,
|
||||
SubCategory: transaction.SubCategory,
|
||||
Amount: line.Amount,
|
||||
TransactionName: line.TransactionName,
|
||||
IsoDate: line.IsoDate,
|
||||
}
|
||||
finalTransactions = append(finalTransactions, combinedTransaction)
|
||||
waitGroup.Add(1)
|
||||
go func(line Transaction, wg *sync.WaitGroup, transactionChan chan Transaction, count int) {
|
||||
defer wg.Done()
|
||||
fmt.Println(line)
|
||||
transaction, err := parseTransaction(line.TransactionName)
|
||||
if err != nil {
|
||||
// continue if parsing error
|
||||
fmt.Println(err)
|
||||
fmt.Println("done with error")
|
||||
return
|
||||
}
|
||||
fmt.Println("done parsing")
|
||||
//fmt.Printf("%+v\n", transaction)
|
||||
combinedTransaction := Transaction{
|
||||
Category: transaction.Category,
|
||||
SubCategory: transaction.SubCategory,
|
||||
Amount: line.Amount,
|
||||
TransactionName: line.TransactionName,
|
||||
IsoDate: line.IsoDate,
|
||||
}
|
||||
transactionChan <- combinedTransaction
|
||||
fmt.Println("done end of function")
|
||||
}(transaction, &waitGroup, transactionChan, count)
|
||||
count++
|
||||
}
|
||||
// Start a goroutine to receive values from the transactionChan
|
||||
go func() {
|
||||
for combinedTransaction := range transactionChan {
|
||||
finalTransactions = append(finalTransactions, combinedTransaction)
|
||||
}
|
||||
}()
|
||||
fmt.Println(count)
|
||||
fmt.Println("waiting...")
|
||||
waitGroup.Wait()
|
||||
close(transactionChan)
|
||||
|
||||
jsonTransactions, err := json.Marshal(finalTransactions)
|
||||
if err != nil {
|
||||
@ -201,6 +226,12 @@ Expected output format:
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
// if 429, wait and try again
|
||||
if strings.Contains(err.Error(), "429") {
|
||||
fmt.Println("429 error, waiting 5 seconds...")
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
return Transaction{}, fmt.Errorf("ChatCompletion request error: %w", err)
|
||||
}
|
||||
fmt.Println(resp.Choices[0].Message.Content)
|
||||
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user