{"modulePath":"github.com/data-preservation-programs/singularity","version":"v1.0.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package analytics\n\n## Variables\n\n```go\nvar (\n\tInstance string\n\tIdentity string\n)\n```\n\n```go\nvar Enabled = true\n```\n\n## Functions\n\n```go\nfunc Init(ctx context.Context, db *gorm.DB) error\n```\nInit initializes the global variables 'Instance' and 'Enabled' based on values stored in the database and environment variables respectively. The function uses the 'instance\\_id' key to fetch the 'Instance' value from the database. If the 'Instance' value is already set, the function returns early. Analytics are opt-in: unless the environment variable 'SINGULARITY\\_ANALYTICS' is set to \"1\", 'Enabled' is set to false.\n\nParameters:\n\n  - ctx: The context for managing timeouts and cancellation.\n  - db: The gorm.DB instance for making database queries.\n\nReturns:\n\n  - An error if there are issues fetching the instance id from the database or if the database appears empty.\n\n## Types\n\n```go\ntype Collector struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nvar Default Collector = Collector{}\n```\n\n```go\nfunc (c *Collector) Flush() error\n```\nFlush ensures that all buffered events in the Collector are sent to the remote metrics service. This function uses batch processing to send events in chunks. Each chunk can have a maximum of 100 events. If the global 'Enabled' flag is false, the function exits early without doing anything.\n\nProcess:\n\n 1. The function checks if there are any events buffered in the Collector.\n 2. If there are more than 100 events of a specific type, it picks the first 100 and sends them. Otherwise, it sends all available events.\n 3. It then encodes the selected events using CBOR and compresses them using the zstandard compression algorithm.\n 4. The compressed data is encoded using Base64 encoding and sent to the remote metrics service using an HTTP POST request.\n 5. If the HTTP response is not '200 OK', the function reads the response body and returns an error.\n\nReturns:\n\n  - nil if all buffered events are successfully sent.\n  - An error if there are issues during any stage of the process.\n\n```go\nfunc (c *Collector) QueueDealEvent(event DealProposalEvent)\n```\n\n```go\nfunc (c *Collector) QueuePushJobEvent(event PackJobEvent)\n```\n\n```go\nfunc (c *Collector) Start(ctx context.Context)\n```\n\n```go\ntype DealProposalEvent struct {\n\tTimestamp int64  `cbor:\"1,keyasint\"`\n\tInstance  string `cbor:\"2,keyasint\"`\n\n\tPieceCID   string `cbor:\"3,keyasint\"`\n\tDataCID    string `cbor:\"4,keyasint\"`\n\tPieceSize  int64  `cbor:\"5,keyasint\"`\n\tProvider   string `cbor:\"6,keyasint\"`\n\tClient     string `cbor:\"7,keyasint\"`\n\tVerified   bool   `cbor:\"8,keyasint\"`\n\tStartEpoch int32  `cbor:\"9,keyasint\"`\n\tEndEpoch   int32  `cbor:\"10,keyasint\"`\n\n\tIdentity string `cbor:\"11,keyasint\"`\n}\n```\n\n```go\ntype EventType int\n```\n\n```go\nconst (\n\tPackJobComplete EventType = iota\n\tDealProposal\n)\n```\n\n```go\ntype Events struct {\n\tPackJobEvents []PackJobEvent      `cbor:\"1,keyasint\"`\n\tDealEvents    []DealProposalEvent `cbor:\"2,keyasint\"`\n}\n```\n\n```go\ntype PackJobEvent struct {\n\tTimestamp int64  `cbor:\"1,keyasint\"`\n\tInstance  string `cbor:\"2,keyasint\"`\n\n\tSourceType string `cbor:\"3,keyasint\"`\n\tOutputType string `cbor:\"4,keyasint\"`\n\tPieceSize  int64  `cbor:\"5,keyasint\"`\n\tPieceCID   string `cbor:\"6,keyasint\"`\n\tCarSize    int64  `cbor:\"7,keyasint\"`\n\tNumOfFiles int64  `cbor:\"8,keyasint\"`\n\n\tIdentity string `cbor:\"9,keyasint\"`\n}\n```\n\n","path":"github.com/data-preservation-programs/singularity/analytics","name":"analytics","synopsis":"","isRedistributable":true}
