1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-24 14:30:56 +01:00

Add PhabricatorFactSpec, for naming and formatting facts

Summary: Not totally sure about this but I think it's okay?

Test Plan: Loaded /fact/, got a more readable page.

Reviewers: vrana, btrahan

Reviewed By: vrana

CC: aran

Maniphest Tasks: T1562

Differential Revision: https://secure.phabricator.com/D3090
This commit is contained in:
epriestley 2012-07-27 17:29:44 -07:00
parent d9296638cd
commit f652123c5a
7 changed files with 178 additions and 5 deletions

View file

@ -638,6 +638,8 @@ phutil_register_library_map(array(
'PhabricatorFactManagementStatusWorkflow' => 'applications/fact/management/PhabricatorFactManagementStatusWorkflow.php', 'PhabricatorFactManagementStatusWorkflow' => 'applications/fact/management/PhabricatorFactManagementStatusWorkflow.php',
'PhabricatorFactManagementWorkflow' => 'applications/fact/management/PhabricatorFactManagementWorkflow.php', 'PhabricatorFactManagementWorkflow' => 'applications/fact/management/PhabricatorFactManagementWorkflow.php',
'PhabricatorFactRaw' => 'applications/fact/storage/PhabricatorFactRaw.php', 'PhabricatorFactRaw' => 'applications/fact/storage/PhabricatorFactRaw.php',
'PhabricatorFactSimpleSpec' => 'applications/fact/spec/PhabricatorFactSimpleSpec.php',
'PhabricatorFactSpec' => 'applications/fact/spec/PhabricatorFactSpec.php',
'PhabricatorFactUpdateIterator' => 'applications/fact/extract/PhabricatorFactUpdateIterator.php', 'PhabricatorFactUpdateIterator' => 'applications/fact/extract/PhabricatorFactUpdateIterator.php',
'PhabricatorFeedBuilder' => 'applications/feed/builder/PhabricatorFeedBuilder.php', 'PhabricatorFeedBuilder' => 'applications/feed/builder/PhabricatorFeedBuilder.php',
'PhabricatorFeedConstants' => 'applications/feed/constants/PhabricatorFeedConstants.php', 'PhabricatorFeedConstants' => 'applications/feed/constants/PhabricatorFeedConstants.php',

View file

@ -19,17 +19,32 @@
final class PhabricatorFactHomeController extends PhabricatorFactController { final class PhabricatorFactHomeController extends PhabricatorFactController {
public function processRequest() { public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
$types = array(
'+N:*',
'+N:DREV',
'updated',
);
$engines = PhabricatorFactEngine::loadAllEngines();
$specs = PhabricatorFactSpec::newSpecsForFactTypes($engines, $types);
$facts = id(new PhabricatorFactAggregate())->loadAllWhere( $facts = id(new PhabricatorFactAggregate())->loadAllWhere(
'factType LIKE %> OR factType = %s', 'factType IN (%Ls)',
'+N:', $types);
'updated');
$rows = array(); $rows = array();
foreach ($facts as $fact) { foreach ($facts as $fact) {
$spec = $specs[$fact->getFactType()];
$name = $spec->getName();
$value = $spec->formatValueForDisplay($user, $fact->getValueX());
$rows[] = array( $rows[] = array(
phutil_escape_html($fact->getFactType()), phutil_escape_html($name),
(int)$fact->getValueX(), phutil_escape_html($value),
); );
} }

View file

@ -21,6 +21,24 @@
*/ */
final class PhabricatorFactCountEngine extends PhabricatorFactEngine { final class PhabricatorFactCountEngine extends PhabricatorFactEngine {
public function getFactSpecs(array $fact_types) {
$results = array();
foreach ($fact_types as $type) {
if (!strncmp($type, '+N:', 3)) {
if ($type == '+N:*') {
$name = 'Total Objects';
} else {
$name = 'Total Objects of type '.substr($type, 3);
}
$results[] = id(new PhabricatorFactSimpleSpec($type))
->setName($name)
->setUnit(PhabricatorFactSimpleSpec::UNIT_COUNT);
}
}
return $results;
}
public function shouldComputeRawFactsForObject(PhabricatorLiskDAO $object) { public function shouldComputeRawFactsForObject(PhabricatorLiskDAO $object) {
return true; return true;
} }

View file

@ -32,6 +32,10 @@ abstract class PhabricatorFactEngine {
return $objects; return $objects;
} }
public function getFactSpecs(array $fact_types) {
return array();
}
public function shouldComputeRawFactsForObject(PhabricatorLiskDAO $object) { public function shouldComputeRawFactsForObject(PhabricatorLiskDAO $object) {
return false; return false;
} }

View file

@ -21,6 +21,18 @@
*/ */
final class PhabricatorFactLastUpdatedEngine extends PhabricatorFactEngine { final class PhabricatorFactLastUpdatedEngine extends PhabricatorFactEngine {
public function getFactSpecs(array $fact_types) {
$results = array();
foreach ($fact_types as $type) {
if ($type == 'updated') {
$results[] = id(new PhabricatorFactSimpleSpec($type))
->setName('Facts Last Updated')
->setUnit(PhabricatorFactSimpleSpec::UNIT_EPOCH);
}
}
return $results;
}
public function shouldComputeAggregateFacts() { public function shouldComputeAggregateFacts() {
return true; return true;
} }

View file

@ -0,0 +1,54 @@
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class PhabricatorFactSimpleSpec extends PhabricatorFactSpec {
private $type;
private $name;
private $unit;
public function __construct($type) {
$this->type = $type;
}
public function getType() {
return $this->type;
}
public function setUnit($unit) {
$this->unit = $unit;
return $this;
}
public function getUnit() {
return $this->unit;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function getName() {
if ($this->name !== null) {
return $this->name;
}
return parent::getName();
}
}

View file

@ -0,0 +1,68 @@
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
abstract class PhabricatorFactSpec {
const UNIT_COUNT = 'unit-count';
const UNIT_EPOCH = 'unit-epoch';
public static function newSpecsForFactTypes(
array $engines,
array $fact_types) {
assert_instances_of($engines, 'PhabricatorFactEngine');
$map = array();
foreach ($engines as $engine) {
$specs = $engine->getFactSpecs($fact_types);
$specs = mpull($specs, null, 'getType');
$map += $specs;
}
foreach ($fact_types as $type) {
if (empty($map[$type])) {
$map[$type] = new PhabricatorFactSimpleSpec($type);
}
}
return $map;
}
abstract public function getType();
public function getUnit() {
return null;
}
public function getName() {
$type = $this->getType();
return "Fact ({$type})";
}
public function formatValueForDisplay(PhabricatorUser $user, $value) {
$unit = $this->getUnit();
switch ($unit) {
case self::UNIT_COUNT:
return number_format($value);
case self::UNIT_EPOCH:
return phabricator_datetime($value, $user);
default:
return $value;
}
}
}