github.com/dgraph-io/dgraph@v1.2.8/graphql/admin/health.go (about)

     1  /*
     2   * Copyright 2019 Dgraph Labs, Inc. and Contributors
     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 admin
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/dgraph-io/dgraph/gql"
    24  	"github.com/dgraph-io/dgraph/graphql/schema"
    25  )
    26  
    27  const (
    28  	errNoConnection healthStatus = "ErrNoConnection"
    29  	noGraphQLSchema healthStatus = "NoGraphQLSchema"
    30  	healthy         healthStatus = "Healthy"
    31  )
    32  
    33  type healthStatus string
    34  
    35  type healthResolver struct {
    36  	status healthStatus
    37  	format string
    38  }
    39  
    40  var statusMessage = map[healthStatus]string{
    41  	errNoConnection: "Unable to contact Dgraph",
    42  	noGraphQLSchema: "Dgraph connection established but there's no GraphQL schema.",
    43  	healthy:         "Dgraph connection established and serving GraphQL schema.",
    44  }
    45  
    46  func (hr *healthResolver) Rewrite(q schema.Query) (*gql.GraphQuery, error) {
    47  	msg := "message"
    48  	status := "status"
    49  
    50  	for _, f := range q.SelectionSet() {
    51  		if f.Name() == "message" {
    52  			msg = f.ResponseName()
    53  		}
    54  		if f.Name() == "status" {
    55  			status = f.ResponseName()
    56  		}
    57  	}
    58  
    59  	hr.format = fmt.Sprintf(`{"%s":[{"%s":"%%s","%s":"%%s"}]}`, q.ResponseName(), msg, status)
    60  	return nil, nil
    61  }
    62  
    63  func (hr *healthResolver) Query(ctx context.Context, query *gql.GraphQuery) ([]byte, error) {
    64  	return []byte(fmt.Sprintf(hr.format, statusMessage[hr.status], string(hr.status))), nil
    65  }