github.com/supabase/cli@v1.168.1/internal/inspect/calls/calls.go (about)

     1  package calls
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"regexp"
     7  
     8  	"github.com/go-errors/errors"
     9  	"github.com/jackc/pgconn"
    10  	"github.com/jackc/pgx/v4"
    11  	"github.com/spf13/afero"
    12  	"github.com/supabase/cli/internal/inspect"
    13  	"github.com/supabase/cli/internal/migration/list"
    14  	"github.com/supabase/cli/internal/utils"
    15  	"github.com/supabase/cli/internal/utils/pgxv5"
    16  )
    17  
    18  type Result struct {
    19  	Total_exec_time string
    20  	Prop_exec_time  string
    21  	Ncalls          string
    22  	Sync_io_time    string
    23  	Query           string
    24  }
    25  
    26  func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
    27  	conn, err := utils.ConnectByConfig(ctx, config, options...)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	rows, err := conn.Query(ctx, inspect.CALLS_QUERY)
    32  	if err != nil {
    33  		return errors.Errorf("failed to query rows: %w", err)
    34  	}
    35  	result, err := pgxv5.CollectRows[Result](rows)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	// TODO: implement a markdown table marshaller
    40  	table := "|Query|Total Execution Time|Proportion of total exec time|Number Calls|Sync IO time|\n|-|-|-|-|-|\n"
    41  	for _, r := range result {
    42  		// remove whitespace from query
    43  		re := regexp.MustCompile(`\s+|\r+|\n+|\t+|\v`)
    44  		query := re.ReplaceAllString(r.Query, " ")
    45  
    46  		// escape pipes in query
    47  		re = regexp.MustCompile(`\|`)
    48  		query = re.ReplaceAllString(query, `\|`)
    49  		table += fmt.Sprintf("|`%s`|`%s`|`%s`|`%s`|`%s`|\n", query, r.Total_exec_time, r.Prop_exec_time, r.Ncalls, r.Sync_io_time)
    50  	}
    51  	return list.RenderTable(table)
    52  }