src/cxx_supportlib/Utils/JsonUtils.h in passenger-5.0.25 vs src/cxx_supportlib/Utils/JsonUtils.h in passenger-5.0.26
- old
+ new
@@ -1,8 +1,8 @@
/*
* Phusion Passenger - https://www.phusionpassenger.com/
- * Copyright (c) 2014-2015 Phusion Holding B.V.
+ * Copyright (c) 2014-2016 Phusion Holding B.V.
*
* "Passenger", "Phusion Passenger" and "Union Station" are registered
* trademarks of Phusion Holding B.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -26,10 +26,11 @@
#ifndef _PASSENGER_UTILS_JSON_UTILS_H_
#define _PASSENGER_UTILS_JSON_UTILS_H_
#include <string>
#include <cstdio>
+#include <cstdlib>
#include <cstddef>
#include <jsoncpp/json.h>
#include <boost/cstdint.hpp>
#include <StaticString.h>
#include <Utils/SystemTime.h>
@@ -200,10 +201,14 @@
* // "relative": "10s ago"
* // }
*/
inline Json::Value
timeToJson(unsigned long long timestamp, unsigned long long now = 0) {
+ if (timestamp == 0) {
+ return Json::Value(Json::nullValue);
+ }
+
Json::Value doc;
time_t time = (time_t) timestamp / 1000000;
char buf[32];
size_t len;
@@ -227,18 +232,66 @@
}
return doc;
}
+inline Json::Value
+durationToJson(unsigned long long duration) {
+ Json::Value doc;
+ char buf[64];
+
+ doc["microseconds"] = duration;
+ if (duration >= 10 * 1000000) {
+ snprintf(buf, sizeof(buf), "%.1fs", duration / 1000000.0);
+ } else {
+ snprintf(buf, sizeof(buf), "%.1fms", duration / 1000.0);
+ }
+ doc["human_readable"] = buf;
+
+ return doc;
+}
+
inline string
formatFloat(double val) {
char buf[64];
int size = snprintf(buf, sizeof(buf), "%.1f", val);
return string(buf, size);
}
+inline double
+capFloatPrecision(double val) {
+ char buf[64];
+ snprintf(buf, sizeof(buf), "%.2f", val);
+ return atof(buf);
+}
+
inline Json::Value
+speedToJson(double speed, const string &per, double nullValue = -1) {
+ Json::Value doc;
+ if (speed == nullValue) {
+ doc["value"] = Json::Value(Json::nullValue);
+ } else {
+ doc["value"] = speed;
+ }
+ doc["per"] = per;
+ return doc;
+}
+
+inline Json::Value
+averageSpeedToJson(double speed, const string &per, const string &averagedOver, double nullValue = -1) {
+ Json::Value doc;
+ if (speed == nullValue) {
+ doc["value"] = Json::Value(Json::nullValue);
+ } else {
+ doc["value"] = speed;
+ }
+ doc["per"] = per;
+ doc["averaged_over"] = averagedOver;
+ return doc;
+}
+
+inline Json::Value
byteSizeToJson(size_t size) {
Json::Value doc;
doc["bytes"] = (Json::UInt64) size;
if (size < 1024) {
doc["human_readable"] = toString(size) + " bytes";
@@ -260,9 +313,42 @@
} else if (absSize < 1024 * 1024) {
doc["human_readable"] = formatFloat(size / 1024.0) + " KB";
} else {
doc["human_readable"] = formatFloat(size / 1024.0 / 1024.0) + " MB";
}
+ return doc;
+}
+
+inline Json::Value
+byteSpeedToJson(double speed, const string &per) {
+ Json::Value doc;
+ if (speed >= 0) {
+ doc = byteSizeToJson(speed);
+ } else {
+ doc = signedByteSizeToJson(speed);
+ }
+ doc["per"] = per;
+ return doc;
+}
+
+inline Json::Value
+byteSpeedToJson(double speed, double nullValue, const string &per) {
+ Json::Value doc;
+ if (speed == nullValue) {
+ doc["bytes"] = Json::Value(Json::nullValue);
+ } else if (speed >= 0) {
+ doc = byteSizeToJson(speed);
+ } else {
+ doc = signedByteSizeToJson(speed);
+ }
+ doc["per"] = per;
+ return doc;
+}
+
+inline Json::Value
+byteSizeAndCountToJson(size_t size, unsigned int count) {
+ Json::Value doc = byteSizeToJson(size);
+ doc["count"] = count;
return doc;
}
} // namespace Passenger