ubm

package module
v0.0.0-...-a0504d6 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2018 License: MIT Imports: 10 Imported by: 0

README

UBM

User Behaviour Model implementation. Simple library for tracking user activity.

MIT licensed GoDoc

Installation

go get -u github.com/dzen-it/ubm

Quick Start

Create a server

db, err := ubm.NewMongoDB("db.example.com:27017", "dbname")
if err != nil {
	panic(err)
}
srv := ubm.NewServerCoala(5683, db)

go func() {
	srv.Serve()
}()

Create client.

client := ubm.NewClientCoala("127.0.0.1:5683")

Add action for user.

if err := client.AddAction("user_id", "action-1"); err != nil {
	panic(err)
}

Get different info about actions for user.

action, err := client.GetAction("user_id", "action-1")
if err!=nil{
    panic(err)
}

fmt.Printf("Last call: %v, total calls: %v\n", action.LastCall, action.Count)
// Display: "Last call: 2018-04-13 13:13:09.876, total calls: 42"

lastAction, err := client.GetLastAction("user_id")
if err!=nil{
    panic(err)
}

fmt.Printf("Last action: %v, last calls: %v\n", lastAction.Name, action.LastCall)
// Display: "Last action: action-1, last call: 2018-04-13 13:13:09.876"

Manage action states using triggers.

// Sets a enabling action-1 and a disabling action-2
client.SetTrigger("action-1", "action-2", time.Second*0)

fmt.Println("action-1 is blocked:", client.TriggerStatus("user_id", "action-1"))
// Display: "action-1 is blocked: true"

// Add a disabling action "action-2"
if err := client.AddAction("user_id", "action-2"); err != nil {
	panic(err)
}

fmt.Println("action-1 is blocked:", client.TriggerStatus("user_id", "action-1"))
// Display: "action-1 is blocked: false"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrActionNotFound = errors.New("action for id not found")
	ErrUserNotFound   = errors.New("id not found")
)

Functions

This section is empty.

Types

type Action

type Action struct {
	LastCall time.Time `bson:"last_call" json:"last_call"`
	Count    int64     `bson:"count" json:"count"`
}

type Client

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

Client implements the wrapper over the concrete transport API

func NewClientCoala

func NewClientCoala(host string) Client

NewClientCoala returns the Client for Coala protocol

func NewClientInMemory

func NewClientInMemory() Client

NewClientInMemory returns the Client working without backend

func (Client) AddAction

func (c Client) AddAction(id string, action string) error

AddAction registers an action for this id

func (Client) GetAction

func (c Client) GetAction(id string, action string) (Action, error)

GetAction gets information about specific action for this id

func (Client) GetLastAction

func (c Client) GetLastAction(id string) (LastAction, error)

GetLastAction gets information about any last action of this id

func (Client) SetTrigger

func (c Client) SetTrigger(enableAction string, disableAction string, lifetime time.Duration)

SetTrigger sets the state tracking on the client by id, per enableAction action, which can be disable via disableAction

func (*Client) TriggerStatus

func (c *Client) TriggerStatus(id string, action string) bool

TriggerStatus checks the action "Is it blocked by a trigger?" If blocked, then returns True. To unlock the required to be added the disableAction.

type DB

type DB interface {
	AddAction(id interface{}, actionName string) error
	GetAction(id interface{}, actionName string) (Action, error)
	GetLastAction(id interface{}) (LastAction, error)
}

DB is abstract database interface for different usage cases

func NewMongoDB

func NewMongoDB(addr string, dbName string) (DB, error)

NewMongoDB returns implemetation of DB interface for MongoDB

func NewWithoutDB

func NewWithoutDB() DB

NewWithoutDB create an empty instance that does not write and does not read anything

type LastAction

type LastAction struct {
	Name     string    `bson:"last_action" json:"last_action"`
	LastCall time.Time `bson:"last_call" json:"last_call"`
}

type Server

type Server interface {
	Serve() error
}

Server Listen and Serve requests

func NewServerCoala

func NewServerCoala(port int, db DB) Server

NewServerCoala returns implementation of Listener interface for Coala protocol

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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