Add rudimentary support for working group metrics.

Change-Id: I50b4b9ffdf9f7f3516f17c0449c6b860d65b02c1
Signed-off-by: Wayne Beaton <wayne.beaton@eclipse-foundation.org>
diff --git a/classes/WorkingGroup.class.inc b/classes/WorkingGroup.class.inc
new file mode 100644
index 0000000..89926ed
--- /dev/null
+++ b/classes/WorkingGroup.class.inc
@@ -0,0 +1,76 @@
+<?php
+/*
+* Copyright (c) Eclipse Foundation and others.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*/
+
+require_once dirname(__FILE__) . '/Project.class.php';
+require_once dirname(__FILE__) . '/database.inc';
+
+/**
+ * This is a simple representation of a working group
+ */
+class WorkingGroup {
+
+	var $data;
+
+	public static function getWorkingGroup($id) {
+		$wg = null;
+		$sql = "select id, name from WorkingGroup where id=':id:';";
+		query('dashboard', $sql, array(':id:' => $id), function($row) use (&$wg) {
+			$wg = new WorkingGroup($row);
+		});
+		return $wg;
+	}
+
+	public static function getAll() {
+		$wg = array();
+		$sql = "select id, name from WorkingGroup";
+		query('dashboard', $sql, array(), function($row) use (&$wg) {
+			$wg[] = new WorkingGroup($row);
+		});
+		return $wg;
+	}
+
+	public static function getAny() {
+		$wg = null;
+		$sql = "select id, name from WorkingGroup order by rand() limit 1";
+		query('dashboard', $sql, array(), function($row) use (&$wg) {
+			$wg = new WorkingGroup($row);
+		});
+		return $wg;
+	}
+
+	private function __construct($data) {
+		$this->data = $data;
+	}
+
+	public function getId() {
+		return $this->data['id'];
+	}
+
+	public function getName() {
+		return $this->data['name'];
+	}
+
+	public function projectsDo($callback) {
+		$sql = "
+			select
+				wgp.id,
+				p.id as project
+			from WorkingGroupProject as wgp
+			join Project as p on wgp.project=p.id
+			where wgp.id=':id:'";
+
+		query('dashboard', $sql, array(':id:' => $this->getId()), function($row) use (&$callback) {
+			if ($project = Project::getProject($row['project'])) {
+				call_user_func($callback, $project);
+			}
+		});
+	}
+}
\ No newline at end of file
diff --git a/tools/wg.php b/tools/wg.php
new file mode 100755
index 0000000..5b839f2
--- /dev/null
+++ b/tools/wg.php
@@ -0,0 +1,146 @@
+<?php
+/***********************************************************************
+ * Copyright (c) Eclipse Foundation and others.
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ ***********************************************************************/
+
+require_once "../../eclipse.org-common/system/app.class.php";
+require_once ($_SERVER ['DOCUMENT_ROOT'] . "/eclipse.org-common/system/nav.class.php");
+require_once ($_SERVER ['DOCUMENT_ROOT'] . "/eclipse.org-common/system/menu.class.php");
+
+$App = new App ();
+$Nav = new Nav ();
+$Menu = new Menu ();
+include ($App->getProjectCommon ());
+
+require_once '../classes/WorkingGroup.class.inc';
+require_once '../classes/Project.class.php';
+require_once '../classes/debug.php';
+require_once '../classes/database.inc';
+require_once '../classes/common.php';
+require_once './charts.inc';
+
+mustBeFoundationEmployee();
+
+$pageTitle = "Eclipse Working Groups Overview";
+$pageKeywords = "";
+$pageAuthor = "Wayne Beaton";
+
+ob_start ();
+
+if (!(($id = @$_GET['id']) && isValidProjectId($id))) $id = null;
+
+$options = array();
+$options['width'] = is_numeric(@$_GET['width']) ? @$_GET['width'] : 500;
+$options['height'] = is_numeric(@$_GET['height']) ? @$_GET['height'] : 200;
+$options['start'] =  @$_GET['start'] ? date('Y-m-d', strtotime(@$_GET['start'])) : null;
+$options['end'] = @$_GET['end'] ? date('Y-m-d', strtotime(@$_GET['end'])) : null;
+
+// Add an entry to focus the report on every
+// top-level project to the navigation bar.
+
+$Nav->addNavSeparator("Report", "./report.php");
+foreach(WorkingGroup::getAll() as $wg) {
+	$Nav->addCustomNav($wg->getName(), "?id={$wg->getId()}", "_self", 2);
+}
+
+$wg = WorkingGroup::getWorkingGroup($id);
+if (!$wg) $wg = WorkingGroup::getAny();
+
+function quarter($date) {
+	if ($date == null) return 'Future';
+
+	$year = date('Y', $date);
+	$month = date('n', $date);
+
+	return $year . 'Q' . (($month - 1) / 3 + 1);
+}
+
+?>
+<style>
+table.metrics {
+	border-spacing: 10px;
+	border-collapse: separate;
+}
+td.number {
+	text-align: right;
+}
+</style>
+<div id="maincontent">
+<div id="midcolumn">
+<h1><?= $wg->getName() ?></h1>
+
+<p>EXPERIMENTAL. This page shows some basic Working Group metrics.</p>
+
+<h2>Projects</h2>
+<?php
+	$projects = array();
+	$wg->projectsDo(function(Project $project) use (&$projects) {
+		$projects[quarter($project->getProvisionedDate())][] = $project;
+	});
+
+	ksort($projects);
+
+	foreach($projects as $quarter => $group) {
+		echo "<h3>{$quarter}</h3>";
+		echo "<ul>";
+		foreach($group as $project) {
+			echo "<li><a href=\"{$project->getUrl()}\">{$project->getFormalName()}</a></li>";
+		}
+		echo "</ul>";
+	}
+?>
+
+<?php
+function showMetrics($wg, $table, $fields) {
+	$columns = explode(',', $fields);
+
+	$sql = "
+		select {$fields}
+		from {$table}
+		where id=':id:';";
+
+	echo "<table class=\"metrics\"><tbody>";
+	echo "<tr>";
+	foreach($columns as $column) {
+		$title = ucfirst($column);
+		echo "<th>{$title}</th>";
+	}
+	echo "</tr>";
+	query('dashboard', $sql, array(':id:' => $wg->getId()), function($row) use (&$columns) {
+		echo "<tr>";
+		foreach($columns as $column) {
+			$value = $row[$column];
+			switch ($column) {
+				case 'year' :
+				case 'quarter' :
+					break;
+				default:
+					$value = number_format($row[$column]);
+			}
+			echo "<td class=\"number\">{$value}</td>";
+		}
+		echo "</tr>";
+	});
+	echo "</tbody></table>";
+}
+?>
+
+<h2>Yearly Metrics</h2>
+<?php showMetrics($wg, 'WorkingGroupYearlySummary', 'year,projects,commits,files,added,removed,authors,committers,companies')?>
+
+<h2>Quarterly Metrics</h2>
+<?php showMetrics($wg, 'WorkingGroupQuarterlySummary', 'quarter,projects,commits,files,added,removed,authors,committers,companies')?>
+
+</div>
+</div>
+
+<?php
+$html = ob_get_contents ();
+ob_end_clean ();
+$App->generatePage ( null, $Menu, $Nav, $pageAuthor, $pageKeywords, $pageTitle, $html );
+?>