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

elmap

v1.0.21

Published

simple elasticsearch queries

Downloads

8

Readme

elmap

travisci npm version Coverage Status

A module that allows you to perform queries with transformations on your elasticsearch. Supposed to be used for your ELK-Stack, where you may want to refine certain kibana queries and add map/reduce functionality to them.

Featuring query-result caching, so no worries if you have to play around with your transformations.

Install

npm install elmap --save

Usage

Given you have an index YourElasticSearchIndex with the data like somewhat likes this:

{
    "serviceName": "my microservice",
    "callDurationMs": "128",
    "message": "Something got badly wrong in 'Someclass' for traceid 'traceid_1' in instance 'instanceid'"
},
{
    "serviceName": "my microservice",
    "callDurationMs": "1024",
    "message": "Something got badly wrong in 'NotImplemented' for traceid 'traceid_1' in instance 'instanceid'"
},
{
    "serviceName": "my microservice",
    "callDurationMs": "42",
    "message": "Something got badly wrong in 'Someclass' for traceid 'traceid_2' in instance 'instanceid'"
},
{
    "serviceName": "my microservice",
    "callDurationMs": "1",
    "message": "Something got badly wrong in 'Someclass' for traceid 'traceid_64' in instance 'anotherinstanceid'"
}

you may notice your index is bad for querying with kibana. Call duration is not a number, so you cannot use mathematical operators. While you can search for the traceid, you cannot easily get all traces for an instance (if you would want that - just an example). But you cannot change your index (because it's 3am in the morning and you are in the middle of an incident, or because DevOps does not exist in your company yet and you are not allowed to do anything about the index).

When you want to search further anyway, elmap can help.

import elmap from "elmap";

const transform = (result) => {
    result.hits.hits.map(hit => {
        const reg = /.*?'(.*?)'.*?'(.*?)'.*?'(.*?)'/;
        const matches = reg.match(result._source.message);
        return {
            duration : parseInt(result._source.callDurationMs),
            trace : matches[2],
            instance : matches[3]
        };
    })
    .filter(hit => hit.duration > 100)
    .reduce((result, current) => {
        const {instance, trace, duration} = result;
        result[instance] = result[instance] || {};
        result[instance][trace] = result[instance][trace] || [];
        result[instance][trace].push(duration);
    }, {});
};

const range = {
    from : new Date(new Date().getTime() + -15*60000),
    to: new Date()
}

elmap({
    url: "http://localhost:9300", 
    index: "YourElasticSearchIndex*", 
    query: 'message:"instanceid"', 
    transform,
    range 
})

Gives you a result that looks like

{
    "instanceid" : {
        "traceid_1": [
            128,
            1024
        ],
        "traceid_2": [
            42
        ]
    }
}

All the beauty of map reduce now with elasticsearch queries.

Params

| Field | Description | |---|---| | url | The url for your elasticsearch instance in the format protocol://url:port, i.e. http://localhost:9300. Supports query through kibana as well, i.e. https://kibana:[email protected]/elasticsearch | | index | The ElasticSearch Index you want to query | | query | Your search query | | transform | A function that applies a specified function on your queries result. See below for more details | | range | A javascript object with two fields, from and to both Date Object as option to specify the daterange to query. Default is the last 15min |

The transformation

Transformation is a function that is called after the query to elasticsearch is completed. It has the following signature:

transform : (result: ElasticResult) => Object

The ElasticResult is a plain ElasticSearch Result Object and is what you see when you view the response results in kibana. Don't worry if you are not entirely sure about the result - After your first query there is a new folder data in your directory that you can use to take a closer look. If the file is too big, make your first transformation to look like this:

(result) => {
    return result.hits.hits[0];
}

This will show your first hit. From there you should be able to proceed.

The caching

Querying ElasticSearch can be slow if you have a lot of items. Which you tend to have, that's why you used elasticsearch in the first place. To be able to execute multiple transformations faster, elmap stores the result for a datetime/query combination in the data folder. If you want this folder for something else you can set it by the environment variable ELMAP_PATH (relative to your current working directory).

If your result changes because you changed your elasticsearch index for instance, pls do not forget to clear your cache first.

Note that as long as you query the last 15 minutes (default) the cache won't do you any good, as the last 15 Minutes change for like every millisecond!

Helpers

To make object creation more convienient, elmap comes with some helpers to make object creation easier.

You can include them using

const helpers = require("elmap").helpers;
// or 
import { helpers } from "elmap";

Result Prettifiers

Result Prettyfiers are functions that should help you make your result more comprehensible.

| Function | Usage | Description | |---|---|---| | sortProps| test | Sorts all properties in all objects and their children. |

Object helpers

Object helpers allow you to safely and easy set fields in objects that does not have existed before. This can be helpful for reduce functions.

| Function | Usage | Description | |---|---|---| | set| test | sets an object at a field. Overwrite everything that was there. | | push| test | Pushes an element onto an array. Creates array if not there yet. | | increment| test | Increments a number on a field. | | stringcat| test | Adds a value to the end of a string |