From 712f929835aa6f3f335221100358f4c6077aad7b Mon Sep 17 00:00:00 2001 From: ginokent <29125616+ginokent@users.noreply.github.com> Date: Thu, 13 Oct 2022 14:56:43 +0900 Subject: [PATCH] fix: fix incorrect order in which graphs are drawn --- .githooks/pre-push | 9 +- Makefile | 14 +-- pkg/domain/plot.go | 30 +++---- pkg/domain/plot_test.go | 88 +++++++++---------- .../bigquery/daily_project_cost_gcp.go | 6 +- .../bigquery/daily_service_cost_gcp.go | 6 +- pkg/repository/bigquery/daily_sku_cost_gcp.go | 6 +- .../bigquery/sum_service_cost_gcp.go | 10 +-- pkg/repository/repository.go | 20 +---- pkg/usecase/mock_test.go | 8 +- pkg/usecase/plot_daily_service_cost_gcp.go | 32 +++---- .../plot_daily_service_cost_gcp_test.go | 11 +-- pkg/usecase/usecase.go | 3 +- 13 files changed, 112 insertions(+), 131 deletions(-) diff --git a/.githooks/pre-push b/.githooks/pre-push index 4eb59d5..cae074f 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -1,7 +1,12 @@ #!/usr/bin/env bash +set -Eeu -o pipefail REPO_ROOT=$(git rev-parse --show-toplevel) -cd "${REPO_ROOT:?}" || exit 1 +__main__() { + if ! cd "${REPO_ROOT:?}"; then + return 1 + fi -make ci + make ci +} diff --git a/Makefile b/Makefile index b6d1d14..31ab004 100644 --- a/Makefile +++ b/Makefile @@ -9,15 +9,15 @@ TIMESTAMP := $(shell git log -1 --format='%cI') .DEFAULT_GOAL := help .PHONY: help -help: githooks ## display this help documents. +help: githooks ## display this help documents @grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-40s\033[0m %s\n", $$1, $$2}' .PHONY: githooks -githooks: ## githooks をインストールします。 +githooks: @[[ -f "${PRE_PUSH}" ]] || cp -ai "${GITROOT}/.githooks/pre-push" "${PRE_PUSH}" .PHONY: lint -lint: ## go mod tidy の後に golangci-lint を実行します。 +lint: ## Run golangci-lint after go mod tidy # tidy go mod tidy git diff --exit-code go.mod go.sum @@ -27,21 +27,21 @@ lint: ## go mod tidy の後に golangci-lint を実行します。 git diff --exit-code .PHONY: credits -credits: ## CREDITS ファイルを生成します。 +credits: ## Generate CREDITS file. command -v gocredits || go install github.com/Songmu/gocredits/cmd/gocredits@latest gocredits . > CREDITS git diff --exit-code .PHONY: test -test: githooks ## go test を実行し coverage を出力します。 +test: githooks ## Run go test and display coverage # test go test -v -race -p=4 -parallel=8 -timeout=300s -cover -coverprofile=./coverage.txt ./... go tool cover -func=./coverage.txt .PHONY: ci -ci: lint credits test ## CI 上で実行する lint や test のコマンドセットです。 +ci: lint credits test ## CI command set .PHONY: goxz -goxz: ci ## goxz を用いて release 用ファイルを生成します。 +goxz: ci ## Run goxz for release command -v goxz || go install github.com/Songmu/goxz/cmd/goxz@latest goxz -d ./.tmp -os=linux,darwin,windows -arch=amd64,arm64 -pv ${VERSION} -build-ldflags "-X ${GOMODULE}/pkg/config.version=${VERSION} -X ${GOMODULE}/pkg/config.revision=${REVISION} -X ${GOMODULE}/pkg/config.branch=${BRANCH} -X ${GOMODULE}/pkg/config.timestamp=${TIMESTAMP}" ./cmd/ccc diff --git a/pkg/domain/plot.go b/pkg/domain/plot.go index a177df8..6bc03f6 100644 --- a/pkg/domain/plot.go +++ b/pkg/domain/plot.go @@ -44,18 +44,18 @@ func WithTicker(ticker plot.Ticker) Option { } type PlotGraphParameters struct { - GraphTitle string - XLabelText string - YLabelText string - Width float64 - Hight float64 - XAxisPointsCount int - From time.Time - To time.Time - TimeZone *time.Location - OrderedLegends []string - LegendValuesMap map[string]plotter.Values - ImageFormat string + GraphTitle string + XLabelText string + YLabelText string + Width float64 + Hight float64 + XAxisPointsCount int + From time.Time + To time.Time + TimeZone *time.Location + OrderedLegendsAsc []string + LegendValuesMap map[string]plotter.Values + ImageFormat string } // nolint: cyclop,funlen @@ -79,14 +79,14 @@ func (d *Domain) PlotGraph( barChartWidth := vg.Points((graphWidth - 100) / float64(ps.XAxisPointsCount)) // NOTE: グラフの幅から固定長(95)を引いて X 軸の値数で割る previousBarChart := (*plotter.BarChart)(nil) - for i, legend := range ps.OrderedLegends { + for i, legend := range ps.OrderedLegendsAsc { barChart, err := plotter.NewBarChart(ps.LegendValuesMap[legend], barChartWidth) if err != nil { return errors.Errorf("plotter.NewBarChart: %w", err) } barChart.Width = barChartWidth barChart.LineStyle.Width = vg.Length(0) // NOTE: グラフの枠線の太さを 0 にする - barChart.Color = constz.GraphColor(i) + barChart.Color = constz.GraphColor(len(ps.OrderedLegendsAsc) - 1 - i) p.Legend.Add(legend, barChart) if previousBarChart != nil { @@ -120,7 +120,7 @@ func (d *Domain) PlotGraph( p.Legend.XOffs = 10 p.Legend.YOffs = -10 legendHight := float64(p.Legend.TextStyle.Height("C")) * 8 - legendsHight := legendHight * float64(len(ps.OrderedLegends)) + legendsHight := legendHight * float64(len(ps.OrderedLegendsAsc)) log.Debugf("legendHight=%f, legendsHight=%f", legendHight, legendsHight) p.Y.Min = 0 p.Y.Max += legendsHight // NOTE: グラフと Legend が被らないように、 Legend の高さ (文字 C の高さで計算) * Legend 数を足して、 Y 軸の高さを確保している diff --git a/pkg/domain/plot_test.go b/pkg/domain/plot_test.go index 694d702..74157ad 100644 --- a/pkg/domain/plot_test.go +++ b/pkg/domain/plot_test.go @@ -54,16 +54,16 @@ func TestPlotGraph(t *testing.T) { - - + + - + legend1 - + legend2 @@ -73,16 +73,16 @@ func TestPlotGraph(t *testing.T) { from := time.Date(2022, 2, 2, 2, 22, 22, 0, constz.TimeZone("Asia/Tokyo")) d := New(WithTicker(plot.DefaultTicks{})) if err := d.PlotGraph(buf, &PlotGraphParameters{ - GraphTitle: "Title", - XLabelText: "XLabel", - YLabelText: "YLabel", - Width: 1280, - Hight: 720, - XAxisPointsCount: 1, - From: from, - To: from.AddDate(0, 0, 1), - TimeZone: constz.TimeZone("Asia/Tokyo"), - OrderedLegends: []string{"legend1", "legend2"}, + GraphTitle: "Title", + XLabelText: "XLabel", + YLabelText: "YLabel", + Width: 1280, + Hight: 720, + XAxisPointsCount: 1, + From: from, + To: from.AddDate(0, 0, 1), + TimeZone: constz.TimeZone("Asia/Tokyo"), + OrderedLegendsAsc: []string{"legend1", "legend2"}, LegendValuesMap: map[string]plotter.Values{ "legend1": []float64{1}, "legend2": []float64{2}, @@ -103,16 +103,16 @@ func TestPlotGraph(t *testing.T) { from := time.Date(2022, 2, 2, 2, 22, 22, 0, constz.TimeZone("Asia/Tokyo")) d := New() if err := d.PlotGraph(buf, &PlotGraphParameters{ - GraphTitle: "Title", - XLabelText: "XLabel", - YLabelText: "YLabel", - Width: 1280, - Hight: 720, - XAxisPointsCount: 1, - From: from, - To: from.AddDate(0, 0, 1), - TimeZone: constz.TimeZone("Asia/Tokyo"), - OrderedLegends: []string{"NoData"}, + GraphTitle: "Title", + XLabelText: "XLabel", + YLabelText: "YLabel", + Width: 1280, + Hight: 720, + XAxisPointsCount: 1, + From: from, + To: from.AddDate(0, 0, 1), + TimeZone: constz.TimeZone("Asia/Tokyo"), + OrderedLegendsAsc: []string{"NoData"}, LegendValuesMap: map[string]plotter.Values{ "legend1": []float64{1}, }, @@ -127,16 +127,16 @@ func TestPlotGraph(t *testing.T) { from := time.Date(2022, 2, 2, 2, 22, 22, 0, constz.TimeZone("Asia/Tokyo")) d := New() if err := d.PlotGraph(buf, &PlotGraphParameters{ - GraphTitle: "Title", - XLabelText: "XLabel", - YLabelText: "YLabel", - Width: 1280, - Hight: 720, - XAxisPointsCount: 1, - From: from, - To: from.AddDate(0, 0, 1), - TimeZone: constz.TimeZone("Asia/Tokyo"), - OrderedLegends: []string{"legend1", "legend2"}, + GraphTitle: "Title", + XLabelText: "XLabel", + YLabelText: "YLabel", + Width: 1280, + Hight: 720, + XAxisPointsCount: 1, + From: from, + To: from.AddDate(0, 0, 1), + TimeZone: constz.TimeZone("Asia/Tokyo"), + OrderedLegendsAsc: []string{"legend1", "legend2"}, LegendValuesMap: map[string]plotter.Values{ "legend1": []float64{1}, "legend2": []float64{2}, @@ -152,16 +152,16 @@ func TestPlotGraph(t *testing.T) { from := time.Date(2022, 2, 2, 2, 22, 22, 0, constz.TimeZone("Asia/Tokyo")) d := New() err := d.PlotGraph(rw, &PlotGraphParameters{ - GraphTitle: "Title", - XLabelText: "XLabel", - YLabelText: "YLabel", - Width: 1280, - Hight: 720, - XAxisPointsCount: 1, - From: from, - To: from.AddDate(0, 0, 1), - TimeZone: constz.TimeZone("Asia/Tokyo"), - OrderedLegends: []string{"legend1", "legend2"}, + GraphTitle: "Title", + XLabelText: "XLabel", + YLabelText: "YLabel", + Width: 1280, + Hight: 720, + XAxisPointsCount: 1, + From: from, + To: from.AddDate(0, 0, 1), + TimeZone: constz.TimeZone("Asia/Tokyo"), + OrderedLegendsAsc: []string{"legend1", "legend2"}, LegendValuesMap: map[string]plotter.Values{ "legend1": []float64{1}, "legend2": []float64{2}, diff --git a/pkg/repository/bigquery/daily_project_cost_gcp.go b/pkg/repository/bigquery/daily_project_cost_gcp.go index ed4c23f..c205d05 100644 --- a/pkg/repository/bigquery/daily_project_cost_gcp.go +++ b/pkg/repository/bigquery/daily_project_cost_gcp.go @@ -34,14 +34,14 @@ WHERE AND DATE(usage_start_time, '{{ .TimeZone }}') >= DATE("{{ .From }}", '{{ .TimeZone }}') AND - DATE(usage_start_time, '{{ .TimeZone }}') <= DATE("{{ .To }}", '{{ .TimeZone }}') + DATE(usage_start_time, '{{ .TimeZone }}') < DATE("{{ .To }}", '{{ .TimeZone }}') AND cost >= {{ .CostThreshold }} GROUP BY day, currency ORDER BY - cost -DESC + day +ASC ;`)) func (c *BigQuery) DailyProjectCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPCost, error) { diff --git a/pkg/repository/bigquery/daily_service_cost_gcp.go b/pkg/repository/bigquery/daily_service_cost_gcp.go index 62c398f..9f838e3 100644 --- a/pkg/repository/bigquery/daily_service_cost_gcp.go +++ b/pkg/repository/bigquery/daily_service_cost_gcp.go @@ -35,14 +35,14 @@ WHERE AND DATE(usage_start_time, '{{ .TimeZone }}') >= DATE("{{ .From }}", '{{ .TimeZone }}') AND - DATE(usage_start_time, '{{ .TimeZone }}') <= DATE("{{ .To }}", '{{ .TimeZone }}') + DATE(usage_start_time, '{{ .TimeZone }}') < DATE("{{ .To }}", '{{ .TimeZone }}') AND cost >= {{ .CostThreshold }} GROUP BY day, service, currency ORDER BY - cost -DESC + day +ASC ;`)) func (c *BigQuery) DailyServiceCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { diff --git a/pkg/repository/bigquery/daily_sku_cost_gcp.go b/pkg/repository/bigquery/daily_sku_cost_gcp.go index c8d7afc..7dc2057 100644 --- a/pkg/repository/bigquery/daily_sku_cost_gcp.go +++ b/pkg/repository/bigquery/daily_sku_cost_gcp.go @@ -37,14 +37,14 @@ WHERE AND DATE(usage_start_time, '{{ .TimeZone }}') >= DATE("{{ .From }}", '{{ .TimeZone }}') AND - DATE(usage_start_time, '{{ .TimeZone }}') <= DATE("{{ .To }}", '{{ .TimeZone }}') + DATE(usage_start_time, '{{ .TimeZone }}') < DATE("{{ .To }}", '{{ .TimeZone }}') AND cost >= {{ .CostThreshold }} GROUP BY day, service, sku, currency ORDER BY - cost -DESC + day +ASC ;`)) func (c *BigQuery) DailySKUCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPSKUCost, error) { diff --git a/pkg/repository/bigquery/sum_service_cost_gcp.go b/pkg/repository/bigquery/sum_service_cost_gcp.go index a9da752..bf27a14 100644 --- a/pkg/repository/bigquery/sum_service_cost_gcp.go +++ b/pkg/repository/bigquery/sum_service_cost_gcp.go @@ -34,17 +34,17 @@ WHERE AND DATE(usage_start_time, '{{ .TimeZone }}') >= DATE("{{ .From }}", '{{ .TimeZone }}') AND - DATE(usage_start_time, '{{ .TimeZone }}') <= DATE("{{ .To }}", '{{ .TimeZone }}') + DATE(usage_start_time, '{{ .TimeZone }}') < DATE("{{ .To }}", '{{ .TimeZone }}') AND cost >= {{ .CostThreshold }} GROUP BY service, currency ORDER BY cost -DESC +ASC ;`)) -func (c *BigQuery) SUMServiceCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { +func (c *BigQuery) SUMServiceCostGCPAsc(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { q, err := buildQuery(sumServiceCostGCPTemplate, sumServiceCostGCPParameter{ TimeZone: tz, GCPBillingTable: billingTable, @@ -59,10 +59,10 @@ func (c *BigQuery) SUMServiceCostGCP(ctx context.Context, billingTable, billingP log.Debugf("%s", q) - results, err := query[domain.GCPServiceCost](ctx, c.client, q) + serviceCostAsc, err := query[domain.GCPServiceCost](ctx, c.client, q) if err != nil { return nil, errors.Errorf("query: %w", err) } - return results, nil + return serviceCostAsc, nil } diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go index ae6cdb4..7fcbfa0 100644 --- a/pkg/repository/repository.go +++ b/pkg/repository/repository.go @@ -33,13 +33,13 @@ func WithBigQuery(bigquery *bigquery.BigQuery) Option { } } -func (r *Repository) SUMServiceCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { - serviceCost, err := r.bigquery.SUMServiceCostGCP(ctx, billingTable, billingProject, from, to, tz, costThreshold) +func (r *Repository) SUMServiceCostGCPAsc(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { + serviceCostAsc, err := r.bigquery.SUMServiceCostGCPAsc(ctx, billingTable, billingProject, from, to, tz, costThreshold) if err != nil { return nil, errors.Errorf("(*bigquery.BigQuery).SUMServiceCostGCP: %w", err) } - return serviceCost, nil + return serviceCostAsc, nil } func (r *Repository) DailyServiceCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { @@ -51,20 +51,6 @@ func (r *Repository) DailyServiceCostGCP(ctx context.Context, billingTable, bill return serviceCost, nil } -func (r *Repository) ServicesOrderBySUMServiceCostGCP(googleCloudPlatformServiceSumCost []domain.GCPServiceCost) []string { - return slice.Uniq( - slice.Select( - slice.Sort( - googleCloudPlatformServiceSumCost, - func(a, b domain.GCPServiceCost) bool { return a.Cost > b.Cost }, - ), - func(index int, source domain.GCPServiceCost) (selected string) { - return source.Service - }, - ), - ) -} - func (r *Repository) DailyServiceCostGCPMapByService(servicesOrderBySUMServiceCostGCP []string, dailyServiceCostGCP []domain.GCPServiceCost) map[string][]domain.GCPServiceCost { serviceCost := make(map[string][]domain.GCPServiceCost) for _, service := range servicesOrderBySUMServiceCostGCP { diff --git a/pkg/usecase/mock_test.go b/pkg/usecase/mock_test.go index 8960972..a602206 100644 --- a/pkg/usecase/mock_test.go +++ b/pkg/usecase/mock_test.go @@ -16,22 +16,16 @@ type repositoryMock struct { SUMServiceCostGCP_GCPServiceCost []domain.GCPServiceCost SUMServiceCostGCP_error error - ServicesOrderBySUMServiceCostGCP_slice_string []string - DailyServiceCostGCP_GCPServiceCost []domain.GCPServiceCost DailyServiceCostGCP_error error DailyServiceCostGCPMapByService_map_string_GCPServiceCost map[string][]domain.GCPServiceCost } -func (m *repositoryMock) SUMServiceCostGCP(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { +func (m *repositoryMock) SUMServiceCostGCPAsc(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { return m.SUMServiceCostGCP_GCPServiceCost, m.SUMServiceCostGCP_error } -func (m *repositoryMock) ServicesOrderBySUMServiceCostGCP(googleCloudPlatformServiceSumCost []domain.GCPServiceCost) []string { - return m.ServicesOrderBySUMServiceCostGCP_slice_string -} - func (m *repositoryMock) DailyServiceCostGCP(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) { return m.DailyServiceCostGCP_GCPServiceCost, m.DailyServiceCostGCP_error } diff --git a/pkg/usecase/plot_daily_service_cost_gcp.go b/pkg/usecase/plot_daily_service_cost_gcp.go index 311f160..c855442 100644 --- a/pkg/usecase/plot_daily_service_cost_gcp.go +++ b/pkg/usecase/plot_daily_service_cost_gcp.go @@ -26,11 +26,11 @@ type PlotDailyServiceCostGCPParameters struct { } func (u *UseCase) PlotDailyServiceCostGCP(ctx context.Context, target io.ReadWriter, ps *PlotDailyServiceCostGCPParameters) error { - sumServiceCostGCP, err := u.repository.SUMServiceCostGCP(ctx, ps.BillingTable, ps.BillingProject, ps.From, ps.To, ps.TimeZone, 0.01) + sumServiceCostGCPAsc, err := u.repository.SUMServiceCostGCPAsc(ctx, ps.BillingTable, ps.BillingProject, ps.From, ps.To, ps.TimeZone, 0.01) if err != nil { return errors.Errorf("(RepositoryIF).SUMServiceCostGCP: %w", err) } - servicesOrderBySUMServiceCost := u.repository.ServicesOrderBySUMServiceCostGCP(sumServiceCostGCP) + servicesOrderBySUMServiceCostAsc := slice.Select(sumServiceCostGCPAsc, func(idx int, source domain.GCPServiceCost) string { return source.Service }) dailyServiceCostGCP, err := u.repository.DailyServiceCostGCP(ctx, ps.BillingTable, ps.BillingProject, ps.From, ps.To, ps.TimeZone, 0.01) log.Debugf("%v", dailyServiceCostGCP) @@ -42,12 +42,14 @@ func (u *UseCase) PlotDailyServiceCostGCP(ctx context.Context, target io.ReadWri return errors.Errorf("%s: %s: %v: %w", ps.BillingTable, ps.BillingProject, currencies, ErrMixedCurrenciesDataSourceIsNotSupported) } currency := currencies[0] - dailyServiceCostGCPMapByService := u.repository.DailyServiceCostGCPMapByService(servicesOrderBySUMServiceCost, dailyServiceCostGCP) + dailyServiceCostGCPMapByService := u.repository.DailyServiceCostGCPMapByService(servicesOrderBySUMServiceCostAsc, dailyServiceCostGCP) dailyServiceCostsForPlot := make(map[string]plotter.Values) var xAxisPointsCount int // NOTE: X 軸の数値の数を数える for k, v := range dailyServiceCostGCPMapByService { dailyServiceCostsForPlot[k] = slice.Select(v, func(_ int, source domain.GCPServiceCost) float64 { return source.Cost }) + + log.Debugf("%s: data count: %d", k, len(v)) if len(v) > xAxisPointsCount { xAxisPointsCount = len(v) } @@ -56,18 +58,18 @@ func (u *UseCase) PlotDailyServiceCostGCP(ctx context.Context, target io.ReadWri if err := u.domain.PlotGraph( target, &domain.PlotGraphParameters{ - GraphTitle: "\n" + fmt.Sprintf("Google Cloud Platform `%s` Cost (from %s to %s)", ps.BillingProject, ps.From.Format(constz.DateOnly), ps.To.Format(constz.DateOnly)), - XLabelText: "\n" + fmt.Sprintf("Date (%s)", ps.TimeZone.String()), - YLabelText: "\n" + currency, - Width: 1280, - Hight: 720, - XAxisPointsCount: xAxisPointsCount, - From: ps.From, - To: ps.To, - TimeZone: ps.TimeZone, - OrderedLegends: servicesOrderBySUMServiceCost, - LegendValuesMap: dailyServiceCostsForPlot, - ImageFormat: ps.ImageFormat, + GraphTitle: "\n" + fmt.Sprintf("Google Cloud Platform `%s` Cost (from %s to %s)", ps.BillingProject, ps.From.Format(constz.DateOnly), ps.To.Format(constz.DateOnly)), + XLabelText: "\n" + fmt.Sprintf("Date (%s)", ps.TimeZone.String()), + YLabelText: "\n" + currency, + Width: 1280, + Hight: 720, + XAxisPointsCount: xAxisPointsCount, + From: ps.From, + To: ps.To, + TimeZone: ps.TimeZone, + OrderedLegendsAsc: servicesOrderBySUMServiceCostAsc, + LegendValuesMap: dailyServiceCostsForPlot, + ImageFormat: ps.ImageFormat, }, ); err != nil { return errors.Errorf("(DomainIF).PlotGraph: %w", err) diff --git a/pkg/usecase/plot_daily_service_cost_gcp_test.go b/pkg/usecase/plot_daily_service_cost_gcp_test.go index 061e0c1..aba9a4a 100644 --- a/pkg/usecase/plot_daily_service_cost_gcp_test.go +++ b/pkg/usecase/plot_daily_service_cost_gcp_test.go @@ -22,7 +22,6 @@ func TestUsecase_PlotDailyServiceCostGCP(t *testing.T) { repository: &repositoryMock{ SUMServiceCostGCP_GCPServiceCost: tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), SUMServiceCostGCP_error: nil, - ServicesOrderBySUMServiceCostGCP_slice_string: []string{"test-project"}, DailyServiceCostGCP_GCPServiceCost: tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), DailyServiceCostGCP_error: nil, DailyServiceCostGCPMapByService_map_string_GCPServiceCost: map[string][]domain.GCPServiceCost{"TestService": tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5)}, @@ -62,10 +61,9 @@ func TestUsecase_PlotDailyServiceCostGCP(t *testing.T) { t.Parallel() u := &UseCase{ repository: &repositoryMock{ - SUMServiceCostGCP_GCPServiceCost: tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), - SUMServiceCostGCP_error: nil, - ServicesOrderBySUMServiceCostGCP_slice_string: []string{"test-project"}, - DailyServiceCostGCP_error: testz.ErrTestError, + SUMServiceCostGCP_GCPServiceCost: tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), + SUMServiceCostGCP_error: nil, + DailyServiceCostGCP_error: testz.ErrTestError, }, } ctx := context.Background() @@ -82,7 +80,6 @@ func TestUsecase_PlotDailyServiceCostGCP(t *testing.T) { repository: &repositoryMock{ SUMServiceCostGCP_GCPServiceCost: append(tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "JPY", 5)...), SUMServiceCostGCP_error: nil, - ServicesOrderBySUMServiceCostGCP_slice_string: []string{"test-project"}, DailyServiceCostGCP_GCPServiceCost: append(tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "JPY", 5)...), DailyServiceCostGCP_error: nil, DailyServiceCostGCPMapByService_map_string_GCPServiceCost: map[string][]domain.GCPServiceCost{"TestService": tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5)}, @@ -102,7 +99,6 @@ func TestUsecase_PlotDailyServiceCostGCP(t *testing.T) { repository: &repositoryMock{ SUMServiceCostGCP_GCPServiceCost: tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), SUMServiceCostGCP_error: nil, - ServicesOrderBySUMServiceCostGCP_slice_string: []string{"test-project"}, DailyServiceCostGCP_GCPServiceCost: tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), DailyServiceCostGCP_error: nil, DailyServiceCostGCPMapByService_map_string_GCPServiceCost: map[string][]domain.GCPServiceCost{"TestService": tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5)}, @@ -125,7 +121,6 @@ func TestUsecase_PlotDailyServiceCostGCP(t *testing.T) { repository: &repositoryMock{ SUMServiceCostGCP_GCPServiceCost: tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), SUMServiceCostGCP_error: nil, - ServicesOrderBySUMServiceCostGCP_slice_string: []string{"test-project"}, DailyServiceCostGCP_GCPServiceCost: tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5), DailyServiceCostGCP_error: nil, DailyServiceCostGCPMapByService_map_string_GCPServiceCost: map[string][]domain.GCPServiceCost{"TestService": tests.NewGCPServiceCosts(tests.TestDate, "test-project", "TestService", 123.45, 1, "USD", 5)}, diff --git a/pkg/usecase/usecase.go b/pkg/usecase/usecase.go index ff6f688..7fa1eac 100644 --- a/pkg/usecase/usecase.go +++ b/pkg/usecase/usecase.go @@ -34,8 +34,7 @@ func New(opts ...Option) *UseCase { var _ RepositoryIF = (*repository.Repository)(nil) type RepositoryIF interface { - SUMServiceCostGCP(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) - ServicesOrderBySUMServiceCostGCP(googleCloudPlatformServiceSumCost []domain.GCPServiceCost) []string + SUMServiceCostGCPAsc(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) DailyServiceCostGCP(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) DailyServiceCostGCPMapByService(servicesOrderBySUMServiceCostGCP []string, dailyServiceCostGCP []domain.GCPServiceCost) map[string][]domain.GCPServiceCost }