github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/api/data_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  	"net/http"
    19  
    20  	"github.com/uber/aresdb/memstore"
    21  	"github.com/uber/aresdb/utils"
    22  
    23  	"github.com/gorilla/mux"
    24  )
    25  
    26  // DataHandler handles data ingestion requests from the ingestion pipeline.
    27  type DataHandler struct {
    28  	memStore memstore.MemStore
    29  }
    30  
    31  // NewDataHandler creates a new DataHandler.
    32  func NewDataHandler(memStore memstore.MemStore) *DataHandler {
    33  	return &DataHandler{
    34  		memStore: memStore,
    35  	}
    36  }
    37  
    38  // Register registers http handlers.
    39  func (handler *DataHandler) Register(router *mux.Router, wrappers ...utils.HTTPHandlerWrapper) {
    40  	router.HandleFunc("/{table}/{shard}", utils.ApplyHTTPWrappers(handler.PostData, wrappers)).Methods(http.MethodPost)
    41  }
    42  
    43  // PostData swagger:route POST /data/{table}/{shard} postData
    44  // Post new data batch to a existing table shard
    45  // Consumes:
    46  //    - application/upsert-data
    47  //
    48  // Responses:
    49  //    default: errorResponse
    50  //        200: noContentResponse
    51  func (handler *DataHandler) PostData(w http.ResponseWriter, r *http.Request) {
    52  	var postDataRequest PostDataRequest
    53  	err := ReadRequest(r, &postDataRequest)
    54  	if err != nil {
    55  		RespondWithError(w, err)
    56  		return
    57  	}
    58  
    59  	upsertBatch, err := memstore.NewUpsertBatch(postDataRequest.Body)
    60  	if err != nil {
    61  		RespondWithBadRequest(w, err)
    62  		return
    63  	}
    64  
    65  	err = handler.memStore.HandleIngestion(postDataRequest.TableName, postDataRequest.Shard, upsertBatch)
    66  	if err != nil {
    67  		RespondWithError(w, err)
    68  		return
    69  	}
    70  
    71  	RespondWithJSONObject(w, nil)
    72  }