| 1 | <?php␊ |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */␊ |
| 3 | /*␊ |
| 4 | # ***** BEGIN LICENSE BLOCK *****␊ |
| 5 | # This file is part of InDefero, an open source project management application.␊ |
| 6 | # Copyright (C) 2008-2011 CĂ©ondo Ltd and contributors.␊ |
| 7 | #␊ |
| 8 | # InDefero is free software; you can redistribute it and/or modify␊ |
| 9 | # it under the terms of the GNU General Public License as published by␊ |
| 10 | # the Free Software Foundation; either version 2 of the License, or␊ |
| 11 | # (at your option) any later version.␊ |
| 12 | #␊ |
| 13 | # InDefero is distributed in the hope that it will be useful,␊ |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
| 16 | # GNU General Public License for more details.␊ |
| 17 | #␊ |
| 18 | # You should have received a copy of the GNU General Public License␊ |
| 19 | # along with this program; if not, write to the Free Software␊ |
| 20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA␊ |
| 21 | #␊ |
| 22 | # ***** END LICENSE BLOCK ***** */␊ |
| 23 | ␊ |
| 24 | /**␊ |
| 25 | * Class that calculates the activity value for all projects on a␊ |
| 26 | * specific date and time.␊ |
| 27 | *␊ |
| 28 | * We do this by counting adds or updates of database objects in␊ |
| 29 | * the particular section (according to the timeline) and relate this␊ |
| 30 | * value to the overall activity of a section in the forge.␊ |
| 31 | *␊ |
| 32 | * To illustrate the behaviour, a simple example could be a forge with␊ |
| 33 | * only two projects that both have only issue tracking enabled.␊ |
| 34 | * The first project created or updated 10 tickets during the past period,␊ |
| 35 | * the other 20. The activity index for the first should therefor be␊ |
| 36 | * calculated as 0.33 and the second as 0.66.␊ |
| 37 | * Note that this simple example doesn't take activity in other␊ |
| 38 | * sections into account, so the the total activity of all projects␊ |
| 39 | * for a certain time period might add up to more than 1.0.␊ |
| 40 | *␊ |
| 41 | * @author tommyd␊ |
| 42 | */␊ |
| 43 | class IDF_ActivityTaxonomy␊ |
| 44 | {␊ |
| 45 | public static function recalculateTaxnomies(DateTime $date)␊ |
| 46 | {␊ |
| 47 | //␊ |
| 48 | // query and normalize the section weights␊ |
| 49 | //␊ |
| 50 | $sectionWeights = Pluf::f('activity_section_weights', array());␊ |
| 51 | $allWeights = array_sum($sectionWeights);␊ |
| 52 | if ($allWeights == 0) {␊ |
| 53 | throw new LogicException('the sum of all "activity_section_weights" must not be 0');␊ |
| 54 | }␊ |
| 55 | foreach ($sectionWeights as $section => $weight) {␊ |
| 56 | $sectionWeights[$section] = $weight / (float) $allWeights;␊ |
| 57 | }␊ |
| 58 | ␊ |
| 59 | //␊ |
| 60 | // determine the date boundaries␊ |
| 61 | //␊ |
| 62 | $lookback = Pluf::f('activity_lookback', 0);␊ |
| 63 | if ($lookback < 1) {␊ |
| 64 | throw new LogicException('lookback must be greater or equal to 1');␊ |
| 65 | }␊ |
| 66 | $dateCopy = new DateTime();␊ |
| 67 | $dateCopy->setTimestamp($date->getTimestamp());␊ |
| 68 | $dateBoundaries = array(␊ |
| 69 | $dateCopy->format('Y-m-d 23:59:59'),␊ |
| 70 | $dateCopy->sub(new DateInterval('P'.$lookback.'D'))->format('Y-m-d 00:00:00')␊ |
| 71 | );␊ |
| 72 | ␊ |
| 73 | //␊ |
| 74 | // now recalculate the values for all projects␊ |
| 75 | //␊ |
| 76 | $projects = Pluf::factory('IDF_Project')->getList();␊ |
| 77 | foreach ($projects as $project) {␊ |
| 78 | self::recalculateTaxonomy($date, $project, $dateBoundaries, $sectionWeights);␊ |
| 79 | }␊ |
| 80 | }␊ |
| 81 | ␊ |
| 82 | private static function recalculateTaxonomy(DateTime $date, IDF_Project $project, array $dateBoundaries, array $sectionWeights)␊ |
| 83 | {␊ |
| 84 | $conf = new IDF_Conf();␊ |
| 85 | $conf->setProject($project);␊ |
| 86 | ␊ |
| 87 | $sectionClasses = array(␊ |
| 88 | 'source' => array('IDF_Commit'),␊ |
| 89 | 'issues' => array('IDF_Issue'),␊ |
| 90 | 'wiki' => array('IDF_Wiki_Page', 'IDF_Wiki_Resource'),␊ |
| 91 | 'review' => array('IDF_Review'),␊ |
| 92 | 'downloads' => array('IDF_Upload')␊ |
| 93 | );␊ |
| 94 | ␊ |
| 95 | $value = 0;␊ |
| 96 | foreach ($sectionWeights as $section => $weight) {␊ |
| 97 | // skip closed / non-existant sections␊ |
| 98 | if ($conf->getVal($section.'_access_rights') === 'none')␊ |
| 99 | continue;␊ |
| 100 | ␊ |
| 101 | if (!array_key_exists($section, $sectionClasses))␊ |
| 102 | continue;␊ |
| 103 | ␊ |
| 104 | $sectionValue = self::calculateActivityValue(␊ |
| 105 | $dateBoundaries, $sectionClasses[$section], $project->id);␊ |
| 106 | $value = ((1 - $weight) * $value) + ($weight * $sectionValue);␊ |
| 107 | }␊ |
| 108 | ␊ |
| 109 | echo "project {$project->name} has an activity value of $value\n";␊ |
| 110 | ␊ |
| 111 | $sql = new Pluf_SQL('project=%s AND date=%s', array($project->id, $date->format('Y-m-d')));␊ |
| 112 | $activity = Pluf::factory('IDF_ProjectActivity')->getOne(array('filter' => $sql->gen()));␊ |
| 113 | ␊ |
| 114 | if ($activity == null) {␊ |
| 115 | $activity = new IDF_ProjectActivity();␊ |
| 116 | $activity->project = $project;␊ |
| 117 | $activity->date = $date->format('Y-m-d');␊ |
| 118 | $activity->value = $value;␊ |
| 119 | $activity->create();␊ |
| 120 | } else {␊ |
| 121 | $activity->value = $value;␊ |
| 122 | $activity->update();␊ |
| 123 | }␊ |
| 124 | }␊ |
| 125 | ␊ |
| 126 | private static function calculateActivityValue(array $dateBoundaries, array $classes, $projectId)␊ |
| 127 | {␊ |
| 128 | $allCount = self::countActivityFor($dateBoundaries, $classes);␊ |
| 129 | if ($allCount == 0) return 0;␊ |
| 130 | $prjCount = self::countActivityFor($dateBoundaries, $classes, $projectId);␊ |
| 131 | return $prjCount / (float) $allCount;␊ |
| 132 | }␊ |
| 133 | ␊ |
| 134 | private static function countActivityFor(array $dateBoundaries, array $classes, $projectId = null)␊ |
| 135 | {␊ |
| 136 | static $cache = array();␊ |
| 137 | $argIdent = md5(serialize(func_get_args()));␊ |
| 138 | if (array_key_exists($argIdent, $cache)) {␊ |
| 139 | return $cache[$argIdent];␊ |
| 140 | }␊ |
| 141 | ␊ |
| 142 | $cache[$argIdent] = 0;␊ |
| 143 | list($higher, $lower) = $dateBoundaries;␊ |
| 144 | $sql = new Pluf_SQL('model_class IN ("'.implode('","', $classes).'") '.␊ |
| 145 | 'AND creation_dtime >= %s AND creation_dtime <= %s',␊ |
| 146 | array($lower, $higher));␊ |
| 147 | ␊ |
| 148 | if ($projectId !== null) {␊ |
| 149 | $sql->SAnd(new Pluf_SQL('project=%s', array($projectId)));␊ |
| 150 | }␊ |
| 151 | ␊ |
| 152 | $cache[$argIdent] = Pluf::factory('IDF_Timeline')->getCount(array('filter' => $sql->gen()));␊ |
| 153 | ␊ |
| 154 | return $cache[$argIdent];␊ |
| 155 | }␊ |
| 156 | } |