github.com/go-graphite/carbonapi@v0.17.0/expr/functions/cairo/png/picture_params.go (about)

     1  package png
     2  
     3  import (
     4  	"math"
     5  	"net/http"
     6  	"net/url"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/go-graphite/carbonapi/expr/types"
    12  	"github.com/go-graphite/carbonapi/pkg/parser"
    13  )
    14  
    15  var DefaultColorList = []string{"blue", "green", "red", "purple", "brown", "yellow", "aqua", "grey", "magenta", "pink", "gold", "rose"}
    16  
    17  type YAxisSide int
    18  
    19  const (
    20  	YAxisSideRight YAxisSide = 1 << iota
    21  	YAxisSideLeft
    22  )
    23  
    24  func getAxisSide(s string, def YAxisSide) YAxisSide {
    25  	if s == "" {
    26  		return def
    27  	}
    28  	if s == "right" {
    29  		return YAxisSideRight
    30  	}
    31  	return YAxisSideLeft
    32  }
    33  
    34  type LineMode int
    35  
    36  const (
    37  	LineModeSlope LineMode = 1 << iota
    38  	LineModeStaircase
    39  	LineModeConnected
    40  )
    41  
    42  type AreaMode int
    43  
    44  const (
    45  	AreaModeNone AreaMode = 1 << iota
    46  	AreaModeFirst
    47  	AreaModeAll
    48  	AreaModeStacked
    49  )
    50  
    51  func getAreaMode(s string, def AreaMode) AreaMode {
    52  	if s == "" {
    53  		return def
    54  	}
    55  	switch s {
    56  	case "first":
    57  		return AreaModeFirst
    58  	case "all":
    59  		return AreaModeAll
    60  	case "stacked":
    61  		return AreaModeStacked
    62  	}
    63  	return AreaModeNone
    64  }
    65  
    66  type PieMode int
    67  
    68  const (
    69  	PieModeMaximum PieMode = 1 << iota
    70  	PieModeMinimum
    71  	PieModeAverage
    72  )
    73  
    74  func getPieMode(s string, def PieMode) PieMode {
    75  	if s == "" {
    76  		return def
    77  	}
    78  	if s == "maximum" {
    79  		return PieModeMaximum
    80  	}
    81  	if s == "minimum" {
    82  		return PieModeMinimum
    83  	}
    84  	return PieModeAverage
    85  }
    86  
    87  func getLineMode(s string, def LineMode) LineMode {
    88  	if s == "" {
    89  		return def
    90  	}
    91  	if s == "slope" {
    92  		return LineModeSlope
    93  	}
    94  	if s == "staircase" {
    95  		return LineModeStaircase
    96  	}
    97  	return LineModeConnected
    98  }
    99  
   100  type FontWeight int
   101  
   102  const (
   103  	FontWeightNormal FontWeight = iota
   104  	FontWeightBold
   105  )
   106  
   107  func getFontWeight(s string, def FontWeight) FontWeight {
   108  	if s == "" {
   109  		return def
   110  	}
   111  	if parser.TruthyBool(s) {
   112  		return FontWeightBold
   113  	}
   114  	return FontWeightNormal
   115  }
   116  
   117  type FontSlant int
   118  
   119  const (
   120  	FontSlantNormal FontSlant = iota
   121  	FontSlantItalic
   122  	FontSlantOblique
   123  )
   124  
   125  func getFontItalic(s string, def FontSlant) FontSlant {
   126  	if s == "" {
   127  		return def
   128  	}
   129  	if parser.TruthyBool(s) {
   130  		return FontSlantItalic
   131  	}
   132  	return FontSlantNormal
   133  }
   134  
   135  type PictureParams struct {
   136  	PixelRatio float64
   137  	Width      float64
   138  	Height     float64
   139  	Margin     int
   140  	LogBase    float64
   141  	FgColor    string
   142  	BgColor    string
   143  	MajorLine  string
   144  	MinorLine  string
   145  	FontName   string
   146  	FontSize   float64
   147  	FontBold   FontWeight
   148  	FontItalic FontSlant
   149  
   150  	GraphOnly  bool
   151  	HideLegend bool
   152  	HideGrid   bool
   153  	HideAxes   bool
   154  	HideYAxis  bool
   155  	HideXAxis  bool
   156  	YAxisSide  YAxisSide
   157  
   158  	Title       string
   159  	Vtitle      string
   160  	VtitleRight string
   161  
   162  	Tz *time.Location
   163  
   164  	ConnectedLimit int
   165  	LineMode       LineMode
   166  	AreaMode       AreaMode
   167  	AreaAlpha      float64
   168  	PieMode        PieMode
   169  	LineWidth      float64
   170  	ColorList      []string
   171  
   172  	YMin    float64
   173  	YMax    float64
   174  	XMin    float64
   175  	XMax    float64
   176  	YStep   float64
   177  	XStep   float64
   178  	MinorY  int
   179  	XFormat string
   180  
   181  	YMaxLeft    float64
   182  	YLimitLeft  float64
   183  	YMaxRight   float64
   184  	YLimitRight float64
   185  	YMinLeft    float64
   186  	YMinRight   float64
   187  	YStepL      float64
   188  	YStepR      float64
   189  
   190  	UniqueLegend   bool
   191  	DrawNullAsZero bool
   192  	DrawAsInfinite bool
   193  
   194  	YUnitSystem string
   195  	YDivisors   []float64
   196  
   197  	RightWidth  float64
   198  	RightDashed bool
   199  	RightColor  string
   200  	LeftWidth   float64
   201  	LeftDashed  bool
   202  	LeftColor   string
   203  
   204  	MinorGridLineColor string
   205  	MajorGridLineColor string
   206  }
   207  
   208  // GetPictureParams returns PictureParams with default settings
   209  func GetPictureParams(r *http.Request, metricData []*types.MetricData) PictureParams {
   210  	return GetPictureParamsWithTemplate(r, "default", metricData)
   211  }
   212  
   213  // GetPictureParamsWithTemplate returns PictureParams with specified template
   214  func GetPictureParamsWithTemplate(r *http.Request, template string, metricData []*types.MetricData) PictureParams {
   215  	t, ok := templates[template]
   216  	if !ok {
   217  		t = templates["default"]
   218  	}
   219  
   220  	pixelRatioParam := ""
   221  	if r.Header.Get("Referer") != "" {
   222  		u, err := url.Parse(r.Header.Get("Referer"))
   223  		if err == nil {
   224  			pixelRatioParam = u.Query().Get("pixelRatio")
   225  		}
   226  	}
   227  	if r.FormValue("pixelRatio") != "" {
   228  		pixelRatioParam = r.FormValue("pixelRatio")
   229  	}
   230  
   231  	return PictureParams{
   232  		PixelRatio: getFloat64(pixelRatioParam, 1.0),
   233  		Width:      getFloat64(r.FormValue("width"), t.Width),
   234  		Height:     getFloat64(r.FormValue("height"), t.Height),
   235  		Margin:     getInt(r.FormValue("margin"), t.Margin),
   236  		LogBase:    getLogBase(r.FormValue("logBase")),
   237  		FgColor:    getString(r.FormValue("fgcolor"), t.FgColor),
   238  		BgColor:    getString(r.FormValue("bgcolor"), t.BgColor),
   239  		MajorLine:  getString(r.FormValue("majorLine"), t.MajorLine),
   240  		MinorLine:  getString(r.FormValue("minorLine"), t.MinorLine),
   241  		FontName:   getString(r.FormValue("fontName"), t.FontName),
   242  		FontSize:   getFloat64(r.FormValue("fontSize"), t.FontSize),
   243  		FontBold:   getFontWeight(r.FormValue("fontBold"), t.FontBold),
   244  		FontItalic: getFontItalic(r.FormValue("fontItalic"), t.FontItalic),
   245  
   246  		GraphOnly:  getBool(r.FormValue("graphOnly"), t.GraphOnly),
   247  		HideLegend: getBool(r.FormValue("hideLegend"), len(metricData) > 10),
   248  		HideGrid:   getBool(r.FormValue("hideGrid"), t.HideGrid),
   249  		HideAxes:   getBool(r.FormValue("hideAxes"), t.HideAxes),
   250  		HideYAxis:  getBool(r.FormValue("hideYAxis"), t.HideYAxis),
   251  		HideXAxis:  getBool(r.FormValue("hideXAxis"), t.HideXAxis),
   252  		YAxisSide:  getAxisSide(r.FormValue("yAxisSide"), t.YAxisSide),
   253  
   254  		Title:       getString(r.FormValue("title"), t.Title),
   255  		Vtitle:      getString(r.FormValue("vtitle"), t.Vtitle),
   256  		VtitleRight: getString(r.FormValue("vtitleRight"), t.VtitleRight),
   257  
   258  		Tz: getTimeZone(r.FormValue("tz"), t.Tz),
   259  
   260  		ConnectedLimit: getInt(r.FormValue("connectedLimit"), t.ConnectedLimit),
   261  		LineMode:       getLineMode(r.FormValue("lineMode"), t.LineMode),
   262  		AreaMode:       getAreaMode(r.FormValue("areaMode"), t.AreaMode),
   263  		AreaAlpha:      getFloat64(r.FormValue("areaAlpha"), t.AreaAlpha),
   264  		PieMode:        getPieMode(r.FormValue("pieMode"), t.PieMode),
   265  		LineWidth:      getFloat64(r.FormValue("lineWidth"), t.LineWidth),
   266  		ColorList:      getStringArray(r.FormValue("colorList"), t.ColorList),
   267  
   268  		YMin:    getFloat64(r.FormValue("yMin"), t.YMin),
   269  		YMax:    getFloat64(r.FormValue("yMax"), t.YMax),
   270  		YStep:   getFloat64(r.FormValue("yStep"), t.YStep),
   271  		XMin:    getFloat64(r.FormValue("xMin"), t.XMin),
   272  		XMax:    getFloat64(r.FormValue("xMax"), t.XMax),
   273  		XStep:   getFloat64(r.FormValue("xStep"), t.XStep),
   274  		XFormat: getString(r.FormValue("xFormat"), t.XFormat),
   275  		MinorY:  getInt(r.FormValue("minorY"), t.MinorY),
   276  
   277  		UniqueLegend:   getBool(r.FormValue("uniqueLegend"), t.UniqueLegend),
   278  		DrawNullAsZero: getBool(r.FormValue("drawNullAsZero"), t.DrawNullAsZero),
   279  		DrawAsInfinite: getBool(r.FormValue("drawAsInfinite"), t.DrawAsInfinite),
   280  
   281  		YMinLeft:    getFloat64(r.FormValue("yMinLeft"), t.YMinLeft),
   282  		YMinRight:   getFloat64(r.FormValue("yMinRight"), t.YMinRight),
   283  		YMaxLeft:    getFloat64(r.FormValue("yMaxLeft"), t.YMaxLeft),
   284  		YMaxRight:   getFloat64(r.FormValue("yMaxRight"), t.YMaxRight),
   285  		YStepL:      getFloat64(r.FormValue("yStepLeft"), t.YStepL),
   286  		YStepR:      getFloat64(r.FormValue("yStepRight"), t.YStepR),
   287  		YLimitLeft:  getFloat64(r.FormValue("yLimitLeft"), t.YLimitLeft),
   288  		YLimitRight: getFloat64(r.FormValue("yLimitRight"), t.YLimitRight),
   289  
   290  		YUnitSystem: getString(r.FormValue("yUnitSystem"), t.YUnitSystem),
   291  		YDivisors:   getFloatArray(r.FormValue("yDivisors"), t.YDivisors),
   292  
   293  		RightWidth:  getFloat64(r.FormValue("rightWidth"), t.RightWidth),
   294  		RightDashed: getBool(r.FormValue("rightDashed"), t.RightDashed),
   295  		RightColor:  getString(r.FormValue("rightColor"), t.RightColor),
   296  		LeftWidth:   getFloat64(r.FormValue("leftWidth"), t.LeftWidth),
   297  		LeftDashed:  getBool(r.FormValue("leftDashed"), t.LeftDashed),
   298  		LeftColor:   getString(r.FormValue("leftColor"), t.LeftColor),
   299  
   300  		MajorGridLineColor: getString(r.FormValue("majorGridLineColor"), t.MajorGridLineColor),
   301  		MinorGridLineColor: getString(r.FormValue("minorGridLineColor"), t.MinorGridLineColor),
   302  	}
   303  }
   304  
   305  func getStringArray(s string, def []string) []string {
   306  	if s == "" {
   307  		return def
   308  	}
   309  
   310  	ss := strings.Split(s, ",")
   311  	var strs []string
   312  	for _, v := range ss {
   313  		strs = append(strs, strings.TrimSpace(v))
   314  	}
   315  
   316  	return strs
   317  }
   318  
   319  func getFloatArray(s string, def []float64) []float64 {
   320  	if s == "" {
   321  		return def
   322  	}
   323  	ss := strings.Split(s, ",")
   324  	var fs []float64
   325  	for _, v := range ss {
   326  		f, err := strconv.ParseFloat(v, 64)
   327  		if err != nil {
   328  			return def
   329  		}
   330  		fs = append(fs, f)
   331  	}
   332  	return fs
   333  }
   334  
   335  func getLogBase(s string) float64 {
   336  	if s == "e" {
   337  		return math.E
   338  	}
   339  	b, err := strconv.ParseFloat(s, 64)
   340  	if err != nil || b < 1 {
   341  		return 0
   342  	}
   343  	return b
   344  }
   345  
   346  func getTimeZone(s string, def *time.Location) *time.Location {
   347  	if s == "" {
   348  		return def
   349  	}
   350  	tz, err := time.LoadLocation(s)
   351  	if err != nil {
   352  		return def
   353  	}
   354  	return tz
   355  }
   356  
   357  // SetTemplate adds a picture param template with specified name and parameters
   358  func SetTemplate(name string, params *PictureParams) {
   359  	templates[name] = *params
   360  }
   361  
   362  var DefaultParams = PictureParams{
   363  	Width:      330,
   364  	Height:     250,
   365  	Margin:     10,
   366  	LogBase:    0,
   367  	FgColor:    "white",
   368  	BgColor:    "black",
   369  	MajorLine:  "rose",
   370  	MinorLine:  "grey",
   371  	FontName:   "Sans",
   372  	FontSize:   10,
   373  	FontBold:   FontWeightNormal,
   374  	FontItalic: FontSlantNormal,
   375  
   376  	GraphOnly:  false,
   377  	HideLegend: false,
   378  	HideGrid:   false,
   379  	HideAxes:   false,
   380  	HideYAxis:  false,
   381  	HideXAxis:  false,
   382  	YAxisSide:  YAxisSideLeft,
   383  
   384  	Title:       "",
   385  	Vtitle:      "",
   386  	VtitleRight: "",
   387  
   388  	Tz: time.Local,
   389  
   390  	ConnectedLimit: math.MaxInt32,
   391  	LineMode:       LineModeSlope,
   392  	AreaMode:       AreaModeNone,
   393  	AreaAlpha:      math.NaN(),
   394  	PieMode:        PieModeAverage,
   395  	LineWidth:      1.2,
   396  	ColorList:      DefaultColorList,
   397  
   398  	YMin:    math.NaN(),
   399  	YMax:    math.NaN(),
   400  	YStep:   math.NaN(),
   401  	XMin:    math.NaN(),
   402  	XMax:    math.NaN(),
   403  	XStep:   math.NaN(),
   404  	XFormat: "",
   405  	MinorY:  1,
   406  
   407  	UniqueLegend:   false,
   408  	DrawNullAsZero: false,
   409  	DrawAsInfinite: false,
   410  
   411  	YMinLeft:    math.NaN(),
   412  	YMinRight:   math.NaN(),
   413  	YMaxLeft:    math.NaN(),
   414  	YMaxRight:   math.NaN(),
   415  	YStepL:      math.NaN(),
   416  	YStepR:      math.NaN(),
   417  	YLimitLeft:  math.NaN(),
   418  	YLimitRight: math.NaN(),
   419  
   420  	YUnitSystem: "si",
   421  	YDivisors:   []float64{4, 5, 6},
   422  
   423  	RightWidth:  1.2,
   424  	RightDashed: false,
   425  	RightColor:  "",
   426  	LeftWidth:   1.2,
   427  	LeftDashed:  false,
   428  	LeftColor:   "",
   429  
   430  	MajorGridLineColor: "white",
   431  	MinorGridLineColor: "grey",
   432  }
   433  
   434  var templates = map[string]PictureParams{
   435  	"default": {
   436  		Width:      330,
   437  		Height:     250,
   438  		Margin:     10,
   439  		LogBase:    0,
   440  		FgColor:    "white",
   441  		BgColor:    "black",
   442  		MajorLine:  "rose",
   443  		MinorLine:  "grey",
   444  		FontName:   "Sans",
   445  		FontSize:   10,
   446  		FontBold:   FontWeightNormal,
   447  		FontItalic: FontSlantNormal,
   448  
   449  		GraphOnly:  false,
   450  		HideLegend: false,
   451  		HideGrid:   false,
   452  		HideAxes:   false,
   453  		HideYAxis:  false,
   454  		HideXAxis:  false,
   455  		YAxisSide:  YAxisSideLeft,
   456  
   457  		Title:       "",
   458  		Vtitle:      "",
   459  		VtitleRight: "",
   460  
   461  		Tz: time.Local,
   462  
   463  		ConnectedLimit: math.MaxInt32,
   464  		LineMode:       LineModeSlope,
   465  		AreaMode:       AreaModeNone,
   466  		AreaAlpha:      math.NaN(),
   467  		PieMode:        PieModeAverage,
   468  		LineWidth:      1.2,
   469  		ColorList:      DefaultColorList,
   470  
   471  		YMin:    math.NaN(),
   472  		YMax:    math.NaN(),
   473  		YStep:   math.NaN(),
   474  		XMin:    math.NaN(),
   475  		XMax:    math.NaN(),
   476  		XStep:   math.NaN(),
   477  		XFormat: "",
   478  		MinorY:  1,
   479  
   480  		UniqueLegend:   false,
   481  		DrawNullAsZero: false,
   482  		DrawAsInfinite: false,
   483  
   484  		YMinLeft:    math.NaN(),
   485  		YMinRight:   math.NaN(),
   486  		YMaxLeft:    math.NaN(),
   487  		YMaxRight:   math.NaN(),
   488  		YStepL:      math.NaN(),
   489  		YStepR:      math.NaN(),
   490  		YLimitLeft:  math.NaN(),
   491  		YLimitRight: math.NaN(),
   492  
   493  		YUnitSystem: "si",
   494  		YDivisors:   []float64{4, 5, 6},
   495  
   496  		RightWidth:  1.2,
   497  		RightDashed: false,
   498  		RightColor:  "",
   499  		LeftWidth:   1.2,
   500  		LeftDashed:  false,
   501  		LeftColor:   "",
   502  
   503  		MajorGridLineColor: "white",
   504  		MinorGridLineColor: "grey",
   505  	},
   506  }