Skip to content

Commit f78350b

Browse files
committed
Add rebalance threshold to benchmarks
1 parent b323149 commit f78350b

17 files changed

+162
-69
lines changed

.vscode/launch.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"mode": "auto",
99
"program": "${fileDirname}"
1010
},
11+
{
12+
"name": "Launch API",
13+
"type": "go",
14+
"request": "launch",
15+
"mode": "auto",
16+
"program": "${workspaceFolder}/cmd/api/main.go"
17+
},
1118
{
1219
"name": "Launch Tests",
1320
"type": "go",

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ migration:
1111
@migrate create -ext sql -dir migrations $(filter-out $@,$(MAKECMDGOALS))
1212

1313
migrate-up:
14-
@go run cmd/migrate/main.go up
14+
@go run cmd/migrate/main.go up
1515

1616
migrate-down:
1717
@go run cmd/migrate/main.go down

api/constants/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ const (
1313

1414
TIMEOUT_MEDIUM = 5 * time.Second
1515
TIMEOUT_LONG = 10 * time.Second
16+
17+
BENCHMARK_REBALANCE_PCT_DEFAULT = 10
1618
)

api/services/benchmark/handler_createbenchmark.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ func (h *BenchmarkHandlerImpl) CreateBenchmark(c fiber.Ctx) error {
3131
benchmark, err := h.benchmarkStore.CreateBenchmark(
3232
c.Context(),
3333
&types.Benchmark{
34-
Name: benchmarkPayload.Name,
35-
Description: benchmarkPayload.Description,
36-
Asset_allocation: benchmarkPayload.Asset_allocation,
37-
Std_dev_pct: benchmarkPayload.Std_dev_pct,
38-
Real_return_pct: benchmarkPayload.Real_return_pct,
39-
Drawdown_yrs: benchmarkPayload.Drawdown_yrs,
40-
Is_deprecated: benchmarkPayload.Is_deprecated,
41-
User_id: userPayload.User_id,
34+
Name: benchmarkPayload.Name,
35+
Description: benchmarkPayload.Description,
36+
Asset_allocation: benchmarkPayload.Asset_allocation,
37+
Std_dev_pct: benchmarkPayload.Std_dev_pct,
38+
Real_return_pct: benchmarkPayload.Real_return_pct,
39+
Drawdown_yrs: benchmarkPayload.Drawdown_yrs,
40+
Rebalance_threshold_pct: benchmarkPayload.Rebalance_threshold_pct,
41+
Is_deprecated: benchmarkPayload.Is_deprecated,
42+
User_id: userPayload.User_id,
4243
},
4344
)
4445

api/services/benchmark/schema_createbenchmark.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
)
99

1010
type CreateBenchmarkPayload struct {
11-
Name string `json:"name"`
12-
Description string `json:"description"`
13-
Asset_allocation []types.AssetAllocationPct `json:"asset_allocation"`
14-
Std_dev_pct float32 `json:"std_dev_pct"`
15-
Real_return_pct float32 `json:"real_return_pct"`
16-
Drawdown_yrs int `json:"drawdown_yrs"`
17-
Is_deprecated bool `json:"is_deprecated"`
11+
Name string `json:"name"`
12+
Description string `json:"description"`
13+
Asset_allocation []types.AssetAllocationPct `json:"asset_allocation"`
14+
Std_dev_pct float32 `json:"std_dev_pct"`
15+
Real_return_pct float32 `json:"real_return_pct"`
16+
Drawdown_yrs int `json:"drawdown_yrs"`
17+
Rebalance_threshold_pct int `json:"rebalance_threshold_pct"`
18+
Is_deprecated bool `json:"is_deprecated"`
1819
}
1920

2021
func (p CreateBenchmarkPayload) Validate() error {
@@ -42,6 +43,7 @@ func (p CreateBenchmarkPayload) Validate() error {
4243
validation.Field(&p.Std_dev_pct, validation.Min(float32(0)), validation.Max(float32(100))),
4344
validation.Field(&p.Real_return_pct, validation.Min(float32(0)), validation.Max(float32(100))),
4445
validation.Field(&p.Drawdown_yrs, validation.Min(0), validation.Max(50)),
46+
validation.Field(&p.Rebalance_threshold_pct, validation.Min(1), validation.Max(100)),
4547
validation.Field(&p.Is_deprecated),
4648
)
4749
}

api/services/benchmark/schema_updatebenchmark.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ func (p UpdateBenchmarkById) Validate() error {
1818
}
1919

2020
type UpdateBenchmarkPayload struct {
21-
Name string `json:"name"`
22-
Description string `json:"description"`
23-
Asset_allocation []types.AssetAllocationPct `json:"asset_allocation"`
24-
Std_dev_pct float32 `json:"std_dev_pct"`
25-
Real_return_pct float32 `json:"real_return_pct"`
26-
Drawdown_yrs int `json:"drawdown_yrs"`
27-
Is_deprecated bool `json:"is_deprecated"`
21+
Name string `json:"name"`
22+
Description string `json:"description"`
23+
Asset_allocation []types.AssetAllocationPct `json:"asset_allocation"`
24+
Std_dev_pct float32 `json:"std_dev_pct"`
25+
Real_return_pct float32 `json:"real_return_pct"`
26+
Drawdown_yrs int `json:"drawdown_yrs"`
27+
Rebalance_threshold_pct int `json:"rebalance_threshold_pct"`
28+
Is_deprecated bool `json:"is_deprecated"`
2829
}
2930

3031
func (p UpdateBenchmarkPayload) Validate() error {
@@ -52,6 +53,7 @@ func (p UpdateBenchmarkPayload) Validate() error {
5253
validation.Field(&p.Std_dev_pct, validation.Min(float32(0)), validation.Max(float32(100))),
5354
validation.Field(&p.Real_return_pct, validation.Min(float32(0)), validation.Max(float32(100))),
5455
validation.Field(&p.Drawdown_yrs, validation.Min(0), validation.Max(50)),
56+
validation.Field(&p.Rebalance_threshold_pct, validation.Min(1), validation.Max(100)),
5557
validation.Field(&p.Is_deprecated),
5658
)
5759
}

api/services/benchmark/store_createbenchmark.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ func (s *PostgresBenchmarkStore) CreateBenchmark(ctx context.Context, b *types.B
1616
return types.Benchmark{}, errors.New("service error: benchmark struct cannot be nil, valid benchmark data is required")
1717
}
1818

19+
var rebalance_threshold_pct int = b.Rebalance_threshold_pct
20+
if b.Rebalance_threshold_pct == 0 {
21+
rebalance_threshold_pct = constants.BENCHMARK_REBALANCE_PCT_DEFAULT
22+
}
23+
1924
row := s.db.QueryRow(
2025
c,
2126
`INSERT INTO benchmarks
22-
(name, description, asset_allocation, std_dev_pct, real_return_pct, drawdown_yrs, is_deprecated, user_id)
23-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
27+
(name, description, asset_allocation, std_dev_pct, real_return_pct, drawdown_yrs, is_deprecated, user_id, rebalance_threshold_pct)
28+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
2429
RETURNING *`,
25-
b.Name, b.Description, b.Asset_allocation, b.Std_dev_pct, b.Real_return_pct, b.Drawdown_yrs, b.Is_deprecated, b.User_id,
30+
b.Name, b.Description, b.Asset_allocation, b.Std_dev_pct, b.Real_return_pct, b.Drawdown_yrs, b.Is_deprecated, b.User_id, rebalance_threshold_pct,
2631
)
2732

2833
benchmark, err := s.parseRowIntoBenchmark(row)

api/services/benchmark/store_getbenchmarkbyid.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (s *PostgresBenchmarkStore) parseRowIntoBenchmark(row pgx.Row) (types.Bench
4949
&b.User_id,
5050
&b.Created_at,
5151
&b.Updated_at,
52+
&b.Rebalance_threshold_pct,
5253
)
5354

5455
if err != nil {

api/services/benchmark/store_getbenchmarks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func (s *PostgresBenchmarkStore) parseRowsIntoBenchmarks(rows pgx.Rows) ([]types
103103
&b.User_id,
104104
&b.Created_at,
105105
&b.Updated_at,
106+
&b.Rebalance_threshold_pct,
106107
&total_items,
107108
)
108109

api/services/benchmark/store_updatebenchmark.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (s *PostgresBenchmarkStore) UpdateBenchmark(ctx context.Context, b *types.B
2929
real_return_pct = $5,
3030
drawdown_yrs = $6,
3131
is_deprecated = $7,
32+
rebalance_threshold_pct = $10,
3233
updated_at = now()
3334
where
3435
benchmark_id = $8
@@ -45,6 +46,7 @@ func (s *PostgresBenchmarkStore) UpdateBenchmark(ctx context.Context, b *types.B
4546
b.Is_deprecated,
4647
b.Benchmark_id,
4748
b.User_id,
49+
b.Rebalance_threshold_pct,
4850
)
4951

5052
benchmark, err := s.parseRowIntoBenchmark(row)

0 commit comments

Comments
 (0)