Skip to content

Commit

Permalink
fixed GoReport recommendations
Browse files Browse the repository at this point in the history
top: fix wrong masks handling when cancel/terminate group of backends
fixed cyclomatic complexity in functions
added proper comments for variables and methods
done code formatting
  • Loading branch information
lesovsky committed Feb 9, 2019
1 parent 201a688 commit f6de7d4
Show file tree
Hide file tree
Showing 41 changed files with 818 additions and 834 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"
)

// Root describes the CLI command of main profram
var Root = &cobra.Command{
Use: ProgramName,
Short: "Admin tool for PostgreSQL",
Expand Down
7 changes: 4 additions & 3 deletions cmd/config/config.go
Expand Up @@ -15,12 +15,13 @@ var (
cfg config.Config
)

// CommandDefinition is the definition of 'config' CLI sub-command
var CommandDefinition = &cobra.Command{
Use: "config",
Short: "configures Postgres to work with pgcenter",
Long: `'pgcenter config' configures Postgres to work with pgcenter`,
Version: "dummy", // use constants from 'cmd' package
PreRun: preFlightSetup,
PreRun: preFlightSetup,
Run: func(command *cobra.Command, args []string) {
config.RunMain(args, conn, cfg)
},
Expand All @@ -35,8 +36,8 @@ func init() {
CommandDefinition.Flags().BoolVarP(&cfg.Uninstall, "uninstall", "u", false, "uninstall stats schema from the database")
}

// Checks passed options
func preFlightSetup(CommandDefinition *cobra.Command, _ []string) {
// preFlightSetup performs sanity checks of passed options
func preFlightSetup(CommandDefinition *cobra.Command, _ []string) {
if !cfg.Install && !cfg.Uninstall {
fmt.Printf("ERROR: using '--install' or '--uninstall' options are mandatory\n")
_ = CommandDefinition.Help()
Expand Down
2 changes: 1 addition & 1 deletion cmd/help.go
Expand Up @@ -5,10 +5,10 @@ package cmd
import (
"fmt"
"github.com/lesovsky/pgcenter/cmd/config"
profile "github.com/lesovsky/pgcenter/cmd/profile"
record "github.com/lesovsky/pgcenter/cmd/record"
report "github.com/lesovsky/pgcenter/cmd/report"
top "github.com/lesovsky/pgcenter/cmd/top"
profile "github.com/lesovsky/pgcenter/cmd/profile"
)

const (
Expand Down
5 changes: 3 additions & 2 deletions cmd/profile/profile.go
Expand Up @@ -10,11 +10,12 @@ import (
)

var (
conn utils.Conninfo
opts profile.TraceOptions
conn utils.Conninfo
opts profile.TraceOptions
frequency int
)

// CommandDefinition is the definition of 'profile' CLI sub-command
var CommandDefinition = &cobra.Command{
Use: "profile",
Short: "wait events profiler",
Expand Down
1 change: 1 addition & 0 deletions cmd/record/record.go
Expand Up @@ -18,6 +18,7 @@ var (
opts record.RecordOptions
oneshot bool

// CommandDefinition is the definition of 'record' CLI sub-command
CommandDefinition = &cobra.Command{
Use: "record",
Short: "record stats to file",
Expand Down
181 changes: 84 additions & 97 deletions cmd/report/report.go
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/lesovsky/pgcenter/lib/stat"
"github.com/lesovsky/pgcenter/report"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"log"
"os"
"regexp"
"strings"
Expand All @@ -19,6 +21,18 @@ const (
)

var (
// CommandDefinition is the definition of 'report' CLI sub-command
CommandDefinition = &cobra.Command{
Use: "report",
Short: "make report based on previously saved statistics",
Long: `'pgcenter report' reads statistics from file and prints reports.`,
Version: "dummy", // use constants from 'cmd' package
PreRun: preFlightSetup,
Run: func(command *cobra.Command, args []string) {
report.RunMain(args, opts)
},
}

opts report.ReportOptions // Settings for the report program
tsStart, tsEnd string // Show stats within an interval
doFilter string // Perform filtering
Expand All @@ -34,15 +48,31 @@ var (
showSizes bool // Show tables sizes
describe bool // Show description of requested stats view

CommandDefinition = &cobra.Command{
Use: "report",
Short: "make report based on previously saved statistics",
Long: `'pgcenter report' reads statistics from file and prints reports.`,
Version: "dummy", // use constants from 'cmd' package
PreRun: preFlightSetup,
Run: func(command *cobra.Command, args []string) {
report.RunMain(args, opts)
},
// basicReports is the reports available for user's choice
basicReports = map[string]struct {
view string
ctx stat.ContextUnit
}{
"activity": {view: stat.ActivityView, ctx: stat.PgStatActivityUnit},
"sizes": {view: stat.SizesView, ctx: stat.PgTablesSizesUnit},
"databases": {view: stat.DatabaseView, ctx: stat.PgStatDatabaseUnit},
"functions": {view: stat.FunctionsView, ctx: stat.PgStatFunctionsUnit},
"replication": {view: stat.ReplicationView, ctx: stat.PgStatReplicationUnit},
"tables": {view: stat.TablesView, ctx: stat.PgStatTablesUnit},
"indexes": {view: stat.IndexesView, ctx: stat.PgStatIndexesUnit},
"vacuum": {view: stat.VacuumView, ctx: stat.PgStatVacuumUnit},
"statements": {view: "_STATEMENTS_"},
}
// statementsReports is the statements reports available for user's choice
statementsReports = map[string]struct {
view string
ctx stat.ContextUnit
}{
"m": {view: stat.StatementsTimingView, ctx: stat.PgSSTimingUnit},
"g": {view: stat.StatementsGeneralView, ctx: stat.PgSSGeneralUnit},
"i": {view: stat.StatementsIOView, ctx: stat.PgSSIoUnit},
"t": {view: stat.StatementsTempView, ctx: stat.PgSSTempUnit},
"l": {view: stat.StatementsLocalView, ctx: stat.PgSSLocalUnit},
}
)

Expand All @@ -67,10 +97,15 @@ func init() {
CommandDefinition.Flags().DurationVarP(&opts.Interval, "interval", "i", 1*time.Second, "delta interval (default: 1s)")
}

// Analyze startup parameters and prepare settings for report program
func preFlightSetup(_ *cobra.Command, _ []string) {
// preFlightSetup analyzes startup parameters and prepares settings for report program
func preFlightSetup(c *cobra.Command, _ []string) {
// select appropriate report and context with settings
selectReport()
c.Flags().Visit(selectReport)

// check the report is selected
if opts.ReportType == "" {
log.Fatalln("ERROR: report not selected, quit")
}

// if user asks to describe a stat view, show a description and exit
if describe {
Expand All @@ -88,7 +123,22 @@ func preFlightSetup(_ *cobra.Command, _ []string) {
parseFilterString()
}

// Setup start and end times for report, don't show stats before start time and after end time
// selectReport selects appropriate type of the report depending on user's choice
func selectReport(f *pflag.Flag) {
if b, ok := basicReports[f.Name]; ok {
if b.view == "_STATEMENTS_" {
if s, ok := statementsReports[f.Value.String()]; ok {
opts.ReportType = s.view
opts.Context = s.ctx
return
}
}
opts.ReportType = b.view
opts.Context = b.ctx
}
}

// checkStartEndTimestampsetup examines start and end times for report, don't show stats before start time and after end time
func checkStartEndTimestamps() {
var err error
var layout = "20060102-150405" // default layout includes date and time
Expand Down Expand Up @@ -142,7 +192,7 @@ func checkStartEndTimestamps() {
}
}

// Setup filtering options. Split a value entered by user to column name and filter pattern.
// parseFilterString parses and defines filtering options. Split a value entered by user to column name and filter pattern.
func parseFilterString() {
if doFilter != "" {
var err error
Expand All @@ -163,90 +213,27 @@ func parseFilterString() {
}
}

// Select appropriate type of the report
func selectReport() {
switch {
case showDatabases == true:
opts.ReportType = stat.DatabaseView
opts.Context = stat.PgStatDatabaseUnit
case showTables == true:
opts.ReportType = stat.TablesView
opts.Context = stat.PgStatDatabaseUnit
case showReplication == true:
opts.ReportType = stat.ReplicationView
opts.Context = stat.PgStatReplicationUnit
case showIndexes == true:
opts.ReportType = stat.IndexesView
opts.Context = stat.PgStatIndexesUnit
case showSizes == true:
opts.ReportType = stat.SizesView
opts.Context = stat.PgTablesSizesUnit
case showFunctions == true:
opts.ReportType = stat.FunctionsView
opts.Context = stat.PgStatFunctionsUnit
case showVacuum == true:
opts.ReportType = stat.VacuumView
opts.Context = stat.PgStatVacuumUnit
case showActivity == true:
opts.ReportType = stat.ActivityView
opts.Context = stat.PgStatActivityUnit
// doDescribe shows detailed description of the requested stats
func doDescribe() {
var m = map[string]string{
stat.DatabaseView: stat.PgStatDatabaseDescription,
stat.ActivityView: stat.PgStatActivityDescription,
stat.ReplicationView: stat.PgStatReplicationDescription,
stat.TablesView: stat.PgStatTablesDescription,
stat.IndexesView: stat.PgStatIndexesDescription,
stat.FunctionsView: stat.PgStatFunctionsDescription,
stat.SizesView: stat.PgStatSizesDescription,
stat.VacuumView: stat.PgStatVacuumDescription,
stat.StatementsTimingView: stat.PgStatStatementsTimingDescription,
stat.StatementsGeneralView: stat.PgStatStatementsGeneralDescription,
stat.StatementsIOView: stat.PgStatStatementsIODescription,
stat.StatementsTempView: stat.PgStatStatementsTempDescription,
stat.StatementsLocalView: stat.PgStatStatementsLocalDescription,
}

if showStatements != "" {
switch {
case showStatements == "m":
opts.ReportType = stat.StatementsTimingView
opts.Context = stat.PgSSTimingUnit
case showStatements == "g":
opts.ReportType = stat.StatementsGeneralView
opts.Context = stat.PgSSGeneralUnit
case showStatements == "i":
opts.ReportType = stat.StatementsIOView
opts.Context = stat.PgSSIoUnit
case showStatements == "t":
opts.ReportType = stat.StatementsTempView
opts.Context = stat.PgSSTempUnit
case showStatements == "l":
opts.ReportType = stat.StatementsLocalView
opts.Context = stat.PgSSLocalUnit
default:
fmt.Printf("WARNING: unknown modificator '%s', ignore...\n", showStatements)
}
}
}

// Show columns description of the used stats
func doDescribe() {
if describe {
switch opts.ReportType {
case stat.DatabaseView:
fmt.Println(stat.PgStatDatabaseDescription)
case stat.ActivityView:
fmt.Println(stat.PgStatActivityDescription)
case stat.ReplicationView:
fmt.Println(stat.PgStatReplicationDescription)
case stat.TablesView:
fmt.Println(stat.PgStatTablesDescription)
case stat.IndexesView:
fmt.Println(stat.PgStatIndexesDescription)
case stat.FunctionsView:
fmt.Println(stat.PgStatFunctionsDescription)
case stat.SizesView:
fmt.Println(stat.PgStatSizesDescription)
case stat.VacuumView:
fmt.Println(stat.PgStatVacuumDescription)
case stat.StatementsTimingView:
fmt.Println(stat.PgStatStatementsTimingDescription)
case stat.StatementsGeneralView:
fmt.Println(stat.PgStatStatementsGeneralDescription)
case stat.StatementsIOView:
fmt.Println(stat.PgStatStatementsIODescription)
case stat.StatementsTempView:
fmt.Println(stat.PgStatStatementsTempDescription)
case stat.StatementsLocalView:
fmt.Println(stat.PgStatStatementsLocalDescription)
default:
fmt.Println("Unknown description requested") // should not be here, but who knows...
}
if description, ok := m[opts.ReportType]; ok {
fmt.Println(description)
} else {
fmt.Println("Unknown description requested")
}
}
3 changes: 2 additions & 1 deletion cmd/top/top.go
Expand Up @@ -6,13 +6,14 @@ import (
//"github.com/lesovsky/pgcenter/cmd" /* code related with 'root command' handling */
"github.com/lesovsky/pgcenter/lib/utils"
"github.com/lesovsky/pgcenter/top" /* code related to 'pgcenter top' functionality */
"github.com/spf13/cobra" /* cli */
"github.com/spf13/cobra" /* cli */
)

var (
conn utils.Conninfo
)

// CommandDefinition is the definition of 'top' CLI sub-command
var CommandDefinition = &cobra.Command{
Use: "top",
Short: "top-like stats viewer",
Expand Down
11 changes: 8 additions & 3 deletions cmd/version.go
Expand Up @@ -7,12 +7,17 @@ import (
)

const (
ProgramName = "pgcenter"
ProgramVersion = "0.5"
ProgramRelease = "0"
// ProgramName is the name of this program
ProgramName = "pgcenter"
// ProgramVersion is the version of this program
ProgramVersion = "0.5"
// ProgramRelease is release number of this program
ProgramRelease = "0"
// ProgramIssuesUrl is the public URL for posting issues, bug reports and asking questions
ProgramIssuesUrl = "https://github.com/lesovsky/pgcenter/issues"
)

// PrintVersion prints the name and version of this program
func PrintVersion() string {
return fmt.Sprintf("%s %s.%s\n", ProgramName, ProgramVersion, ProgramRelease)
}
3 changes: 2 additions & 1 deletion config/config.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lib/pq"
)

// Config describes actions which can be performed against pgcenter's config
type Config struct {
Install bool
Uninstall bool
Expand All @@ -27,7 +28,7 @@ var (
err error
)

// Main function for 'pgcenter config' command.
// RunMain is the main entry point for 'pgcenter config' command.
func RunMain(args []string, c utils.Conninfo, cfg Config) {
conninfo = c // copy conninfo from external struct into local one
utils.HandleExtraArgs(args, &conninfo)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -3,14 +3,14 @@ module github.com/lesovsky/pgcenter
require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869
github.com/jmoiron/sqlx v1.2.0 // indirect
github.com/jmoiron/sqlx v1.2.0
github.com/jroimartin/gocui v0.4.0
github.com/lib/pq v1.0.0
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/nsf/termbox-go v0.0.0-20180819125858-b66b20ab708e // indirect
github.com/pkg/errors v0.8.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2 // indirect
github.com/spf13/pflag v1.0.2
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b
golang.org/x/sys v0.0.0-20180918153733-ee1b12c67af4 // indirect
)

0 comments on commit f6de7d4

Please sign in to comment.