util

package
v0.0.0-...-d8099e1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2023 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const AppbaseIDEnvName = "APPBASE_ID"
View Source
const ArcIDEnvName = "ARC_ID"
View Source
const ClusterIDEnvName = "CLUSTER_ID"

Variables

View Source
var ACCAPI = "https://accapi.appbase.io/"

ACCAPI URL

View Source
var AppbasePublicKey = "f6c7f3e774cc07b73cf97f6a561d940274cd20abd5f64d0ebe6f9ef7a63667f1"

Appbase Public Key to validate the offline license

View Source
var Billing string

Billing is a build time variable

View Source
var ClusterBilling string

ClusterBilling is a build time variable

View Source
var ClusterID string

ClusterID represents the (decrypted) cluster id for appbase.io cloud and byoc clusters

View Source
var CreateSchema bool = false

Whether or not to create schema

View Source
var HostedBilling string

HostedBilling is a build time variable

View Source
var IsTelemetryEnabled bool = true

We set it in the `main.go` based on the env variable and run time flag defaults to `true`

View Source
var MachineID string

Machine ID

View Source
var MemoryAllocated uint64

Memory Allocated in number of bytes

View Source
var NodeCount = 1

NodeCount is the current node count, defaults to 1

View Source
var OfflineBilling bool

OfflineBilling is true when license key is defined

View Source
var OfflineGracePeriod = 30 // in days

OfflineGracePeriod is the time duration in days that defines the grace period for expired license. Arc would start throwing 402 error when OfflineGracePeriod is passed.

View Source
var Opensource string

Opensource is a build time variable

View Source
var Port int

Port ReactiveSearch port

View Source
var RunTime string

RunTime

View Source
var TestURL = "http://foo:bar@localhost:8000"

TestURL for ReactiveSearch

View Source
var Version string

Version ReactiveSearch version

Functions

func AddMigrationScript

func AddMigrationScript(migration Migration)

AddMigrationScript allows you to add a migration script

func AddSyncScript

func AddSyncScript(syncScript SyncPluginCache)

AddSyncScript allows you to add a sync cache script

func ApplyDelta

func ApplyDelta(text string, delta string) (string, error)

ApplyDelta applies the delta to the passed text1 and returns the text2.

func BillingBlacklistedPaths

func BillingBlacklistedPaths() []string

BillingBlacklistedPaths will return an array of paths that should not be affected if billing is enabled.

func BillingMiddleware

func BillingMiddleware(next http.Handler) http.Handler

BillingMiddleware function to be called for each request

func BillingMiddlewareOffline

func BillingMiddlewareOffline(next http.Handler) http.Handler

BillingMiddlewareOffline function to be called for each request when offline billing is used

func CalculateBodyDiff

func CalculateBodyDiff(originalReqBody io.ReadCloser, modifiedReqBody io.ReadCloser) string

Calculate the diff in the body

func CalculateBodyStringDiff

func CalculateBodyStringDiff(originalReqBody string, modifiedReqBody string) string

Calculate the diff in the body

func CalculateHeaderDiff

func CalculateHeaderDiff(originalReqHeader http.Header, modifiedReqHeader http.Header) string

func CalculateMethodDiff

func CalculateMethodDiff(originalReq *http.Request, modifiedReq *http.Request) string

Calculate method difference

func CalculateMethodStringDiff

func CalculateMethodStringDiff(originalReqMethod string, modifiedReqMethod string) string

Calculate method difference

func CalculateRequestDiff

func CalculateRequestDiff(originalReq *http.Request, modifiedReq *http.Request) *difference.Difference

Calculate the diff between the passed bodies. We will find the difference between - body - headers - URI - method

func CalculateResponseDiff

func CalculateResponseDiff(originalRes *httptest.ResponseRecorder, modifiedRes *httptest.ResponseRecorder) *difference.Difference

Calculate the diff between the passed bodies. We will find the difference between - body. - headers

func CalculateStringDiff

func CalculateStringDiff(text1 string, text2 string) string

CalculateStringDiff calculates the delta diff between the passed strings

