scenariolib

package
Version: v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2020 License: Apache-2.0 Imports: 16 Imported by: 2

Documentation

Overview

Package scenariolib handles everything need to execute a scenario and send all information to the usage analytics endpoint

Package scenariolib handles everything need to execute a scenario and send all information to the usage analytics endpoint

Package scenariolib handles everything need to execute a scenario and send all information to the usage analytics endpoint

Package scenariolib handles everything need to execute a scenario and send all information to the usage analytics endpoint

Package scenariolib handles everything need to execute a scenario and send all information to the usage analytics endpoint

Index

Constants

View Source
const (
	// JSUIVERSION Change this to the version of JSUI you want to appear to be using.
	JSUIVERSION string = "0.0.0.0;0.0.0.0"
	// DEFAULTTIMEBETWEENACTIONS The time in seconds to wait between the different actions inside a visit
	DEFAULTTIMEBETWEENACTIONS int = 5
	// ORIGINALL The origin level of All
	ORIGINALL string = "ALL"
)
View Source
const DEFAULTTIMEBETWEENVISITS int = 300

DEFAULTTIMEBETWEENVISITS The time for the bot to wait between visits, between 0 and X Seconds

View Source
const DEFAULT_STANDARD_DEVIATION_BETWEEN_VISITS int = 150

DEFAULT_STANDARD_DEVIATION_BETWEEN_VISITS The standard deviation when updating time between visits

View Source
const WEEKEND_MODIFIER = 10

WEEKEND_MODIFIER The modifier to multiply DEFAULTTIMEBETWEENVISITS during weekends

Variables

View Source
var (
	// Trace Trace logging level
	Trace *log.Logger

	// Info Info logging level
	Info *log.Logger

	// Warning Warning logging level
	Warning *log.Logger

	// Error Error logging level
	Error *log.Logger
)

Functions

func InitLogger

func InitLogger(traceHandle io.Writer, infoHandle io.Writer, warningHandle io.Writer, errorHandle io.Writer)

InitLogger Initialize the logger with different io.Writer for the the different logging levels

func Max

func Max(a, b int) int

Max function returning the maximum between two values of type int

func Min

func Min(a, b int) int

Min Function to return the minimal value between two integers, because Go "forgot" to code it...

func WaitBetweenActions

func WaitBetweenActions(timeToWait int, isConstant bool)

WaitBetweenActions Wait a random or constant number of seconds between user actions

Types

type ClickEvent

type ClickEvent struct {
	Probability  float64                `json:"probability"`
	ClickRank    int                    `json:"docNo"`
	Offset       int                    `json:"offset,omitempty"`
	Quickview    bool                   `json:"quickview,omitempty"`
	CustomData   map[string]interface{} `json:"customData,omitempty"`
	FakeClick    bool                   `json:"fakeClick,omitempty"`
	FakeResponse json.RawMessage        `json:"fakeResponse,omitempty"`
}

ClickEvent a struct representing a click, it is defined by a clickRank, an offset and a probability to click.

func (*ClickEvent) Execute

func (click *ClickEvent) Execute(v *Visit) error

Execute Execute the click event, sending a click event to the usage analytics

func (*ClickEvent) IsValid added in v0.4.0

func (click *ClickEvent) IsValid() (bool, string)

IsValid Validate a click event by applying different validation rules of dependant parameters etc.

type Config

