Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added status basic endpoint #347

Merged
merged 1 commit into from
Oct 6, 2021

Conversation

ilbuonmarcio
Copy link
Contributor

Description

Added /status basic endpoint for monitoring using Grafana/Prometheus/UptimeKuma.
I just added a /status endpoint, which replies (fast) with an HTTP 200 and a basic json object if the body.

This implementation right now doesn't check for mongodb or redis availability, yet it's still pretty useful in itself to make sure the bot is at least up.

Related Issue

#341

Motivation and Context

Monitoring the bot is a good thing, and this is an easy way to help with that!

How Has This Been Tested?

Tested on TEST environment, should be ok even in production environment aswell.

@chrisleekr chrisleekr merged commit 06eb81b into chrisleekr:master Oct 6, 2021
@ilbuonmarcio
Copy link
Contributor Author

Hi @chrisleekr, sorry to bother you everytime but I just checked the repo and it's missing a test for this new feature so it's marking it as failing.
I'm trying to build a test for it, taking 404.test.js file as an example, but I cannot figure out why it's not working.

Here's the code snippet I'm currently at:

/* eslint-disable global-require */
describe('webserver/handlers/status', () => {
  const appMock = {};

  let resSendMock;
  let loggerMock;

  beforeEach(async () => {
    resSendMock = jest.fn().mockResolvedValue(true);
    appMock.route = jest.fn(() => ({
      get: jest.fn().mockImplementation((_path, func) => {
        func(null, { send: resSendMock });
      })
    }));

    const logger = require('../../../../helpers');
    loggerMock = logger;

    const { handleStatus } = require('../status');

    await handleStatus(loggerMock, appMock);
  });

  it('triggers res.send', () => {
    expect(resSendMock).toHaveBeenCalledWith({
      success: true,
      status: 200,
      message: 'OK'
    });
  });
});

The following error occurs after running npm run test:

PS C:\Users\Alessandro Marchioro\Desktop\Clients\AleMarioDario\binance-trading-bot> npm run test

