github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/api/sql_query_handler.go (about)

     1  //  Copyright (c) 2017-2018 Uber Technologies, 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package api
    16  
    17  import (
    18  	"github.com/uber/aresdb/query"
    19  	"github.com/uber/aresdb/utils"
    20  	"net/http"
    21  )
    22  
    23  // HandleSQL swagger:route POST /query/sql querySQL
    24  // query in SQL
    25  //
    26  // Consumes:
    27  //    - application/json
    28  //    - application/hll
    29  //
    30  // Produces:
    31  //    - application/json
    32  //
    33  // Responses:
    34  //    default: errorResponse
    35  //        200: aqlResponse
    36  //        400: aqlResponse
    37  func (handler *QueryHandler) HandleSQL(w http.ResponseWriter, r *http.Request) {
    38  	sqlRequest := SQLRequest{Device: -1}
    39  
    40  	if err := ReadRequest(r, &sqlRequest); err != nil {
    41  		RespondWithBadRequest(w, err)
    42  		utils.GetLogger().With(
    43  			"error", err,
    44  			"statusCode", http.StatusBadRequest,
    45  		).Error("failed to parse query")
    46  		return
    47  	}
    48  
    49  	var aqlQueries []query.AQLQuery
    50  	if sqlRequest.Body.Queries != nil {
    51  		aqlQueries = make([]query.AQLQuery, len(sqlRequest.Body.Queries))
    52  		startTs := utils.Now()
    53  		for i, sqlQuery := range sqlRequest.Body.Queries {
    54  			parsedAQLQuery, err := query.Parse(sqlQuery, utils.GetLogger())
    55  			if err != nil {
    56  				RespondWithBadRequest(w, err)
    57  				return
    58  			}
    59  			aqlQueries[i] = *parsedAQLQuery
    60  		}
    61  		sqlParseTimer := utils.GetRootReporter().GetTimer(utils.QuerySQLParsingLatency)
    62  		duration := utils.Now().Sub(startTs)
    63  		sqlParseTimer.Record(duration)
    64  
    65  	}
    66  
    67  	aqlRequest := AQLRequest{
    68  		Device:                sqlRequest.Device,
    69  		Verbose:               sqlRequest.Verbose,
    70  		Debug:                 sqlRequest.Debug,
    71  		Profiling:             sqlRequest.Profiling,
    72  		DeviceChoosingTimeout: sqlRequest.DeviceChoosingTimeout,
    73  		Accept:                sqlRequest.Accept,
    74  		Origin:                sqlRequest.Origin,
    75  		Body: query.AQLRequest{
    76  			Queries: aqlQueries,
    77  		},
    78  	}
    79  	handler.handleAQLInternal(aqlRequest, w, r)
    80  }