Coming from #32124: Enable download statistics.

We currently track download stats in the project_release table. We should make this data visible on the project pages.

The way the reported installs is done is in drupalorg_project module, so I assume that needs to happen here too. :)

Comments

webchick’s picture

Here's the project usage hunk of drupalorg_project:

  // Usage stats.
  $total_usage = project_usage_get_project_total_usage($node->nid);
  if ($total_usage) {
    $label = t('Reported installs');
    $info[$label] = format_plural($total_usage,
      '<span class="count">1</span> site currently reports using this @project-type. <a href="/project/usage/!project-shortname">View usage statistics</a>.',
      '<span class="count">@count</span> sites currently report using this @project-type. <a href="/project/usage/!project-shortname">View usage statistics</a>.',
      array(
        '@project-type' => drupal_strtolower($project_type->label),
        '!project-shortname' => $node->project['uri'],
      )
    );
  }
webchick’s picture

And here's the relevant functions from project_usage module:

/**
 * Return the total usage data for a given project across all versions.
 *
 * @param $nid
 *   The project node ID.
 *
 * @return
 *   The total reported usage for all versions of the given project.
 */
function project_usage_get_project_total_usage($nid) {
  $active_tids = project_release_compatibility_list();
  static $total = array();
  if (empty($total[$nid])) {
    $total[$nid] = 0;
    foreach ($active_tids as $tid => $term_name) {
      $total[$nid] += project_usage_get_project_usage($nid, $tid);
    }
  }
  return $total[$nid];
}

/**
 * Return usage data for a given API version of a project.
 *
 * @param $nid
 *   The project node ID.
 * @param $tid
 *   The API compatibility taxonomy term ID to get usage for.
 *
 * @return
 *   The total reported usage for the given version of the given project.
 */
function project_usage_get_project_usage($nid, $tid) {
  static $usage = array();
  module_load_include('inc', 'project_usage', 'includes/date_api');
  if (empty($usage[$nid][$tid])) {
    $usage[$nid][$tid] = db_result(db_query("SELECT count FROM {project_usage_week_project} WHERE nid = %d AND tid = %d AND timestamp = %d", $nid, $tid, project_usage_get_current_active_week()));
  }
  return $usage[$nid][$tid];
}

/**
 * Return usage data for a given release.
 *
 * @param $nid
 *   The release node ID.
 *
 * @return
 *   The total reported usage for the given release.
 */
function project_usage_get_release_usage($nid) {
  static $usage = array();
  module_load_include('inc', 'project_usage', 'includes/date_api');
  if (empty($usage[$nid])) {
    $usage[$nid] = db_result(db_query("SELECT count FROM {project_usage_week_release} WHERE nid = %d AND timestamp = %d", $nid, project_usage_get_current_active_week()));
  }
  return $usage[$nid];
}

webchick’s picture

And the downloads are stored in the project_release_file table:

CREATE TABLE `project_release_file` (
  `fid` int(10) unsigned NOT NULL default '0',
  `nid` int(10) unsigned NOT NULL default '0',
  `filehash` varchar(32) NOT NULL default '',
  `weight` tinyint(4) NOT NULL default '0',
  `downloads` int(11) NOT NULL default '0',
  PRIMARY KEY  (`fid`),
  KEY `nid` (`nid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
dww’s picture

Status: Active » Postponed

I don't think this should be done until the plumbing that's populating {project_release_file}.downloads isn't held together with duct tape and bailing wire. ;) Did fixing that ever happen? Looking at #32124: Enable download statistics it doesn't seem so...

Also, presumably on the project page we'd want total downloads across all files of all releases for that project? That's not exactly a trivial query to run on every project page load. So, we should either:

A) Pre-compute these totals in bdragon's scripts via mongo and populate another {project_release_project_download} table (or something) - effectively denormalize it for this.

B) Do the expensive query on demand, but cache it into {cache} or {cache_project_release} or something.

Either of those solutions seems more appropriate to handle over at #32124 (or yet another split-off issue) not here, but I wanted to raise the point to further support "postponed" as the currently-appropriate status here. ;)

Cheers,
-Derek

webchick’s picture

webchick’s picture

One other caveat is that the download tracking only goes back as far as January 2009. We should make sure to note that next to the count.

sun’s picture

Priority: Normal » Minor
drumm’s picture

Priority: Minor » Normal
Status: Postponed » Active
dww’s picture

Status: Active » Postponed

Cool, nice to see progress here, thanks!

However, http://drupalcode.org/project/infrastructure.git/blob/HEAD:/live/process... from #32124: Enable download statistics doesn't handle the project-wide totals I talked about in #4 (quick skim seems to indicate we're computing them in mongo, but not saving them to the SQL DB so we can use them), nor is there any support in project_release to save those numbers. So, either we need to reopen #32124 or we need a new issue for this one to move forward. Pre-computing those totals would also help project_solr when indexing project nodes so we could have filters/sorts based on them at /project/modules and friends.

Thanks,
-Derek

drumm’s picture

Assigned: Unassigned » drumm
Status: Postponed » Active

If we want a simple number for the front of the project page,

SELECT sum(downloads) FROM project_release_nodes prn INNER JOIN project_release_file prf ON prf.nid = prn.nid WHERE prn.pid = 3060;

says 7,613,248. Explain doesn't look too bad:

+----+-------------+-------+------+------------------------------+----------------------+---------+----------------+------+-------------+
| id | select_type | table | type | possible_keys                | key                  | key_len | ref            | rows | Extra       |
+----+-------------+-------+------+------------------------------+----------------------+---------+----------------+------+-------------+
|  1 | SIMPLE      | prn   | ref  | PRIMARY,project_releases_pid | project_releases_pid | 4       | const          |  152 | Using index | 
|  1 | SIMPLE      | prf   | ref  | nid                          | nid                  | 4       | drupal.prn.nid |    1 |             | 
+----+-------------+-------+------+------------------------------+----------------------+---------+----------------+------+-------------+
drumm’s picture

(Didn't mean to un-postpone, just had the values from an old comment.)

I'm not sure that doing the query on the fly for project and release pages will be all that bad. We shouldn't expose that as a filter for views or anything, but we can stuff the number in Solr and let it sort.

dww’s picture

drumm’s picture

Status: Active » Fixed
Issue tags: +needs drupal.org deployment
drumm’s picture

Issue tags: -needs drupal.org deployment

Deployed.

juan_g’s picture

Really really useful, together with usage stats. And download count is working nicely for distributions as well. Thank you!

Automatically closed -- issue fixed for 2 weeks with no activity.

  • Commit 2aa2d45 on 6.x-3.x, 7.x-3.x-dev by drumm:
    [#1353138] Display download count on project page