type Config struct {
	// ScenarioMap A map of the scenarios in the config and their weights.
	ScenarioMap []*Scenario

	// OrgName The name of the Org where you run the bot.
	OrgName string `json:"orgName"`

	// GoodQueries An array of queries that are considered good (return results and good click rank).
	GoodQueries []string `json:"randomGoodQueries"`

	// BadQueries An array of queries that are considered bad (return no results or bad click rank).
	BadQueries []string `json:"randomBadQueries"`

	// GoodQueriesInLang An array of languages containing GoodQueries.
	GoodQueriesInLang map[string][]string `json:"goodQueriesInLanguage"`

	// BadQueriesInLang An array of languages containing BadQueries.
	BadQueriesInLang map[string][]string `json:"badQueriesInLanguage"`

	// Scenarios An array of scenarios to execute
	Scenarios []*Scenario `json:"scenarios"`

	// GlobalFilter A query expression to send along with each queries.
	GlobalFilter string `json:"globalfilter,omitempty"`

	// SearchEndpoint Override of the SearchEndpoint where to send the queries.
	SearchEndpoint string `json:"searchendpoint,omitempty"`

	// AnalyticsEndpoint Override of the default AnalyticsEndpoint where to send analytics.
	AnalyticsEndpoint string `json:"analyticsendpoint,omitempty"`

	// RandomData Override the bot default fake data.
	RandomData RandomData `json:"randomData,omitempty"`

	// PartialMatch PartialMath param to send with queries.
	PartialMatch bool `json:"partialMatch,omitempty"`

	// PartialMatchKeywords partialMatchKeywords param to send with queries.
	PartialMatchKeywords int `json:"partialMatchKeywords,omitempty"`

	// PartialMatchThreshold partialMatchThreshold param to send with queries.
	PartialMatchThreshold string `json:"partialMatchThreshold,omitempty"`

	// Pipeline The pipeline for the search queries.
	Pipeline string `json:"pipeline,omitempty"`

	// DontWaitBetweenVisits Do not wait between the visits.
	DontWaitBetweenVisits bool `json:"dontWaitBetweenVisits"`

	// DontWaitBetweenActions Do not wait between actions.
	DontWaitBetweenActions bool `json:"dontWaitBetweenActions"`

	// TimeBetweenVisits Time to wait between the visits in seconds
	TimeBetweenVisits int `json:"timeBetweenVisits,omitempty"`

	// TimeBetweenActions The time to wait between actions in seconds
	TimeBetweenActions int `json:"timeBetweenActions,omitempty"`

	// AnonymousThreshold The percentage of visits that are anonymous [0..1].
	AnonymousThreshold float64 `json:"anonymousThreshold,omitempty"`

	// AllowEntitlements Don't use that...
	AllowEntitlements bool `json:"allowEntitlements,omitempty"`

	// RandomCustomData An array of RandomCustomData to send with each queries.
	RandomCustomData []*RandomCustomData `json:"randomCustomData,omitempty"`

	// IsWaitConstant Do you want the wait time to be constant.
	IsWaitConstant bool `json:"isWaitConstant,omitempty"`

	// DefaultOriginLevel1 Override of the default OriginLevel1.
	DefaultOriginLevel1 string `json:"defaultOriginLevel1,omitempty"`

	// DefaultOriginLevel2 Override of the default OriginLevel2.
	DefaultOriginLevel2 string `json:"defaultOriginLevel2,omitempty"`

	// DefaultOriginLevel3 Override of the default OriginLevel3.
	DefaultOriginLevel3 string `json:"defaultOriginLevel3,omitempty"`

	// DefaultPageViewField Override of the DefaultPageViewField for ALL pageView Events.
	DefaultPageViewField string `json:"defaultPageViewField,omitempty"`
}

Config This is the struct that holds all the info on the current bot session.

func NewConfigFromPath

func NewConfigFromPath(jsonPath string) (*Config, error)

NewConfigFromPath Create a new config from a JSON config file path

jsonPath The path to the JSON file.

func NewConfigFromURL

func NewConfigFromURL(jsonURL string) (*Config, error)

NewConfigFromURL Create a new Config from an url to a JSON file

jsonURL The URL to the json config path

type CustomEvent

type CustomEvent struct {
	EventType  string                 `json:"eventType"`
	EventValue string                 `json:"eventValue"`
	CustomData map[string]interface{} `json:"customData,omitempty"`
}