func CalculateUriDiff

func CalculateUriDiff(originalReq *http.Request, modifiedReq *http.Request) string

Calculate the difference in the URI

func CheckIfIndexExists

func CheckIfIndexExists(ctx context.Context, indexName string) bool

func CleanPasswordFromURL

func CleanPasswordFromURL(URL string) string

CleanPasswordFromURL will clean password from the URL if it is present

func Contains

func Contains(slice []string, val string) bool

Contains checks the presence of a string in the given string slice.

func CountComponents

func CountComponents(route string) (int, int)

CountComponents returns the numbers of "/" and "vars" present in the route.

func DaysInCurrentYear

func DaysInCurrentYear() int

DaysInCurrentYear returns the number of days in the current year.

func DaysInMonth

func DaysInMonth(m time.Month, year int) int

DaysInMonth returns the number of days in a month for a given year.

func DaysInYear

func DaysInYear(year int) int

DaysInYear returns the number of days in a given year.

func DebugDeprecationWarns

func DebugDeprecationWarns(formattedStr string) bool

DebugDeprecationWarns converts all the error logs containing deprecation warnings to debug logs so that it doesn't invoke sentry

func DecodeBase64Key

func DecodeBase64Key(encoded string) ([]byte, error)

DecodeBase64Key decodes a base64 input

func DeepCloneRequest

func DeepCloneRequest(req *http.Request) (*http.Request, error)

Deep clone the request body by also reading the body and keeping the body back in the original one.

func DeepCloneResponse

func DeepCloneResponse(res *httptest.ResponseRecorder) (*httptest.ResponseRecorder, error)

Deep clone the response body by also reading the body and keeping the body back in the original response.

func GetAppbaseID

func GetAppbaseID() (string, error)

GetAppbaseID to get appbase id

func GetArcID

func GetArcID() (string, error)

GetArcID returns the cluster ID

func GetCachedPlanDetails

func GetCachedPlanDetails() ([]byte, error)

func GetClient6

func GetClient6() *es6.Client

GetClient6 returns the es6 client

func GetClient7

func GetClient7() *es7.Client

GetClient7 returns the es7 client

func GetESURL

func GetESURL() string

GetESURL returns elasticsearch url with escaped auth

func GetExpiryTime

func GetExpiryTime() time.Time

GetExpiryTime returns the expiry time

func GetFeatureCache

func GetFeatureCache() bool

GetFeatureCache returns the featureCache

func GetFeatureCustomEvents

func GetFeatureCustomEvents() bool

GetFeatureCustomEvents returns the featureCustomEvents

func GetFeatureEcommerce

func GetFeatureEcommerce() bool

GetFeatureEcommerce returns the featureEcommerce

func GetFeaturePipelines

func GetFeaturePipelines() bool

GetFeatureCache returns the featurePipelines

func GetFeatureRules

func GetFeatureRules() bool

GetFeatureRules returns the featureRules

func GetFeatureSearchGrader

func GetFeatureSearchGrader() bool

GetFeatureSearchGrader returns the featureSearchGrader

func GetFeatureSearchRelevancy

func GetFeatureSearchRelevancy() bool

GetFeatureSearchRelevancy returns the featureSearchRelevancy

func GetFeatureSuggestions

func GetFeatureSuggestions() bool

GetFeatureSuggestions returns the featureSuggestions

func GetFeatureUIBuilderPremium

func GetFeatureUIBuilderPremium() bool

GetFeatureUIBuilderPremium returns the featureUIBuilder

func GetHitsForIndex

func GetHitsForIndex(response *elastic.SearchResult, index string) []*elastic.SearchHit

Filters ES hits by index name

func GetIndexFilterQueryEs6

func GetIndexFilterQueryEs6(query *es6.BoolQuery, indices ...string) *es6.BoolQuery

GetIndexFilterQueryEs6 apply the index filtering logic

func GetIndexFilterQueryEs7

func GetIndexFilterQueryEs7(query *es7.BoolQuery, indices ...string) *es7.BoolQuery

GetIndexFilterQueryEs7 apply the index filtering logic

