visigo

package module
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2019 License: MIT Imports: 4 Imported by: 0

README

Visigo

Build Status Build Status GoDoc Go Report Card GitHub issues License

Visigo is http middleware for page unique visits counting. It uses HyperLogLog as a counter, so it's pretty fast.

Warning: Visigo stores HyperLogLog++ in map, so this implementation should be used only on smaller sites.

HyperLogLog++

HyperLogLog++ paper
Google article about HyperLogLog++

From Wikipedia

HyperLogLog is an algorithm for the count-distinct problem, approximating the number of distinct elements in a multiset. Calculating the exact cardinality of a multiset requires an amount of memory proportional to the cardinality, which is impractical for very large data sets. Probabilistic cardinality estimators, such as the HyperLogLog algorithm, use significantly less memory than this, at the cost of obtaining only an approximation of the cardinality. The HyperLogLog algorithm is able to estimate cardinalities of > 109 with a typical accuracy of 2%, using 1.5 kB of memory. HyperLogLog is an extension of the earlier LogLog algorithm, itself deriving from the 1984 Flajolet–Martin algorithm.

Install

Via go get tool

$ go get github.com/matoous/visigo

Usage

package main

import (
	"fmt"
	"net/http"

	"github.com/matoous/visigo"
)

func main() {
	http.Handle("/", visigo.Counter(http.HandlerFunc(final)))
	http.Handle("/total", visigo.Counter(http.HandlerFunc(total)))
	http.ListenAndServe(":3000", nil)
}

func final(w http.ResponseWriter, r *http.Request) {
	count, _ := visigo.Visits(r)
	response := fmt.Sprintf("This page was viewed by %d unique visitors", count)
	w.Write([]byte(response))
}

func total(w http.ResponseWriter, r *http.Request) {
	count, _ := visigo.TotalVisits()
	response := fmt.Sprintf("This website had %d unique visitors in total", count)
	w.Write([]byte(response))
}

Testing

$ go test ./...

Notice

If you use Visigo on your site or in your project, please let me know!

If you have any issues, just feel free and open it in this repository, thanks!

License

The MIT License (MIT). Please see License File for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCount = errors.New("count not found or error in HyperLogLog")

ErrCount - error returned when you try to get count but didn't register middleware

Functions

func Counter

func Counter(next http.Handler) http.Handler

Counter - registers middleware for visits counting

func TotalVisits

func TotalVisits() (uint64, error)

TotalVisits gets total visits to all sites

func Visits

func Visits(r *http.Request) (uint64, error)

Visits - get visits for given URL

Types

This section is empty.

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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