gorgonia.org/gorgonia@v0.9.17/x/dataviz/traces.go (about)

     1  package dataviz
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"strconv"
     7  	"time"
     8  
     9  	"gorgonia.org/gorgonia"
    10  	xvm "gorgonia.org/gorgonia/x/vm"
    11  )
    12  
    13  // DumpTrace suitable for https://github.com/vasturiano/timelines-chart
    14  func DumpTrace(traces []xvm.Trace, g *gorgonia.ExprGraph, w io.Writer) error {
    15  	var zerotime time.Time
    16  	groups := make(map[string]group)
    17  	// generate all labels
    18  	for _, trace := range traces {
    19  		if trace.End == zerotime {
    20  			continue
    21  		}
    22  		if _, ok := groups[trace.StateFunction]; !ok {
    23  			groups[trace.StateFunction] = group{
    24  				Group: trace.StateFunction,
    25  			}
    26  		}
    27  		label := dataLabel{
    28  			TimeRange: []time.Time{
    29  				trace.Start,
    30  				trace.End,
    31  			},
    32  			Val: strconv.Itoa(int(trace.ID)),
    33  		}
    34  		dGroup := dataGroup{
    35  			Label: g.Node(trace.ID).(*gorgonia.Node).Name(),
    36  			Data: []dataLabel{
    37  				label,
    38  			},
    39  		}
    40  		g := groups[trace.StateFunction]
    41  		g.Data = append(g.Data, dGroup)
    42  		groups[trace.StateFunction] = g
    43  	}
    44  	grps := make([]group, 0, len(groups))
    45  	for _, grp := range groups {
    46  		grps = append(grps, grp)
    47  	}
    48  	enc := json.NewEncoder(w)
    49  	return enc.Encode(grps)
    50  }
    51  
    52  type group struct {
    53  	Group string      `json:"group"`
    54  	Data  []dataGroup `json:"data"`
    55  }
    56  
    57  type dataGroup struct {
    58  	Label string      `json:"label"`
    59  	Data  []dataLabel `json:"data"`
    60  }
    61  
    62  type dataLabel struct {
    63  	TimeRange []time.Time `json:"timeRange"`
    64  	Val       interface{} `json:"val"`
    65  }