Skip to content

Conversation

@sanposhiho
Copy link
Member

@sanposhiho sanposhiho commented Dec 27, 2020

What this PR does / why we need it:

Change the insight collector logic to reduce communication with datastore.

Which issue(s) this PR fixes:

Fixes #1314

Does this PR introduce a user-facing change?:

NONE

@pipecd-bot
Copy link
Collaborator

GO_LINTER

The following files are not gofmt-ed. By commenting /golinter fmt, the formatted one will be appended to this pull request automatically.

pkg/app/ops/insightcollector/collector_test.go
--- pkg/app/ops/insightcollector/collector_test.go.orig
+++ pkg/app/ops/insightcollector/collector_test.go
@@ -20,10 +20,12 @@
 	"testing"
 	"time"
 
-	"github.com/pipe-cd/pipe/pkg/filestore/filestoretest"
 	"go.uber.org/zap"
 
+	"github.com/pipe-cd/pipe/pkg/filestore/filestoretest"
+
 	"github.com/golang/mock/gomock"
+
 	"github.com/pipe-cd/pipe/pkg/datastore"
 
 	"github.com/pipe-cd/pipe/pkg/datastore/datastoretest"

Copy link
Collaborator

@pipecd-bot pipecd-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GO_LINTER

Some issues were detected while linting go source files in your changes.

i.logger.Error("failed to update application chunks", zap.Error(err))
}
var returnErr error
for appId, ds := range appMap {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
return nil
}
for proId, ds := range projectMap {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.logger.Error("failed to update application chunks", zap.Error(err))
}
var returnErr error
for appId, ds := range appMap {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxCreatedAt = apps[len(apps)-1].CreatedAt
}
return nil
for proId, ds := range projectMap {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return err
}

dc, err := i.getDeploymentsWithCreatedAt(ctx, m.DeploymentCreatedAtMilestone, targetDate.Unix())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ineffectual assignment to err

