db

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContextBackgroundDuration = 5
)
View Source
const (
	DayTime = time.Hour * 24
)

Variables

View Source
var (
	ErrInvalidID            = errors.New("invalid id")
	ErrFailedCursor         = errors.New("failed to get cursor for logs by")
	ErrFailedFindLog        = errors.New("failed to get logs by")
	ErrFailedFindLatestLogs = errors.New("failed to get latest n logs")
	ErrFailedIndexCreation  = errors.New("failed to create index for")
)

Functions

func AddCondition

func AddCondition(query bson.M, condition string, add interface{}) bson.M

AddCondition returns the query passed as input with the query passed as input.

func AddMultipleCondition

func AddMultipleCondition(query bson.M, condition string, add []bson.M) bson.M

AddMultipleCondition returns the query passed as input with the multiple queries passed as input.

func AddTimeRangeToQuery

func AddTimeRangeToQuery(lt, gt string, filter bson.M) bson.M

AddTimeRangeToQuery.

func BuildFilter

func BuildFilter(query map[string]interface{}) bson.M

BuildFilter returns a bson.M object representing the mongoDB filter in a query.

func ConnectDB

func ConnectDB(connectionString string) (*mongo.Client, error)

ConnectDB creates and returns a client connected by a connection string to mongoDB. Also checks the connection if everything is ok.

func CreateBodyIndex

func CreateBodyIndex(ctx context.Context, client *mongo.Client, collection *mongo.Collection) error

CreateBodyIndex creates the index for text search for body.

func CreateHeadersIndex

func CreateHeadersIndex(ctx context.Context, client *mongo.Client, collection *mongo.Collection) error

CreateHeadersIndex creates the index for text search for headers.

func GetDatabase

func GetDatabase(client *mongo.Client, databaseName string) *mongo.Database

GetDatabase returns the pointer to the database (input).

func GetLogs

func GetLogs(database *mongo.Database) *mongo.Collection

GetLogs returns the collection of logs.

func GetNumberOfLogs

func GetNumberOfLogs(ctx context.Context, client *mongo.Client, collection *mongo.Collection) (int64, error)

GetNumberOfLogs returns the number of logs in the database.

func InsertLog

func InsertLog(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	record Log) interface{}

InsertLog inserts a log record into the logs collection.

Types

type AggregatedResult

type AggregatedResult struct {
	ID    string `bson:"_id"`
	Count int    `bson:"count"`
}

AggregatedResult.

func GetAggregatedLogs

func GetAggregatedLogs(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	filter []bson.M) ([]AggregatedResult, error)

GetAggregatedLogs returns a slice of aggregated logs using the filter taken as input. If the result is empty err won't be nil.

type Log

type Log struct {
	ID        primitive.ObjectID  `bson:"_id"`
	IP        string              `bson:"ip"`
	Method    string              `bson:"method"`
	Path      string              `bson:"path"`
	Headers   map[string][]string `bson:"headers"`
	Body      string              `bson:"body"`
	Timestamp int64               `bson:"timestamp"`
}

Log defines the structure of a log record in the database.

func GetLatestNLogs

func GetLatestNLogs(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	n int64) ([]Log, error)

GetLatestNLogs returns a slice of the latest inserted N logs. If they are not present in the database err won't be nil.

func GetLogByID

func GetLogByID(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	id string) (Log, error)

GetLogByID returns a log struct with the defined ID. If the ID is not present in the database err won't be nil.

func GetLogsByBody

func GetLogsByBody(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	body string) ([]Log, error)

GetLogsByBody returns a slice of logs with the defined Body. If the Body is not present in the database err won't be nil.

func GetLogsByDate

func GetLogsByDate(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	date time.Time) ([]Log, error)

GetLogsByDate returns a slice of logs within the defined date. If the Date is not present in the database err won't be nil.

func GetLogsByIP

func GetLogsByIP(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	ip string) ([]Log, error)

GetLogsByIP returns a slice of logs with the defined IP. If the IP is not present in the database err won't be nil.

func GetLogsByMethod

func GetLogsByMethod(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	method string) ([]Log, error)

GetLogsByMethod returns a slice of logs with the defined HTTP Method. If the Method is not present in the database err won't be nil.

func GetLogsByPath

func GetLogsByPath(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	path string) ([]Log, error)

GetLogsByPath returns a slice of logs with the defined Path. If the Path is not present in the database err won't be nil.

func GetLogsByRange

func GetLogsByRange(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	dateStart time.Time, dateEnd time.Time) ([]Log, error)

GetLogsByRange returns a slice of logs within the defined range (date to date). If the Range is not present in the database err won't be nil.

func GetLogsWithFilter

func GetLogsWithFilter(ctx context.Context, client *mongo.Client, collection *mongo.Collection,
	filter bson.M, findOptions *options.FindOptions) ([]Log, error)

GetLogsWithFilter returns a slice of logs using the filter taken as input. If the result is empty err won't be nil.

func (*Log) IsEmpty

func (log *Log) IsEmpty() bool

IsEmpty checks if a Log is a new one (just created).

Jump to

Keyboard shortcuts

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