-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
356 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package mau | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/bucketeer-io/bucketeer/pkg/batch/jobs" | ||
ecclient "github.com/bucketeer-io/bucketeer/pkg/eventcounter/client" | ||
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql" | ||
"github.com/bucketeer-io/bucketeer/proto/eventcounter" | ||
) | ||
|
||
type mauSummarizer struct { | ||
mysqlClient mysql.Client | ||
eventCounterClient ecclient.Client | ||
location *time.Location | ||
opts *jobs.Options | ||
logger *zap.Logger | ||
} | ||
|
||
func NewMAUSummarizer( | ||
mysqlClient mysql.Client, | ||
eventCounterClient ecclient.Client, | ||
location *time.Location, | ||
opts ...jobs.Option) jobs.Job { | ||
dopts := &jobs.Options{ | ||
Timeout: 5 * time.Minute, | ||
Logger: zap.NewNop(), | ||
} | ||
for _, opt := range opts { | ||
opt(dopts) | ||
} | ||
return &mauSummarizer{ | ||
mysqlClient: mysqlClient, | ||
eventCounterClient: eventCounterClient, | ||
location: location, | ||
opts: dopts, | ||
logger: dopts.Logger.Named("mau-count-watcher"), | ||
} | ||
} | ||
|
||
func (s *mauSummarizer) Run(ctx context.Context) error { | ||
ctx, cancel := context.WithTimeout(ctx, s.opts.Timeout) | ||
defer cancel() | ||
now := time.Now().In(s.location) | ||
yesterday := s.getYesterday(now) | ||
_, err := s.eventCounterClient.SummarizeMAUCounts( | ||
ctx, | ||
&eventcounter.SummarizeMAUCountsRequest{ | ||
YearMonth: s.newYearMonth(int32(yesterday.Year()), int32(yesterday.Month())), | ||
IsFinishDay: s.isEndDateOfMonth(yesterday), | ||
}, | ||
) | ||
if err != nil { | ||
s.logger.Error("Failed to summarize MAU counts", | ||
zap.Error(err), | ||
zap.Int32("year", int32(yesterday.Year())), | ||
zap.Int32("month", int32(yesterday.Month())), | ||
) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *mauSummarizer) getYesterday(now time.Time) time.Time { | ||
return now.AddDate(0, 0, -1) | ||
} | ||
|
||
func (s *mauSummarizer) newYearMonth(year, month int32) string { | ||
return fmt.Sprintf("%d%02d", year, month) | ||
} | ||
|
||
func (s *mauSummarizer) isEndDateOfMonth(target time.Time) bool { | ||
next := target.AddDate(0, 0, 1) | ||
return next.Day() == 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Copyright 2023 The Bucketeer Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mau | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
"go.uber.org/zap" | ||
|
||
"github.com/bucketeer-io/bucketeer/pkg/batch/jobs" | ||
ecclientmock "github.com/bucketeer-io/bucketeer/pkg/eventcounter/client/mock" | ||
mysqlmock "github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql/mock" | ||
ecproto "github.com/bucketeer-io/bucketeer/proto/eventcounter" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
t.Parallel() | ||
mockController := gomock.NewController(t) | ||
defer mockController.Finish() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
patterns := []struct { | ||
desc string | ||
setup func(r *mauSummarizer) | ||
expected error | ||
}{ | ||
{ | ||
desc: "fail", | ||
setup: func(r *mauSummarizer) { | ||
r.eventCounterClient.(*ecclientmock.MockClient).EXPECT().SummarizeMAUCounts(gomock.Any(), gomock.Any()).Return( | ||
nil, errors.New("test")) | ||
}, | ||
expected: errors.New("test"), | ||
}, | ||
{ | ||
desc: "success", | ||
setup: func(r *mauSummarizer) { | ||
r.eventCounterClient.(*ecclientmock.MockClient).EXPECT().SummarizeMAUCounts(gomock.Any(), gomock.Any()).Return( | ||
&ecproto.SummarizeMAUCountsResponse{}, nil) | ||
}, | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for _, p := range patterns { | ||
t.Run(p.desc, func(t *testing.T) { | ||
summarizer := newMockMAUSummarizer(t, mockController) | ||
p.setup(summarizer) | ||
err := summarizer.Run(ctx) | ||
assert.Equal(t, p.expected, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetYesterday(t *testing.T) { | ||
t.Parallel() | ||
jst := time.FixedZone("Asia/Tokyo", 9*60*60) | ||
patterns := []struct { | ||
desc string | ||
input time.Time | ||
expected time.Time | ||
}{ | ||
{ | ||
desc: "success", | ||
input: time.Date(2023, 1, 31, 1, 00, 00, 0, time.UTC), | ||
expected: time.Date(2023, 1, 30, 1, 00, 00, 0, time.UTC), | ||
}, | ||
{ | ||
desc: "over month", | ||
input: time.Date(2023, 2, 1, 1, 00, 00, 0, time.UTC), | ||
expected: time.Date(2023, 1, 31, 1, 00, 00, 0, time.UTC), | ||
}, | ||
{ | ||
desc: "over year", | ||
input: time.Date(2023, 1, 1, 1, 00, 00, 0, time.UTC), | ||
expected: time.Date(2022, 12, 31, 1, 00, 00, 0, time.UTC), | ||
}, | ||
{ | ||
desc: "success JST", | ||
input: time.Date(2023, 1, 31, 1, 00, 00, 0, jst), | ||
expected: time.Date(2023, 1, 30, 1, 00, 00, 0, jst), | ||
}, | ||
{ | ||
desc: "over month JST", | ||
input: time.Date(2023, 2, 1, 1, 00, 00, 0, jst), | ||
expected: time.Date(2023, 1, 31, 1, 00, 00, 0, jst), | ||
}, | ||
{ | ||
desc: "over year JST", | ||
input: time.Date(2023, 1, 1, 1, 00, 00, 0, jst), | ||
expected: time.Date(2022, 12, 31, 1, 00, 00, 0, jst), | ||
}, | ||
} | ||
for _, p := range patterns { | ||
t.Run(p.desc, func(t *testing.T) { | ||
summarizer := &mauSummarizer{} | ||
actual := summarizer.getYesterday(p.input) | ||
assert.Equal(t, p.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestIsEndDateOfMonth(t *testing.T) { | ||
t.Parallel() | ||
jst := time.FixedZone("Asia/Tokyo", 9*60*60) | ||
patterns := []struct { | ||
desc string | ||
input time.Time | ||
expected bool | ||
}{ | ||
{ | ||
desc: "not end of month", | ||
input: time.Date(2023, 1, 30, 1, 00, 00, 0, time.UTC), | ||
expected: false, | ||
}, | ||
{ | ||
desc: "success1", | ||
input: time.Date(2023, 1, 31, 1, 00, 00, 0, time.UTC), | ||
expected: true, | ||
}, | ||
{ | ||
desc: "success2", | ||
input: time.Date(2023, 2, 1, 1, 00, 00, 0, jst), | ||
expected: false, | ||
}, | ||
} | ||
for _, p := range patterns { | ||
t.Run(p.desc, func(t *testing.T) { | ||
summarizer := &mauSummarizer{} | ||
actual := summarizer.isEndDateOfMonth(p.input) | ||
assert.Equal(t, p.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func newMockMAUSummarizer(t *testing.T, c *gomock.Controller) *mauSummarizer { | ||
t.Helper() | ||
return &mauSummarizer{ | ||
mysqlClient: mysqlmock.NewMockClient(c), | ||
eventCounterClient: ecclientmock.NewMockClient(c), | ||
location: time.UTC, | ||
opts: &jobs.Options{ | ||
Timeout: 30 * time.Second, | ||
}, | ||
logger: zap.NewNop().Named("test-mau-summarizer"), | ||
} | ||
} |
Oops, something went wrong.