studyengine

package
v0.0.0-...-c81fb16 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: Apache-2.0 Imports: 17 Imported by: 1

Documentation

Index

Constants

View Source
const (
	STUDY_EVENT_TYPE_ENTER  = "ENTER"
	STUDY_EVENT_TYPE_SUBMIT = "SUBMIT"
	STUDY_EVENT_TYPE_TIMER  = "TIMER"
	STUDY_EVENT_TYPE_CUSTOM = "CUSTOM"
	STUDY_EVENT_TYPE_MERGE  = "MERGE"
	STUDY_EVENT_TYPE_LEAVE  = "LEAVE"
)

Variables

View Source
var Now func() time.Time = time.Now

Now function control the current time used by the expressions.

Functions

func ExpressionEval

func ExpressionEval(expression studyTypes.Expression, evalCtx EvalContext) (val interface{}, err error)

func FormatTimeWithDateFns

func FormatTimeWithDateFns(t time.Time, format string) string

FormatTimeWithDateFns formats a time.Time value using date-fns style tokens

Usage example:

t := time.Date(2023, 12, 25, 14, 30, 45, 0, time.UTC)
formatted := FormatTimeWithDateFns(t, "yyyy-MM-dd HH:mm:ss")
// Result: "2023-12-25 14:30:45"

formatted2 := FormatTimeWithDateFns(t, "MM/dd/yy hh:mm a")
// Result: "12/25/23 02:30 PM"

Supported tokens:

  • yyyy: 4-digit year (2006)
  • yy: 2-digit year (06)
  • MM: 2-digit month (01-12)
  • M: 1-digit month (1-12)
  • dd: 2-digit day (01-31)
  • d: 1-digit day (1-31)
  • HH: 2-digit hour 24h (00-23)
  • hh: 2-digit hour 12h (01-12)
  • mm: 2-digit minute (00-59)
  • m: 1-digit minute (0-59)
  • ss: 2-digit second (00-59)
  • s: 1-digit second (0-59)
  • a: AM/PM indicator

func InitStudyEngine

func InitStudyEngine(dbService StudyDBService, externalServices []ExternalService)

Types

type ActionData

type ActionData struct {
	PState          studyTypes.Participant
	ReportsToCreate []studyTypes.Report
}

func ActionEval

func ActionEval(action studyTypes.Expression, oldState ActionData, event StudyEvent) (newState ActionData, err error)

type EvalContext

type EvalContext struct {
	Event            StudyEvent
	ParticipantState studyTypes.Participant
}

EvalContext contains all the data that can be looked up by expressions

func (EvalContext) ExpressionArgResolver

func (ctx EvalContext) ExpressionArgResolver(arg studyTypes.ExpressionArg) (interface{}, error)

type ExternalEventPayload

type ExternalEventPayload struct {
	ParticipantState studyTypes.Participant    `json:"participantState"`
	EventType        string                    `json:"eventType"`
	StudyKey         string                    `json:"studyKey"`
	InstanceID       string                    `json:"instanceID"`
	Response         studyTypes.SurveyResponse `json:"surveyResponses"`
	EventKey         string                    `json:"eventKey"`
	Payload          map[string]interface{}    `json:"payload"`
}

type ExternalService

type ExternalService struct {
	Name            string           `yaml:"name"`
	URL             string           `yaml:"url"`
	APIKey          string           `yaml:"apiKey"`
	Timeout         int              `yaml:"timeout"`
	MutualTLSConfig *MutualTLSConfig `yaml:"mTLSConfig"`
}

type MutualTLSConfig

type MutualTLSConfig struct {
	CertFile string `yaml:"certFile"`
	KeyFile  string `yaml:"keyFile"`
	CAFile   string `yaml:"caFile"`
}

type SendOptions

type SendOptions struct {
	ExpiresAt        int64 // if message could not sent until this time, it will be discarded
	LanguageOverride string
}

SendOptions defines optional parameters for sending study emails.

type StudyDBService

type StudyDBService interface {
	GetResponses(instanceID string, studyKey string, filter bson.M, sort bson.M, page int64, limit int64) (responses []studyTypes.SurveyResponse, paginationInfo *studyDB.PaginationInfos, err error)
	DeleteConfidentialResponses(instanceID string, studyKey string, participantID string, key string) (count int64, err error)
	SaveResearcherMessage(instanceID string, studyKey string, message studyTypes.StudyMessage) error
	// Study code lists:
	StudyCodeListEntryExists(instanceID string, studyKey string, listKey string, code string) (bool, error)
	DeleteStudyCodeListEntry(instanceID string, studyKey string, listKey string, code string) error
	DrawStudyCode(instanceID string, studyKey string, listKey string) (string, error)
	// Study counters:
	GetCurrentStudyCounterValue(instanceID string, studyKey string, scope string) (int64, error)
	IncrementAndGetStudyCounterValue(instanceID string, studyKey string, scope string) (int64, error)
	RemoveStudyCounterValue(instanceID string, studyKey string, scope string) error

	// Study variables:
	GetStudyVariableByStudyKeyAndKey(instanceID string, studyKey string, key string, onlyValue bool) (studyTypes.StudyVariables, error)
	UpdateStudyVariableValue(instanceID string, studyKey string, key string, value any) (studyTypes.StudyVariables, error)
}

type StudyEngine

type StudyEngine struct {
	// contains filtered or unexported fields
}
var (
	CurrentStudyEngine *StudyEngine
)

func (*StudyEngine) RegisterStudyMessageSender

func (se *StudyEngine) RegisterStudyMessageSender(sender StudyMessageSender)

RegisterStudyMessageSender allows to set a message sender implementation that can be swapped for simulator/test mode.

type StudyEvent

type StudyEvent struct {
	InstanceID                            string
	StudyKey                              string
	Type                                  string                    // what kind of event (TIMER, SUBMISSION, ENTER etc.)
	Response                              studyTypes.SurveyResponse // if something is submitted during the event is added here
	Payload                               map[string]interface{}    // additional data
	EventKey                              string                    // key of the event	(for custom events)
	MergeWithParticipant                  studyTypes.Participant    // if need to merge with other participant state, is added here
	ParticipantIDForConfidentialResponses string
}

type StudyMessageSender

type StudyMessageSender interface {
	SendInstantStudyEmail(
		instanceID string,
		studyKey string,
		confidentialPID string,
		messageType string,
		extraPayload map[string]string,
		opts SendOptions,
	) error
}

StudyMessageSender abstracts immediate message sending from the study engine. Implementations may send via SMTP bridge or capture messages in tests/simulators.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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