Skip to content

Commit 5bca1b2

Browse files
committed
Move defaults to a separate file
1 parent 5bbe5b9 commit 5bca1b2

File tree

2 files changed

+87
-81
lines changed

2 files changed

+87
-81
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2025 NVIDIA CORPORATION
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package api
5+
6+
import "time"
7+
8+
func (up *UsageParams) SetDefaults() {
9+
if up.HalfLifePeriod == nil {
10+
// noop: disabled by default
11+
}
12+
if up.WindowSize == nil {
13+
windowSize := time.Hour * 24 * 7
14+
up.WindowSize = &windowSize
15+
}
16+
if up.WindowType == nil {
17+
windowType := SlidingWindow
18+
up.WindowType = &windowType
19+
}
20+
}
21+
22+
// WindowType defines the type of time window for aggregating usage data
23+
type WindowType string
24+
25+
const (
26+
// TumblingWindow represents non-overlapping, fixed-size time windows
27+
// Example: 1-hour windows at 0-1h, 1-2h, 2-3h
28+
TumblingWindow WindowType = "tumbling"
29+
30+
// SlidingWindow represents overlapping time windows that slide forward
31+
// Example: a 1-hour sliding window will consider the usage of the last 1 hour prior to the current time.
32+
SlidingWindow WindowType = "sliding"
33+
)
34+
35+
// IsValid returns true if the WindowType is a valid value
36+
func (wt WindowType) IsValid() bool {
37+
switch wt {
38+
case TumblingWindow, SlidingWindow:
39+
return true
40+
default:
41+
return false
42+
}
43+
}
44+
45+
// GetDefaultWindowType returns the default window type (sliding)
46+
func GetDefaultWindowType() WindowType {
47+
return SlidingWindow
48+
}
49+
50+
// GetWindowTypeOrDefault returns the window type if set, otherwise returns the default (sliding)
51+
func (up *UsageParams) GetWindowTypeOrDefault() WindowType {
52+
if up.WindowType == nil {
53+
return GetDefaultWindowType()
54+
}
55+
return *up.WindowType
56+
}
57+
58+
func (up *UsageParams) GetExtraDurationParamOrDefault(key string, defaultValue time.Duration) time.Duration {
59+
if up.ExtraParams == nil {
60+
return defaultValue
61+
}
62+
63+
value, exists := up.ExtraParams[key]
64+
if !exists {
65+
return defaultValue
66+
}
67+
68+
duration, err := time.ParseDuration(value)
69+
if err != nil {
70+
return defaultValue
71+
}
72+
73+
return duration
74+
}
75+
76+
func (up *UsageParams) GetExtraStringParamOrDefault(key string, defaultValue string) string {
77+
if up.ExtraParams == nil {
78+
return defaultValue
79+
}
80+
81+
value, exists := up.ExtraParams[key]
82+
if !exists {
83+
return defaultValue
84+
}
85+
86+
return value
87+
}

pkg/scheduler/cache/usagedb/api/interface.go

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -40,84 +40,3 @@ type UsageParams struct {
4040
// ExtraParams are extra parameters for the usage db client, which are client specific.
4141
ExtraParams map[string]string `yaml:"extraParams" json:"extraParams"`
4242
}
43-
44-
func (up *UsageParams) SetDefaults() {
45-
if up.HalfLifePeriod == nil {
46-
// noop: disabled by default
47-
}
48-
if up.WindowSize == nil {
49-
windowSize := time.Hour * 24 * 7
50-
up.WindowSize = &windowSize
51-
}
52-
if up.WindowType == nil {
53-
windowType := SlidingWindow
54-
up.WindowType = &windowType
55-
}
56-
}
57-
58-
// WindowType defines the type of time window for aggregating usage data
59-
type WindowType string
60-
61-
const (
62-
// TumblingWindow represents non-overlapping, fixed-size time windows
63-
// Example: 1-hour windows at 0-1h, 1-2h, 2-3h
64-
TumblingWindow WindowType = "tumbling"
65-
66-
// SlidingWindow represents overlapping time windows that slide forward
67-
// Example: a 1-hour sliding window will consider the usage of the last 1 hour prior to the current time.
68-
SlidingWindow WindowType = "sliding"
69-
)
70-
71-
// IsValid returns true if the WindowType is a valid value
72-
func (wt WindowType) IsValid() bool {
73-
switch wt {
74-
case TumblingWindow, SlidingWindow:
75-
return true
76-
default:
77-
return false
78-
}
79-
}
80-
81-
// GetDefaultWindowType returns the default window type (sliding)
82-
func GetDefaultWindowType() WindowType {
83-
return SlidingWindow
84-
}
85-
86-
// GetWindowTypeOrDefault returns the window type if set, otherwise returns the default (sliding)
87-
func (up *UsageParams) GetWindowTypeOrDefault() WindowType {
88-
if up.WindowType == nil {
89-
return GetDefaultWindowType()
90-
}
91-
return *up.WindowType
92-
}
93-
94-
func (up *UsageParams) GetExtraDurationParamOrDefault(key string, defaultValue time.Duration) time.Duration {
95-
if up.ExtraParams == nil {
96-
return defaultValue
97-
}
98-
99-
value, exists := up.ExtraParams[key]
100-
if !exists {
101-
return defaultValue
102-
}
103-
104-
duration, err := time.ParseDuration(value)
105-
if err != nil {
106-
return defaultValue
107-
}
108-
109-
return duration
110-
}
111-
112-
func (up *UsageParams) GetExtraStringParamOrDefault(key string, defaultValue string) string {
113-
if up.ExtraParams == nil {
114-
return defaultValue
115-
}
116-
117-
value, exists := up.ExtraParams[key]
118-
if !exists {
119-
return defaultValue
120-
}
121-
122-
return value
123-
}

0 commit comments

Comments
 (0)