https://github.com/oeo/trk2
a lightweight metrics tracking library for redis
https://github.com/oeo/trk2
Last synced: 2 months ago
JSON representation
a lightweight metrics tracking library for redis
- Host: GitHub
- URL: https://github.com/oeo/trk2
- Owner: oeo
- Created: 2024-10-24T01:33:50.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2024-10-24T16:53:03.000Z (7 months ago)
- Last Synced: 2025-01-25T22:22:06.458Z (4 months ago)
- Language: CoffeeScript
- Homepage:
- Size: 558 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# trk2
a lightweight metrics tracking library for redis
## installation
```bash
npm install trk2 --save
```## usage
### basic setup
```javascript
const Redis = require('ioredis');
const Metrics = require('trk2');const redis = new Redis();
const metrics = new Metrics({
redis: redis,
key: 'myapp',
map: {
bmp: ['ip'],
add: ['event', 'event~offer'],
addv: [{ key: 'offer~event', addKey: 'amount' }],
top: ['geo', 'offer']
}
});
```### recording events
```javascript
await metrics.record({
ip: '192.168.1.1',
event: 'offer_impression',
offer: '123456',
geo: 'US',
amount: 100
});
```
![]()
### querying data
```javascript
// query last 10 days
const results = await metrics.queryDays(10);// get unique visitor counts
const unique = results.find({
type: 'bmp',
key: 'ip'
});// get add value for amount
const addValue = results.find({
type: 'addv',
key: 'offer~event',
addKey: 'amount'
});// get event counts
const impressions = results.find({
type: 'add',
key: 'event',
merge: false
});// get top geos
const topGeos = results.find('top/geo');// get top offers
const topOffers = results.find('top/offer');
```## api
### `new Metrics(options)`
creates a new metrics instance.
- `options.redis`: redis client instance
- `options.key`: prefix for redis keys
- `options.map`: configuration for tracking different metrics types### `metrics.record(event)`
records a single event.
### `metrics.queryDays(numDays)`
queries data for the last `numDays` days.
### `results.find(options)`
finds specific metrics in the query results.
- `options.type`: type of metric ('bmp', 'add', 'addv', 'top')
- `options.key`: key for the metric
- `options.addKey`: additional key for 'addv' type
- `options.merge`: whether to merge results across days## examples
see the `src/examples` directory for more detailed usage examples.
## performance
on my apple m2 max w 64gb ramcoffee/node
```bash
taky:~/www/trk2/src$ coffee examples/record-events.coffeeFinished recording 100000 events (series) in 12799ms
Series events digested/sec: 7813.110399249942Finished recording 100000 events (parallel) in 10263ms
Parallel events digested/sec: 9743.739647276625
```bun
```bash
taky:~/www/trk2/build$ bun examples/record-events.jsFinished recording 100000 events (series) in 10294ms
Series events digested/sec: 9714.396735962697Finished recording 100000 events (parallel) in 7499ms
Parallel events digested/sec: 13335.111348179758
```## license
mit