Skip to content

Commit

Permalink
tools/rw-heatmaps: clean up code
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Valdes <[email protected]>
  • Loading branch information
ivanvc committed Feb 1, 2024
1 parent 64a03f5 commit 5dd3d61
Show file tree
Hide file tree
Showing 6 changed files with 569 additions and 362 deletions.
97 changes: 97 additions & 0 deletions tools/rw-heatmaps/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2024 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.etcd.io/etcd/v3/tools/rw-heatmaps/pkg/chart"
"go.etcd.io/etcd/v3/tools/rw-heatmaps/pkg/dataset"
)

var (
// ErrMissingTitleArg is returned when the title argument is missing.
ErrMissingTitleArg = fmt.Errorf("missing title argument")
// ErrMissingOutputImageFileArg is returned when the output image file argument is missing.
ErrMissingOutputImageFileArg = fmt.Errorf("missing output image file argument")
// ErrMissingInputFileArg is returned when the input file argument is missing.
ErrMissingInputFileArg = fmt.Errorf("missing input file argument")
// ErrInvalidOutputFormat is returned when the output format is invalid.
ErrInvalidOutputFormat = fmt.Errorf("invalid output format, must be one of png, jpg, jpeg, tiff")
)

// NewRootCommand returns the root command for the rw-heatmaps tool.
func NewRootCommand() *cobra.Command {
o := newOptions()
rootCmd := &cobra.Command{
Use: "rw-heatmaps [input file in csv format]",
Short: "A tool to generate read/write heatmaps for etcd3",
Long: "rw-heatmaps is a tool to generate read/write heatmaps images for etcd3.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Validate(); err != nil {
return err
}
dataset, err := dataset.LoadCSVData(args[0])
if err != nil {
return err
}

return chart.PlotHeatMaps(dataset, o.title, o.outputImageFile, o.outputFormat)
},
}

o.AddFlags(rootCmd.Flags())
return rootCmd
}

// options holds the options for the command.
type options struct {
title string
outputImageFile string
outputFormat string
}

// Returns a new options for the command with the default values applied.
func newOptions() options {
return options{
outputFormat: "png",
}
}

// AddFlags sets the flags for the command.
func (o *options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.title, "title", o.title, "plot graph title (required)")
fs.StringVar(&o.outputImageFile, "output-image-file", o.outputImageFile, "output image filename (required)")
fs.StringVar(&o.outputFormat, "output-format", o.outputFormat, "output image file format")
}

// Validate returns an error if the options are invalid.
func (o *options) Validate() error {
if o.title == "" {
return ErrMissingTitleArg
}
if o.outputImageFile == "" {
return ErrMissingOutputImageFileArg
}
switch o.outputFormat {
case "png", "jpg", "jpeg", "tiff":
default:
return ErrInvalidOutputFormat
}
return nil
}
Loading

0 comments on commit 5dd3d61

Please sign in to comment.