app.use_after("/api/", new PopTracker(config)) app.use_after("/admin/", new PopTracker(config))

To retrieve your tracker data use the `PopTrackerAPI` which serves the tracker
data in JSON format.

app.use("/api/tracker_data", new PopTrackerAPI(config))

Introduced classes

class LogEntry

popcorn :: LogEntry

A tracker log entry used to store HTTP requests and their given HTTP responses
class PopTracker

popcorn :: PopTracker

Saves logs into a MongoDB collection
class PopTrackerAPI

popcorn :: PopTrackerAPI

JSON API of the PopTracker
class PopTrackerBrowsers

popcorn :: PopTrackerBrowsers

Group and count entries by browser
class PopTrackerEntries

popcorn :: PopTrackerEntries

List all tracker log entries
class PopTrackerQueries

popcorn :: PopTrackerQueries

Group and count entries by query string
class PopTrackerResponseTime

popcorn :: PopTrackerResponseTime

Return last month response time
abstract class TrackerHandler

popcorn :: TrackerHandler

Base tracker handler
class TrackerRepo

popcorn :: TrackerRepo

Repository to store track logs.

Redefined classes

redef class AppConfig

popcorn :: pop_tracker $ AppConfig

Configuration file for Popcorn apps
redef abstract class Deserializer

popcorn :: pop_tracker $ Deserializer

Abstract deserialization service

All class definitions

redef class AppConfig

popcorn :: pop_tracker $ AppConfig

Configuration file for Popcorn apps
redef abstract class Deserializer

popcorn :: pop_tracker $ Deserializer

Abstract deserialization service
class LogEntry

popcorn $ LogEntry

A tracker log entry used to store HTTP requests and their given HTTP responses
class PopTracker

popcorn $ PopTracker

Saves logs into a MongoDB collection
class PopTrackerAPI

popcorn $ PopTrackerAPI

JSON API of the PopTracker
class PopTrackerBrowsers

popcorn $ PopTrackerBrowsers

Group and count entries by browser
class PopTrackerEntries

popcorn $ PopTrackerEntries

List all tracker log entries
class PopTrackerQueries

popcorn $ PopTrackerQueries

Group and count entries by query string
class PopTrackerResponseTime

popcorn $ PopTrackerResponseTime

Return last month response time
abstract class TrackerHandler

popcorn $ TrackerHandler

Base tracker handler
class TrackerRepo

popcorn $ TrackerRepo

Repository to store track logs.
package_diagram popcorn::pop_tracker pop_tracker popcorn popcorn popcorn::pop_tracker->popcorn popcorn::pop_json pop_json popcorn::pop_tracker->popcorn::pop_json popcorn::pop_repos pop_repos popcorn::pop_tracker->popcorn::pop_repos github github popcorn->github config config popcorn->config csv csv popcorn->csv json json popcorn->json logger logger popcorn->logger realtime realtime popcorn->realtime mongodb mongodb popcorn->mongodb nitcorn nitcorn popcorn->nitcorn pthreads pthreads popcorn->pthreads template template popcorn->template popcorn::pop_json->json popcorn::pop_handlers pop_handlers popcorn::pop_json->popcorn::pop_handlers popcorn::pop_validation pop_validation popcorn::pop_json->popcorn::pop_validation popcorn::pop_config pop_config popcorn::pop_repos->popcorn::pop_config mongodb::queries queries popcorn::pop_repos->mongodb::queries ...github ... ...github->github ...config ... ...config->config ...csv ... ...csv->csv ...json ... ...json->json ...logger ... ...logger->logger ...realtime ... ...realtime->realtime ...mongodb ... ...mongodb->mongodb ...nitcorn ... ...nitcorn->nitcorn ...pthreads ... ...pthreads->pthreads ...template ... ...template->template ...popcorn::pop_handlers ... ...popcorn::pop_handlers->popcorn::pop_handlers ...popcorn::pop_validation ... ...popcorn::pop_validation->popcorn::pop_validation ...popcorn::pop_config ... ...popcorn::pop_config->popcorn::pop_config ...mongodb::queries ... ...mongodb::queries->mongodb::queries a_star-m a_star-m a_star-m->popcorn::pop_tracker

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module base64

base64 :: base64

Offers the base 64 encoding and decoding algorithms
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module c

c :: c

Structures and services for compatibility with the C language
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module config

config :: config

Configuration options for nit tools and apps
module console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module csv

csv :: csv

CSV document handling.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module error

json :: error

Intro JsonParseError which is exposed by all JSON reading APIs
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module file_server

nitcorn :: file_server

Provides the FileServer action, which is a standard and minimal file server
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module http_errors

nitcorn :: http_errors

Offers ErrorTemplate to display error pages
module http_request

nitcorn :: http_request

Provides the HttpRequest class and services to create it
module http_request_buffer

nitcorn :: http_request_buffer

Http request parsing for buffered inputs.
module http_response

nitcorn :: http_response

Provides the HttpResponse class and http_status_codes
module ini

ini :: ini

Read and write INI configuration files
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module json

json :: json

Read and write JSON formatted text using the standard serialization services
module kernel

core :: kernel

Most basic classes and methods.
module libevent

libevent :: libevent

Low-level wrapper around the libevent library to manage events on file descriptors
module list

core :: list

This module handle double linked lists
module logger

logger :: logger

A simple logger for Nit
module math

core :: math

Mathematical operations
module md5

md5 :: md5

Native MD5 digest implementation as Text::md5
module media_types

nitcorn :: media_types

Services to identify Internet media types (or MIME types, Content-types)
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module mongodb

mongodb :: mongodb

MongoDB Nit Driver.
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module native

core :: native

Native structures for text and bytes
module native_mongodb

mongodb :: native_mongodb

Native wrapper for the MongoDB C Driver
module nitcorn

nitcorn :: nitcorn

The nitcorn Web server framework creates server-side Web apps in Nit
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module parser_base

parser_base :: parser_base

Simple base for hand-made parsers of all kinds
module pop_config

popcorn :: pop_config

Configuration file and options for Popcorn apps
module pop_handlers

popcorn :: pop_handlers

Route handlers.
module pop_routes

popcorn :: pop_routes

Internal routes representation.
module pop_sessions

popcorn :: pop_sessions

Session handlers
module pop_tasks

popcorn :: pop_tasks

Popcorn threaded tasks
module pop_validation

popcorn :: pop_validation

Quick and easy validation framework for Json inputs
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module pthreads

pthreads :: pthreads

Main POSIX threads support and intro the classes Thread, Mutex and Barrier
module queries

mongodb :: queries

Mongo queries framework
module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module reactor

nitcorn :: reactor

Core of the nitcorn project, provides HttpFactory and Action
module realtime

realtime :: realtime

Services to keep time of the wall clock time
module ropes

core :: ropes

Tree-based representation of a String.
module safe

serialization :: safe

Services for safer deserialization engines
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module serialization_read

json :: serialization_read

Services to read JSON: deserialize_json and JsonDeserializer
module serialization_write

json :: serialization_write

Services to write Nit objects to JSON strings: serialize_to_json and JsonSerializer
module server_config

nitcorn :: server_config

Classes and services to configure the server
module sessions

nitcorn :: sessions

Automated session management
module signal_handler

nitcorn :: signal_handler

Handle SIGINT and SIGTERM to close the server after all active events
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module static

json :: static

Static interface to read Nit objects from JSON strings
module stream

core :: stream

Input and output streams of characters
module template

template :: template

Basic template system
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module token

nitcorn :: token

Simple generate_token service, independent of the rest of the nitcorn framework
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module vararg_routes

nitcorn :: vararg_routes

Routes with parameters.

Parents

module pop_json

popcorn :: pop_json

Introduce useful services for JSON REST API handlers.
module pop_repos

popcorn :: pop_repos

Repositories for data management.
module popcorn

popcorn :: popcorn

Application server abstraction on top of nitcorn.

Children

module a_star-m

a_star-m

# ~~~nitish
# app.use_after("/api/*", new PopTracker(config))
# app.use_after("/admin/*", new PopTracker(config))
# ~~~
#
# To retrieve your tracker data use the `PopTrackerAPI` which serves the tracker
# data in JSON format.
#
# ~~~nitish
# app.use("/api/tracker_data", new PopTrackerAPI(config))
# ~~~
module pop_tracker

import popcorn
import popcorn::pop_config
import popcorn::pop_json
import popcorn::pop_repos

redef class AppConfig

	# Logs collection
	var tracker_logs = new TrackerRepo(db.collection("tracker_logs"))

	# Tracker handler
	var tracker = new PopTracker(self)
end

# JSON API of the PopTracker
#
# Serves the collected data in JSON format.
class PopTrackerAPI
	super Router

	# Config used to access tracker db
	var config: AppConfig

	init do
		use("/entries", new PopTrackerEntries(config))
		use("/queries", new PopTrackerQueries(config))
		use("/browsers", new PopTrackerBrowsers(config))
		use("/times", new PopTrackerResponseTime(config))
	end
end

# Base tracker handler
abstract class TrackerHandler
	super Handler

	# Config used to access tracker db
	var config: AppConfig

	# Get the `limit` GET argument from `req`
	#
	# Return `10` by default.
	fun limit(req: HttpRequest): Int do return req.int_arg("limit") or else 10
