npm-statistic

1.2.0 • Public • Published

npm-statistic

NPM version node dependencies License MIT

NPM

npm-statistic get npm download stats for chosen packages and save it to JSON files.
It's a console command for regularly invoke (by cron, for example).
Package statistics taken from package's npm page.

Usage

You need a node version >=6.0.0.
Install npm-statistic localy or global, then add some packages to config, and run update command for saving current packages stats (config.json and files like stats/package-name/09.2016.json created automatically in npm-statistic directory):

$ npm-statistic add express
$ npm-statistic update
$ npm-statistic show express

You can use npm-statistic programmatically:

const npmStatistic = require('npm-statistic');
 
/**
 * @param {string[]} Args list.
 */
npmStatistic([`add`, 'express']);
npmStatistic([`update`]); // or npmStatistic([]);
npmStatistic([`show`, 'express']);

Commands

add

Add package (by package name) to config for regular updating his statistics:

$ npm-statistic add package-name

For example, add react package:

$ npm-statistic add react

update

Default command. Update statistics for all packages from config. No arguments:

$ npm-statistic update

or

$ npm-statistic

get

Get config (as JSON object) or his parts.
Get full config:

$ npm-statistic get

Get foo field of config (as JSON object):

$ npm-statistic get foo

Get foo.bar field of config (as JSON object):

$ npm-statistic get foo.bar

For example, field packages contain array of all config packages:

$ npm-statistic get packages

First package:

$ npm-statistic get packages.0

Third package:

$ npm-statistic get packages.2

set

Set config parts (as JSON object); "packages" array in config contains all packages for which stats should be updated. Additional fields in the config file can be used to extend the functionality.
Set string value of foo field of config:

$ npm-statistic set foo value

Set JSON value of foo.bar field of config:

$ npm-statistic set foo.bar {a: 2}

For example, field timeout contain response timeout in milliseconds (if there is no such field, default timeout is 16 * 1024):

$ npm-statistic set timeout 4000

Now timeout is 4 seconds.

Field open contain maximum number of opened stats requests (if there is no such field, default value is 2). The higher the value, the faster will be updated packages statistics, but also the greater the likelihood of disconnection.

$ npm-statistic set open 4

Now npm-statistic will send four requests at a time.

Field retry contain timeout (in milliseconds) between attempts to send stat request (if there is no such field, default value is 512):

$ npm-statistic set retry 100

Now when you run update command each package will check for available connections ten times per second.

Field attempts contain number of stat requests attempts (each sended request is the one attempt). If there is no such field, default value is 4.

$ npm-statistic set attempts 2

Now if first request for some package will be aborted, npm-statistic send only one new request for this package.

For deleting, for example, third package from config you can set it to null:

$ npm-statistic set packages.2 null

For temporary skipping package in statistic update you can set package field skip to true:

$ npm-statistic set packages.3.skip true

Now package statistics do not updated.

$ npm-statistic set packages.3.skip false

Package statistics updated again.

For deleting last added package you can shorten the length of list of packages per one:

$ npm-statistic get packages.length
  14
$ npm-statistic set packages.length 13

show

Show raw saved month statistics of package by package name.
Show full statistics of package for current month (if it is):

$ npm-statistic show package-name

Show full statistics of package for custom month (if it is):

$ npm-statistic show package-name 10.2016

Show last four statistic snapshots for custom month (if it is):

$ npm-statistic show -4 package-name 10.2016

Show last statistic snapshots for current month (if it is):

$ npm-statistic show -1 package-name

last

Show some fields of last statistic snapshots for all packages (day is the default field).
A list of fields separated by spaces:

$ npm-statistic last version week

It displays a list of all config packages with the latest versions and the number of downloads in the last week.

$ npm-statistic last month

It displays a list of all config packages with the number of downloads in the last month.
A list of all available fields is below.

logs

Show last log messages.
All messages (like cat logs.txt):

$ npm-statistic logs

Last three messages (like tail -3 logs.txt, but messages could be multiline):

$ npm-statistic logs -3

version

Show version of npm-statistic.

help

Show short help.

Details

Important: if you update (reinstall) npm-statistic, config and accumulated statistics disappear (as they are stored in the installation directory). You can save config.json and stats/ in other directory before update, and move them to npm-statistic directory after update.

Example of one package statistic snapshot:

{ date: '2016-09-19T22:33:50.436Z',
  httpStatus: 200,
  name: 'react',
  version: '15.3.1',
  release: 88,
  dependencies: 3,
  publisher: 'zpao',
  publishDate: '2016-08-19T18:50:25.665Z',
  day: 80366,
  week: 469720,
  month: 2056691 }
  • date: date of snapshot
  • httpStatus: HTTP status code of response
  • name: package name
  • version: current version of package
  • release: number of releases
  • dependencies: number of package dependencies
  • publisher: package publisher (name of NPM user)
  • publishDate: date of publishing
  • day: number of downloads in the last day
  • week: number of downloads in the last week
  • month: number of downloads in the last month

npm-statistic does all file system operations in synchronous mode (but statistics https-requests are asynchronous, of course).

All errors are written to logs.txt in npm-statistic directory.

You can run npm-statistic with any frequency, because if the package has not changed statistics, new records in the file does not occur (changes only the timestamp of the last update statistics). Therefore, the statistics file size is always limited by the number of changes in the statistics on package npm-page.

If you add nonexistent package to config, npm-statistic saves "stats" for this package, but with response status 404 (it is convenient to store the statuses 500, 503 and so on, when www.npmjs.com works with problems).

Tests

45 Mocha tests:

$ npm install
$ npm test

License

MIT

Package Sidebar

Install

npm i npm-statistic

Weekly Downloads

3

Version

1.2.0

License

MIT

Last publish

Collaborators

  • uid-11222