github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/layer_generators/timer_layer.go.tmpl (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  // Code generated by "make store-layers"
     5  // DO NOT EDIT
     6  
     7  package store
     8  
     9  import (
    10  	"context"
    11  	timemodule "time"
    12  	
    13      "github.com/vnforks/kid/v5/einterfaces"
    14  	"github.com/vnforks/kid/v5/model"
    15  )
    16  
    17  type {{.Name}} struct {
    18  	Store
    19  	Metrics einterfaces.MetricsInterface
    20  {{range $index, $element := .SubStores}}	{{$index}}Store {{$index}}Store
    21  {{end}}
    22  }
    23  
    24  {{range $index, $element := .SubStores}}func (s *{{$.Name}}) {{$index}}() {{$index}}Store {
    25  	return s.{{$index}}Store
    26  }
    27  
    28  {{end}}
    29  
    30  {{range $index, $element := .SubStores}}type {{$.Name}}{{$index}}Store struct {
    31  	{{$index}}Store
    32  	Root *{{$.Name}}
    33  }
    34  
    35  {{end}}
    36  
    37  {{range $substoreName, $substore := .SubStores}}
    38  {{range $index, $element := $substore.Methods}}
    39  func (s *{{$.Name}}{{$substoreName}}Store) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
    40  	start := timemodule.Now()
    41  	{{if $element.Results | len | eq 0}}
    42  	s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
    43  	{{else}}
    44  	{{$element.Results | genResultsVars}} := s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
    45  	{{end}}
    46  	elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
    47  	if s.Root.Metrics != nil {
    48  		success := "false"
    49  		if {{$element.Results | errorToBoolean}} {
    50  			success = "true"
    51  		}
    52  		s.Root.Metrics.ObserveStoreMethodDuration("{{$substoreName}}Store.{{$index}}", success, elapsed)
    53  	{{ with ($element.Results | genResultsVars) -}}
    54  	}
    55  	return {{ . }}
    56  	{{- else -}}
    57  	}
    58  	{{- end }}
    59  }
    60  {{end}}
    61  {{end}}
    62  
    63  {{range $index, $element := .Methods}}
    64  func (s *{{$.Name}}) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
    65  	{{if $element.Results | len | eq 0}}s.Store.{{$index}}({{$element.Params | joinParams}})
    66  	{{else}}return s.Store.{{$index}}({{$element.Params | joinParams}})
    67  	{{end}}}
    68  {{end}}
    69  
    70  func New{{.Name}}(childStore Store, metrics einterfaces.MetricsInterface) *{{.Name}} {
    71  	newStore := {{.Name}}{
    72  		Store: childStore,
    73  		Metrics: metrics,
    74  	}
    75  	{{range $substoreName, $substore := .SubStores}}
    76  	newStore.{{$substoreName}}Store = &{{$.Name}}{{$substoreName}}Store{{"{"}}{{$substoreName}}Store: childStore.{{$substoreName}}(), Root: &newStore}{{end}}
    77  	return &newStore
    78  }