Commit b7868fb6 authored by drumm's avatar drumm
Browse files

Issue #3117465: Group usage by branch instead of API compatibility in RestWS responses

parent f05fbdfd
......@@ -203,54 +203,6 @@ function project_usage_node_view($node) {
}
}
/**
* Returns the TIDs for the active core compatibility taxonomy terms.
*
* @return array
*/
function project_usage_get_active_tids() {
static $active_tids;
if (!isset($active_tids)) {
$term_query = new EntityFieldQuery();
$result = $term_query->entityCondition('entity_type', 'taxonomy_term')
->fieldCondition('field_release_recommended', 'value', 1)
->execute();
$active_tids = array_keys($result['taxonomy_term']);
}
return $active_tids;
}
/**
* Returns the latest project usage counts for all active versions of the given
* project.
*
* @param array $nids
* The project node IDs.
*
* @return array
*/
function project_usage_get_project_usage_for_active_tids(array $nids) {
$usage = &drupal_static(__FUNCTION__, []);
$flipped_nids = array_fill_keys($nids, []);
if ($remaining_nids = array_diff($nids, array_keys($usage))) {
$usage += $flipped_nids;
module_load_include('inc', 'project_usage', 'includes/date_api');
$result = db_query('SELECT pu.nid, ttd.name, pu.count FROM {project_usage_week_project} pu INNER JOIN {taxonomy_term_data} ttd ON pu.tid = ttd.tid WHERE pu.nid IN (:nids) AND pu.tid IN (:tids) AND pu.timestamp = :timestamp', [
':nids' => $remaining_nids,
':tids' => project_usage_get_active_tids(),
':timestamp' => project_usage_get_current_active_week(),
]);
foreach ($result as $row) {
$usage[$row->nid][$row->name] = $row->count;
}
}
return array_intersect_key($usage, $flipped_nids);
}
/**
* Implements hook_restws_response_alter().
*/
......@@ -275,8 +227,39 @@ function project_usage_restws_response_alter(&$response, $function, $formatName,
}
}
if (!empty($project_nids)) {
foreach (project_usage_get_project_usage_for_active_tids($project_nids) as $nid => $usage) {
$response['list'][$nid_map[$nid]]['project_usage'] = $usage;
module_load_include('inc', 'project_usage', 'includes/date_api');
$result = db_query('SELECT pu.project_nid, pu.nid, pu.count FROM {project_usage_week_release} pu WHERE pu.project_nid IN (:nids) AND pu.timestamp = :timestamp', [
':nids' => $project_nids,
':timestamp' => project_usage_get_current_active_week(),
]);
$usage = [];
$release_nids = [];
foreach ($result as $row) {
$usage[$row->project_nid][$row->nid] = $row->count;
$release_nids[] = $row->nid;
}
$result = (new EntityFieldQuery())->entityCondition('entity_type', 'node')
->propertyCondition('status', NODE_PUBLISHED)
->propertyCondition('nid', $release_nids)
->execute();
if (!empty($result['node'])) {
field_attach_load('node', $result['node'], FIELD_LOAD_CURRENT, [
'field_id' => field_info_field('field_release_version')['id'],
]);
$release_branches = [];
foreach ($result['node'] as $release) {
$release_branches[$release->nid] = project_release_get_branch($release->field_release_version[LANGUAGE_NONE][0]['value']) . 'x';
}
foreach ($usage as $project_nid => $releases) {
foreach ($releases as $nid => $count) {
if (isset($response['list'][$nid_map[$project_nid]]['project_usage'][$release_branches[$nid]])) {
$response['list'][$nid_map[$project_nid]]['project_usage'][$release_branches[$nid]] += $count;
}
else {
$response['list'][$nid_map[$project_nid]]['project_usage'][$release_branches[$nid]] = $count;
}
}
}
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment