Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lint): corrected all the linting issues #6

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 38 additions & 29 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -15,7 +16,9 @@ import (
)

const (
YearsToFetch = 5 // Number of years to fetch data for
YearsToFetch = 5 // Number of years to fetch data for
PercentageMultiplier = 100
NumberOfDaysInYear = 365
)

// ETF represents an ETF and its dividend cash amounts by year
Expand All @@ -30,8 +33,8 @@ type ETF struct {
func (e *ETF) ShowDividendsPerYear(startYear, totalYears int) []string {
formatted := make([]string, totalYears)

for i := 0; i < totalYears; i++ {
year := fmt.Sprintf("%d", startYear-i)
for i := range make([]struct{}, totalYears) {
year := strconv.Itoa(startYear - i)
if value, exists := e.AmountDividendsPerYear[year]; exists {
formatted[i] = fmt.Sprintf("$%.2f", value)
} else {
Expand All @@ -49,8 +52,8 @@ func (e *ETF) AverageDividends(startYear, totalYears int) float64 {

var sum float64
var count int
for i := 0; i < totalYears; i++ {
year := fmt.Sprintf("%d", startYear-i)
for i := range make([]struct{}, totalYears) {
year := strconv.Itoa(startYear - i)
if value, exists := e.AmountDividendsPerYear[year]; exists {
sum += value
count++
Expand All @@ -67,8 +70,8 @@ func (e *ETF) AverageDividends(startYear, totalYears int) float64 {
func (e *ETF) ShowClosingPricesPerYear(startYear, totalYears int) []string {
formatted := make([]string, totalYears)

for i := 0; i < totalYears; i++ {
year := fmt.Sprintf("%d", startYear-i)
for i := range make([]struct{}, totalYears) {
year := strconv.Itoa(startYear - i)
if value, exists := e.AverageClosingPricePerYear[year]; exists {
formatted[i] = fmt.Sprintf("$%.2f", value)
} else {
Expand All @@ -86,8 +89,8 @@ func (e *ETF) AverageClosingPrices(startYear, totalYears int) float64 {

var sum float64
var count int
for i := 0; i < totalYears; i++ {
year := fmt.Sprintf("%d", startYear-i)
for i := range make([]struct{}, totalYears) {
year := strconv.Itoa(startYear - i)
if value, exists := e.AverageClosingPricePerYear[year]; exists {
sum += value
count++
Expand All @@ -105,11 +108,11 @@ func (e *ETF) ShowDividendYieldPerYear(startYear, totalYears int) []string {
formatted := make([]string, totalYears)
e.DividendYieldPerYear = make(map[string]float64)

for i := 0; i < totalYears; i++ {
year := fmt.Sprintf("%d", startYear-i)
if dividend, exists := e.AmountDividendsPerYear[year]; exists {
if closingPrice, exists := e.AverageClosingPricePerYear[year]; exists && closingPrice != 0 {
yield := (dividend / closingPrice) * 100
for i := range make([]struct{}, totalYears) {
year := strconv.Itoa(startYear - i)
if dividend, dividendExists := e.AmountDividendsPerYear[year]; dividendExists {
if closingPrice, priceExists := e.AverageClosingPricePerYear[year]; priceExists && closingPrice != 0 {
yield := (dividend / closingPrice) * PercentageMultiplier
e.DividendYieldPerYear[year] = yield
formatted[i] = fmt.Sprintf("%.2f%%", yield)
} else {
Expand All @@ -131,8 +134,8 @@ func (e *ETF) AverageDividendYield(startYear int, totalYears int) float64 {

var sum float64
var count int
for i := 0; i < totalYears; i++ {
year := fmt.Sprintf("%d", startYear-i)
for i := range make([]struct{}, totalYears) {
year := strconv.Itoa(startYear - i)
if yield, exists := e.DividendYieldPerYear[year]; exists {
sum += yield
count++
Expand Down Expand Up @@ -189,9 +192,8 @@ func crawlingDividendsPerYear(etf string) (map[string]float64, error) {
})

url := fmt.Sprintf("https://dividendhistory.org/payout/%s/", etf)
err := c.Visit(url)
if err != nil {
return nil, err
if err := c.Visit(url); err != nil {
return nil, fmt.Errorf("failed to visit URL: %w", err)
}

return yearlyTotals, nil
Expand All @@ -204,13 +206,20 @@ func fetchAverageClosingPricesPerYear(etf string) (map[string]float64, error) {
fromDate := fmt.Sprintf("%d-01-01", currentYear-YearsToFetch)
toDate := fmt.Sprintf("%d-12-31", currentYear)

url := fmt.Sprintf("https://api.nasdaq.com/api/quote/%s/historical?assetclass=etf&fromdate=%s&todate=%s&limit=%d&offset=0", etf, fromDate, toDate, YearsToFetch*365)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0")
url := fmt.Sprintf(
"https://api.nasdaq.com/api/quote/%s/historical?assetclass=etf&fromdate=%s&todate=%s&limit=%d&offset=0",
etf, fromDate, toDate, YearsToFetch*NumberOfDaysInYear,
)
ctx := context.Background()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
req.Header.Set(
"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "+
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",
)

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to fetch data: %w", err)
}
defer resp.Body.Close()

Expand All @@ -225,16 +234,16 @@ func fetchAverageClosingPricesPerYear(etf string) (map[string]float64, error) {
} `json:"data"`
}

if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
if err = json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

yearlySums := make(map[string]float64)
yearlyCounts := make(map[string]int)

for _, row := range result.Data.TradesTable.Rows {
closePrice, err := strconv.ParseFloat(strings.ReplaceAll(row.Close, "$", ""), 64)
if err == nil {
closePrice, parseErr := strconv.ParseFloat(strings.ReplaceAll(row.Close, "$", ""), 64)
if parseErr == nil {
year := strings.Split(row.Date, "/")[2]
yearlySums[year] += closePrice
yearlyCounts[year]++
Expand Down Expand Up @@ -281,8 +290,8 @@ func main() {
totalYears := YearsToFetch
currentYear := time.Now().Year()
headers := []string{"ETF"}
for i := 0; i < totalYears; i++ {
headers = append(headers, fmt.Sprintf("%d", currentYear-i))
for i := range make([]struct{}, totalYears) {
headers = append(headers, strconv.Itoa(currentYear-i))
}
headers = append(headers, "Averages")
table.SetHeader(headers)
Expand Down
23 changes: 11 additions & 12 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/olekukonko/tablewriter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -38,56 +37,56 @@ func (suite *ETFTestSuite) TestShowDividendsPerYear() {
suite.Run("should return formatted dividends per year", func() {
expected := []string{"$10.00", "$15.00", "$20.00", "-", "-"}
result := suite.etf.ShowDividendsPerYear(2023, 5)
assert.Equal(suite.T(), expected, result)
suite.Equal(expected, result)
})
}

func (suite *ETFTestSuite) TestAverageDividends() {
suite.Run("should calculate average dividends", func() {
expected := 15.0
result := suite.etf.AverageDividends(2023, 5)
assert.Equal(suite.T(), expected, result)
suite.InEpsilon(expected, result, 0.01)
})
}

func (suite *ETFTestSuite) TestShowClosingPricesPerYear() {
suite.Run("should return formatted closing prices per year", func() {
expected := []string{"$100.00", "$150.00", "$200.00", "-", "-"}
result := suite.etf.ShowClosingPricesPerYear(2023, 5)
assert.Equal(suite.T(), expected, result)
suite.Equal(expected, result)
})
}

func (suite *ETFTestSuite) TestAverageClosingPrices() {
suite.Run("should calculate average closing prices", func() {
expected := 150.0
result := suite.etf.AverageClosingPrices(2023, 5)
assert.Equal(suite.T(), expected, result)
suite.InEpsilon(expected, result, 0.01)
})
}

func (suite *ETFTestSuite) TestShowDividendYieldPerYear() {
suite.Run("should return formatted dividend yields per year", func() {
expected := []string{"10.00%", "10.00%", "10.00%", "-", "-"}
result := suite.etf.ShowDividendYieldPerYear(2023, 5)
assert.Equal(suite.T(), expected, result)
suite.Equal(expected, result)
})
}

func (suite *ETFTestSuite) TestAverageDividendYield() {
suite.Run("should calculate average dividend yield", func() {
expected := 15.0
result := suite.etf.AverageDividendYield(2023, 5)
assert.Equal(suite.T(), expected, result)
suite.InEpsilon(expected, result, 0.01)
})
}

func (suite *ETFTestSuite) TestProcessETF() {
suite.Run("should process ETF data", func() {
etf := processETF("TestETF")
assert.Equal(suite.T(), "TestETF", etf.Name)
assert.NotEmpty(suite.T(), etf.AmountDividendsPerYear)
assert.NotEmpty(suite.T(), etf.AverageClosingPricePerYear)
etf := processETF("SPY")
suite.Equal("SPY", etf.Name)
suite.NotEmpty(etf.AmountDividendsPerYear)
suite.NotEmpty(etf.AverageClosingPricePerYear)
})
}

Expand All @@ -100,7 +99,7 @@ func (suite *ETFTestSuite) TestGetColors() {
{tablewriter.FgGreenColor},
}
result := getColors(row)
assert.Equal(suite.T(), expected, result)
suite.Equal(expected, result)
})
}

Expand Down
Loading