github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_pt_window.go (about)

     1  package funcs
     2  
     3  import (
     4  	"github.com/GuanceCloud/platypus/pkg/ast"
     5  	"github.com/GuanceCloud/platypus/pkg/engine/runtime"
     6  	"github.com/GuanceCloud/platypus/pkg/errchain"
     7  )
     8  
     9  var (
    10  	defaultStreamTags = []string{"filepath", "host"}
    11  )
    12  
    13  func PtWindowChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    14  	// fn pt_window(before, after, stream_tags := ["filepath", "host"])
    15  	if err := normalizeFuncArgsDeprecated(funcExpr, []string{
    16  		"before", "after", "stream_tags",
    17  	}, 2); err != nil {
    18  		return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos)
    19  	}
    20  
    21  	return nil
    22  }
    23  
    24  func PtWindow(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    25  	before, vtype, errP := runtime.RunStmt(ctx, funcExpr.Param[0])
    26  	if errP != nil {
    27  		return errP
    28  	}
    29  	bf := before.(int64)
    30  
    31  	if vtype != ast.Int {
    32  		return runtime.NewRunError(ctx, "param data type expect int",
    33  			funcExpr.Param[0].StartPos())
    34  	}
    35  
    36  	after, vtype, errP := runtime.RunStmt(ctx, funcExpr.Param[1])
    37  	if errP != nil {
    38  		return errP
    39  	}
    40  	af := after.(int64)
    41  
    42  	if vtype != ast.Int {
    43  		return runtime.NewRunError(ctx, "param data type expect int",
    44  			funcExpr.Param[1].StartPos())
    45  	}
    46  
    47  	var tags []string
    48  	if funcExpr.Param[2] != nil {
    49  		streamTags, vtype, errP := runtime.RunStmt(ctx, funcExpr.Param[2])
    50  		if errP != nil {
    51  			return errP
    52  		}
    53  		if vtype != ast.List {
    54  			return runtime.NewRunError(ctx, "param data type expect array",
    55  				funcExpr.Param[2].StartPos())
    56  		}
    57  		if tagKey, ok := streamTags.([]any); ok && len(tagKey) != 0 {
    58  			for _, v := range tagKey {
    59  				if tag, ok := v.(string); ok {
    60  					tags = append(tags, tag)
    61  				} else {
    62  					return nil
    63  				}
    64  			}
    65  		}
    66  	}
    67  	if len(tags) == 0 {
    68  		tags = defaultStreamTags
    69  	}
    70  
    71  	tagsVal := make([]string, 0, len(tags))
    72  	for _, v := range tags {
    73  		if val, err := ctx.GetKey(v); err != nil {
    74  			return nil
    75  		} else {
    76  			if val.DType == ast.String {
    77  				tagsVal = append(tagsVal, val.Value.(string))
    78  			} else {
    79  				return nil
    80  			}
    81  		}
    82  	}
    83  
    84  	pt, err := getPoint(ctx.InData())
    85  	if err != nil {
    86  		return nil
    87  	}
    88  
    89  	pt.PtWinRegister(int(bf), int(af), tags, tagsVal)
    90  	return nil
    91  }
    92  
    93  func PtWindowHitChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    94  	return nil
    95  }
    96  
    97  func PtWindowHit(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    98  	pt, err := getPoint(ctx.InData())
    99  	if err != nil {
   100  		return nil
   101  	}
   102  
   103  	pt.PtWinHit()
   104  	return nil
   105  }