···170170171171// this is a mega query, but the most useful one:
172172// get N pipelines, for each one get the latest status of its N workflows
173173+//
174174+// the pipelines table is aliased to `p`
175175+// the triggers table is aliased to `t`
173176func GetPipelineStatuses(e Execer, limit int, filters ...orm.Filter) ([]models.Pipeline, error) {
174177 var conditions []string
175178 var args []any
176179 for _, filter := range filters {
177177- filter.Key = "p." + filter.Key // the table is aliased in the query to `p`
178180 conditions = append(conditions, filter.Condition())
179181 args = append(args, filter.Arg()...)
180182 }
···366368367369 return all, nil
368370}
371371+372372+// the pipelines table is aliased to `p`
373373+// the triggers table is aliased to `t`
374374+func GetTotalPipelineStatuses(e Execer, filters ...orm.Filter) (int64, error) {
375375+ var conditions []string
376376+ var args []any
377377+ for _, filter := range filters {
378378+ conditions = append(conditions, filter.Condition())
379379+ args = append(args, filter.Arg()...)
380380+ }
381381+382382+ whereClause := ""
383383+ if conditions != nil {
384384+ whereClause = " where " + strings.Join(conditions, " and ")
385385+ }
386386+387387+ query := fmt.Sprintf(`
388388+ select
389389+ count(1)
390390+ from
391391+ pipelines p
392392+ join
393393+ triggers t ON p.trigger_id = t.id
394394+ %s
395395+ `, whereClause)
396396+397397+ rows, err := e.Query(query, args...)
398398+ if err != nil {
399399+ return 0, err
400400+ }
401401+ defer rows.Close()
402402+403403+ for rows.Next() {
404404+ var count int64
405405+ err := rows.Scan(&count)
406406+ if err != nil {
407407+ return 0, err
408408+ }
409409+410410+ return count, nil
411411+ }
412412+413413+ // unreachable
414414+ return 0, nil
415415+}