Skip to content

Commit

Permalink
Partial revert
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Dec 5, 2023
1 parent d8aa900 commit bcfa093
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,6 @@ github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6
github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231122224424-d95957e76b4e h1:y24HWm0yVrlwE9a8oM+vKLDDW5F/zco/lFB+qX9Fx3I=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231122224424-d95957e76b4e/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231130233906-2adb3bfd1d44 h1:kRFPzcrs2SihxsTu+hOVTCRIICw6p+A2HlR9LzvXX6w=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231130233906-2adb3bfd1d44/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201022717-a7acfe4abe4b h1:2O4SNVGuxBCQDwxC1oyiJSTWmfLytQJwO9ZokE1n5Yg=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201022717-a7acfe4abe4b/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201023749-4dbd4e03793d h1:0Tfw/JoAkRvDQvuJUhhJALVTl16zTvMd3dGUPecPJUw=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201023749-4dbd4e03793d/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231202005632-a9ea742322f7 h1:rwkKzYzl05Q4TM++L9RIJbXPjIvURoMN5vEr8dvUV/Q=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231202005632-a9ea742322f7/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc=
Expand Down
16 changes: 8 additions & 8 deletions meters/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func init() {

type LimitMeterProvider struct {
Enabled bool
DefaultLimits []userMeterLimit
DefaultLimits []UserMeterLimit
MeterProvider
}

Expand All @@ -42,9 +42,9 @@ type LimitMeter struct {
ApiMeter
}

func (c *LimitMeter) GetLimits(meterName string, checkDims Dimensions) []userMeterLimit {
func (c *LimitMeter) GetLimits(meterName string, checkDims Dimensions) []UserMeterLimit {
// The limit matches the event dimensions if all of the LIMIT dimensions are contained in event
var lims []userMeterLimit
var lims []UserMeterLimit
for _, userLimit := range parseGkUserLimits(c.userData) {
if userLimit.MeterName == meterName && dimsContainedIn(userLimit.Dims, checkDims) {
lims = append(lims, userLimit)
Expand Down Expand Up @@ -74,15 +74,15 @@ func (c *LimitMeter) Meter(meterName string, value float64, extraDimensions Dime
return c.ApiMeter.Meter(meterName, value, extraDimensions)
}

type userMeterLimit struct {
type UserMeterLimit struct {
User string
MeterName string
Dims Dimensions
Period string
Limit float64
}

func (lim *userMeterLimit) Span() (time.Time, time.Time) {
func (lim *UserMeterLimit) Span() (time.Time, time.Time) {
now := time.Now().In(time.UTC)
d1 := now
d2 := now
Expand All @@ -107,11 +107,11 @@ func (lim *userMeterLimit) Span() (time.Time, time.Time) {
return d1, d2
}

func parseGkUserLimits(v string) []userMeterLimit {
var lims []userMeterLimit
func parseGkUserLimits(v string) []UserMeterLimit {
var lims []UserMeterLimit
for _, productLimit := range gjson.Get(v, "product_limits").Map() {
for _, plim := range productLimit.Array() {
lim := userMeterLimit{
lim := UserMeterLimit{
MeterName: plim.Get("amberflo_meter").String(),
Limit: plim.Get("limit_value").Float(),
Period: plim.Get("time_period").String(),
Expand Down
10 changes: 5 additions & 5 deletions meters/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestLimitMeter(t *testing.T) {
mp := NewDefaultMeterProvider()
cmp := NewLimitMeterProvider(mp)
cmp.Enabled = true
cmp.DefaultLimits = []userMeterLimit{lim}
cmp.DefaultLimits = []UserMeterLimit{lim}
testLimitMeter(t,
cmp,
lim.MeterName,
Expand All @@ -42,7 +42,7 @@ func TestLimitMeter_Amberflo(t *testing.T) {
t.Run("", func(t *testing.T) {
cmp := NewLimitMeterProvider(mp)
cmp.Enabled = true
cmp.DefaultLimits = []userMeterLimit{lim}
cmp.DefaultLimits = []UserMeterLimit{lim}
testLimitMeter(t,
cmp,
lim.MeterName,
Expand Down Expand Up @@ -94,9 +94,9 @@ func TestLimitMeter_Gatekeeper(t *testing.T) {
}
}

func testLims(meterName string) []userMeterLimit {
func testLims(meterName string) []UserMeterLimit {
testKey := 1 // time.Now().In(time.UTC).Unix()
lims := []userMeterLimit{
lims := []UserMeterLimit{
// foo tests
{
MeterName: meterName,
Expand Down Expand Up @@ -139,7 +139,7 @@ func testLims(meterName string) []userMeterLimit {
return lims
}

func testLimitMeter(t *testing.T, cmp *LimitMeterProvider, meterName string, user testUser, lim userMeterLimit) {
func testLimitMeter(t *testing.T, cmp *LimitMeterProvider, meterName string, user testUser, lim UserMeterLimit) {
incr := 1.0
m := cmp.NewMeter(user)
startTime, endTime := lim.Span()
Expand Down
2 changes: 1 addition & 1 deletion meters/meters.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func GetProvider(cfg Config) (MeterProvider, error) {
if cfg.EnableRateLimits {
mp := NewLimitMeterProvider(meterProvider)
mp.Enabled = true
// mp.DefaultLimits = append(mp.DefaultLimits, meters.userMeterLimit{Limit: 10, Period: "monthly", MeterName: "rest"})
// mp.DefaultLimits = append(mp.DefaultLimits, meters.UserMeterLimit{Limit: 10, Period: "monthly", MeterName: "rest"})
meterProvider = mp
}
return meterProvider, nil
Expand Down
2 changes: 1 addition & 1 deletion meters/meters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type testMeterConfig struct {
}

func testMeter(t *testing.T, mp MeterProvider, cfg testMeterConfig) {
d1, d2 := (&userMeterLimit{Period: "hourly"}).Span()
d1, d2 := (&UserMeterLimit{Period: "hourly"}).Span()
t.Run("Meter", func(t *testing.T) {
m := mp.NewMeter(cfg.user1)
v, _ := m.GetValue(cfg.testMeter1, d1, d2, nil)
Expand Down

0 comments on commit bcfa093

Please sign in to comment.