Documentation ¶
Index ¶
- Constants
- Variables
- func AddCondition(query bson.M, condition string, add interface{}) bson.M
- func AddMultipleCondition(query bson.M, condition string, add []bson.M) bson.M
- func AddTimeRangeToQuery(lt, gt string, filter bson.M) bson.M
- func BuildFilter(query map[string]interface{}) bson.M
- func ConnectDB(connectionString string) (*mongo.Client, error)
- func CreateBodyIndex(ctx context.Context, client *mongo.Client, collection *mongo.Collection) error
- func CreateHeadersIndex(ctx context.Context, client *mongo.Client, collection *mongo.Collection) error
- func GetDatabase(client *mongo.Client, databaseName string) *mongo.Database
- func GetLogs(database *mongo.Database) *mongo.Collection
- func GetNumberOfLogs(ctx context.Context, client *mongo.Client, collection *mongo.Collection) (int64, error)
- func InsertLog(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) interface{}
- type AggregatedResult
- type Log
- func GetLatestNLogs(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) ([]Log, error)
- func GetLogByID(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) (Log, error)
- func GetLogsByBody(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) ([]Log, error)
- func GetLogsByDate(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) ([]Log, error)
- func GetLogsByIP(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) ([]Log, error)
- func GetLogsByMethod(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) ([]Log, error)
- func GetLogsByPath(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) ([]Log, error)
- func GetLogsByRange(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) ([]Log, error)
- func GetLogsWithFilter(ctx context.Context, client *mongo.Client, collection *mongo.Collection, ...) ([]Log, error)
Constants ¶
const (
ContextBackgroundDuration = 5
)
const (
DayTime = time.Hour * 24
)
Variables ¶
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 ¶
AddCondition returns the query passed as input with the query passed as input.
func AddMultipleCondition ¶
AddMultipleCondition returns the query passed as input with the multiple queries passed as input.
func AddTimeRangeToQuery ¶
AddTimeRangeToQuery.
func BuildFilter ¶
BuildFilter returns a bson.M object representing the mongoDB filter in a query.
func ConnectDB ¶
ConnectDB creates and returns a client connected by a connection string to mongoDB. Also checks the connection if everything is ok.
func CreateBodyIndex ¶
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 ¶
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.
Types ¶
type AggregatedResult ¶
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.