func GetNumberOfMachines

func GetNumberOfMachines() int64

To retrieve the total number of machines

func GetPlanDetails

func GetPlanDetails() *[]byte

GetPlanDetails returns the plan details

func GetReplicas

func GetReplicas() int

GetReplicas calculates the number of replicas to set

func GetSemanticVersion

func GetSemanticVersion() string

GetSemanticVersion returns the es version

func GetSyncInterval

func GetSyncInterval() int

func GetTimeValidity

func GetTimeValidity() int64

GetTimeValidity returns the time validity

func GetTotalNodes

func GetTotalNodes() (int, error)

GetTotalNodes retrieves the number of es nodes

func GetVersion

func GetVersion() int

GetVersion returns the es version

func GetZincData

func GetZincData() (string, string, string)

GetZincData will return the zinc data from the environment.

The return will be three strings: - URL - username - password

func HTTPClient

func HTTPClient() *http.Client

HTTPClient returns an http client with reasonable timeout defaults. See: https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779 This client will cap the TCP connect and TLS handshake timeouts, as well as establishing an end-to-end request timeout.

func HiddenIndexSettings

func HiddenIndexSettings() string

HiddenIndexSettings to set plugin indices as hidden index

func IndicesFromRequest

func IndicesFromRequest(r *http.Request) []string

IndicesFromRequest extracts index patterns from the request url (from var "{index}"). It returns an empty slice if {index} is not present in the route.

func IntervalForRange

func IntervalForRange(from, to string) (string, error)

IntervalForRange returns the interval in seconds for a given time range. It expects the time arguments in RFC3339 format. The interval is calculated by: I = (25 * D) seconds, where D = duration (in hours), I = interval.

func IsBillingEnabled

func IsBillingEnabled() bool

func IsExists

func IsExists(a string, list []string) bool

IsExists searches for an element in an array

func IsProductionPlan

func IsProductionPlan() bool

IsProductionPlan validates if the user's plan is a production plan

func IsRSAPIValidateRoute

func IsRSAPIValidateRoute(req *http.Request) bool

Returns true if req path matches with RS API validate route

func IsSubset

func IsSubset(sub, super []string) bool

IsSubset returns true if the first slice is completely contained in the second slice. There must be at least the same number of duplicate values in second as there are in first.

func MakeHttpRequest

func MakeHttpRequest(method string, url string, requestBody interface{}) (interface{}, error, *http.Response)

func MakeRequest

func MakeRequest(url, method string, reqBody []byte) ([]byte, *http.Response, error)

MakeRequest helps in proxing http requests

func MakeRequestWithHeader

func MakeRequestWithHeader(url, method string, reqBody []byte, headers http.Header) ([]byte, *http.Response, error)

MakeRequestWithHeader helps in proxing http requests with header support

func Min

func Min(a, b int) int

Min return min of two integers

func NewClient

func NewClient()

NewClient instantiates the ES v6 and v7 clients

func NewZincClient

func NewZincClient()

NewClient instantiates the Zinc Client

func ProxyACCAPI

func ProxyACCAPI(proxyConfig ProxyConfig) (*http.Response, error)

func RandStr

func RandStr() string

RandStr returns "node" field of a UUID. See: https://tools.ietf.org/html/rfc4122#section-4.1.6

func ReportHostedArcUsage

func ReportHostedArcUsage()

ReportHostedArcUsage reports ReactiveSearch usage by hosted cluster, intended to be called every hour

func ReportUsage

func ReportUsage()

ReportUsage reports ReactiveSearch usage, intended to be called every hour

func SetClusterPlan

func SetClusterPlan()

SetClusterPlan fetches the cluster plan & sets the Tier value

func SetDefaultIndexTemplate

func SetDefaultIndexTemplate() error

SetDefaultIndexTemplate sets default template for user indexes

func SetDefaultTier

func SetDefaultTier()

SetDefaultTier sets the default tier when billing is disabled

func SetExpiryTime

func SetExpiryTime(time time.Time)

SetTimeValidity sets the expiry time

func SetFeatureCache

