npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

timeseries-stat

v0.0.1

Published

timeseries-stat

Readme

Timeseries statistics util

统计数据的时间变化趋势

统计数据根据时间的变化趋势

  • model 数据模型,模型必须有时间字段
  • options
    • timeFieldName {String}时间字段名称,默认是'createdAt'
    • unit - {String}时间统计单位,为枚举值,{'year','month','day','hour','minute']
    • start - {Date}开始时间
    • end - {Date}结束时间
    • query - {Object}时间之外的查询条件
    • reduceFields - {[String]}要累计的字段,如果为空,则进行count计算
    • autofit - {Boolean, default:false} 是否进行数据填充,当某个时间序点数值为空时,自动进行填充.

样例

var statUtils = require('timeseries-stat');

statUtils.statByTime(model, {unit: 'month', reduceFields: ['totalFee']}, function (err, results) {
        console.info('autofit result\n', results);
 })

输出结果为

 { month: 
   [ '2015-01',
     '2015-02',
     '2015-03',
     '2015-04',
     '2015-05',
     '2015-06',
     '2015-07',
     '2015-08',
     '2015-09',
     '2015-10',
     '2015-11',
     '2015-12' ],
  count: [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 4, 1 ],
  totalFee: [ 0, 0, 0, 0, 0, 0, 0, 0, 5000, 5000, 21500, 6000 ] }

执行用例可以参见test/timeseries-stat-test.js文件

时间序列填充

进行时间序列统计时,经常遇到时间空缺的场景,可以对空缺值进行自动填充:

样例

var t = require('timeseries-stat').autofit;

var inputObj = {};
inputObj.month = ['2014-09','2015-09','2015-11'];
inputObj.income = [50,100001,98888];
inputObj.cost = [90,70001,78888];
t.fitTimeSeries4ArrayInObj(inputObj, {timeField: 'month'});

输出结果为:

 { month: 
   [ '2014-12',
     '2015-01',
     '2015-02',
     '2015-03',
     '2015-04',
     '2015-05',
     '2015-06',
     '2015-07',
     '2015-08',
     '2015-09',
     '2015-10',
     '2015-11' ],
  income: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 100001, 0, 98888 ],
  cost: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 70001, 0, 78888 ] }

另外也可以对下列格式内容进行数值填充

var input = [{month: '2015-09', income: 100001, cost: 70001},
    {month: '2015-11', income: 98888, cost: 78888}];
t.fitTimeSeries4ObjInArray(input,{timeField:'month'}));

输出结果为:

 [ { month: '2014-12', income: 0, cost: 0 },
  { month: '2015-01', income: 0, cost: 0 },
  { month: '2015-02', income: 0, cost: 0 },
  { month: '2015-03', income: 0, cost: 0 },
  { month: '2015-04', income: 0, cost: 0 },
  { month: '2015-05', income: 0, cost: 0 },
  { month: '2015-06', income: 0, cost: 0 },
  { month: '2015-07', income: 0, cost: 0 },
  { month: '2015-08', income: 0, cost: 0 },
  { month: '2015-09', income: 100001, cost: 70001 },
  { month: '2015-10', income: 0, cost: 0 },
  { month: '2015-11', income: 98888, cost: 78888 } ]