Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
fix: avoid toString as downloads count key (#1438)
Browse files Browse the repository at this point in the history
fix sql error as below:

```
near 'toString() { [native code] }1, gmt_modified='2019-02-21 01:05:55' WHERE id = 185' at line 1; name: toString, count: function toString() { [native code] }1, date: 2019-02-21
```
  • Loading branch information
fengmk2 authored Feb 21, 2019
1 parent f7e9670 commit f9d4858
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions controllers/registry/package/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var packageService = require('../../../services/package');
var downloadTotalService = require('../../../services/download_total');
var config = require('../../../config');

var _downloads = {};
let globalDownloads = new Map();

module.exports = function* download(next) {
var name = this.params.name || this.params[0];
Expand Down Expand Up @@ -43,11 +43,13 @@ module.exports = function* download(next) {
}
this.status = 302;
this.set('Location', url);
_downloads[name] = (_downloads[name] || 0) + 1;
const count = (globalDownloads.get(name) || 0) + 1;
globalDownloads.set(name, count);
return;
}

_downloads[name] = (_downloads[name] || 0) + 1;
const count = (globalDownloads.get(name) || 0) + 1;
globalDownloads.set(name, count);

if (config.downloadRedirectToNFS && url) {
this.status = 302;
Expand Down Expand Up @@ -85,14 +87,13 @@ defer.setInterval(function* () {
// save download count
var totals = [];
var allCount = 0;
for (var name in _downloads) {
var count = _downloads[name];
for (const [ name, count ] of globalDownloads) {
if (name !== '__all__') {
totals.push([name, count]);
}
allCount += count;
}
_downloads = {};
globalDownloads = new Map();

if (allCount === 0) {
return;
Expand All @@ -114,8 +115,9 @@ defer.setInterval(function* () {
err.message += '; name: ' + name + ', count: ' + count + ', date: ' + date;
logger.error(err);
}
// save back to _downloads, try again next time
_downloads[name] = (_downloads[name] || 0) + count;
// save back to globalDownloads, try again next time
count = (globalDownloads.get(name) || 0) + count;
globalDownloads.set(name, count);
}
}
saving = false;
Expand Down

0 comments on commit f9d4858

Please sign in to comment.