forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ivan Valdes <[email protected]>
- Loading branch information
Showing
6 changed files
with
569 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.