parallelize the calls
This commit is contained in:
67
main.go
67
main.go
@ -10,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -93,29 +94,53 @@ func getCategories() ([]Transaction, error) {
|
|||||||
return []Transaction{}, fmt.Errorf("error reading csv: %w", err)
|
return []Transaction{}, fmt.Errorf("error reading csv: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
waitGroup := sync.WaitGroup{}
|
||||||
|
transactionChan := make(chan Transaction)
|
||||||
|
|
||||||
var finalTransactions []Transaction
|
var finalTransactions []Transaction
|
||||||
count := 0
|
count := 0
|
||||||
for _, line := range transactions {
|
fmt.Printf("Transaction Count: %v\n", len(transactions))
|
||||||
//if count > 100 {
|
for _, transaction := range transactions {
|
||||||
// break
|
if count != 0 && count%100 == 0 {
|
||||||
//}
|
fmt.Printf("At %v waiting...\n", count)
|
||||||
fmt.Println(line)
|
time.Sleep(60 * time.Second)
|
||||||
transaction, err := parseTransaction(line.TransactionName)
|
fmt.Println("starting... next 100")
|
||||||
if err != nil {
|
|
||||||
// continue if parsing error
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
}
|
||||||
//fmt.Printf("%+v\n", transaction)
|
waitGroup.Add(1)
|
||||||
combinedTransaction := Transaction{
|
go func(line Transaction, wg *sync.WaitGroup, transactionChan chan Transaction, count int) {
|
||||||
Category: transaction.Category,
|
defer wg.Done()
|
||||||
SubCategory: transaction.SubCategory,
|
fmt.Println(line)
|
||||||
Amount: line.Amount,
|
transaction, err := parseTransaction(line.TransactionName)
|
||||||
TransactionName: line.TransactionName,
|
if err != nil {
|
||||||
IsoDate: line.IsoDate,
|
// continue if parsing error
|
||||||
}
|
fmt.Println(err)
|
||||||
finalTransactions = append(finalTransactions, combinedTransaction)
|
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++
|
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)
|
jsonTransactions, err := json.Marshal(finalTransactions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -201,6 +226,12 @@ Expected output format:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
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)
|
return Transaction{}, fmt.Errorf("ChatCompletion request error: %w", err)
|
||||||
}
|
}
|
||||||
fmt.Println(resp.Choices[0].Message.Content)
|
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