func SetFeatureCache(val bool)

SetFeatureEcommerce sets the featureCache

func SetFeatureCustomEvents

func SetFeatureCustomEvents(val bool)

SetFeatureCustomEvents returns the time validity

func SetFeatureEcommerce

func SetFeatureEcommerce(val bool)

SetFeatureEcommerce sets the featureEcommerce

func SetFeaturePipelines

func SetFeaturePipelines(val bool)

SetFeatureEcommerce sets the featurePipelines

func SetFeatureRules

func SetFeatureRules(val bool)

SetFeatureRules sets the featureRules

func SetFeatureSearchGrader

func SetFeatureSearchGrader(val bool)

SetFeatureSearchGrader sets the featureSearchGrader

func SetFeatureSearchRelevancy

func SetFeatureSearchRelevancy(val bool)

SetFeatureSearchRelevancy sets the featureSearchRelevancy

func SetFeatureSuggestions

func SetFeatureSuggestions(val bool)

SetFeatureSuggestions returns the time validity

func SetFeatureUIBuilderPremium

func SetFeatureUIBuilderPremium(val bool)

SetFeatureUIBuilderPremium sets the featureUIBuilder

func SetPlanDetailsHook

func SetPlanDetailsHook(fn *func([]byte))

func SetSyncInterval

func SetSyncInterval(interval int) error

func SetSystemIndexTemplate

func SetSystemIndexTemplate() error

SetSystemIndexTemplate sets default template for system indexes

func SetTier

func SetTier(plan *Plan)

SetTier sets the tier value

func SetTimeValidity

func SetTimeValidity(time int64)

SetTimeValidity returns the time validity

func ShouldProxyToACCAPI

func ShouldProxyToACCAPI() bool

To decide whether to proxy the ACCAPI

func StructToMap

func StructToMap(response interface{}) interface{}

func ToStringSlice

func ToStringSlice(g interface{}) ([]string, error)

ToStringSlice converts a interface{} type to []string. It basically converts all the elements of the slice to its string representation using fmt.Sprint.

func ValidateArcID

func ValidateArcID(statusCode int)

ValidateArcID validates the APPBASE_ID by checking the response returned from the ACCAPI

func ValidateIndex

func ValidateIndex(pattern string, index string) (bool, error)

ValidateIndex validates an index against a pattern

func ValidatePlans

func ValidatePlans(validPlans []Plan, byPassValidation bool) bool

ValidatePlans validates the user's plan against the valid plans

func WithPrecision

func WithPrecision(num float64, precision int) float64

WithPrecision returns the floating point number with the given precision.

func WriteBackError

func WriteBackError(w http.ResponseWriter, err string, code int)

WriteBackError writes the given error message as a json response to the response writer.

func WriteBackMessage

func WriteBackMessage(w http.ResponseWriter, message string, code int)

WriteBackMessage writes the given message as a json response to the response writer.

func WriteBackRaw

func WriteBackRaw(w http.ResponseWriter, raw []byte, code int)

WriteBackRaw writes the given json encoded bytes to the response writer.

Types

type ArcInstance

type ArcInstance struct {
	SubscriptionID string `json:"subscription_id"`
}

ArcInstance TBD: remove struct

type ArcInstanceDetails

type ArcInstanceDetails struct {
	NodeCount               int                    `json:"node_count"`
	Description             string                 `json:"description"`
	SubscriptionID          string                 `json:"subscription_id"`
	SubscriptionCanceled    bool                   `json:"subscription_canceled"`
	Trial                   bool                   `json:"trial"`
	TrialValidity           int64                  `json:"trial_validity"`
	CreatedAt               int64                  `json:"created_at"`
	Tier                    *Plan                  `json:"tier"`
	TierValidity            int64                  `json:"tier_validity"`
	TimeValidity            int64                  `json:"time_validity"`
	Metadata                map[string]interface{} `json:"metadata"`
	FeatureCustomEvents     bool                   `json:"feature_custom_events"`
	FeatureSuggestions      bool                   `json:"feature_suggestions"`
	FeatureRules            bool                   `json:"feature_rules"`
	FeatureSearchRelevancy  bool                   `json:"feature_search_relevancy"`
	FeatureSearchGrader     bool                   `json:"feature_search_grader"`
	FeatureEcommerce        bool                   `json:"feature_ecommerce"`
	FeatureUIBuilderPremium bool                   `json:"feature_uibuilder_premium"`
	FeatureCache            bool                   `json:"feature_cache"`
	FeaturePipelines        bool                   `json:"feature_pipelines"`
	ClusterID               string                 `json:"cluster_id"`
	NumberOfMachines        int64                  `json:"number_of_machines"`
	CustomerID              string                 `json:"customer_id"`
}

