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

firestore-metrics

v1.0.9

Published

A library which uses the Cloud Monitoring API v3 to view Firestore metrics.

Downloads

5

Readme

Firestore Metrics npm npm

This is a library which uses the Cloud Monitoring API v3 to view Firestore usage metrics.

Pre-requisites

  1. A service account with permission to access the API.
    1. Viewer role should suffice.
  2. Project must be billing enabled.
    1. This might be more of a bug as requests that use the bearer token obtained from a service account results in a 403 error when the project is not billing enabled.

How to use

  1. Create a service account.
    1. Follow steps here to create a service account.
    2. Select the Console tab.
    3. When selecting a role, under Quick access > Basic, pick Viewer.
  2. Download the service account keys.
    1. Follow the steps here to download the service account key.
  3. Pass the service-account key file path or service-account credentials to FirestoreMetrics

Method references

Get read metrics

import { FirestoreMetrics } from "firestore-metrics";

async function testApi() {
  const firestoreMetrics = new FirestoreMetrics({
    serviceAccountPath: "./service-account.json",
  });
  const readUsage = await firestoreMetrics.getReadCount(
    "2023-07-22T08:00:00Z",
    "2023-07-22T22:42:15Z"
  );

  console.log(JSON.stringify(readUsage, null, 4));
}

testApi();

Output would look like:

[
  {
    "type": "QUERY",
    "interval": {
      "startTime": "2023-08-21T16:15:00Z",
      "endTime": "2023-08-21T16:16:00Z"
    },
    "count": 26
  },
  {
    "type": "QUERY",
    "interval": {
      "startTime": "2023-08-21T16:14:00Z",
      "endTime": "2023-08-21T16:15:00Z"
    },
    "count": 15
  },
  {
    "type": "QUERY",
    "interval": {
      "startTime": "2023-08-21T15:51:00Z",
      "endTime": "2023-08-21T15:52:00Z"
    },
    "count": 11
  }
]

Get write metrics

import { FirestoreMetrics } from "firestore-metrics";

async function testApi() {
  const firestoreMetrics = new FirestoreMetrics({
    serviceAccountPath: "./service-account.json",
  });
  const writeUsage = await firestoreMetrics.getWriteCount(
    "2023-08-21T15:00:00Z",
    "2023-08-21T20:00:00Z"
  );

  console.log(JSON.stringify(writeUsage, null, 4));
}

testApi();

Output would look like:

[
  {
    "op": "CREATE",
    "interval": {
      "startTime": "2023-08-21T16:09:00Z",
      "endTime": "2023-08-21T16:10:00Z"
    },
    "count": 1
  },
  {
    "op": "UPDATE",
    "interval": {
      "startTime": "2023-08-21T16:15:00Z",
      "endTime": "2023-08-21T16:16:00Z"
    },
    "count": 1
  },
  {
    "op": "UPDATE",
    "interval": {
      "startTime": "2023-08-21T16:09:00Z",
      "endTime": "2023-08-21T16:10:00Z"
    },
    "count": 1
  }
]

Ways to authenticate request

Using a service-account file

Just provide the path to your service-account file

const firestoreMetrics = new FirestoreMetrics({
  serviceAccountPath: "./service-account.json",
});

Using service-account credentials

Just provide the JSON object of your service-account credentials

const firestoreMetrics = new FirestoreMetrics({
  credentials: {
    type: "service_account",
    project_id: "<PROJECT_ID>",
    private_key_id: "<PRIVATE_KEY_ID>",
    private_key: "<PRIVATE_KEY>",
    client_email: "<CLIENT_EMAIL>",
    client_id: "<CLIENT_ID>",
    auth_uri: "<AUTH_URI>",
    token_uri: "<TOKEN_URI>",
    auth_provider_x509_cert_url: "<AUTH_PROVIDER_X509_CERT_URL>",
    client_x509_cert_url: "<CLIENT_X509_CERT_URL>",
    universe_domain: "<UNIVERSE_DOMAIN>",
  },
});