go-hep.org/x/hep@v0.38.1/groot/rsrv/api.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rsrv
     6  
     7  import (
     8  	"encoding/json"
     9  	"image/color"
    10  	"math"
    11  	"strings"
    12  
    13  	"go-hep.org/x/hep/groot/rsrv/internal/hexcolor"
    14  	"gonum.org/v1/plot/vg"
    15  	"gonum.org/v1/plot/vg/vgimg"
    16  )
    17  
    18  // OpenFileRequest describes a request to open a file located at the
    19  // provided URI.
    20  type OpenFileRequest struct {
    21  	URI string `json:"uri"`
    22  }
    23  
    24  type CloseFileRequest struct {
    25  	URI string `json:"uri"`
    26  }
    27  
    28  type File struct {
    29  	URI     string `json:"uri"`
    30  	Version int    `json:"version"`
    31  }
    32  
    33  type ListResponse struct {
    34  	Files []File `json:"files"`
    35  }
    36  
    37  type DirentRequest struct {
    38  	URI       string `json:"uri"`
    39  	Dir       string `json:"dir,omitempty"`
    40  	Recursive bool   `json:"recursive,omitempty"`
    41  }
    42  
    43  type DirentResponse struct {
    44  	URI     string   `json:"uri"`
    45  	Content []Dirent `json:"content,omitempty"`
    46  }
    47  
    48  type Dirent struct {
    49  	Path  string `json:"path"`
    50  	Type  string `json:"type"`
    51  	Name  string `json:"name"`
    52  	Title string `json:"title,omitempty"`
    53  	Cycle int    `json:"cycle"`
    54  }
    55  
    56  type Tree struct {
    57  	Type     string   `json:"type"`
    58  	Name     string   `json:"name"`
    59  	Title    string   `json:"title"`
    60  	Entries  int64    `json:"entries"`
    61  	Branches []Branch `json:"branches"`
    62  	Leaves   []Leaf   `json:"leaves"`
    63  }
    64  
    65  type Branch struct {
    66  	Type     string   `json:"type"`
    67  	Name     string   `json:"name"`
    68  	Branches []Branch `json:"branches"`
    69  	Leaves   []Leaf   `json:"leaves"`
    70  }
    71  
    72  type Leaf struct {
    73  	Type string `json:"type"`
    74  	Name string `json:"name"`
    75  }
    76  
    77  type TreeRequest struct {
    78  	URI string `json:"uri"`
    79  	Dir string `json:"dir"`
    80  	Obj string `json:"obj"`
    81  }
    82  
    83  type TreeResponse struct {
    84  	URI  string `json:"uri"`
    85  	Dir  string `json:"dir"`
    86  	Obj  string `json:"obj"`
    87  	Tree Tree   `json:"tree"`
    88  }
    89  
    90  type PlotH1Request struct {
    91  	URI string `json:"uri"`
    92  	Dir string `json:"dir"`
    93  	Obj string `json:"obj"`
    94  
    95  	Options PlotOptions `json:"options"`
    96  }
    97  
    98  type PlotH2Request struct {
    99  	URI string `json:"uri"`
   100  	Dir string `json:"dir"`
   101  	Obj string `json:"obj"`
   102  
   103  	Options PlotOptions `json:"options"`
   104  }
   105  
   106  type PlotS2Request struct {
   107  	URI string `json:"uri"`
   108  	Dir string `json:"dir"`
   109  	Obj string `json:"obj"`
   110  
   111  	Options PlotOptions `json:"options"`
   112  }
   113  
   114  type PlotTreeRequest struct {
   115  	URI  string   `json:"uri"`
   116  	Dir  string   `json:"dir"`
   117  	Obj  string   `json:"obj"`
   118  	Vars []string `json:"vars"`
   119  
   120  	Options PlotOptions `json:"options"`
   121  }
   122  
   123  type PlotResponse struct {
   124  	URI string `json:"uri"`
   125  	Dir string `json:"dir"`
   126  	Obj string `json:"obj"`
   127  
   128  	Data string `json:"data"`
   129  }
   130  
   131  type PlotOptions struct {
   132  	Title string `json:"title,omitempty"`
   133  	X     string `json:"x,omitempty"`
   134  	Y     string `json:"y,omitempty"`
   135  
   136  	Type   string    `json:"type"`
   137  	Width  vg.Length `json:"width"`
   138  	Height vg.Length `json:"height"`
   139  
   140  	Line      LineStyle   `json:"line,omitempty"`
   141  	FillColor color.Color `json:"fill_color,omitempty"`
   142  }
   143  
   144  func (opt *PlotOptions) init() {
   145  	if opt.Type == "" {
   146  		opt.Type = "png"
   147  	}
   148  	opt.Type = strings.ToLower(opt.Type)
   149  
   150  	switch {
   151  	case opt.Width <= 0 && opt.Height <= 0:
   152  		opt.Width = vgimg.DefaultWidth
   153  		opt.Height = vgimg.DefaultWidth / math.Phi
   154  	case opt.Width <= 0:
   155  		opt.Width = opt.Height * math.Phi
   156  	case opt.Height <= 0:
   157  		opt.Height = opt.Width / math.Phi
   158  	}
   159  
   160  	opt.Line.init()
   161  	if opt.FillColor == nil {
   162  		opt.FillColor = color.Transparent
   163  	}
   164  }
   165  
   166  func (opt PlotOptions) MarshalJSON() ([]byte, error) {
   167  	if opt.FillColor == nil {
   168  		opt.FillColor = color.Transparent
   169  	}
   170  	raw := jsonPlotOptions{
   171  		Title:     opt.Title,
   172  		X:         opt.X,
   173  		Y:         opt.Y,
   174  		Type:      opt.Type,
   175  		Width:     opt.Width,
   176  		Height:    opt.Height,
   177  		Line:      opt.Line.toJSON(),
   178  		FillColor: hexcolor.HexModel.Convert(opt.FillColor).(hexcolor.Hex),
   179  	}
   180  	return json.Marshal(raw)
   181  }
   182  
   183  func (opt *PlotOptions) UnmarshalJSON(p []byte) error {
   184  	var raw jsonPlotOptions
   185  	err := json.Unmarshal(p, &raw)
   186  	if err != nil {
   187  		return err
   188  	}
   189  	opt.Title = raw.Title
   190  	opt.X = raw.X
   191  	opt.Y = raw.Y
   192  	opt.Type = raw.Type
   193  	opt.Width = raw.Width
   194  	opt.Height = raw.Height
   195  	opt.Line = raw.Line.fromJSON()
   196  	opt.FillColor = raw.FillColor
   197  	return nil
   198  }
   199  
   200  type LineStyle struct {
   201  	Color   color.Color `json:"color,omitempty"`
   202  	Width   vg.Length   `json:"width,omitempty"`
   203  	Dashes  []vg.Length `json:"dashes,omitempty"`
   204  	DashOff vg.Length   `json:"dash_offset,omitempty"`
   205  }
   206  
   207  func (sty *LineStyle) init() {
   208  	if sty.Color == nil {
   209  		sty.Color = color.RGBA{255, 0, 0, 255}
   210  	}
   211  }
   212  
   213  type jsonPlotOptions struct {
   214  	Title string `json:"title,omitempty"`
   215  	X     string `json:"x,omitempty"`
   216  	Y     string `json:"y,omitempty"`
   217  
   218  	Type   string    `json:"type"`
   219  	Width  vg.Length `json:"width"`
   220  	Height vg.Length `json:"height"`
   221  
   222  	Line      jsonLineStyle `json:"line,omitempty"`
   223  	FillColor hexcolor.Hex  `json:"fill_color,omitempty"`
   224  }
   225  
   226  type jsonLineStyle struct {
   227  	Color   hexcolor.Hex `json:"color,omitempty"`
   228  	Width   vg.Length    `json:"width,omitempty"`
   229  	Dashes  []vg.Length  `json:"dashes,omitempty"`
   230  	DashOff vg.Length    `json:"dash_offset,omitempty"`
   231  }
   232  
   233  func (sty jsonLineStyle) fromJSON() LineStyle {
   234  	return LineStyle{
   235  		Color:   sty.Color,
   236  		Width:   sty.Width,
   237  		Dashes:  sty.Dashes,
   238  		DashOff: sty.DashOff,
   239  	}
   240  }
   241  
   242  func (sty LineStyle) toJSON() jsonLineStyle {
   243  	if sty.Color == nil {
   244  		sty.Color = defaultLineColor
   245  	}
   246  	o := jsonLineStyle{
   247  		Width:   sty.Width,
   248  		Dashes:  sty.Dashes,
   249  		DashOff: sty.DashOff,
   250  		Color:   hexcolor.HexModel.Convert(sty.Color).(hexcolor.Hex),
   251  	}
   252  	return o
   253  }
   254  
   255  var (
   256  	defaultLineColor = color.RGBA{R: 255, A: 255}
   257  )