github.com/m3db/m3@v1.5.0/scripts/comparator/grafana/generate_dash.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package main
    22  
    23  import (
    24  	"flag"
    25  	"os"
    26  	"text/template"
    27  	"time"
    28  
    29  	"github.com/m3db/m3/scripts/comparator/utils"
    30  	"github.com/m3db/m3/src/x/instrument"
    31  
    32  	"go.uber.org/zap"
    33  )
    34  
    35  // TemplateData is a collection of template data.
    36  type TemplateData struct {
    37  	Revision string
    38  	Start    string
    39  	End      string
    40  	Queries  []utils.GrafanaQueries
    41  }
    42  
    43  func paramError(err string, log *zap.Logger) {
    44  	log.Error(err)
    45  	flag.Usage()
    46  }
    47  
    48  func main() {
    49  	var (
    50  		iOpts = instrument.NewOptions()
    51  		log   = iOpts.Logger()
    52  
    53  		pRevision  = flag.String("r", "", "the git revision")
    54  		pQueryFile = flag.String("q", "", "the query file")
    55  		pTemplate  = flag.String("t", "", "the template file")
    56  		pOutput    = flag.String("o", "", "the output file")
    57  
    58  		pStart = flag.Int64("s", time.Now().Unix(), "start")
    59  		pEnd   = flag.Int64("e", time.Now().Unix(), "end")
    60  	)
    61  
    62  	flag.Parse()
    63  	var (
    64  		revision     = *pRevision
    65  		qFile        = *pQueryFile
    66  		output       = *pOutput
    67  		templateFile = *pTemplate
    68  	)
    69  
    70  	if len(revision) == 0 {
    71  		paramError("No revision found", log)
    72  		os.Exit(1)
    73  	}
    74  
    75  	if len(qFile) == 0 {
    76  		paramError("No query file found", log)
    77  		os.Exit(1)
    78  	}
    79  
    80  	if len(output) == 0 {
    81  		paramError("No output found", log)
    82  		os.Exit(1)
    83  	}
    84  
    85  	if len(templateFile) == 0 {
    86  		paramError("No template file found", log)
    87  		os.Exit(1)
    88  	}
    89  
    90  	queries, err := utils.ParseFileToGrafanaQueries(qFile, log)
    91  	if err != nil {
    92  		log.Error("could not parse file to Grafana queries", zap.Error(err))
    93  		os.Exit(1)
    94  	}
    95  
    96  	opts := os.O_RDWR | os.O_CREATE | os.O_TRUNC
    97  	outputFile, err := os.OpenFile(output, opts, 0777)
    98  	if err != nil {
    99  		log.Error("could not open output file", zap.Error(err))
   100  		os.Exit(1)
   101  	}
   102  
   103  	defer outputFile.Close()
   104  
   105  	start := time.Unix(*pStart, 0)
   106  	end := time.Unix(*pEnd, 0)
   107  
   108  	templateData := TemplateData{
   109  		Revision: revision,
   110  		Queries:  queries,
   111  		Start:    start.Format(time.RFC3339),
   112  		End:      end.Format(time.RFC3339),
   113  	}
   114  
   115  	t := template.Must(template.ParseFiles(templateFile))
   116  	err = t.Execute(outputFile, templateData)
   117  	if err != nil {
   118  		log.Error("could not write to output file", zap.Error(err))
   119  		os.Exit(1)
   120  	}
   121  }