···170171// this is a mega query, but the most useful one:172// get N pipelines, for each one get the latest status of its N workflows000173func GetPipelineStatuses(e Execer, limit int, filters ...orm.Filter) ([]models.Pipeline, error) {174 var conditions []string175 var args []any176 for _, filter := range filters {177- filter.Key = "p." + filter.Key // the table is aliased in the query to `p`178 conditions = append(conditions, filter.Condition())179 args = append(args, filter.Arg()...)180 }···367 })368369 return all, nil000000000000000000000000000000000000000000000370}
···170171// this is a mega query, but the most useful one:172// get N pipelines, for each one get the latest status of its N workflows173+//174+// the pipelines table is aliased to `p`175+// the triggers table is aliased to `t`176func GetPipelineStatuses(e Execer, limit int, filters ...orm.Filter) ([]models.Pipeline, error) {177 var conditions []string178 var args []any179 for _, filter := range filters {0180 conditions = append(conditions, filter.Condition())181 args = append(args, filter.Arg()...)182 }···365 })366367 return all, nil368+}369+370+// the pipelines table is aliased to `p`371+// the triggers table is aliased to `t`372+func GetTotalPipelineStatuses(e Execer, filters ...orm.Filter) (int64, error) {373+ var conditions []string374+ var args []any375+ for _, filter := range filters {376+ conditions = append(conditions, filter.Condition())377+ args = append(args, filter.Arg()...)378+ }379+380+ whereClause := ""381+ if conditions != nil {382+ whereClause = " where " + strings.Join(conditions, " and ")383+ }384+385+ query := fmt.Sprintf(`386+ select387+ count(1)388+ from389+ pipelines p390+ join391+ triggers t ON p.trigger_id = t.id392+ %s393+ `, whereClause)394+395+ rows, err := e.Query(query, args...)396+ if err != nil {397+ return 0, err398+ }399+ defer rows.Close()400+401+ for rows.Next() {402+ var count int64403+ err := rows.Scan(&count)404+ if err != nil {405+ return 0, err406+ }407+408+ return count, nil409+ }410+411+ // unreachable412+ return 0, nil413}