github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/server/utils/utils.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 server_utils
    18  
    19  import (
    20  	"github.com/siglens/siglens/pkg/hooks"
    21  	"github.com/siglens/siglens/pkg/segment/structs"
    22  	"github.com/siglens/siglens/pkg/utils"
    23  	"github.com/valyala/fasthttp"
    24  )
    25  
    26  const ELASTIC_PREFIX string = "/elastic"
    27  const OTSDB_PREFIX string = "/otsdb"
    28  const INFLUX_PREFIX string = "/influx"
    29  const PROMQL_PREFIX string = "/promql"
    30  const OTLP_PREFIX string = "/otlp"
    31  const API_PREFIX string = "/api"
    32  const LOKI_PREFIX string = "/loki"
    33  const HEROKU_ADDON_PREFIX string = "/heroku/resources"
    34  
    35  // This function reduces some boilerplate code by handling the logic for
    36  // injecting orgId if necessary, or using the default.
    37  func CallWithOrgIdQuery(handler func(*fasthttp.RequestCtx, uint64), ctx *fasthttp.RequestCtx) {
    38  	orgId := uint64(0)
    39  	var err error
    40  	if hook := hooks.GlobalHooks.GetOrgIdHookQuery; hook != nil {
    41  		orgId, err = hook(ctx)
    42  		if err != nil {
    43  			responsebody := make(map[string]interface{})
    44  			ctx.SetStatusCode(fasthttp.StatusUnauthorized)
    45  			responsebody["error"] = err.Error()
    46  			utils.WriteJsonResponse(ctx, responsebody)
    47  			return
    48  		}
    49  	}
    50  
    51  	handler(ctx, orgId)
    52  }
    53  
    54  func CallWithOrgId(handler func(*fasthttp.RequestCtx, uint64), ctx *fasthttp.RequestCtx) {
    55  	orgId := uint64(0)
    56  	var err error
    57  	if hook := hooks.GlobalHooks.GetOrgIdHook; hook != nil {
    58  		orgId, err = hook(ctx)
    59  		if err != nil {
    60  			responsebody := make(map[string]interface{})
    61  			ctx.SetStatusCode(fasthttp.StatusUnauthorized)
    62  			responsebody["error"] = err.Error()
    63  			utils.WriteJsonResponse(ctx, responsebody)
    64  			return
    65  		}
    66  	}
    67  
    68  	handler(ctx, orgId)
    69  }
    70  
    71  func ExtractKibanaRequests(kibanaIndices []string, qid uint64) map[string]*structs.SegmentSearchRequest {
    72  	ssr := make(map[string]*structs.SegmentSearchRequest)
    73  
    74  	if hook := hooks.GlobalHooks.ExtractKibanaRequestsHook; hook != nil {
    75  		interfaces := hook(kibanaIndices, qid)
    76  		for k, v := range interfaces {
    77  			ssr[k] = v.(*structs.SegmentSearchRequest)
    78  		}
    79  	}
    80  
    81  	return ssr
    82  }