github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/inspection_common.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package interlock
    15  
    16  import (
    17  	"context"
    18  	"sort"
    19  
    20  	causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded"
    21  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    22  	"github.com/whtcorpsinc/milevadb/types"
    23  )
    24  
    25  type inspectionMemruleRetriever struct {
    26  	dummyCloser
    27  	retrieved bool
    28  	extractor *causetembedded.InspectionMemruleBlockExtractor
    29  }
    30  
    31  const (
    32  	inspectionMemruleTypeInspection string = "inspection"
    33  	inspectionMemruleTypeSummary    string = "summary"
    34  )
    35  
    36  func (e *inspectionMemruleRetriever) retrieve(ctx context.Context, sctx stochastikctx.Context) ([][]types.Causet, error) {
    37  	if e.retrieved || e.extractor.SkipRequest {
    38  		return nil, nil
    39  	}
    40  	e.retrieved = true
    41  
    42  	tps := inspectionFilter{set: e.extractor.Types}
    43  	var finalEvents [][]types.Causet
    44  
    45  	// Select inspection rules
    46  	if tps.enable(inspectionMemruleTypeInspection) {
    47  		for _, r := range inspectionMemrules {
    48  			finalEvents = append(finalEvents, types.MakeCausets(
    49  				r.name(),
    50  				inspectionMemruleTypeInspection,
    51  				// TODO: add rule explanation
    52  				"",
    53  			))
    54  		}
    55  	}
    56  	// Select summary rules
    57  	if tps.enable(inspectionMemruleTypeSummary) {
    58  		// Get ordered key of map inspectionSummaryMemrules
    59  		summaryMemrules := make([]string, 0)
    60  		for rule := range inspectionSummaryMemrules {
    61  			summaryMemrules = append(summaryMemrules, rule)
    62  		}
    63  		sort.Strings(summaryMemrules)
    64  
    65  		for _, rule := range summaryMemrules {
    66  			finalEvents = append(finalEvents, types.MakeCausets(
    67  				rule,
    68  				inspectionMemruleTypeSummary,
    69  				// TODO: add rule explanation
    70  				"",
    71  			))
    72  		}
    73  	}
    74  	return finalEvents, nil
    75  }