Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 401b1a4

Browse files
author
Noah Hanjun Lee
authored
Move producing statistics logic into 'server' package (#168)
* Fix the typo * Move producing statistics into the 'server' pkg
1 parent 59a14c4 commit 401b1a4

14 files changed

+218
-183
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package interactor
2+
3+
import (
4+
"context"
5+
6+
"github.com/gitploy-io/gitploy/ent"
7+
)
8+
9+
func (i *Interactor) ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error) {
10+
s, err := i.Store.FindDeploymentStatisticsOfRepoByEnv(ctx, r, d.Env)
11+
12+
if ent.IsNotFound(err) {
13+
return i.Store.CreateDeploymentStatistics(ctx, &ent.DeploymentStatistics{
14+
Namespace: r.Namespace,
15+
Name: r.Name,
16+
Env: d.Env,
17+
Count: 1,
18+
})
19+
}
20+
21+
s.Count = s.Count + 1
22+
23+
return i.Store.UpdateDeploymentStatistics(ctx, s)
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package interactor
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/gitploy-io/gitploy/ent"
8+
"github.com/gitploy-io/gitploy/internal/interactor/mock"
9+
"github.com/golang/mock/gomock"
10+
)
11+
12+
func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
13+
t.Run("Create the statistics when the record is not found.", func(t *testing.T) {
14+
input := struct {
15+
repo *ent.Repo
16+
d *ent.Deployment
17+
}{
18+
repo: &ent.Repo{
19+
Namespace: "octocat",
20+
Name: "HelloWorld",
21+
},
22+
d: &ent.Deployment{
23+
Env: "production",
24+
},
25+
}
26+
27+
ctrl := gomock.NewController(t)
28+
store := mock.NewMockStore(ctrl)
29+
scm := mock.NewMockSCM(ctrl)
30+
31+
t.Log("MOCK - Find the deployment_statistics by the environment.")
32+
store.
33+
EXPECT().
34+
FindDeploymentStatisticsOfRepoByEnv(gomock.Any(), gomock.Eq(input.repo), gomock.Eq(input.d.Env)).
35+
Return(nil, &ent.NotFoundError{})
36+
37+
t.Log("MOCK - Create a new statistics.")
38+
store.
39+
EXPECT().
40+
CreateDeploymentStatistics(gomock.Any(), gomock.AssignableToTypeOf(&ent.DeploymentStatistics{})).
41+
Return(&ent.DeploymentStatistics{ID: 1}, nil)
42+
43+
i := newMockInteractor(store, scm)
44+
45+
_, err := i.ProduceDeploymentStatisticsOfRepo(context.Background(), input.repo, input.d)
46+
if err != nil {
47+
t.Fatalf("ProduceDeploymentStatisticsOfRepo returns an error: %s", err)
48+
}
49+
})
50+
}

internal/interactor/interface.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ type (
5656

5757
CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
5858

59-
ListAllDeploymentStatisticss(ctx context.Context) ([]*ent.DeploymentStatistics, error)
60-
ListDeploymentStatisticssGreaterThanTime(ctx context.Context, updated time.Time) ([]*ent.DeploymentStatistics, error)
59+
FindDeploymentStatisticsOfRepoByEnv(ctx context.Context, r *ent.Repo, env string) (*ent.DeploymentStatistics, error)
60+
CreateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)
61+
UpdateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)
62+
63+
ListAllDeploymentStatistics(ctx context.Context) ([]*ent.DeploymentStatistics, error)
64+
ListDeploymentStatisticsGreaterThanTime(ctx context.Context, updated time.Time) ([]*ent.DeploymentStatistics, error)
6165

6266
CreateCallback(ctx context.Context, cb *ent.Callback) (*ent.Callback, error)
6367
FindCallbackByHash(ctx context.Context, hash string) (*ent.Callback, error)

internal/interactor/mock/pkg.go

Lines changed: 57 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/store/deployment.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import (
77
"entgo.io/ent/dialect/sql"
88
"github.com/gitploy-io/gitploy/ent"
99
"github.com/gitploy-io/gitploy/ent/deployment"
10-
"github.com/gitploy-io/gitploy/ent/deploymentstatistics"
1110
"github.com/gitploy-io/gitploy/ent/perm"
1211
"github.com/gitploy-io/gitploy/ent/predicate"
13-
"go.uber.org/zap"
1412
)
1513

1614
func (s *Store) SearchDeployments(ctx context.Context, u *ent.User, ss []deployment.Status, owned bool, from time.Time, to time.Time, page, perPage int) ([]*ent.Deployment, error) {
@@ -239,47 +237,5 @@ func (s *Store) UpdateDeployment(ctx context.Context, d *ent.Deployment) (*ent.D
239237
return nil, err
240238
}
241239

242-
if d.Status != deployment.StatusSuccess {
243-
return d, nil
244-
}
245-
246-
// Update the statistics of deployment by
247-
// increasing the count.
248-
err = s.WithTx(ctx, func(tx *ent.Tx) error {
249-
r, err := tx.Repo.Get(ctx, d.RepoID)
250-
if err != nil {
251-
return err
252-
}
253-
254-
dc, err := s.c.DeploymentStatistics.
255-
Query().
256-
Where(
257-
deploymentstatistics.NamespaceEQ(r.Namespace),
258-
deploymentstatistics.NameEQ(r.Name),
259-
deploymentstatistics.EnvEQ(d.Env),
260-
).
261-
Only(ctx)
262-
if ent.IsNotFound(err) {
263-
s.c.DeploymentStatistics.
264-
Create().
265-
SetNamespace(r.Namespace).
266-
SetName(r.Name).
267-
SetEnv(d.Env).
268-
Save(ctx)
269-
return nil
270-
} else if err != nil {
271-
return err
272-
}
273-
274-
s.c.DeploymentStatistics.
275-
UpdateOne(dc).
276-
SetCount(dc.Count + 1).
277-
Save(ctx)
278-
return nil
279-
})
280-
if err != nil {
281-
zap.L().Error("It has failed to increase the deployment count.", zap.Error(err))
282-
}
283-
284240
return d, nil
285241
}

internal/pkg/store/deployment_test.go

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -426,99 +426,4 @@ func TestStore_UpdateDeployment(t *testing.T) {
426426
t.Fatalf("UpdateDeployment = %v, wanted %v", d.Status, expected)
427427
}
428428
})
429-
430-
t.Run("Add a new statistics when the status of deployment is updated.", func(t *testing.T) {
431-
ctx := context.Background()
432-
433-
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1",
434-
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
435-
)
436-
defer client.Close()
437-
438-
client.Repo.
439-
Create().
440-
SetID(1).
441-
SetNamespace("octocat").
442-
SetName("Hello").
443-
SetDescription("").
444-
SaveX(ctx)
445-
446-
d := client.Deployment.Create().
447-
SetType(deployment.TypeBranch).
448-
SetNumber(1).
449-
SetType("branch").
450-
SetRef("main").
451-
SetEnv("prod").
452-
SetStatus(deployment.StatusCreated).
453-
SetUserID(1).
454-
SetRepoID(1).
455-
SaveX(ctx)
456-
457-
s := NewStore(client)
458-
459-
d.Status = deployment.StatusSuccess
460-
461-
_, err := s.UpdateDeployment(ctx, d)
462-
if err != nil {
463-
t.Fatalf("UpdateDeployment returns an error: %s", err)
464-
}
465-
466-
expected := 1
467-
dc := client.DeploymentStatistics.GetX(ctx, 1)
468-
469-
if dc.Count != expected {
470-
t.Fatalf("The statistics was not created.")
471-
}
472-
})
473-
474-
t.Run("Update the statistics is updated when the status of deployment is updated.", func(t *testing.T) {
475-
ctx := context.Background()
476-
477-
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1",
478-
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
479-
)
480-
defer client.Close()
481-
482-
client.Repo.
483-
Create().
484-
SetID(1).
485-
SetNamespace("octocat").
486-
SetName("Hello").
487-
SetDescription("").
488-
SaveX(ctx)
489-
490-
d := client.Deployment.Create().
491-
SetType(deployment.TypeBranch).
492-
SetNumber(1).
493-
SetType("branch").
494-
SetRef("main").
495-
SetEnv("prod").
496-
SetStatus(deployment.StatusCreated).
497-
SetUserID(1).
498-
SetRepoID(1).
499-
SaveX(ctx)
500-
501-
t.Log("Add the deployment count.")
502-
client.DeploymentStatistics.Create().
503-
SetNamespace("octocat").
504-
SetName("Hello").
505-
SetEnv("prod").
506-
SaveX(ctx)
507-
508-
s := NewStore(client)
509-
510-
d.Status = deployment.StatusSuccess
511-
512-
_, err := s.UpdateDeployment(ctx, d)
513-
if err != nil {
514-
t.Fatalf("UpdateDeployment returns an error: %s", err)
515-
}
516-
517-
expected := 2
518-
dc := client.DeploymentStatistics.GetX(ctx, 1)
519-
520-
if dc.Count != expected {
521-
t.Fatalf("The statistics was not created.")
522-
}
523-
})
524429
}

internal/pkg/store/deploymentcount.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)