CustomEvent a struct representing a search, is defined by a query to execute

func (*CustomEvent) Execute

func (custom *CustomEvent) Execute(v *Visit) error

Execute the search event, runs the query and sends a search event to the analytics.

func (*CustomEvent) IsValid added in v0.4.0

func (custom *CustomEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal.

type Event

type Event interface {
	Execute(v *Visit) error
	IsValid() (bool, string)
}

Event Generic interface for abstract type Event. All specific event types must define the Execute function

func ParseEvent

func ParseEvent(e *JSONEvent, c *Config) (Event, error)

ParseEvent A factory to create the correct event type coming from the JSON parse of the scenario definition.

type FacetEvent

type FacetEvent struct {
	FacetTitle string                 `json:"facetTitle"`
	FacetValue string                 `json:"facetValue"`
	FacetField string                 `json:"facetField"`
	CustomData map[string]interface{} `json:"customData,omitempty"`
}

FacetEvent represents a tab change event

func (*FacetEvent) Execute

func (facet *FacetEvent) Execute(v *Visit) error

Execute Sends the tabchange event to the analytics and modify the CQ for the following queries in the visit

func (*FacetEvent) IsValid added in v0.4.0

func (facet *FacetEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal.

type FakeSearchEvent

type FakeSearchEvent struct {
	FakeResponse *search.Response `json:"fakeResponse"`
}

FakeSearchEvent a struct representing a search, is the response to set in the visit

func (*FakeSearchEvent) Execute

func (fakeSearch *FakeSearchEvent) Execute(v *Visit) error

Execute the fake search event, set the Last response to the fake response

func (*FakeSearchEvent) IsValid added in v0.4.0

func (fakeSearch *FakeSearchEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal.

type JSONEvent

type JSONEvent struct {
	Type      string          `json:"type"`
	Arguments json.RawMessage `json:"arguments"`
}

JSONEvent An action taken by the user such as a search, a click, a SearchAndClick, etc. Type A string describing the type of event Arguments An array of the arguments to the event, specific to the type of event.

type RandomCustomData

type RandomCustomData struct {
	APIName string   `json:"apiname"`
	Values  []string `json:"values"`
}

RandomCustomData Structure of random values for a specific API name.

type RandomData added in v0.4.0

type RandomData struct {

	// Emails Override the defaults fake emails.
	Emails []string `json:"emailSuffixes,omitempty"`

	// FirstNames Override the defaults fake FirstNames.
	FirstNames []string `json:"firstNames,omitempty"`

	// LastNames Override the defaults fake LastNames.
	LastNames []string `json:"lastNames,omitempty"`

	// RandomIPs Override the defaults fake RandomIPs.
	RandomIPs []string `json:"randomIPs,omitempty"`

	// UserAgents Override the defaults fake UserAgents.
	UserAgents []string `json:"useragents,omitempty"`

	// MobileUserAgents Override the defaults fake MobileUserAgents.
	MobileUserAgents []string `json:"mobileuseragents,omitempty"`

	// Languages Override the defaults fake Languages.
	Languages []string `json:"languages,omitempty"`
}

RandomData An override of the bot default random/fake data.

type Scenario

type Scenario struct {
	// A Name given to the scenario for easier logging.
	Name string `json:"name"`

	// A Weight for randomizing scenarios.
	Weight int `json:"weight"`

	// A UserAgent string representing the visit
	UserAgent string `json:"useragent,omitempty"`

	// An Events array of actions the user will take
	Events []JSONEvent `json:"events"`

	// A Language for this scenario
	Language string `json:"language,omitempty"`

	// Mobile A boolean value if this visit is forced on mobile
	Mobile bool `json:"mobile,omitempty"`
}

Scenario Represents one visit to the search

type SearchAndClickEvent

type SearchAndClickEvent struct {
	Query        string                 `json:"queryText"`
	Probability  float64                `json:"probability"`
	DocTitle     string                 `json:"docClickTitle,omitempty"`
	MatchField   string                 `json:"matchField,,omitempty"`
	MatchPattern string                 `json:"matchPattern,omitempty"`
	Quickview    bool                   `json:"quickview,omitempty"`
	CaseSearch   bool                   `json:"caseSearch,omitempty"`
	InputTitle   string                 `json:"inputTitle,omitempty"`
	CustomData   map[string]interface{} `json:"customData,omitempty"`
	RegexMatch   *regexp.Regexp
}

SearchAndClickEvent represents a search event followed by a click on a specific document found by the title

func (*SearchAndClickEvent) Execute

func (searchClick *SearchAndClickEvent) Execute(v *Visit) error

Execute the search and click event sending both events to the analytics

func (*SearchAndClickEvent) IsValid added in v0.4.0

func (searchClick *SearchAndClickEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal. And compilation of the regex if available.

type SearchEvent

type SearchEvent struct {
	Query         string                 `json:"queryText,omitempty"`
	IgnoreEvent   bool                   `json:"ignoreEvent,omitempty"`
	GoodQuery     bool                   `json:"goodQuery,omitempty"`
	ActionCause   string                 `json:"actionCause,omitempty"`
	CaseSearch    bool                   `json:"caseSearch,omitempty"`
	InputTitle    string                 `json:"inputTitle,omitempty"`
	MatchLanguage bool                   `json:"matchLanguage,omitempty"`
	CustomData    map[string]interface{} `json:"customData,omitempty"`
	Keywords      string
	ActionType    string
}

SearchEvent a struct representing a search, is defined by a query to execute

func (*SearchEvent) Execute

func (search *SearchEvent) Execute(visit *Visit) (err error)

Execute the search event, runs the query and sends a search event to the analytics. Returns an error if something went wrong.

func (*SearchEvent) IsValid added in v0.4.0

func (search *SearchEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal.

type SetOriginEvent

type SetOriginEvent struct {
	OriginLevel1 string `json:"originLevel1"`
	OriginLevel2 string `json:"originLevel2,omitempty"`
	OriginLevel3 string `json:"originLevel3,omitempty"`
}

SetOriginEvent The action of changing the originLevel1-2-3 of the current visit.

func (*SetOriginEvent) Execute

func (origin *SetOriginEvent) Execute(v *Visit) error

Execute the set origin event. Replaces the originLevel1-2-3 in the current visit.

func (*SetOriginEvent) IsValid added in v0.4.0

func (origin *SetOriginEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal.

type SetReferrerEvent

type SetReferrerEvent struct {
	Referrer string `json:"referrer"`
}

SetReferrerEvent The action of changing the referrer

func (*SetReferrerEvent) Execute

func (referrer *SetReferrerEvent) Execute(v *Visit) error

Execute Execute the event

func (*SetReferrerEvent) IsValid added in v0.4.0

func (referrer *SetReferrerEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal.

type TabChangeEvent

type TabChangeEvent struct {
	Name               string `json:"name"`
	ConstantExpression string `json:"cq,omitempty"`
}

TabChangeEvent represents a tab change event

func (*TabChangeEvent) Execute

func (tab *TabChangeEvent) Execute(v *Visit) error

Execute Sends the tabchange event to the analytics and modify the CQ for the following queries in the visit

func (*TabChangeEvent) IsValid added in v0.4.0

func (tab *TabChangeEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal.

type Uabot

type Uabot interface {
	Run(quitChannel chan bool) error
}

Uabot is the interface that allows you to run a bot.

func NewUabot

func NewUabot(local bool, scenarioURL string, searchToken string, analyticsToken string) Uabot

NewUabot will start a bot to run some scenarios. It needs the url/path where to find the scenarions {scenarioURL}, the searchToken, the analyticsToken and a randomizer.

type ViewEvent

type ViewEvent struct {
	ClickRank     int                    `json:"docNo"`
	Probability   float64                `json:"probability"`
	PageViewField string                 `json:"pageViewField"`
	Offset        int                    `json:"offset,omitempty"`
	ContentType   string                 `json:"contentType,omitempty"`
	CustomData    map[string]interface{} `json:"customData,omitempty"`
}

ViewEvent a struct representing a view, it is defined by a clickRank, an offset, a probability to click, the contentType and a pageViewField. We keep a similar structure to a click event because we can simulate that the user visited a page that was returned as a result from a search. The view event sent will contain {contentIdKey: "@pageViewField", contentIdValue: result['pageViewField']}

func (*ViewEvent) Execute

func (view *ViewEvent) Execute(v *Visit) error

Execute the view event, sending a view event to the usage analytics

func (*ViewEvent) IsValid added in v0.4.0

func (view *ViewEvent) IsValid() (bool, string)

IsValid Additional validation after the json unmarshal.

type Visit

type Visit struct {
	SearchClient       search.Client
	UAClient           ua.Client
	LastQuery          *search.Query
	LastResponse       *search.Response
	Username           string
	OriginLevel1       string
	OriginLevel2       string
	OriginLevel3       string
	Referrer           string
	LastTab            string
	Config             *Config
	IP                 string
	Anonymous          bool
	Language           string
	WaitBetweenActions bool
}

Visit The struct visit is used to store one visit to the site. SearchClient The http client to send search queries UAClient The http client to send the usage analytics data LastQuery The last query that was searched LastResponse The last response that was received Username The name of the user visiting OriginLevel1 Page/Hub where the events originate from OriginLevel2 Tab where the events originate from OriginLevel3 The HTTP identifier of the page from which any type of event originates Referrer Same as OriginLevel3 LastTab The tab the user last visited

func NewVisit

func NewVisit(_searchtoken string, _uatoken string, _useragent string, language string, c *Config) (*Visit, error)

NewVisit Creates a new visit to the search page _searchtoken The token used to be able to search _uatoken The token used to send usage analytics events _useragent The user agent the analytics events will see

func (*Visit) DecorateCustomMetadata added in v0.4.0

func (v *Visit) DecorateCustomMetadata(evt *ua.ActionEvent, customData map[string]interface{})

DecorateCustomMetadata is used to handle all the customMetadata for the events that allow it.

func (*Visit) DecorateEvent added in v0.4.0

func (v *Visit) DecorateEvent(evt *ua.ActionEvent)

DecorateEvent is used to assign all the common data to send with all analytics events

func (*Visit) ExecuteScenario

func (v *Visit) ExecuteScenario(scenario Scenario, c *Config) error

ExecuteScenario Execute a specific scenario, send the config for all the potential random we need to do.

func (*Visit) FindDocumentRankByMatchingField added in v0.4.0

func (v *Visit) FindDocumentRankByMatchingField(field string, regexPattern *regexp.Regexp) int

FindDocumentRankByMatchingField Looks through the last response to a query to find a document rank by matching a field and its value (described as a regex pattern)

func (*Visit) FindDocumentRankByTitle

func (v *Visit) FindDocumentRankByTitle(toFind string) int

FindDocumentRankByTitle Looks through the last response to a query to find a document rank by his title

func (*Visit) SendSearchEvent added in v0.4.0

func (v *Visit) SendSearchEvent(event *ua.SearchEvent) (err error)

SendSearchEvent to the UAClient.

func (*Visit) SendViewEvent added in v0.4.0

func (v *Visit) SendViewEvent(event *ua.ViewEvent) (err error)

SendViewEvent to the UAClient

func (*Visit) SetupGeneral

func (v *Visit) SetupGeneral()

SetupGeneral Function to instanciate with non-specific values

func (*Visit) SetupNTO

func (v *Visit) SetupNTO()

SetupNTO Function to instanciate with specific values for NTO demo queries

Jump to

Keyboard shortcuts

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