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

@lpgroup/feathers-counters-service

v1.9.1

Published

Services to handle counters in microservices

Downloads

805

Readme

@lpgroup/feathers-counters-service

npm version Known VulnerabilitiesLicence MIT tested with jest codecov

Feathersjs hook that adds a counter key to data or result.

The package consists of the service counters and the hook counter. The hook will store the number of times a specific event has occured in a database through the service.

This funcionality can for example be used for statistics purpose. Counting the number of times GET has occured on a specific endpoint. Number of times a specific user has accessed a service. It can also be used to calculate a unique customer number on a customer endpoint on a specific organisation.

Install

Installation of the npm

npm install @lpgroup/feathers-counters-service
// or
yarn add @lpgroup/feathers-counters-service

The hook

| before | after | methods | multi | source | | ---------- | --------- | ----------- | --------- | ------------------------------------------------------------------------------------------------------------------ | | yes | yes | all | yes | source |

arguments

| property | type | default | description | | ------------- | -------- | -------- | -------------------------------------------------------------------------------------- | | format | string | required | A format that makes the counter unique | | periodFmt | string | required | The date period that the counter is counting. https://date-fns.org/v2.28.0/docs/format | | key | string | true | The name of the key in the returned json data/result | | step | number | 1 | Number of steps to increase the counter each time, postive or negative. | | description | string | null | Description of the counter |

The service

The service is required by the hook. To install it, modify feathers service file, app-ng/src/services/index.js, add the follwoing.

import { counters } from "@lpgroup/feathers-counters-service";

export default (app) => {
  /* ... */
  app.configure(counters);
};

Endpoints

POST: /counters
GET: /counters
GET: /counters/:alias
PATCH: /counters/:alias
PUT: /counters/:alias
DELETE: /counters/:alias

Object

{
  "alias": "status-visits-2021-11",
  "group": "status-visits",
  "period": "2021-11",
  "value": 50,
  "description": "status-visits"
  /* commonAttributes */
}

Example 1

Give each customer on an organisation a unique customer number, on the endpoint /organisations/:organisationAlias/customer.

The before hook will add the key customerNumber to data and store it in the database handled by the endpoint.

{organisationAlias} comes internally from the url in params.query.

import { counter } from "@lpgroup/feathers-counters-service";

export default {
  before: {
    all: [],
    find: [],
    get: [],
    create: [
      counter("{organisationAlias}-visitors", null, "customerNumber", 1, "Latest customerNumber"),
    ],
    update: [],
    patch: [],
    remove: [],
  },
};

Example 2

Calculate the number of times a user has accessed the GET method on /status?user=admin.

{user} comes internally from the query parameter in params.query.

import { counter } from "@lpgroup/feathers-counters-service";

export default {
  after: {
    all: [],
    find: [],
    get: [
      counter(
        "total_gets-{user}",
        null,
        "userAccesses",
        1,
        "Total number of calls done by a specific user",
      ),
    ],
    create: [],
    update: [],
    patch: [],
    remove: [],
  },
};

Example 3

Calculate number of calls during a month.

The counter will start from the beginning again every month. The statistics for earlier months will be stored in the /counters service.

import { counter } from "@lpgroup/feathers-counters-service";

export default {
  after: {
    all: [],
    find: [],
    get: [],
    create: [counter("visitors", "yyyy-MM", "visits", 1, "Number of visits during a month")],
    update: [],
    patch: [],
    remove: [],
  },
};

Example 4

Keep track of a stack of files, and for every create will increase 1 and for every delete will decrease 1.

import { counter } from "@lpgroup/feathers-counters-service";

export default {
  after: {
    all: [],
    find: [],
    get: [],
    create: [counter("files", null, "files", 1, "Total number of files")],
    update: [],
    patch: [],
    remove: [counter("files", null, "files", -1, "Total number of files")],
  },
};

Contribute

See contribute

License

MIT - See licence