github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/scripts/decode-resp/decode.go (about)

     1  package main
     2  
     3  //revive:disable:max-public-structs Config structs
     4  
     5  import (
     6  	"github.com/pyroscope-io/pyroscope/pkg/storage/segment"
     7  	"github.com/pyroscope-io/pyroscope/pkg/storage/tree"
     8  )
     9  
    10  type Input struct {
    11  	Timeline    *segment.Timeline `json:"timeline"`
    12  	Flamebearer *tree.Flamebearer `json:"flamebearer"`
    13  	Metadata    *InputMetadata    `json:"metadata"`
    14  }
    15  
    16  type InputMetadata struct {
    17  	SpyName    string      `json:"spyName"`
    18  	SampleRate uint32      `json:"sampleRate"`
    19  	Units      string      `json:"units"`
    20  	Format     tree.Format `json:"format"`
    21  }
    22  
    23  type Output struct {
    24  	Flamebearer *OutputFlamebearer `json:"flamebearer"`
    25  }
    26  
    27  type OutputFlamebearer struct {
    28  	Levels [][]OutputItem `json:"levels"`
    29  }
    30  
    31  type OutputItem interface{}
    32  
    33  type OutputItemSingle struct {
    34  	Row    int    `json:"_row"`
    35  	Col    int    `json:"_col"`
    36  	Name   string `json:"name"`
    37  	Total  int    `json:"total"`
    38  	Self   int    `json:"self"`
    39  	Offset int    `json:"offset"`
    40  }
    41  
    42  type OutputItemDouble struct {
    43  	Row        int    `json:"_row"`
    44  	Col        int    `json:"_col"`
    45  	Name       string `json:"name"`
    46  	LeftSelf   int    `json:"left_self"`
    47  	LeftTotal  int    `json:"left_total"`
    48  	LeftOffset int    `json:"left_offset"`
    49  	RghtSelf   int    `json:"right_self"`
    50  	RghtTotal  int    `json:"right_total"`
    51  	RghtOffset int    `json:"right_offset"`
    52  }
    53  
    54  func decodeLevels(in *Input) *Output {
    55  	names, levels := in.Flamebearer.Names, in.Flamebearer.Levels
    56  	outLevels := make([][]OutputItem, 0, len(levels))
    57  	isSingle := in.Flamebearer.Format != tree.FormatDouble
    58  
    59  	step := 4
    60  	if !isSingle {
    61  		step = 7
    62  	}
    63  
    64  	for rowIdx, row := range levels {
    65  		offsetLeft, offsetRght := 0, 0
    66  		outRow := make([]OutputItem, 0, len(row))
    67  
    68  		for i, N := 0, len(row); i < N; i += step {
    69  			var outItem OutputItem
    70  			if isSingle {
    71  				offsetLeft += row[i+0]
    72  				outItem = OutputItemSingle{
    73  					Row: rowIdx, Col: i / step,
    74  					Offset: offsetLeft,
    75  					Total:  row[i+1],
    76  					Self:   row[i+2],
    77  					Name:   names[row[i+3]],
    78  				}
    79  			} else {
    80  				offsetLeft += row[i+0]
    81  				offsetRght += row[i+3]
    82  				outItem = OutputItemDouble{
    83  					Row: rowIdx, Col: i / step,
    84  					LeftOffset: offsetLeft,
    85  					LeftTotal:  row[i+1],
    86  					LeftSelf:   row[i+2],
    87  					RghtOffset: offsetRght,
    88  					RghtTotal:  row[i+4],
    89  					RghtSelf:   row[i+6],
    90  					Name:       names[row[i+6]],
    91  				}
    92  			}
    93  			outRow = append(outRow, outItem)
    94  		}
    95  		outLevels = append(outLevels, outRow)
    96  	}
    97  
    98  	out := &Output{
    99  		Flamebearer: &OutputFlamebearer{
   100  			Levels: outLevels,
   101  		},
   102  	}
   103  	return out
   104  }