1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-12-27 21:35:41 +00:00

Add page_time token to mustache.

That's the elapsed time from program start to
the moment before the final invocation to
mustache rendering. So not super accurate but
that's what I can do for now.
This commit is contained in:
King_DuckZ 2017-06-14 09:16:30 +01:00
parent 131903c607
commit 9fc9cf851c
5 changed files with 40 additions and 2 deletions

View file

@ -36,6 +36,7 @@
#include <iterator> #include <iterator>
#include <ciso646> #include <ciso646>
#include <iostream> #include <iostream>
#include <chrono>
//www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4150.pdf //www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4150.pdf
@ -142,6 +143,8 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) {
using kamokan::Response; using kamokan::Response;
using tawashi::RequestMethodType; using tawashi::RequestMethodType;
std::chrono::time_point<std::chrono::steady_clock> app_start_time = std::chrono::steady_clock::now();
if (2 == parArgc and boost::string_view(parArgv[1]) == "--show-paths") { if (2 == parArgc and boost::string_view(parArgv[1]) == "--show-paths") {
print_buildtime_info(); print_buildtime_info();
return 0; return 0;
@ -166,6 +169,7 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) {
resp_factory.register_maker("paste.cgi", RequestMethodType::POST, &make_response<SubmitPasteResponse>); resp_factory.register_maker("paste.cgi", RequestMethodType::POST, &make_response<SubmitPasteResponse>);
resp_factory.register_maker("error.cgi", RequestMethodType::GET, &make_response<ErrorResponse>); resp_factory.register_maker("error.cgi", RequestMethodType::GET, &make_response<ErrorResponse>);
resp_factory.register_jolly_maker(&make_response<PastieResponse>, RequestMethodType::GET); resp_factory.register_jolly_maker(&make_response<PastieResponse>, RequestMethodType::GET);
resp_factory.set_app_start_time(app_start_time);
std::unique_ptr<Response> response = resp_factory.make_response( std::unique_ptr<Response> response = resp_factory.make_response(
tawashi::cgi::drop_arguments(cgi_env->request_uri_relative()), tawashi::cgi::drop_arguments(cgi_env->request_uri_relative()),

View file

@ -135,6 +135,7 @@ namespace kamokan {
//m_page_basename(fetch_page_basename(m_cgi_env)), //m_page_basename(fetch_page_basename(m_cgi_env)),
m_cgi_env(parCgiEnv), m_cgi_env(parCgiEnv),
m_settings(parSettings), m_settings(parSettings),
m_time0(std::chrono::steady_clock::now()),
m_website_root(make_root_path(*parSettings)), m_website_root(make_root_path(*parSettings)),
m_base_uri(make_base_uri(m_settings, m_cgi_env)), m_base_uri(make_base_uri(m_settings, m_cgi_env)),
m_stream_out(parStreamOut) m_stream_out(parStreamOut)
@ -187,9 +188,16 @@ namespace kamokan {
*m_stream_out << http_header; *m_stream_out << http_header;
if (http_header.body_required()) { if (http_header.body_required()) {
using std::chrono::steady_clock;
using std::chrono::milliseconds;
using std::chrono::duration_cast;
SPDLOG_TRACE(statuslog, "Raising event on_mustache_prepare"); SPDLOG_TRACE(statuslog, "Raising event on_mustache_prepare");
this->on_mustache_prepare(mustache_context); this->on_mustache_prepare(mustache_context);
const auto time1 = steady_clock::now();
const int page_time = duration_cast<milliseconds>(time1 - m_time0).count();
mustache_context["page_time"] = page_time;
SPDLOG_TRACE(statuslog, "Rendering in mustache"); SPDLOG_TRACE(statuslog, "Rendering in mustache");
*m_stream_out << mstch::render( *m_stream_out << mstch::render(
on_mustache_retrieve(), on_mustache_retrieve(),
@ -261,4 +269,9 @@ namespace kamokan {
oss << "error.cgi?reason=" << parReason._to_integral(); oss << "error.cgi?reason=" << parReason._to_integral();
return make_redirect(redir_code, oss.str()); return make_redirect(redir_code, oss.str());
} }
void Response::set_app_start_time (const std::chrono::time_point<std::chrono::steady_clock>& parTime) {
assert(parTime <= m_time0);
m_time0 = parTime;
}
} //namespace kamokan } //namespace kamokan

View file

@ -27,6 +27,7 @@
#include <iostream> #include <iostream>
#include <boost/utility/string_view.hpp> #include <boost/utility/string_view.hpp>
#include <memory> #include <memory>
#include <chrono>
namespace tawashi { namespace tawashi {
namespace cgi { namespace cgi {
@ -43,6 +44,7 @@ namespace kamokan {
virtual ~Response() noexcept; virtual ~Response() noexcept;
void send(); void send();
void set_app_start_time (const std::chrono::time_point<std::chrono::steady_clock>& parTime);
protected: protected:
Response ( Response (
@ -71,6 +73,7 @@ namespace kamokan {
Storage m_storage; Storage m_storage;
Kakoune::SafePtr<cgi::Env> m_cgi_env; Kakoune::SafePtr<cgi::Env> m_cgi_env;
Kakoune::SafePtr<SettingsBag> m_settings; Kakoune::SafePtr<SettingsBag> m_settings;
std::chrono::time_point<std::chrono::steady_clock> m_time0;
std::string m_website_root; std::string m_website_root;
std::string m_base_uri; std::string m_base_uri;
std::ostream* m_stream_out; std::ostream* m_stream_out;

View file

@ -34,11 +34,13 @@ namespace kamokan {
boost::container::flat_map<std::string, ResponseMakerFunc> makers_post; boost::container::flat_map<std::string, ResponseMakerFunc> makers_post;
std::array<ResponseMakerFunc, tawashi::RequestMethodType::_size()> jolly_makers; std::array<ResponseMakerFunc, tawashi::RequestMethodType::_size()> jolly_makers;
Kakoune::SafePtr<tawashi::cgi::Env> cgi_env; Kakoune::SafePtr<tawashi::cgi::Env> cgi_env;
std::chrono::time_point<std::chrono::steady_clock> time0;
}; };
ResponseFactory::ResponseFactory (const Kakoune::SafePtr<SettingsBag>& parSettings, const Kakoune::SafePtr<tawashi::cgi::Env>& parCgiEnv) : ResponseFactory::ResponseFactory (const Kakoune::SafePtr<SettingsBag>& parSettings, const Kakoune::SafePtr<tawashi::cgi::Env>& parCgiEnv) :
m_local_data(std::make_unique<LocalData>()) m_local_data(std::make_unique<LocalData>())
{ {
m_local_data->time0 = std::chrono::steady_clock::now();
m_local_data->settings = parSettings; m_local_data->settings = parSettings;
m_local_data->cgi_env = parCgiEnv; m_local_data->cgi_env = parCgiEnv;
std::fill(m_local_data->jolly_makers.begin(), m_local_data->jolly_makers.end(), nullptr); std::fill(m_local_data->jolly_makers.begin(), m_local_data->jolly_makers.end(), nullptr);
@ -61,11 +63,11 @@ namespace kamokan {
auto maker_it = makers.find(name); auto maker_it = makers.find(name);
if (makers.end() != maker_it) { if (makers.end() != maker_it) {
return maker_it->second(m_local_data->settings, m_local_data->cgi_env); return make_response(maker_it->second);
} }
else if (m_local_data->jolly_makers[parReqType]) { else if (m_local_data->jolly_makers[parReqType]) {
spdlog::get("statuslog")->info("no exact match found for \"{}\", assuming it's a pastie's token", name); spdlog::get("statuslog")->info("no exact match found for \"{}\", assuming it's a pastie's token", name);
return m_local_data->jolly_makers[parReqType](m_local_data->settings, m_local_data->cgi_env); return make_response(m_local_data->jolly_makers[parReqType]);
} }
else { else {
spdlog::get("statuslog")->critical("no exact match found for \"{}\" with method {} and no jolly maker given, this should not happen", name, parReqType._to_string()); spdlog::get("statuslog")->critical("no exact match found for \"{}\" with method {} and no jolly maker given, this should not happen", name, parReqType._to_string());
@ -73,6 +75,13 @@ namespace kamokan {
} }
} }
std::unique_ptr<Response> ResponseFactory::make_response (ResponseMakerFunc parMaker) const {
auto retval = parMaker(m_local_data->settings, m_local_data->cgi_env);
assert(retval);
retval->set_app_start_time(m_local_data->time0);
return retval;
}
void ResponseFactory::register_maker (std::string&& parName, ResponseMakerFunc parMaker) { void ResponseFactory::register_maker (std::string&& parName, ResponseMakerFunc parMaker) {
m_local_data->makers_get[parName] = parMaker; m_local_data->makers_get[parName] = parMaker;
m_local_data->makers_post[std::move(parName)] = parMaker; m_local_data->makers_post[std::move(parName)] = parMaker;
@ -92,4 +101,9 @@ namespace kamokan {
void ResponseFactory::register_jolly_maker (ResponseMakerFunc parMaker, tawashi::RequestMethodType parReqType) { void ResponseFactory::register_jolly_maker (ResponseMakerFunc parMaker, tawashi::RequestMethodType parReqType) {
m_local_data->jolly_makers[parReqType] = parMaker; m_local_data->jolly_makers[parReqType] = parMaker;
} }
void ResponseFactory::set_app_start_time (const std::chrono::time_point<std::chrono::steady_clock>& parTime) {
assert(parTime <= m_local_data->time0);
m_local_data->time0 = parTime;
}
} //namespace kamokan } //namespace kamokan

View file

@ -21,6 +21,7 @@
#include "kakoune/safe_ptr.hh" #include "kakoune/safe_ptr.hh"
#include "request_method_type.hpp" #include "request_method_type.hpp"
#include <memory> #include <memory>
#include <chrono>
namespace tawashi { namespace tawashi {
namespace cgi { namespace cgi {
@ -43,10 +44,13 @@ namespace kamokan {
void register_maker (std::string&& parName, ResponseMakerFunc parMaker); void register_maker (std::string&& parName, ResponseMakerFunc parMaker);
void register_maker (std::string&& parName, tawashi::RequestMethodType parReqType, ResponseMakerFunc parMaker); void register_maker (std::string&& parName, tawashi::RequestMethodType parReqType, ResponseMakerFunc parMaker);
void register_jolly_maker (ResponseMakerFunc parMaker, tawashi::RequestMethodType parReqType); void register_jolly_maker (ResponseMakerFunc parMaker, tawashi::RequestMethodType parReqType);
void set_app_start_time (const std::chrono::time_point<std::chrono::steady_clock>& parTime);
private: private:
struct LocalData; struct LocalData;
std::unique_ptr<Response> make_response (ResponseMakerFunc parMaker) const;
std::unique_ptr<LocalData> m_local_data; std::unique_ptr<LocalData> m_local_data;
}; };
} //namespace kamokan } //namespace kamokan