end

# Saves logs into a MongoDB collection
class PopTracker
	super TrackerHandler

	redef fun all(req, res) do
		config.tracker_logs.save new LogEntry(req, res)
	end
end

# List all tracker log entries
class PopTrackerEntries
	super TrackerHandler

	redef fun get(req, res) do
		res.json new JsonArray.from(config.tracker_logs.find_all)
	end
end

# Group and count entries by query string
class PopTrackerQueries
	super TrackerHandler

	redef fun get(req, res) do
		var pipe = new MongoPipeline
		pipe.group((new MongoGroup("$request.uri")).
			sum("visits", 1).
			avg("response_time", "$response_time").
			addToSet("uniq", "$session"))
		pipe.sort((new MongoMatch).eq("visits", -1))
		pipe.limit(limit(req))
		res.json new JsonArray.from(config.tracker_logs.collection.aggregate(pipe))
	end
end

# Group and count entries by browser
class PopTrackerBrowsers
	super TrackerHandler

	# MongoMatch query used for each browser key
	#
	# Because parsing user agent string is a pain in the nit, we go lazy on this
	# one. We associate each broswer key like `Chromium` to the query that allows
	# us to count the number of visits.
	var browser_queries: HashMap[String, MongoMatch] do
		var map = new HashMap[String, MongoMatch]
		map["Chromium"] = (new MongoMatch).regex("user_agent", "Chromium")
		map["Edge"] = (new MongoMatch).regex("user_agent", "Edge")
		map["Firefox"] = (new MongoMatch).regex("user_agent", "Firefox")
		map["IE"] = (new MongoMatch).regex("user_agent", "(MSIE)|(Trident)")
		map["Netscape"] = (new MongoMatch).regex("user_agent", "Netscape")
		map["Opera"] = (new MongoMatch).regex("user_agent", "Opera")
		map["Safari"] = (new MongoMatch).land(null, [
				(new MongoMatch).regex("user_agent", "Safari"),
				(new MongoMatch).regex("user_agent", "^((?!Chrome).)*$")])
		map["Chrome"] = (new MongoMatch).land(null, [
				(new MongoMatch).regex("user_agent", "Chrome"),
				(new MongoMatch).regex("user_agent", "^((?!Edge).)*$")])

		return map
	end

	# Apply the `query` on `TrackerRepo::count`
	fun browser_count(query: MongoMatch): Int do return config.tracker_logs.count(query)

	redef fun get(req, res) do
		var browsers = new Array[BrowserCount]
		for browser, query in self.browser_queries do
			var count = new BrowserCount(browser, browser_count(query))
			if count.count > 0 then browsers.add count
		end
		var sum = 0
		for browser in browsers do sum += browser.count
		var other = config.tracker_logs.count - sum
		if other > 0 then browsers.add new BrowserCount("Other", other)
		default_comparator.sort(browsers)
		res.json new JsonArray.from(browsers)
	end
end

# Associate each browser to its count.
#
# Only used to serialize the results.
private class BrowserCount
	super Comparable
	super RepoObject
	serialize

	redef type OTHER: SELF

	var browser: String
	var count: Int

	redef fun <=>(o) do return o.count <=> count
end

# Return last month response time
class PopTrackerResponseTime
	super TrackerHandler

	redef fun get(req, res) do
		var limit = get_time - (3600 * 24 * 30)
		var pipe = new MongoPipeline
		pipe.match((new MongoMatch).gte("timestamp", limit))
		pipe.group((new MongoGroup("$timestamp")).
			sum("visits", 1).
			avg("response_time", "$response_time"))
		pipe.sort((new MongoMatch).eq("_id", -1))
		res.json new JsonArray.from(config.tracker_logs.collection.aggregate(pipe))
	end
end

# A tracker log entry used to store HTTP requests and their given HTTP responses
class LogEntry
	super RepoObject
	serialize

	# HTTP request that triggered that log entry
	var request: HttpRequest

	# HTTP response returned by the serveur
	var response: HttpResponse

	# Request user-agent shortcut (easier for db requests
	var user_agent: nullable String is lazy do return request.header["User-Agent"]

	# Processing time in miliseconds (or null if no clock was found in request)
	var response_time: nullable Int is lazy do
		var clock = request.clock
		if clock == null then return null
		return (clock.total * 1000.0).to_i
	end

	# Log entry timestamp
	var timestamp: Int = get_time

	# Session ID associated to this entry
	var session: nullable String is lazy do
		var session = request.session
		if session == null then return null
		return session.id_hash
	end
end

# Repository to store track logs.
class TrackerRepo
	super MongoRepository[LogEntry]
end

lib/popcorn/pop_tracker.nit:34,1--237,3