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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@vanioinformatika/stats-collector

v1.0.0

Published

Collector for runtime stats of a task

Downloads

4

Readme

Build Status Coverage Status NPM Version npm

node-stats-collector

Collector for runtime stats of a task

Usage

const StatsCollector = require('@vanioinformatika/stats-collector')

const maxErrorCount = 10 // if more than 10 errors occur, a maxErrorCountReached event will be emitted
const timeWindowMillis = 60 * 1000 // error occurences are counted in a 60 sec time window
const errorHistorySize = 100 // the last 100 errors are kept

const statCollector = new StatsCollector(maxErrorCount, timeWindowMillis, errorHistorySize)
statsCollector.on('maxErrorCountReached', () => {
  console.log('Maximum error count reached, something should be done...')
})

...

try {
  doSomeProcessing()
  statsCollector.success() // processing was successful
} catch (err) {
  statsCollector.error(err) // an error occured during processing
}

...

// publish runtime stats via HTTP
app.get('/stats', (req, res) => {
  res.json(statsCollector.toJSON(true)) // return error history in the response too
})

API

Constructor

StatsCollector(maxErrorCount: integer, timeWindowMillis: integer, errorHistorySize: integer): StatsCounter

Parameters:

  • maxErrorCount: The maximum number of errors before a maxErrorCountReached event is emitted
  • timeWindowMillis: The time window in milliseconds in which the number of errors are counted
  • errorHistorySize: The size of the error history

success

success(): void

Registers processing success. Increases totalCounter and totalSuccessCounter.

success

error(error: Error | errors: Array<Error>): void

Registers processing failure. Increases totalCounter and totalErrorCounter. Saves the error(s) in the errorHistory and recentErrors stores.

If the number of collected errors is greater than maxErrorCount, a maxErrorCountReached event is emitted. The recentErrors collector only stores the last maxErrorCount number of errors.

See also resetRecentErrors().

resetRecentErrors

resetRecentErrors(): void

Resets the recentErrors collector.

getRecentErrors

getRecentErrors(): Array<Error>

Returns the recently occured errors (the contents of the recentErrors collector).

getErrorHistory

getErrorHistory(): Array<Error>

Returns all errors from the error history. The error history stores the last errorHistorySize number of errors since the StatsCollector instance is created.

getTotalCounter

getTotalCounter(): integer

Returns the number of success() and error() calls together since the StatsCollector instance is created.

getTotalSuccessCounter

getTotalSuccessCounter(): integer

Returns the number of successful processing (i.e. the number of success() calls) since the StatsCollector instance is created.

getTotalErrorCounter

getTotalErrorCounter(): integer

Returns the number of errors since the StatsCollector instance is created. Note that this may not equal to the number of error() calls, since an error() call may register multiple errors.

toJSON

toJSON (showErrorHistory: boolean): Object

Returns an object with the properties of the StatsCollector. If showErrorHistory is true the error history is returned as well. This can be useful e.g. if you want to publish stats through an HTTP endpont.

toString

toString (showErrorHistory: boolean): Object

Returns some basic info about the StatsCollector object.