github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/es/query/docqueryhelper.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package query 18 19 import ( 20 "math" 21 22 dtu "github.com/siglens/siglens/pkg/common/dtypeutils" 23 . "github.com/siglens/siglens/pkg/segment/structs" 24 . "github.com/siglens/siglens/pkg/segment/utils" 25 utils "github.com/siglens/siglens/pkg/utils" 26 log "github.com/sirupsen/logrus" 27 ) 28 29 var TS_FOUR_MONTHS uint64 = 5259486000 30 31 func CreateSingleDocReqASTNode(columnName string, columnValue string, isKibana bool, qid uint64) *ASTNode { 32 33 colValEnc, err := CreateDtypeEnclosure(columnValue, qid) 34 if err != nil { 35 // TODO: handle error 36 log.Errorf("qid=%d, CreateSingleDocReqASTNode: failed to create DtypeEnclosure: %v", qid, err) 37 return nil 38 } 39 docFilterCriteria := FilterCriteria{ 40 ExpressionFilter: &ExpressionFilter{ 41 LeftInput: &FilterInput{Expression: &Expression{LeftInput: &ExpressionInput{ColumnName: columnName}}}, 42 FilterOperator: Equals, 43 RightInput: &FilterInput{Expression: &Expression{LeftInput: &ExpressionInput{ColumnValue: colValEnc}}}, 44 }, 45 } 46 47 var startMs, endMs uint64 48 49 if isKibana { 50 endMs = math.MaxUint64 51 startMs = uint64(0) 52 } else { 53 endMs = utils.GetCurrentTimeInMs() 54 // TODO: when "retention config" is done, pull value from config. 55 // StartEpochMs set to 4 months ago 56 startMs = endMs - TS_FOUR_MONTHS 57 58 } 59 60 singleNode := &ASTNode{ 61 AndFilterCondition: &Condition{FilterCriteria: []*FilterCriteria{&docFilterCriteria}}, 62 TimeRange: &dtu.TimeRange{ 63 StartEpochMs: startMs, 64 EndEpochMs: endMs, 65 }, 66 } 67 return singleNode 68 } 69 70 func CreateMgetReqASTNode(idColName string, idVal string, docTypeColName string, docTypeVal string, qid uint64) *ASTNode { 71 andFilterConditions := make([]*FilterCriteria, 0) 72 idDtype, err := CreateDtypeEnclosure(idVal, qid) 73 if err != nil { 74 log.Errorf("qid=%d, CreateMgetReqASTNode: failed to create DtypeEnclosure error: %+v", qid, err) 75 } 76 andFilterConditions = append(andFilterConditions, 77 &FilterCriteria{ 78 ExpressionFilter: &ExpressionFilter{ 79 LeftInput: &FilterInput{Expression: &Expression{LeftInput: &ExpressionInput{ColumnName: idColName}}}, 80 FilterOperator: Equals, 81 RightInput: &FilterInput{Expression: &Expression{LeftInput: &ExpressionInput{ColumnValue: idDtype}}}, 82 }, 83 }) 84 85 docTypeValDtype, err := CreateDtypeEnclosure(docTypeVal, qid) 86 if err != nil { 87 log.Errorf("qid=%d, CreateMgetReqASTNode: failed to create DtypeEnclosure error: %+v", qid, err) 88 } 89 andFilterConditions = append(andFilterConditions, 90 &FilterCriteria{ 91 ExpressionFilter: &ExpressionFilter{ 92 LeftInput: &FilterInput{Expression: &Expression{LeftInput: &ExpressionInput{ColumnName: docTypeColName}}}, 93 FilterOperator: Equals, 94 RightInput: &FilterInput{Expression: &Expression{LeftInput: &ExpressionInput{ColumnValue: docTypeValDtype}}}, 95 }, 96 }) 97 98 tsNow := utils.GetCurrentTimeInMs() 99 100 singleNode := &ASTNode{ 101 AndFilterCondition: &Condition{FilterCriteria: andFilterConditions}, 102 TimeRange: &dtu.TimeRange{ 103 // TODO: when "retention config" is done, pull value from config. 104 // StartEpochMs set to 4 months ago 105 StartEpochMs: tsNow - TS_FOUR_MONTHS, 106 //TODO: add a way to deal with this being empty 107 EndEpochMs: tsNow, 108 }, 109 } 110 return singleNode 111 }