github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/components/linegraph/model.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package linegraph
    16  
    17  import (
    18  	"sync"
    19  
    20  	structure "github.com/erda-project/erda-infra/providers/component-protocol/components/commodel/data-structure"
    21  )
    22  
    23  // Below is standard struct for line graph related.
    24  type (
    25  	// Data includes list.
    26  	Data struct {
    27  		Title      string     `json:"title"`
    28  		SubTitle   string     `json:"subTitle"`
    29  		Dimensions []string   `json:"dimensions"`
    30  		XAxis      *Axis      `json:"xAxis"` // x axis
    31  		YAxis      []*Axis    `json:"yAxis"` // y axis
    32  		XOptions   *Options   `json:"xOptions"`
    33  		YOptions   []*Options `json:"yOptions"`
    34  		Inverse    bool       `json:"inverse"` // inverted xAxis and yAxis
    35  		sync.RWMutex
    36  	}
    37  
    38  	// Axis defined struct.
    39  	Axis struct {
    40  		Dimension string        `json:"dimension,omitempty"` // The xAxis can have no dimensions
    41  		Values    []interface{} `json:"values"`
    42  	}
    43  
    44  	// Options .
    45  	Options struct {
    46  		Dimension string                   `json:"dimension,omitempty"`
    47  		Structure *structure.DataStructure `json:"structure"`
    48  		Inverse   bool                     `json:"inverse"` // inverted values
    49  	}
    50  
    51  	// DataBuilder .
    52  	DataBuilder struct {
    53  		data *Data
    54  	}
    55  
    56  	// OptionsBuilder .
    57  	OptionsBuilder struct {
    58  		options *Options
    59  	}
    60  )
    61  
    62  // NewOptionsBuilder .
    63  func NewOptionsBuilder() *OptionsBuilder {
    64  	return &OptionsBuilder{options: &Options{Structure: &structure.DataStructure{}}}
    65  }
    66  
    67  // WithDimension .
    68  func (o *OptionsBuilder) WithDimension(dimension string) *OptionsBuilder {
    69  	o.options.Dimension = dimension
    70  	return o
    71  }
    72  
    73  // WithType .
    74  func (o *OptionsBuilder) WithType(dataType structure.Type) *OptionsBuilder {
    75  	o.options.Structure.Type = dataType
    76  	return o
    77  }
    78  
    79  // WithEnable .
    80  func (o *OptionsBuilder) WithEnable(enable bool) *OptionsBuilder {
    81  	o.options.Structure.Enable = enable
    82  	return o
    83  }
    84  
    85  // WithPrecision .
    86  func (o *OptionsBuilder) WithPrecision(precision structure.Precision) *OptionsBuilder {
    87  	o.options.Structure.Precision = precision
    88  	return o
    89  }
    90  
    91  // Build .
    92  func (o *OptionsBuilder) Build() *Options {
    93  	return o.options
    94  }
    95  
    96  // NewDataBuilder .
    97  func NewDataBuilder() *DataBuilder {
    98  	return &DataBuilder{data: &Data{Dimensions: []string{}, XAxis: &Axis{}, YAxis: []*Axis{}, XOptions: &Options{}, YOptions: []*Options{}}}
    99  }
   100  
   101  // WithTitle .
   102  func (d *DataBuilder) WithTitle(title string) *DataBuilder {
   103  	d.data.Title = title
   104  	return d
   105  }
   106  
   107  // WithXAxis .
   108  func (d *DataBuilder) WithXAxis(values ...interface{}) *DataBuilder {
   109  	d.data.XAxis.Values = append(d.data.XAxis.Values, values...)
   110  	return d
   111  }
   112  
   113  // WithYAxis .
   114  func (d *DataBuilder) WithYAxis(dimension string, values ...interface{}) *DataBuilder {
   115  	d.data.Dimensions = append(d.data.Dimensions, dimension)
   116  	d.data.YAxis = append(d.data.YAxis, &Axis{Dimension: dimension, Values: values})
   117  	return d
   118  }
   119  
   120  // WithXOptions .
   121  func (d *DataBuilder) WithXOptions(options *Options) *DataBuilder {
   122  	d.data.XOptions = options
   123  	return d
   124  }
   125  
   126  // WithYOptions .
   127  func (d *DataBuilder) WithYOptions(options ...*Options) *DataBuilder {
   128  	d.data.YOptions = append(d.data.YOptions, options...)
   129  	return d
   130  }
   131  
   132  // Build .
   133  func (d *DataBuilder) Build() *Data {
   134  	return d.data
   135  }
   136  
   137  // New .
   138  func New(title string) *Data {
   139  	return &Data{
   140  		Title:      title,
   141  		Dimensions: *new([]string),
   142  		XAxis:      new(Axis),
   143  		YAxis:      *new([]*Axis),
   144  		XOptions:   new(Options),
   145  		YOptions:   *new([]*Options),
   146  		Inverse:    false,
   147  	}
   148  }
   149  
   150  // SetXAxis .
   151  func (d *Data) SetXAxis(values ...interface{}) {
   152  	d.Lock()
   153  	defer d.Unlock()
   154  	d.XAxis.Values = append(d.XAxis.Values, values...)
   155  }
   156  
   157  // SetYAxis .
   158  func (d *Data) SetYAxis(dimension string, values ...interface{}) {
   159  	d.Lock()
   160  	defer d.Unlock()
   161  	d.Dimensions = append(d.Dimensions, dimension)
   162  	d.YAxis = append(d.YAxis, &Axis{Dimension: dimension, Values: values})
   163  }
   164  
   165  // SetXOptions .
   166  func (d *Data) SetXOptions(options *Options) {
   167  	d.XOptions = options
   168  }
   169  
   170  // SetYOptions .
   171  func (d *Data) SetYOptions(options ...*Options) {
   172  	d.YOptions = append(d.YOptions, options...)
   173  }