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

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/go-errors/errors"
     8  	"github.com/jackc/pgconn"
     9  	"github.com/jackc/pgx/v4"
    10  	"github.com/spf13/afero"
    11  	"github.com/supabase/cli/internal/inspect"
    12  	"github.com/supabase/cli/internal/migration/list"
    13  	"github.com/supabase/cli/internal/utils"
    14  	"github.com/supabase/cli/internal/utils/pgxv5"
    15  )
    16  
    17  type Result struct {
    18  	Name  string
    19  	Ratio float64
    20  }
    21  
    22  func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
    23  	conn, err := utils.ConnectByConfig(ctx, config, options...)
    24  	if err != nil {
    25  		return err
    26  	}
    27  	defer conn.Close(context.Background())
    28  	rows, err := conn.Query(ctx, inspect.CACHE_QUERY)
    29  	if err != nil {
    30  		return errors.Errorf("failed to query rows: %w", err)
    31  	}
    32  	result, err := pgxv5.CollectRows[Result](rows)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	// TODO: implement a markdown table marshaller
    37  	table := "|Name|Ratio|OK?|Explanation|\n|-|-|-|-|\n"
    38  	for _, r := range result {
    39  		ok := "Yup!"
    40  		if r.Ratio < 0.94 {
    41  			ok = "Maybe not..."
    42  		}
    43  		var explanation string
    44  		if r.Name == "index hit rate" {
    45  			explanation = "This is the ratio of index hits to index scans. If this ratio is low, it means that the database is not using indexes effectively. Check the `index-usage` command for more info."
    46  		} else if r.Name == "table hit rate" {
    47  			explanation = "This is the ratio of table hits to table scans. If this ratio is low, it means that your queries are not finding the data effectively. Check your query performance and it might be worth increasing your compute."
    48  		}
    49  		table += fmt.Sprintf("|`%s`|`%.6f`|`%s`|`%s`|\n", r.Name, r.Ratio, ok, explanation)
    50  	}
    51  	return list.RenderTable(table)
    52  }