An open API service indexing awesome lists of open source software.

https://github.com/nodesource/ah-processor.utils

Utilities used by the ah-*.processor modules.
https://github.com/nodesource/ah-processor.utils

Last synced: 10 months ago
JSON representation

Utilities used by the ah-*.processor modules.

Awesome Lists containing this project

README

          

# ah-processor.utils

Utilities used by the ah-\*.processor modules.

## Installation

npm install ah-processor.utils

**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [API](#api)
- [idsTriggeredBy](#idstriggeredby)
- [oldestId](#oldestid)
- [immediatelyBeforeId](#immediatelybeforeid)
- [prettyNs](#prettyns)
- [safeGetVal](#safegetval)
- [safeFirstStamp](#safefirststamp)
- [uniqueUserFunctions](#uniqueuserfunctions)
- [separateUserFunctions](#separateuserfunctions)
- [mergeUserFunctions](#mergeuserfunctions)
- [lifeCycle](#lifecycle)
- [License](#license)

## [API](https://nodesource.github.io/ah-processor.utils)

### idsTriggeredBy

Finds all ids of activities that are triggered by the given id.
This function works recursive, i.e. any activity triggered by
a child of the root activity is included as well.

For this to work, activities are assumed to be in the order that
they were triggered. This can easily be achieved by simply sorting
the activities by their init timestamp.

**Parameters**

- `activities` **[Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), Activity>** collected via [ah-fs](https://github.com/nodesource/ah-fs)
- `id` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the id of the root activity we whose triggered _children_ we are trying to find
- `stop` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a predicate that will finish the activity walk if it returns `true`.
Function signature: `(id, activity)`.

Returns **[Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** the provided root id and all ids of activities triggered by it or any of it's children,
grandchildren, etc.

### oldestId

Finds the oldest activity of the ones supplied via the ids.
We consider an activity older than another one if it's init timestamp
is further in the past.

Any ids we can't find as in the activities are ignored.

**Parameters**

- `activities` **[Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)<[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** the collected async activities
- `ids` **[Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)<[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** the ids to consider when finding the oldest

Returns **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the id of the oldest activity or `null` if no activity for any id was found

### immediatelyBeforeId

Finds the activity of the ones supplied via the ids that initialized immediately
before the one with the given id initialized.

**Parameters**

- `activities` **[Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)<[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** the collected async activities
- `ids` **[Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)<[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** the ids to consider when finding most immediate
- `the` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** id of the activity for which we will find the activity that initialized immediately before

Returns **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the id that initialized immediately before the activity with `id` or `null` if no activity for any
id was found

### prettyNs

Prettifies the provided timestamp which is expected to be in nanoseconds.

**Parameters**

- `ns` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** timestamp in nanoseconds

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** an object with an `ms` property which is the prettified version
of the provided timestamp in milliseconds and `ns`, the originally passed timestamp.

### safeGetVal

Safely extracts the `val` property from the object `x`.

**Parameters**

- `x` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the object which has the `val` property

Returns **any** the `val` property if `x` was defined, otherwise `null`

### safeFirstStamp

Safely gets the first time stamp of the given timestamps.
If no timestamp is found, a time of `0` is returned.

**Parameters**

- `x` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** the timestamps

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the prettified first time stamp

### firstNonZeroStamp

Finds the first stamp in the array of time stamp arrays that
has a valid time stamp.
If none is found it will return a zero time stamp.

**Parameters**

- `stamps` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>** array of arrays to query for a valid time stamp

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** prettified time stamp or `prettyNs(0)` if not found

### uniqueUserFunctions

Identifies all user functions within the given functions, adds location and
propertyPath strings and returns the result.

The `propertyPath` is deduced from the `path` array.

If a user function is found twice it will only be included once.

**Parameters**

- `fns` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** all functions found attached to a particular async resource
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options
- `$0.pathPrefix` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** prefix used for the property paths (optional, default `'root'`)

Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** all user functions with the above mentioned details added

### separateUserFunctions

Pulls user functions from all resources of the provided info and attaches
them as the `userFunctions` property to the info object directly.

As a result, instead of having a `userFunctions` array on each resource
we end up with just one on the info object.

**Parameters**

- `info` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the info object which references the resources

### mergeUserFunctions

Merges the `userFunctions` found on the `info` object, such that all functions
with the same `location` property are represented as one.

As the result the `propertyPath` array is removed from all functions and replaced
with the `propertyPaths` array which combines the `propertyPath`s of all functions
that matched the `location`.

**Parameters**

- `info` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the object that references the `userFunctions` property

### lifeCycle

Obtains the life cycle information using the `init` and `destroy` time stamps
of the given activity.

All time stamps have the format: `{ ns:

The return value includes the following properties:

- created: when the resource was created, i.e. it's init time stamp
- destroyed: when the resource ceased to exist, i.e. it's destroy time stamp
- timeAlive: the difference between the above two

If the `destroy` time stamp isn't avaible it is set to a prettified version of `0`.
The same is true in that case for the `timeAlive`.

**Parameters**

- `activity` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the activity whose life cycle to assess

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the life cycle information

### processActivities

Applies multiple processors to the given activities in order to process them
into operations. Each activity that is identified as part of an operation is removed
from the activities map, i.e. it is modified in place.
Therefore any activities still present in the map after processing
completes, couldn't be processed into an operation.

Each processor needs to be instantiable and have a `process` function that returns
`{ groups, operations}`.
Additionally each processor needs to have the following static properties:

- operationSteps: how many steps (resources) are needed to identify an operation
- operation: the description of the operation the processor identifies

**Parameters**

- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options
- `$0.activities` **[Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)<([Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) \| [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)), [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** activities to be processed
- `$0.processors` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** collection of processors to be applied
- `$0.includeActivities` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** if `true` the activities that were used to identify
a step of an operation are attached to the data about that step

Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** a collection of operations that were identified from the
given activities

## License

MIT