-
Notifications
You must be signed in to change notification settings - Fork 523
/
sfn_state_machine.go
163 lines (140 loc) 路 5.49 KB
/
sfn_state_machine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package aws
import (
"github.com/infracost/infracost/internal/resources"
"github.com/infracost/infracost/internal/schema"
"github.com/shopspring/decimal"
"github.com/infracost/infracost/internal/usage"
"strings"
)
type SFnStateMachine struct {
Address string
Region string
Type string
MonthlyRequests *int64 `infracost_usage:"monthly_requests"`
WorkflowDurationMs *int64 `infracost_usage:"workflow_duration_ms"`
MemoryMB *int64 `infracost_usage:"memory_mb"`
MonthlyTransitions *int64 `infracost_usage:"monthly_transitions"`
}
func (r *SFnStateMachine) CoreType() string {
return "SFnStateMachine"
}
func (r *SFnStateMachine) UsageSchema() []*schema.UsageItem {
return []*schema.UsageItem{
{Key: "monthly_requests", ValueType: schema.Int64, DefaultValue: 0},
{Key: "workflow_duration_ms", ValueType: schema.Int64, DefaultValue: 0},
{Key: "memory_mb", ValueType: schema.Int64, DefaultValue: 0},
{Key: "monthly_transitions", ValueType: schema.Int64, DefaultValue: 0},
}
}
func (r *SFnStateMachine) PopulateUsage(u *schema.UsageData) {
resources.PopulateArgsWithUsage(r, u)
}
func (r *SFnStateMachine) BuildResource() *schema.Resource {
costComponents := make([]*schema.CostComponent, 0)
tier := r.Type
if tier == "" {
tier = "STANDARD"
}
if strings.ToLower(tier) == "standard" {
var transitions *decimal.Decimal
if r.MonthlyTransitions != nil {
transitions = decimalPtr(decimal.NewFromInt(*r.MonthlyTransitions))
}
costComponents = append(costComponents, r.transistionsCostComponent(transitions))
}
if strings.ToLower(tier) == "express" {
var requests *decimal.Decimal
if r.MonthlyRequests != nil {
requests = decimalPtr(decimal.NewFromInt(*r.MonthlyRequests))
}
costComponents = append(costComponents, r.requestsCostComponent(requests))
if r.WorkflowDurationMs != nil && r.MonthlyRequests != nil && r.MemoryMB != nil {
memoryRequest := decimalPtr(decimal.NewFromInt(*r.MemoryMB))
duration := decimalPtr(decimal.NewFromInt(*r.WorkflowDurationMs))
gbSeconds := decimalPtr(r.calculateGBSeconds(*memoryRequest, *duration, *requests))
pushLimits := []int{3600000, 14400000}
pushQuantities := usage.CalculateTierBuckets(*gbSeconds, pushLimits)
costComponents = append(costComponents, r.durationCostComponent("Duration (first 1K)", "0", &pushQuantities[0]))
if pushQuantities[1].GreaterThan(decimal.Zero) {
costComponents = append(costComponents, r.durationCostComponent("Duration (next 4K)", "3600000", &pushQuantities[1]))
}
if pushQuantities[2].GreaterThan(decimal.Zero) {
costComponents = append(costComponents, r.durationCostComponent("Duration (over 5K)", "18000000", &pushQuantities[2]))
}
} else {
var unknown *decimal.Decimal
costComponents = append(costComponents, r.durationCostComponent("Duration (first 1K)", "0", unknown))
}
}
return &schema.Resource{
Name: r.Address,
CostComponents: costComponents,
UsageSchema: r.UsageSchema(),
}
}
func (r *SFnStateMachine) transistionsCostComponent(quantity *decimal.Decimal) *schema.CostComponent {
return &schema.CostComponent{
Name: "Transitions",
Unit: "1K transitions",
UnitMultiplier: decimal.NewFromInt(1000),
MonthlyQuantity: quantity,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AmazonStates"),
ProductFamily: strPtr("AWS Step Functions"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "usagetype", ValueRegex: strPtr("/StateTransition/")},
},
},
UsageBased: true,
}
}
func (r *SFnStateMachine) requestsCostComponent(quantity *decimal.Decimal) *schema.CostComponent {
return &schema.CostComponent{
Name: "Requests",
Unit: "1M requests",
UnitMultiplier: decimal.NewFromInt(1000000),
MonthlyQuantity: quantity,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AmazonStates"),
ProductFamily: strPtr("AWS Step Functions"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "usagetype", ValueRegex: strPtr("/StepFunctions-Request/")},
},
},
UsageBased: true,
}
}
func (r *SFnStateMachine) durationCostComponent(name string, startUsageAmt string, gbSeconds *decimal.Decimal) *schema.CostComponent {
return &schema.CostComponent{
Name: name,
Unit: "GB-hours",
UnitMultiplier: decimal.NewFromInt(3600),
MonthlyQuantity: gbSeconds,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AmazonStates"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "usagetype", ValueRegex: strPtr("/StepFunctions-GB-Second/")},
},
},
PriceFilter: &schema.PriceFilter{
StartUsageAmount: strPtr(startUsageAmt),
},
UsageBased: true,
}
}
func (r *SFnStateMachine) calculateGBSeconds(memorySize decimal.Decimal, averageRequestDuration decimal.Decimal, monthlyRequests decimal.Decimal) decimal.Decimal {
if memorySize.LessThan(decimal.NewFromInt(64)) {
memorySize = decimal.NewFromInt(64)
}
roundedMemory := memorySize.Div(decimal.NewFromInt(64)).Ceil().Mul(decimal.NewFromInt(64))
roundedDuration := averageRequestDuration.Div(decimal.NewFromInt(100)).Ceil().Mul(decimal.NewFromInt(100))
durationSeconds := monthlyRequests.Mul(roundedDuration).Mul(decimal.NewFromFloat(0.001))
gbSeconds := durationSeconds.Mul(roundedMemory).Div(decimal.NewFromInt(1024))
return gbSeconds
}