Home > Mobile >  generate response time and save it in mongo
generate response time and save it in mongo

Time:06-28

I want to save the response time (from the time a request is sent, until it returns) in mongo db, I am not an expert on the subject, and I would like you to please shake my hand on this topic.

  query: async (params = []) => {
    const startTime = new Date().getTime();
    let result = null;
    let error = null;
    try {
      result = await mongoose.query(params);
      const executionTime = (new Date().getTime() - startTime) / 1000;
      logger.info(`${executionTime} s`);
    } catch (err) {
      error = err;
      const executionTime = (new Date().getTime() - startTime) / 1000;
      logger.info(`${executionTime} s`);
    }

    if (result !== null) { return result; }
    throw error;
  }
}; ```
this is what it returns:
```(node:48160) UnhandledPromiseRejectionWarning: TypeError: mongoose.query is not a function

previously working with postgres and where it says query placed pol, it was more intuitive. I would appreciate any help. thanks in advance.

CodePudding user response:

I would recommend subscribing on CommandSucceeded/CommandFailed events and analyze duration field. See details here

CodePudding user response:

the solution was simpler than the problem, when I sin I was making balls, for anyone who wants to save a duration of a response time is in the following way:

    const startTime = new Date().getTime();//start of time counter
    const result = { success: false, statusCode: null, data: {} };
    const { action } = config;

    delete payload.action;
    const resultRequest = await watsonSupport.proxyRequest(action, payload);

    result.success = resultRequest.success;
    result.statusCode = resultRequest.statusCode;
    result.data = resultRequest.response;
    try {
      const executionTime = (new Date().getTime() - startTime) / 1000;//a new counter is started and subtracted with the previous counter
      logger.info(`${executionTime} s`);

      const saveLog = new RequestLog({
        action: config.action,
        authorizedSkill: config.skillId,
        url: resultRequest.url,
        method: resultRequest.method,
        request: payload,
        response: resultRequest.response,
        responseStr: JSON.stringify(resultRequest.response),
        statusCode: resultRequest.statusCode,
        conversationId: config.conversationId,
        responseTime: executionTime
      });
      await saveLog.save();
    } catch (error) {
      logger.error(error);
      const executionTime = (new Date().getTime() - startTime) / 1000;
      logger.info(`${executionTime} s`);```
  • Related