summaryrefslogtreecommitdiff
path: root/src/modules/ping.hpp
blob: b6409caef813fd08288d4ecd9bcdb500fbae2f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once

#include <sys/resource.h>
#include <sys/types.h>
#include <unistd.h>

#include <chrono>
#include <string>
#include <variant>
#include <vector>

#include "../bundle.hpp"
#include "../commands/command.hpp"
#include "../utils/chrono.hpp"

namespace bot {
  namespace mod {
    class Ping : public command::Command {
        std::string get_name() const override { return "ping"; }

        std::variant<std::vector<std::string>, std::string> run(
            const InstanceBundle &bundle,
            const command::Request &request) const override {
          auto now = std::chrono::steady_clock::now();
          auto duration = now - START_TIME;
          auto seconds =
              std::chrono::duration_cast<std::chrono::seconds>(duration);
          std::string uptime = utils::chrono::format_timestamp(seconds.count());

          struct rusage usage;
          getrusage(RUSAGE_SELF, &usage);

          int used_memory = usage.ru_maxrss / 1024;

          return bundle.localization
              .get_formatted_line(request, loc::LineId::PingResponse,
                                  {uptime, std::to_string(used_memory)})
              .value();
        }
    };
  }
}