ArcInstanceDetails contains the info about a ReactiveSearch Instance

type ArcInstanceResponse

type ArcInstanceResponse struct {
	ArcInstances []ArcInstanceDetails `json:"instances"`
}

ArcInstanceResponse TBD: Remove struct

type ArcUsage

type ArcUsage struct {
	ArcID          string `json:"arc_id"`
	SubscriptionID string `json:"subscription_id"`
	Quantity       int    `json:"quantity"`
	ClusterID      string `json:"cluster_id"`
	MachineID      string `json:"machine_id"`
}

ArcUsage struct is used to report time usage

type ArcUsageResponse

type ArcUsageResponse struct {
	Accepted      bool   `json:"accepted"`
	FailureReason string `json:"failure_reason"`
	ErrorMsg      string `json:"error_msg"`
	WarningMsg    string `json:"warning_msg"`
	StatusCode    int    `json:"status_code"`
	TimeValidity  int64  `json:"time_validity"`
}

ArcUsageResponse stores the response from ACCAPI

type BuildArc

type BuildArc struct {
	Tier                *Plan
	FeatureCustomEvents bool
	FeatureSuggestions  bool
	// contains filtered or unexported fields
}

func StartArc

func StartArc(b *BuildArc) BuildArc

func (*BuildArc) Close

func (b *BuildArc) Close()

func (*BuildArc) Start

func (b *BuildArc) Start()

type ClusterPlan

type ClusterPlan struct {
	Tier                    *Plan  `json:"tier"`
	FeatureCustomEvents     bool   `json:"feature_custom_events"`
	FeatureSuggestions      bool   `json:"feature_suggestions"`
	FeatureRules            bool   `json:"feature_rules"`
	FeatureSearchRelevancy  bool   `json:"feature_search_relevancy"`
	FeatureSearchGrader     bool   `json:"feature_search_grader"`
	FeatureEcommerce        bool   `json:"feature_ecommerce"`
	FeatureCache            bool   `json:"feature_cache"`
	FeaturePipelines        bool   `json:"feature_pipelines"`
	FeatureUIBuilderPremium bool   `json:"feature_uibuilder_premium"`
	Trial                   bool   `json:"trial"`
	TrialValidity           int64  `json:"trial_validity"`
	TierValidity            int64  `json:"tier_validity"`
	TimeValidity            int64  `json:"time_validity"`
	SubscriptionID          string `json:"subscription_id"`
	ClusterID               string `json:"cluster_id"`
	NumberOfMachines        int64  `json:"number_of_machines"`
	SubscriptionCanceled    bool   `json:"subscription_canceled"`
	CreatedAt               int64  `json:"created_at"`
	CustomerID              string `json:"customer_id"`
}

type ClusterPlanResponse

type ClusterPlanResponse struct {
	Plan ClusterPlan `json:"plan"`
}

Cluster plan response type

type Error

type Error struct {
	Message string
	Err     error
}

type IndexMappingResponse

type IndexMappingResponse map[string]interface{}

Handle unstrctured JSON data from the mapping endpoint

func GetIndexMapping

func GetIndexMapping(indexName string, ctx context.Context) (resp IndexMappingResponse, err error)

Fetch the index mapping manually using the following function Make the request using an es7 client method that allows direct requests. Using that gives us the addition of automatic request retries in case the first request fails (due to ES not being available?)

We will extract the unstructured JSON data from the endpoint and parse it to a map so that it can be directly used.

