@@ -83,31 +83,50 @@ func workflow(db *gorm.DB, clusterName uint, namespace, name string) *gorm.DB {
83
83
Where ("tfo_resources.deleted_at is null and tfo_resources.cluster_id = ? and tfo_resources.namespace = ? and tfo_resources.name = ?" , clusterName , namespace , name )
84
84
}
85
85
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
89
89
tfo_resources.uuid,
90
90
tfo_resources.current_generation,
91
91
tfo_resources.name,
92
92
tfo_resources.namespace,
93
93
tfo_resources.current_state,
94
- tfo_resources.updated_at,
95
94
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 )
100
117
}
101
118
102
119
func (h APIHandler ) TotalResources (c * gin.Context ) {
103
- query := workflows (h .DB )
104
120
121
+ name := ""
122
+ namespace := ""
123
+ clusterName := ""
105
124
matchAny , _ := c .GetQuery ("matchAny" )
106
125
if matchAny != "" {
107
126
m := fmt .Sprintf ("%%%s%%" , matchAny )
108
- name : = m
109
- namespace : = m
110
- clusterName : = m
127
+ name = m
128
+ namespace = m
129
+ clusterName = m
111
130
112
131
if strings .Contains (matchAny , "=" ) {
113
132
name = "%"
@@ -121,26 +140,20 @@ func (h APIHandler) TotalResources(c *gin.Context) {
121
140
key := columnQuery [0 ]
122
141
value := columnQuery [1 ]
123
142
if key == "name" {
124
- query . Where ( "tfo_resources. name LIKE ?" , fmt . Sprintf ( "%%%s%%" , value ))
143
+ name = value
125
144
}
126
145
if key == "namespace" {
127
- query . Where ( "tfo_resources. namespace LIKE ?" , fmt . Sprintf ( "%%%s%%" , value ))
146
+ namespace = value
128
147
}
129
148
if strings .HasPrefix (key , "cluster" ) {
130
- query . Where ( "clusters.name LIKE ?" , fmt . Sprintf ( "%%%s%%" , value ))
149
+ clusterName = value
131
150
}
132
151
}
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
- )
139
152
}
140
153
}
141
154
142
155
var count int64
143
- query .Count (& count )
156
+ workflows ( h . DB , name , namespace , clusterName , 0 , 1000000 ) .Count (& count )
144
157
c .JSON (http .StatusOK , response (http .StatusOK , "" , []int64 {count }))
145
158
}
146
159
0 commit comments