> binance-trading-bot@0.0.79 test C:\Users\Alessandro Marchioro\Desktop\Clients\AleMarioDario\binance-trading-bot
> cross-env NODE_ENV=test jest --coverage --detectOpenHandles

 FAIL  app/cronjob/trailingTrade/step/__tests__/ensure-grid-trade-order-executed.test.js (7.483 s)
  ● ensure-grid-trade-order-executed.js › execute › when found last buy order › last buy order is NEW and still NEW after checking the order › triggers saveGridTradeOrder for last buy order as not filled

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

      {"child": [Function anonymous], "debug": [Function mockConstructor], "error": [Function mockConstructor], "info": [Function mockConstructor], "warn": [Function mockConstructor]},
      "BNBUSDT-grid-trade-last-buy-order",
    @@ -1,8 +1,8 @@
      Object {
        "currentGridTradeIndex": 0,
    -   "nextCheck": "2020-01-02T00:00:10+00:00",
    +   "nextCheck": "2020-01-02T01:00:10+01:00",
        "orderId": 2705449295,
        "origQty": "0.03320000",
        "price": "302.09000000",
        "side": "BUY",
        "status": "NEW",,

    Number of calls: 1

      852 |                 // do else thing
      853 |                 it('triggers saveGridTradeOrder for last buy order as not filled', () => {
    > 854 |                   expect(mockSaveGridTradeOrder).toHaveBeenCalledWith(
          |                                                  ^
      855 |                     loggerMock,
      856 |                     `${t.symbol}-grid-trade-last-buy-order`,
      857 |                     {

      at Object.<anonymous> (app/cronjob/trailingTrade/step/__tests__/ensure-grid-trade-order-executed.test.js:854:50)

  ● ensure-grid-trade-order-executed.js › execute › when found last buy order › last buy order is NEW but error › triggers saveGridTradeOrder for last buy order as order throws error

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

      {"child": [Function anonymous], "debug": [Function mockConstructor], "error": [Function mockConstructor], "info": [Function mockConstructor], "warn": [Function mockConstructor]},
      "BNBUSDT-grid-trade-last-buy-order",
    @@ -1,8 +1,8 @@
      Object {
        "currentGridTradeIndex": 0,
    -   "nextCheck": "2020-01-02T00:00:10+00:00",
    +   "nextCheck": "2020-01-02T01:00:10+01:00",
        "orderId": 2705449295,
        "origQty": "0.03320000",
        "price": "302.09000000",
        "side": "BUY",
        "status": "NEW",,

    Number of calls: 1

      740 |               // order throws an error
      741 |               it('triggers saveGridTradeOrder for last buy order as order throws error', () => {
    > 742 |                 expect(mockSaveGridTradeOrder).toHaveBeenCalledWith(
          |                                                ^
      743 |                   loggerMock,
      744 |                   `${t.symbol}-grid-trade-last-buy-order`,
      745 |                   {

      at Object.<anonymous> (app/cronjob/trailingTrade/step/__tests__/ensure-grid-trade-order-executed.test.js:742:48)

  ● ensure-grid-trade-order-executed.js › execute › when found last sell order › last sell order is NEW and still NEW after checking the order › triggers saveGridTradeOrder for last sell order as not filled

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

      {"child": [Function anonymous], "debug": [Function mockConstructor], "error": [Function mockConstructor], "info": [Function mockConstructor], "warn": [Function mockConstructor]},
      "BNBUSDT-grid-trade-last-sell-order",
    @@ -1,8 +1,8 @@
      Object {
        "currentGridTradeIndex": 0,
    -   "nextCheck": "2020-01-02T00:00:10+00:00",
    +   "nextCheck": "2020-01-02T01:00:10+01:00",
        "orderId": 2705449295,
        "origQty": "0.03320000",
        "price": "302.09000000",
        "side": "SELL",
        "status": "NEW",,

    Number of calls: 1

      1580 |                 // do else thing
      1581 |                 it('triggers saveGridTradeOrder for last sell order as not filled', () => {
    > 1582 |                   expect(mockSaveGridTradeOrder).toHaveBeenCalledWith(
           |                                                  ^
      1583 |                     loggerMock,
      1584 |                     `${t.symbol}-grid-trade-last-sell-order`,
      1585 |                     {

      at Object.<anonymous> (app/cronjob/trailingTrade/step/__tests__/ensure-grid-trade-order-executed.test.js:1582:50)

  ● ensure-grid-trade-order-executed.js › execute › when found last sell order › last sell order is NEW but error › triggers saveGridTradeOrder for last sell order as order throws error

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

      {"child": [Function anonymous], "debug": [Function mockConstructor], "error": [Function mockConstructor], "info": [Function mockConstructor], "warn": [Function mockConstructor]},
      "BNBUSDT-grid-trade-last-sell-order",
    @@ -1,8 +1,8 @@
      Object {
        "currentGridTradeIndex": 0,
    -   "nextCheck": "2020-01-02T00:00:10+00:00",
    +   "nextCheck": "2020-01-02T01:00:10+01:00",
        "orderId": 2705449295,
        "origQty": "0.03320000",
        "price": "302.09000000",
        "side": "SELL",
        "status": "NEW",,

    Number of calls: 1

      1476 |               // order throws an error
      1477 |               it('triggers saveGridTradeOrder for last sell order as order throws error', () => {
    > 1478 |                 expect(mockSaveGridTradeOrder).toHaveBeenCalledWith(
           |                                                ^
      1479 |                   loggerMock,
      1480 |                   `${t.symbol}-grid-trade-last-sell-order`,
      1481 |                   {

      at Object.<anonymous> (app/cronjob/trailingTrade/step/__tests__/ensure-grid-trade-order-executed.test.js:1478:48)

 FAIL  app/frontend/webserver/handlers/__tests__/index.test.js
  ● index › triggers handleAuth

    TypeError: funcLogger.child is not a function

      2 |
      3 | const handleStatus = async (funcLogger, app) => {
    > 4 |   const logger = funcLogger.child({ endpoint: '/status' });
        |                             ^
      5 |
      6 |   app.route('/status').get(async (req, res) => {
      7 |     const clientIp = requestIp.getClientIp(req);

      at handleStatus (app/frontend/webserver/handlers/status.js:4:29)
      at Object.setHandlers (app/frontend/webserver/handlers/index.js:13:9)
      at Object.<anonymous> (app/frontend/webserver/handlers/__tests__/index.test.js:45:5)

  ● index › triggers handleGridTradeArchiveGet

    TypeError: funcLogger.child is not a function

      2 |
      3 | const handleStatus = async (funcLogger, app) => {
    > 4 |   const logger = funcLogger.child({ endpoint: '/status' });
        |                             ^
      5 |
      6 |   app.route('/status').get(async (req, res) => {
      7 |     const clientIp = requestIp.getClientIp(req);

      at handleStatus (app/frontend/webserver/handlers/status.js:4:29)
      at Object.setHandlers (app/frontend/webserver/handlers/index.js:13:9)
      at Object.<anonymous> (app/frontend/webserver/handlers/__tests__/index.test.js:45:5)

  ● index › triggers handleGridTradeArchiveDelete

    TypeError: funcLogger.child is not a function

      2 |
      3 | const handleStatus = async (funcLogger, app) => {
    > 4 |   const logger = funcLogger.child({ endpoint: '/status' });
        |                             ^
      5 |
      6 |   app.route('/status').get(async (req, res) => {
      7 |     const clientIp = requestIp.getClientIp(req);

      at handleStatus (app/frontend/webserver/handlers/status.js:4:29)
      at Object.setHandlers (app/frontend/webserver/handlers/index.js:13:9)
      at Object.<anonymous> (app/frontend/webserver/handlers/__tests__/index.test.js:45:5)

  ● index › triggers handleClosedTradesSetPeriod

    TypeError: funcLogger.child is not a function

      2 |
      3 | const handleStatus = async (funcLogger, app) => {
    > 4 |   const logger = funcLogger.child({ endpoint: '/status' });
        |                             ^
      5 |
      6 |   app.route('/status').get(async (req, res) => {
      7 |     const clientIp = requestIp.getClientIp(req);

      at handleStatus (app/frontend/webserver/handlers/status.js:4:29)
      at Object.setHandlers (app/frontend/webserver/handlers/index.js:13:9)
      at Object.<anonymous> (app/frontend/webserver/handlers/__tests__/index.test.js:45:5)

  ● index › triggers handle404

    TypeError: funcLogger.child is not a function

      2 |
      3 | const handleStatus = async (funcLogger, app) => {
    > 4 |   const logger = funcLogger.child({ endpoint: '/status' });
        |                             ^
      5 |
      6 |   app.route('/status').get(async (req, res) => {
      7 |     const clientIp = requestIp.getClientIp(req);

      at handleStatus (app/frontend/webserver/handlers/status.js:4:29)
      at Object.setHandlers (app/frontend/webserver/handlers/index.js:13:9)
      at Object.<anonymous> (app/frontend/webserver/handlers/__tests__/index.test.js:45:5)

 PASS  app/cronjob/trailingTrade/step/__tests__/place-buy-order.test.js (5.834 s)

 RUNS  app/cronjob/trailingTradeHelper/__tests__/common.test.js
Terminate batch job (Y/N)?
^C
PS C:\Users\Alessandro Marchioro\Desktop\Clients\AleMarioDario\binance-trading-bot> ^C```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants