Skip to content

Commit db71cae

Browse files
committed
new workflows query
1 parent 45ec9bc commit db71cae

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

pkg/api/get_record.go

+10-18
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,14 @@ func (h APIHandler) workflows(c *gin.Context) {
130130
UpdatedAt time.Time `json:"updated_at"`
131131
}
132132

133-
query := workflows(h.DB).
134-
Limit(l).
135-
Offset(n).
136-
Order("tfo_resources.updated_at DESC")
137-
133+
name := ""
134+
namespace := ""
135+
clusterName := ""
138136
if matchAny != "" {
139137
m := fmt.Sprintf("%%%s%%", matchAny)
140-
name := m
141-
namespace := m
142-
clusterName := m
138+
name = m
139+
namespace = m
140+
clusterName = m
143141

144142
if strings.Contains(matchAny, "=") {
145143
name = "%"
@@ -153,25 +151,19 @@ func (h APIHandler) workflows(c *gin.Context) {
153151
key := columnQuery[0]
154152
value := columnQuery[1]
155153
if key == "name" {
156-
query.Where("tfo_resources.name LIKE ?", fmt.Sprintf("%%%s%%", value))
154+
name = value
157155
}
158156
if key == "namespace" {
159-
query.Where("tfo_resources.namespace LIKE ?", fmt.Sprintf("%%%s%%", value))
157+
namespace = value
160158
}
161159
if strings.HasPrefix(key, "cluster") {
162-
query.Where("clusters.name LIKE ?", fmt.Sprintf("%%%s%%", value))
160+
clusterName = value
163161
}
164162
}
165-
} else {
166-
query.Where("(tfo_resources.name LIKE ? or tfo_resources.namespace LIKE ? or clusters.name LIKE ?)",
167-
name,
168-
namespace,
169-
clusterName,
170-
)
171163
}
172164
}
173165

174-
query.Debug().Scan(&result)
166+
workflows(h.DB, name, namespace, clusterName, n, l).Scan(&result)
175167

176168
c.JSON(http.StatusOK, response(http.StatusOK, "", result))
177169
}

pkg/api/metrics.go

+35-22
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,50 @@ func workflow(db *gorm.DB, clusterName uint, namespace, name string) *gorm.DB {
8383
Where("tfo_resources.deleted_at is null and tfo_resources.cluster_id = ? and tfo_resources.namespace = ? and tfo_resources.name = ?", clusterName, namespace, name)
8484
}
8585

86-
func workflows(db *gorm.DB) *gorm.DB {
87-
return db.Debug().Table("tfo_resources").
88-
Select(`
86+
func workflows(db *gorm.DB, name, namespace, clusterName string, offset, limit int) *gorm.DB {
87+
queryString := fmt.Sprintf(`
88+
SELECT
8989
tfo_resources.uuid,
9090
tfo_resources.current_generation,
9191
tfo_resources.name,
9292
tfo_resources.namespace,
9393
tfo_resources.current_state,
94-
tfo_resources.updated_at,
9594
tfo_resources.created_at,
96-
clusters.name AS cluster_name
97-
`).
98-
Joins("JOIN clusters ON tfo_resources.cluster_id = clusters.id").
99-
Where("tfo_resources.deleted_at is null")
95+
clusters.name as cluster_name,
96+
tfo_resources.updated_at as resource_updated_at,
97+
logs.updated_at as updated_at
98+
FROM tfo_resources
99+
LEFT JOIN (
100+
SELECT task_pods.tfo_resource_uuid, MAX(tfo_task_logs.updated_at) as updated_at
101+
FROM tfo_task_logs
102+
JOIN task_pods on task_pods.uuid = tfo_task_logs.task_pod_uuid
103+
WHERE tfo_task_logs.updated_at IS NOT NULL
104+
GROUP BY task_pods.tfo_resource_uuid
105+
) logs ON logs.tfo_resource_uuid = tfo_resources.uuid
106+
JOIN clusters ON clusters.id = tfo_resources.cluster_id
107+
WHERE tfo_resources.deleted_at IS NULL
108+
AND tfo_resources.name LIKE '%%%s%%'
109+
AND tfo_resources.namespace LIKE '%%%s%%'
110+
AND clusters.name LIKE '%%%s%%'
111+
ORDER BY logs.updated_at DESC NULLS LAST
112+
OFFSET %d
113+
LIMIT %d
114+
`, name, namespace, clusterName, offset, limit)
115+
116+
return db.Raw(queryString)
100117
}
101118

102119
func (h APIHandler) TotalResources(c *gin.Context) {
103-
query := workflows(h.DB)
104120

121+
name := ""
122+
namespace := ""
123+
clusterName := ""
105124
matchAny, _ := c.GetQuery("matchAny")
106125
if matchAny != "" {
107126
m := fmt.Sprintf("%%%s%%", matchAny)
108-
name := m
109-
namespace := m
110-
clusterName := m
127+
name = m
128+
namespace = m
129+
clusterName = m
111130

112131
if strings.Contains(matchAny, "=") {
113132
name = "%"
@@ -121,26 +140,20 @@ func (h APIHandler) TotalResources(c *gin.Context) {
121140
key := columnQuery[0]
122141
value := columnQuery[1]
123142
if key == "name" {
124-
query.Where("tfo_resources.name LIKE ?", fmt.Sprintf("%%%s%%", value))
143+
name = value
125144
}
126145
if key == "namespace" {
127-
query.Where("tfo_resources.namespace LIKE ?", fmt.Sprintf("%%%s%%", value))
146+
namespace = value
128147
}
129148
if strings.HasPrefix(key, "cluster") {
130-
query.Where("clusters.name LIKE ?", fmt.Sprintf("%%%s%%", value))
149+
clusterName = value
131150
}
132151
}
133-
} else {
134-
query.Where("(tfo_resources.name LIKE ? or tfo_resources.namespace LIKE ? or clusters.name LIKE ?)",
135-
name,
136-
namespace,
137-
clusterName,
138-
)
139152
}
140153
}
141154

142155
var count int64
143-
query.Count(&count)
156+
workflows(h.DB, name, namespace, clusterName, 0, 1000000).Count(&count)
144157
c.JSON(http.StatusOK, response(http.StatusOK, "", []int64{count}))
145158
}
146159

0 commit comments

Comments
 (0)