On error, an empty data body will be returned along with the error itself.

Errors will be returned accordingly and verbosed if the error occurs while extracting the JSON data. There will be no verbose if the error occurs while hitting the endpoint. Those errors are expected to be handled by the calling function.

type Migration

type Migration interface {
	// ConditionCheck method allows you to control the script
	// execution only when a certain confition met
	ConditionCheck() (bool, *Error)
	// This function allows you to execute the migration logic
	// Execute the non-blocking scripts in a go routine and return the Error as nil
	Script() *Error
	// To determine wether to run script synchronously or asynchronously.
	// Sync scripts will cause the fatal error if failed
	IsAsync() bool
}

func GetMigrationScripts

func GetMigrationScripts() []Migration

type Plan

type Plan int

An enum having a list of valid plans

const (
	ArcBasic Plan = iota
	ArcStandard
	ArcEnterprise
	HostedArcBasic
	HostedArcBasicV2
	HostedArcStandard
	HostedArcEnterprise
	Sandbox
	Hobby
	Starter
	ProductionFirst
	ProductionSecond
	ProductionThird
	Sandbox2019
	Hobby2019
	Starter2019
	Sandbox2020
	Hobby2020
	Starter2020
	ProductionFirst2019
	ProductionSecond2019
	ProductionThird2019
	ProductionFourth2019
	Starter2021
	ProductionFirst2021
	ProductionSecond2021
	ProductionThird2021
	HostedArcStandard2021
	HostedArcEnterprise2021
)

func GetTier

func GetTier() *Plan

GetTier returns the current tier

func (Plan) MarshalJSON

func (o Plan) MarshalJSON() ([]byte, error)

MarshalJSON is the implementation of the Marshaler interface for marshaling Plan type.

func (Plan) String

func (o Plan) String() string

String is the implementation of Stringer interface that returns the string representation of Plan type.

func (*Plan) UnmarshalJSON

func (o *Plan) UnmarshalJSON(bytes []byte) error

UnmarshalJSON is the implementation of the Unmarshaler interface for unmarshaling Plan type.

type ProxyConfig

type ProxyConfig struct {
	URL    string                 `json:"url"`
	Method string                 `json:"method"`
	Body   map[string]interface{} `json:"body,omitempty"`
}

type Retrier

type Retrier struct {
	// contains filtered or unexported fields
}

Retrier is a custom Retry implementation.

func NewRetrier

func NewRetrier() *Retrier

NewRetrier returns a new retrier with exponential backoff strategy.

func (*Retrier) Retry

func (r *Retrier) Retry(ctx context.Context, retry int, req *http.Request, resp *http.Response, err error) (time.Duration, bool, error)

Retry is a custom retry implementation.

type Subscripton

type Subscripton struct {
	SubscriptonCanceled bool
	SubscriptionID      string
	CreatedAt           int64
}

type SyncPluginCache

type SyncPluginCache interface {
	// Plugin name
	PluginName() string
	// Index to retrieve data
	Index() string
	// Method to set the ES data to plugin cache
	SetCache(response *elastic.SearchResult) error
}

func GetSyncScripts

func GetSyncScripts() []SyncPluginCache

type WrapKitLoggerDebug

type WrapKitLoggerDebug struct {
	log.Logger
}

func (WrapKitLoggerDebug) Printf

func (logger WrapKitLoggerDebug) Printf(format string, vars ...interface{})

type WrapKitLoggerError

type WrapKitLoggerError struct {
	log.Logger
}

func (WrapKitLoggerError) Printf

func (logger WrapKitLoggerError) Printf(format string, vars ...interface{})

type ZincClient

type ZincClient struct {
	URL        string
	Username   string
	Password   string
	AuthHeader string
}

func GetZincClient

func GetZincClient() *ZincClient

GetZincClient will return the zinc client and only init it once.

func (*ZincClient) MakeRequest

func (zc *ZincClient) MakeRequest(endpoint string, method string, body []byte, headers *http.Header) (*http.Response, error)

MakeRequest will allow making a request to zinc index.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL