Skip to content

Commit 84ff3cb

Browse files
committed
refactor(filters): use dry principle
Signed-off-by: Amr-Shams <amr.shams2015.as@gmail.com>
1 parent e157eea commit 84ff3cb

File tree

2 files changed

+46
-70
lines changed

2 files changed

+46
-70
lines changed

models/meshmodel/registry/v1alpha3/relationship_summary.go

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -115,49 +115,32 @@ func (relationshipFilter *RelationshipSummaryFilter) GetSummary(db *database.Han
115115
return slices.Contains(relationshipFilter.Include, dim)
116116
}
117117

118-
if shouldCompute(RelationshipSummaryByModel) {
119-
var rows []RelationshipGroupEntry
120-
err := base.Session(&gorm.Session{}).
121-
Select("model_dbs.name as Key, COUNT(DISTINCT(relationship_definition_dbs.id)) as Count").
122-
Group("model_dbs.name").
123-
Scan(&rows).Error
124-
if err != nil {
125-
return nil, err
126-
}
127-
summary.ByModel = rows
128-
}
129-
if shouldCompute(RelationshipSummaryByKind) {
130-
var rows []RelationshipGroupEntry
131-
err := base.Session(&gorm.Session{}).
132-
Select("relationship_definition_dbs.kind as Key, COUNT(DISTINCT(relationship_definition_dbs.id)) as Count").
133-
Group("relationship_definition_dbs.kind").
134-
Scan(&rows).Error
135-
if err != nil {
136-
return nil, err
137-
}
138-
summary.ByKind = rows
118+
type dimensionInfo struct {
119+
dim RelationshipSummaryDimension
120+
selectExpr string
121+
groupExpr string
122+
receiver *[]RelationshipGroupEntry
139123
}
140-
if shouldCompute(RelationshipSummaryByType) {
141-
var rows []RelationshipGroupEntry
142-
err := base.Session(&gorm.Session{}).
143-
Select("relationship_definition_dbs.type as Key, COUNT(DISTINCT(relationship_definition_dbs.id)) as Count").
144-
Group("relationship_definition_dbs.type").
145-
Scan(&rows).Error
146-
if err != nil {
147-
return nil, err
148-
}
149-
summary.ByType = rows
124+
125+
dimensions := []dimensionInfo{
126+
{RelationshipSummaryByModel, "model_dbs.name as Key, COUNT(DISTINCT(relationship_definition_dbs.id)) as Count", "model_dbs.name", &summary.ByModel},
127+
{RelationshipSummaryByKind, "relationship_definition_dbs.kind as Key, COUNT(DISTINCT(relationship_definition_dbs.id)) as Count", "relationship_definition_dbs.kind", &summary.ByKind},
128+
{RelationshipSummaryByType, "relationship_definition_dbs.type as Key, COUNT(DISTINCT(relationship_definition_dbs.id)) as Count", "relationship_definition_dbs.type", &summary.ByType},
129+
{RelationshipSummaryBySubType, "relationship_definition_dbs.sub_type as Key, COUNT(DISTINCT(relationship_definition_dbs.id)) as Count", "relationship_definition_dbs.sub_type", &summary.BySubType},
150130
}
151-
if shouldCompute(RelationshipSummaryBySubType) {
152-
var rows []RelationshipGroupEntry
153-
err := base.Session(&gorm.Session{}).
154-
Select("relationship_definition_dbs.sub_type as Key, COUNT(DISTINCT(relationship_definition_dbs.id)) as Count").
155-
Group("relationship_definition_dbs.sub_type").
156-
Scan(&rows).Error
157-
if err != nil {
158-
return nil, err
131+
132+
for _, d := range dimensions {
133+
if shouldCompute(d.dim) {
134+
var rows []RelationshipGroupEntry
135+
err := base.Session(&gorm.Session{}).
136+
Select(d.selectExpr).
137+
Group(d.groupExpr).
138+
Scan(&rows).Error
139+
if err != nil {
140+
return nil, err
141+
}
142+
*d.receiver = rows
159143
}
160-
summary.BySubType = rows
161144
}
162145
return summary, nil
163146
}

models/meshmodel/registry/v1beta1/component_summary.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -106,39 +106,32 @@ func (componentFilter *ComponentSummaryFilter) GetSummary(db *database.Handler)
106106
}
107107
return slices.Contains(componentFilter.Include, dim)
108108
}
109-
// partial error is not tolerated so the populated summary should all be correct
110-
if shouldCompute(ComponentSummaryByModel) {
111-
var rows []ComponentGroupEntry
112-
err := base.Session(&gorm.Session{}).
113-
Select("model_dbs.name as key, COUNT(DISTINCT component_definition_dbs.id) as count").
114-
Group("model_dbs.name").
115-
Scan(&rows).Error
116-
if err != nil {
117-
return nil, err
118-
}
119-
summary.ByModel = rows
109+
type dimensionInfo struct {
110+
dim ComponentSummaryDimension
111+
selectExpr string
112+
groupExpr string
113+
receiver *[]ComponentGroupEntry
120114
}
121-
if shouldCompute(ComponentSummaryByCategory) {
122-
var rows []ComponentGroupEntry
123-
err := base.Session(&gorm.Session{}).
124-
Select("category_dbs.name as key, COUNT(DISTINCT component_definition_dbs.id) as count").
125-
Group("category_dbs.name").
126-
Scan(&rows).Error
127-
if err != nil {
128-
return nil, err
129-
}
130-
summary.ByCategory = rows
115+
116+
dimensions := []dimensionInfo{
117+
{ComponentSummaryByModel, "model_dbs.name as Key, COUNT(DISTINCT(component_definition_dbs.id)) as Count", "model_dbs.name", &summary.ByModel},
118+
{ComponentSummaryByCategory, "model_dbs.category_id as Key, COUNT(DISTINCT(component_definition_dbs.id)) as Count", "model_dbs.category_id", &summary.ByCategory},
119+
{ComponentSummaryByRegistrant, "connections.name as Key, COUNT(DISTINCT(component_definition_dbs.id)) as Count", "connections.name", &summary.ByRegistrant},
131120
}
132-
if shouldCompute(ComponentSummaryByRegistrant) {
133-
var rows []ComponentGroupEntry
134-
err := base.Session(&gorm.Session{}).
135-
Select("connections.name as key, COUNT(DISTINCT component_definition_dbs.id) as count").
136-
Group("connections.name").
137-
Scan(&rows).Error
138-
if err != nil {
139-
return nil, err
121+
122+
// partial error is not tolerated so the populated summary should all be correct
123+
for _, d := range dimensions {
124+
if shouldCompute(d.dim) {
125+
var rows []ComponentGroupEntry
126+
err := base.Session(&gorm.Session{}).
127+
Select(d.selectExpr).
128+
Group(d.groupExpr).
129+
Scan(&rows).Error
130+
if err != nil {
131+
return nil, err
132+
}
133+
*d.receiver = rows
140134
}
141-
summary.ByRegistrant = rows
142135
}
143136

144137
return summary, nil

0 commit comments

Comments
 (0)