continue
}
for _, k := range aggregateKinds {
dc, err := i.getDeploymentsWithCompletedAt(ctx, m.DeploymentCompletedAtMilestone, targetDate.Unix())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ineffectual assignment to err

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 33.86%. This pull request decreases coverage by -0.15%.

File Function Base Head Diff
pkg/app/ops/insightcollector/collector.go InsightCollector.AggregateWithCreatedAt -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.AggregateWithCompletedAt -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCreatedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCompletedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.groupingDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.deploymentToDeployFrequency -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.deploymentToChangeFailureRate -- 94.12% +94.12%
pkg/insight/filestore.go Store.PutMilestone -- 0.00% +0.00%
pkg/insight/milestone.go Milestone.UpdateMilestone -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDailyInsightData 93.33% 94.44% +1.11%
pkg/app/ops/insightcollector/collector.go InsightCollector.updateApplicationChunks 0.00% 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectProjectsInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectApplicationInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForDeployFrequency 93.75% -- -93.75%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForChangeFailureRate 96.15% -- -96.15%
pkg/insight/filestore.go Store.putMilestone 0.00% -- +-0.00%
pkg/insight/milestone.go Milestone.updateMilestone 0.00% -- +-0.00%

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 33.88%. This pull request decreases coverage by -0.14%.

File Function Base Head Diff
pkg/app/ops/insightcollector/collector.go InsightCollector.AggregateWithCreatedAt -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.AggregateWithCompletedAt -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCreatedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCompletedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.groupingDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.deploymentToDeployFrequency -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.deploymentToChangeFailureRate -- 94.12% +94.12%
pkg/insight/filestore.go Store.PutMilestone -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.updateApplicationChunks 0.00% 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDailyInsightData 93.33% 94.44% +1.11%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectProjectsInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectApplicationInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForDeployFrequency 93.75% -- -93.75%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForChangeFailureRate 96.15% -- -96.15%
pkg/insight/filestore.go Store.putMilestone 0.00% -- +-0.00%
pkg/insight/milestone.go Milestone.updateMilestone 0.00% -- +-0.00%

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 33.87%. This pull request decreases coverage by -0.14%.

File Function Base Head Diff
pkg/app/ops/insightcollector/collector.go InsightCollector.AggregateWithCreatedAt -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.AggregateWithCompletedAt -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCreatedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCompletedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.groupingDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.deploymentToDeployFrequency -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.deploymentToChangeFailureRate -- 94.12% +94.12%
pkg/insight/filestore.go Store.PutMilestone -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.updateChunk 0.00% 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDailyInsightData 93.33% 94.44% +1.11%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectProjectsInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectApplicationInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForDeployFrequency 93.75% -- -93.75%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForChangeFailureRate 96.15% -- -96.15%
pkg/insight/filestore.go Store.putMilestone 0.00% -- +-0.00%
pkg/insight/milestone.go Milestone.updateMilestone 0.00% -- +-0.00%

@sanposhiho sanposhiho marked this pull request as ready for review December 27, 2020 11:28
@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 33.87%. This pull request decreases coverage by -0.14%.

File Function Base Head Diff
pkg/app/ops/insightcollector/collector.go InsightCollector.AggregateWithCreatedAt -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.AggregateWithCompletedAt -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.extractDailyInsightDataPoints -- 94.44% +94.44%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCreatedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCompletedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.groupingDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.extractDeployFrequency -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.extractChangeFailureRate -- 94.12% +94.12%
pkg/insight/filestore.go Store.PutMilestone -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.updateChunk 0.00% 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectProjectsInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectApplicationInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDailyInsightData 93.33% -- -93.33%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForDeployFrequency 93.75% -- -93.75%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForChangeFailureRate 96.15% -- -96.15%
pkg/insight/filestore.go Store.putMilestone 0.00% -- +-0.00%
pkg/insight/milestone.go Milestone.updateMilestone 0.00% -- +-0.00%

t.Logger.Info("application insight collector successfully finished", zap.Duration("duration", time.Since(start)))
retryAggregateWithCompletedAt := true
retryAggregateWithCreatedAt := true
for i := 0; i < cfg.InsightCollector.RetryTime; i++ {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks 👍

// updated all project's insights completely
break
func (i *InsightCollector) AggregateWithCreatedAt(ctx context.Context) error {
currentTime := time.Now()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: now := time.Now()

if len(projects) == 0 {
// updated all project's insights completely
break
func (i *InsightCollector) AggregateWithCreatedAt(ctx context.Context) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: processNewlyCreatedDeployments

if len(apps) == 0 {
// updated all application's insights completely
break
func (i *InsightCollector) AggregateWithCompletedAt(ctx context.Context) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: processNewlyCompletedDeployments

appMap, projectMap := i.groupingDeployments(dc)

var returnErr error
for appID, ds := range appMap {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for id, ds := range apps

if err != nil {
return err
}
appMap, projectMap := i.groupingDeployments(dc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: apps, projects := i.groupDeployments(dc)

}
appMap, projectMap := i.groupingDeployments(dc)

var returnErr error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: updateErr

if returnErr == nil {
m.DeploymentCreatedAtMilestone = targetDate.Unix()
}
if err := i.insightstore.PutMilestone(ctx, m); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update the milestone when returnErr was not nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly 👍

@nghialv
Copy link
Member

nghialv commented Jan 4, 2021

@sanposhiho Hi, thanks for your great work!
I have just left some comments. Please take a look.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 33.87%. This pull request decreases coverage by -0.14%.

File Function Base Head Diff
pkg/app/ops/insightcollector/collector.go InsightCollector.ProcessNewlyCreatedDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.ProcessNewlyCompletedDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.extractDailyInsightDataPoints -- 94.44% +94.44%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCreatedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDeploymentsWithCompletedAt -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.groupingDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.extractDeployFrequency -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.extractChangeFailureRate -- 94.12% +94.12%
pkg/insight/filestore.go Store.PutMilestone -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.updateChunk 0.00% 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectProjectsInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectApplicationInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDailyInsightData 93.33% -- -93.33%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForDeployFrequency 93.75% -- -93.75%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForChangeFailureRate 96.15% -- -96.15%
pkg/insight/filestore.go Store.putMilestone 0.00% -- +-0.00%
pkg/insight/milestone.go Milestone.updateMilestone 0.00% -- +-0.00%

@sanposhiho
Copy link
Member Author

@nghialv
Thanks for your reviews, fixed them :D

@nakabonne
Copy link
Member

Yep, looks neat 👍
/lgtm

@pipecd-bot pipecd-bot added the lgtm label Jan 5, 2021
Schedule string `json:"schedule"`
Schedule string `json:"schedule"`
RetryTime int `json:"retryTime"`
RetryIntervalHour int `json:"retryIntervalHour"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set the default value for these fields. Currently, they are zero it means no jobs will be executed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK 👍

var failed bool
start := time.Now()
if err = collector.ProcessNewlyCompletedDeployments(ctx); err != nil {
t.Logger.Error("failed to process the insight collector with completedAt", zap.Error(err))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: failed to process the newly completed deployments while accumulating insight data

t.Logger.Error("failed to process the insight collector with completedAt", zap.Error(err))
failed = true
} else {
t.Logger.Info("processing the insight collector with completedAt successfully finished", zap.Duration("duration", time.Since(start)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: successfully processed the newly completed deployments while accumulating insight data


start = time.Now()
if err = collector.ProcessNewlyCreatedDeployments(ctx); err != nil {
t.Logger.Error("failed to process the insight collector with createdAt", zap.Error(err))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: failed to process the newly created deployments while accumulating insight data

t.Logger.Error("failed to process the insight collector with createdAt", zap.Error(err))
failed = true
} else {
t.Logger.Info("processing the insight collector with createdAt successfully finished", zap.Duration("duration", time.Since(start)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: successfully processed the newly created deployments while accumulating insight data

for retry.WaitNext(ctx) {
var failed bool
start := time.Now()
if err = collector.ProcessNewlyCompletedDeployments(ctx); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you skip calling this function when it was completed successfully while ProcessNewlyCreatedDeployments was failed? I know that the insight data will not be affected but we can save some unnecessary DB calls by skip calling this one.


// getInsightDataForDeployFrequency accumulate insight data in target range for deploy frequency.
func (i *InsightCollector) getInsightDataForDeployFrequency(
func (i *InsightCollector) getDeploymentsWithCreatedAt(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: findDeploymentsCreatedInRange

i.logger.Error("failed to get deployments", zap.Error(err))
return &insight.ChangeFailureRate{}, time.Time{}, fmt.Errorf("failed to get deployments")
// groupingDeployments groups deoloyments by applicationID and projectID
func (i *InsightCollector) groupingDeployments(deployments []*model.Deployment) (map[string][]*model.Deployment, map[string][]*model.Deployment) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: groupDeployments

if err != nil {
i.logger.Error("failed to get deployments", zap.Error(err))
return &insight.ChangeFailureRate{}, time.Time{}, fmt.Errorf("failed to get deployments")
// groupingDeployments groups deoloyments by applicationID and projectID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: deployments

return &insight.ChangeFailureRate{}, time.Time{}, fmt.Errorf("failed to get deployments")
// groupingDeployments groups deoloyments by applicationID and projectID
func (i *InsightCollector) groupingDeployments(deployments []*model.Deployment) (map[string][]*model.Deployment, map[string][]*model.Deployment) {
appmap := map[string][]*model.Deployment{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: apps, projects

)

// extractDeployFrequency extracts deploy frequency from deployments with specified range
func (i *InsightCollector) extractDeployFrequency(deployments []*model.Deployment, from, to int64, targetTimestamp int64) (*insight.DeployFrequency, []*model.Deployment) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be an independent function, doesn't need to belong to InsightCollector.

}

// extractDeployFrequency extracts change failure rate from deployments with specified range
func (i *InsightCollector) extractChangeFailureRate(deployments []*model.Deployment, from, to int64, targetTimestamp int64) (*insight.ChangeFailureRate, []*model.Deployment) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be an independent function, doesn't need to belong to InsightCollector.

@nghialv
Copy link
Member

nghialv commented Jan 6, 2021

@sanposhiho Thanks for your updates. Could you take a look at my recent reviews?

@pipecd-bot pipecd-bot removed the lgtm label Jan 6, 2021
@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 33.94%. This pull request decreases coverage by -0.08%.

File Function Base Head Diff
pkg/app/ops/insightcollector/collector.go InsightCollector.ProcessNewlyCreatedDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.ProcessNewlyCompletedDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.extractDailyInsightDataPoints -- 94.44% +94.44%
pkg/app/ops/insightcollector/collector.go InsightCollector.findDeploymentsCreatedInRange -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.findDeploymentsCompletedInRange -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.groupDeployments -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go extractDeployFrequency -- 100.00% +100.00%
pkg/app/ops/insightcollector/collector.go extractChangeFailureRate -- 94.12% +94.12%
pkg/config/control_plane.go ControlPlaneInsightCollector.UnmarshalJSON -- 100.00% +100.00%
pkg/insight/filestore.go Store.PutMilestone -- 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.updateChunk 0.00% 0.00% +0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectProjectsInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.CollectApplicationInsight 0.00% -- +-0.00%
pkg/app/ops/insightcollector/collector.go InsightCollector.getDailyInsightData 93.33% -- -93.33%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForDeployFrequency 93.75% -- -93.75%
pkg/app/ops/insightcollector/collector.go InsightCollector.getInsightDataForChangeFailureRate 96.15% -- -96.15%
pkg/insight/filestore.go Store.putMilestone 0.00% -- +-0.00%
pkg/insight/milestone.go Milestone.updateMilestone 0.00% -- +-0.00%

@nghialv
Copy link
Member

nghialv commented Jan 6, 2021

@sanposhiho Many thanks for your efforts.
Looks good.
/approve

@pipecd-bot
Copy link
Collaborator

APPROVE

This pull request is APPROVED by nghialv.

Approvers can cancel the approval by writing /approve cancel in a comment. Any additional commits also will change this pull request to be not-approved.

@pipecd-bot pipecd-bot merged commit 0bcff24 into master Jan 6, 2021
@pipecd-bot pipecd-bot deleted the improve-insight-collector branch January 6, 2021 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve insight collector

5 participants