File: Stats.php

package info (click to toggle)
simplesamlphp 1.13.1-2%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 11,304 kB
  • sloc: php: 65,124; xml: 629; python: 376; sh: 193; perl: 185; makefile: 43
file content (91 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

/**
 * Statistics handler class.
 *
 * This class is responsible for taking a statistics event and logging it.
 *
 * @package simpleSAMLphp
 */
class SimpleSAML_Stats {

	/**
	 * Whether this class is initialized.
	 * @var boolean
	 */
	private static $initialized = FALSE;


	/**
	 * The statistics output callbacks.
	 * @var array
	 */
	private static $outputs = NULL;


	/**
	 * Create an output from a configuration object.
	 *
	 * @param SimpleSAML_Configuration $config  The configuration object.
	 * @return
	 */
	private static function createOutput(SimpleSAML_Configuration $config) {
		$cls = $config->getString('class');
		$cls = SimpleSAML_Module::resolveClass($cls, 'Stats_Output', 'SimpleSAML_Stats_Output');

		$output = new $cls($config);
		return $output;
	}


	/**
	 * Initialize the outputs.
	 */
	private static function initOutputs() {

		$config = SimpleSAML_Configuration::getInstance();
		$outputCfgs = $config->getConfigList('statistics.out', array());

		self::$outputs = array();
		foreach ($outputCfgs as $cfg) {
			self::$outputs[] = self::createOutput($cfg);
		}
	}


	/**
	 * Notify about an event.
	 *
	 * @param string $event  The event.
	 * @param array $data  Event data. Optional.
	 */
	public static function log($event, array $data = array()) {
		assert('is_string($event)');
		assert('!isset($data["op"])');
		assert('!isset($data["time"])');
		assert('!isset($data["_id"])');

		if (!self::$initialized) {
			self::initOutputs();
			self::$initialized = TRUE;
		}

		if (empty(self::$outputs)) {
			/* Not enabled. */
			return;
		}

		$data['op'] = $event;
		$data['time'] = microtime(TRUE);

		/* The ID generation is designed to cluster IDs related in time close together. */
		$int_t = (int)$data['time'];
		$hd = SimpleSAML_Utilities::generateRandomBytes(16);
		$data['_id'] = sprintf('%016x%s', $int_t, bin2hex($hd));

		foreach (self::$outputs as $out) {
			$out->emit($data);
		}
	}

}