Skip to content

Commit

Permalink
Show available metrics in Program views, in addition to existing metrics
Browse files Browse the repository at this point in the history
Move retention offset to Event::AVAILABLE_METRICS

Bug: https://phabricator.wikimedia.org/T187380
  • Loading branch information
MusikAnimal committed Feb 28, 2018
1 parent 9b8e998 commit e54eb5a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/Resources/views/programs/index.html.twig
Expand Up @@ -46,7 +46,7 @@
</td>
{% for metric in metrics|keys %}
<td class="text-nowrap">
{{ program.statistic(metric)|num_format }}
{{ (program.statistic(metric) ? program.statistic(metric).value|num_format : '&ndash;')|raw }}
</td>
{% endfor %}
</tr>
Expand Down
4 changes: 2 additions & 2 deletions app/Resources/views/programs/show.html.twig
Expand Up @@ -64,9 +64,9 @@
</td>
{% endif %}
<td class="text-nowrap">{{ event.numParticipants|num_format }}</td>
{% for metric in metrics|keys %}
{% for metric in metrics %}
<td class="text-nowrap">
{{ event.statistic(metric) ? event.statistic(metric).value|num_format : 0 }}
{{ (event.statistic(metric) ? event.statistic(metric).value|num_format : '&ndash;')|raw }}
</td>
{% endfor %}
</tr>
Expand Down
3 changes: 0 additions & 3 deletions app/config/parameters.yml.dist
Expand Up @@ -39,9 +39,6 @@ parameters:
# Do not include a trailing slash.
app.root_path: ''

# Number of days after the event, at which a user is consider to have been retained.
app.retention_offset: 7

# Number of revisions to show per page on the Event Data page.
app.revisions_per_page: 100

Expand Down
7 changes: 3 additions & 4 deletions src/AppBundle/Controller/EventController.php
Expand Up @@ -348,7 +348,8 @@ public function showAction()

/**
* Get EventStats from the given Event. If there are none, empty EventStats
* are returned for each metric type specified by self::METRIC_TYPES.
* are returned for each metric type specified by EventStat::METRIC_TYPES,
* with the default 'offset' values sepcified by Event::getAvailableMetrics().
* This way we can show placeholders in the view.
* @param Event $event
* @return EventStat[]
Expand All @@ -360,9 +361,7 @@ private function getEventStats(Event $event)
}

return array_map(function ($metric) use ($event) {
$offset = $metric === 'retention'
? $this->container->getParameter('app.retention_offset')
: null;
$offset = Event::getAvailableMetrics()[$metric];
return new EventStat($event, $metric, null, $offset);
}, EventStat::getMetricTypes());
}
Expand Down
5 changes: 3 additions & 2 deletions src/AppBundle/Controller/ProgramController.php
Expand Up @@ -5,6 +5,7 @@

namespace AppBundle\Controller;

use AppBundle\Model\Event;
use AppBundle\Model\Program;
use AppBundle\Model\Organizer;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function indexAction()
'programs' => $organizer->getPrograms(),
'programRepo' => $programRepo,
'gmTitle' => 'my-programs',
'retentionThreshold' => $this->container->getParameter('app.retention_offset'),
'retentionThreshold' => Event::getAvailableMetrics()['retention'],
'metrics' => $organizerRepo->getUniqueMetrics($organizer),
]);
}
Expand Down Expand Up @@ -140,7 +141,7 @@ public function showAction()

return $this->render('programs/show.html.twig', [
'program' => $this->program,
'retentionThreshold' => $this->container->getParameter('app.retention_offset'),
'retentionThreshold' => Event::getAvailableMetrics()['retention'],
'metrics' => $programRepo->getUniqueMetrics($this->program),
'isOrganizer' => $this->authUserIsOrganizer($this->program),
]);
Expand Down
29 changes: 29 additions & 0 deletions src/AppBundle/Model/Event.php
Expand Up @@ -35,6 +35,22 @@
*/
class Event
{
/**
* Available metrics type, hard-coded here for accessibility,
* while the logic to compute these stats lives in EventProcessor.
*
* Keys are i18n message keys, values are the 'offset' values.
*
* @see EventProcessor
* @see EventStat
*/
const AVAILABLE_METRICS = [
'new-editors' => null,
'pages-created' => null,
'pages-improved' => null,
'retention' => 7,
];

/**
* NOTE: Some methods pertaining to titles and Participants
* live in the TitleUserTrait trait.
Expand Down Expand Up @@ -174,6 +190,19 @@ public function getId()
return $this->id;
}

/**
* Get the available metric types and their default offset values.
* @return array
* @static
*
* No need to test a hard-coded list.
* @codeCoverageIgnore
*/
public static function getAvailableMetrics()
{
return self::AVAILABLE_METRICS;
}

/***********
* PROGRAM *
***********/
Expand Down
9 changes: 8 additions & 1 deletion src/AppBundle/Repository/ProgramRepository.php
Expand Up @@ -5,6 +5,7 @@

namespace AppBundle\Repository;

use AppBundle\Model\Event;
use AppBundle\Model\EventStat;
use AppBundle\Model\Organizer;
use AppBundle\Model\Program;
Expand All @@ -31,6 +32,8 @@ public function getEntityClass()

/**
* Get the unique metrics for this Program, across all Events.
* This also combines the configured metrics in Event::getAvailableMetrics(),
* regardless if the stats exist on the Program or its Events.
* @param Program $program
* @return string[]
*/
Expand All @@ -49,8 +52,11 @@ public function getUniqueMetrics(Program $program)
$eventWikiMetrics = $this->getEventWikiMetrics($rqb, $eventIds);

$metrics = array_merge($eventMetrics, $eventWikiMetrics);
$uniqueMetrics = [];

// Start with available metrics.
$uniqueMetrics = Event::getAvailableMetrics();

// Merge in any differing metrics that exist on the Program.
foreach ($metrics as $metric) {
// For each $metric, the first element is the metric name,
// and the second element is the offset value.
Expand All @@ -59,6 +65,7 @@ public function getUniqueMetrics(Program $program)
}
}

// Include configured metrics.
return $uniqueMetrics;
}

Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Service/EventProcessor.php
Expand Up @@ -161,7 +161,7 @@ private function setRetention()
{
$this->log("\nFetching retention...");

$retentionOffset = (int)$this->container->getParameter('app.retention_offset');
$retentionOffset = Event::getAvailableMetrics()['retention'];
$end = $this->event->getEndWithTimezone()->modify("+$retentionOffset days");
$usernames = $this->event->getParticipantNames();

Expand Down

0 comments on commit e54eb5a

Please sign in to comment.