display traffic volume since last visit
This commit is contained in:
@@ -36,6 +36,7 @@ namespace c {
|
||||
const qint64 DB_DAILY_TOP_TABLE_ACTUALIZE_MINIMAL_INTERVAL_MS = 300000; // 5 min
|
||||
const qint64 CACHE_ACTUALIZE_TOP_LISTS_FROM_DB_MINIMAL_INTERVAL_MS = 5000; // 5 sec
|
||||
const QString HA_PAGE_TITLE = "{{PAGE_TITLE}}";
|
||||
const QString HA_TRAFFIC_SECTION_TITLE = "{{TRAFFIC_SECTION_TITLE}}";
|
||||
const QString HA_CUSTOM_CSS = "{{CUSTOM_CSS}}";
|
||||
const QString HA_DAILY_UPLOAD = "{{DAILY_UPLOAD}}";
|
||||
const QString HA_DAILY_UPLOAD_DECIMAL = "{{DAILY_UPLOAD_DECIMAL}}";
|
||||
|
||||
@@ -38,6 +38,7 @@ namespace c /* for Constants */ {
|
||||
extern const qint64 DB_DAILY_TOP_TABLE_ACTUALIZE_MINIMAL_INTERVAL_MS;
|
||||
extern const qint64 CACHE_ACTUALIZE_TOP_LISTS_FROM_DB_MINIMAL_INTERVAL_MS;
|
||||
extern const QString HA_PAGE_TITLE;
|
||||
extern const QString HA_TRAFFIC_SECTION_TITLE;
|
||||
extern const QString HA_CUSTOM_CSS;
|
||||
extern const QString HA_DAILY_UPLOAD;
|
||||
extern const QString HA_DAILY_UPLOAD_DECIMAL;
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
</h1>
|
||||
<section class="proxyService__handledTraffic handledTraffic section">
|
||||
<div class="handledTraffic__mainTitle section__title">
|
||||
Traffic
|
||||
{{TRAFFIC_SECTION_TITLE}}
|
||||
</div>
|
||||
<div class="handledTraffic__blocks">
|
||||
<div class="handledTraffic__block handledTraffic__block_upload">
|
||||
|
||||
@@ -53,6 +53,7 @@ const QString HttpDocument::CommonHeader::ContentType = "Content-Type";
|
||||
const QString HttpDocument::CommonHeader::ContentLength = "Content-Length";
|
||||
const QString HttpDocument::CommonHeader::ETag = "ETag";
|
||||
const QString HttpDocument::CommonHeader::ProcessingDuration = "Processing-duration";
|
||||
const QString HttpDocument::CommonHeader::SetCookie = "Set-Cookie";
|
||||
|
||||
HttpDocument::HttpDocument() :
|
||||
m_processingDurationStartMarker(QDateTime::currentMSecsSinceEpoch())
|
||||
@@ -67,7 +68,11 @@ void HttpDocument::setCode(const QString &code)
|
||||
|
||||
void HttpDocument::setHeader(const QString &name, const QString &value)
|
||||
{
|
||||
if (name.isEmpty() or value.isEmpty()) return;
|
||||
if (name.isEmpty() or value.isEmpty())
|
||||
{
|
||||
qWarning() << "HTTP header setting rejected (empty value):" << name << ";" << value;
|
||||
return;
|
||||
}
|
||||
|
||||
m_headers.insert(name, value);
|
||||
}
|
||||
@@ -159,8 +164,7 @@ QByteArray HttpDocument::document() const
|
||||
|
||||
QByteArray result = "HTTP/1.0 " + code().toUtf8() + HTML_NEWLINE;
|
||||
|
||||
auto h = headers();
|
||||
QMapIterator<QString, QString> iter(h);
|
||||
QMapIterator<QString, QString> iter( headers() );
|
||||
while (iter.hasNext())
|
||||
{
|
||||
iter.next();
|
||||
|
||||
+2
-1
@@ -33,6 +33,7 @@ public:
|
||||
static const QString ContentLength;
|
||||
static const QString ETag;
|
||||
static const QString ProcessingDuration;
|
||||
static const QString SetCookie;
|
||||
};
|
||||
|
||||
struct ContentType
|
||||
@@ -68,7 +69,7 @@ public:
|
||||
QString code() const { return m_code; }
|
||||
|
||||
void setHeader(const QString& name, const QString& value);
|
||||
QMap<QString, QString> headers() const { return m_headers; }
|
||||
const QMap<QString, QString>& headers() const { return m_headers; }
|
||||
|
||||
void setBody(const QByteArray& body);
|
||||
void setBody(const QJsonObject& body);
|
||||
|
||||
+14
-2
@@ -26,6 +26,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
constexpr const int getValueBeforeSpaceLIMIT = 200;
|
||||
constexpr const int getValueBeforeBodyLIMIT = 1000;
|
||||
|
||||
constexpr const char* lastTrafficVolumeCookie = "trafficvolume";
|
||||
|
||||
SocketRunnable::SocketRunnable(qintptr socketDescriptor, QObject *parent) :
|
||||
SocketRunnableBase(socketDescriptor, parent)
|
||||
{
|
||||
@@ -35,7 +37,17 @@ void SocketRunnable::get()
|
||||
{
|
||||
if (urlPath() == "/index.html")
|
||||
{
|
||||
httpDocument().setBody(WebPage::document());
|
||||
quint64 lastTrafficVolume = 0;
|
||||
const QString cookieString = g::getValue(m_headers, "Cookie", g::GetValueType::HttpHeader);
|
||||
if (not cookieString.isEmpty())
|
||||
{
|
||||
lastTrafficVolume = g::getValue(m_headers, lastTrafficVolumeCookie).toULongLong();
|
||||
}
|
||||
|
||||
quint64 currentTrafficVolume = 0;
|
||||
httpDocument().setBody( WebPage::document(lastTrafficVolume, ¤tTrafficVolume) );
|
||||
httpDocument().setHeader(HttpDocument::CommonHeader::SetCookie,
|
||||
QString(lastTrafficVolumeCookie)+"="+QString::number(currentTrafficVolume)+"; max-age=31536000;");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -79,7 +91,7 @@ void SocketRunnable::reader()
|
||||
}
|
||||
else if (reqType != "HEAD")
|
||||
{
|
||||
httpDocument().setCode(HttpDocument::Code::_400); // default code is 200
|
||||
httpDocument().setCode(HttpDocument::Code::_400);
|
||||
}
|
||||
|
||||
setDataToWrite(httpDocument().document());
|
||||
|
||||
+24
-3
@@ -89,7 +89,7 @@ struct HumanReadableValue
|
||||
QString measure = "anon";
|
||||
};
|
||||
|
||||
QByteArray WebPage::document()
|
||||
QByteArray WebPage::document(quint64 lastTrafficVolume, quint64* currentTrafficVolume)
|
||||
{
|
||||
QFile src(":/html/index.html");
|
||||
if (not src.open(QIODevice::ReadOnly))
|
||||
@@ -104,12 +104,18 @@ QByteArray WebPage::document()
|
||||
setTitle(page);
|
||||
customStyles(page);
|
||||
lastDestinations(page);
|
||||
trafficBlock(page);
|
||||
topDestinations(page);
|
||||
blockedDestinations(page);
|
||||
informationBlock(page);
|
||||
footer(page);
|
||||
|
||||
quint64 traffic = trafficBlock(page, lastTrafficVolume);
|
||||
|
||||
if (currentTrafficVolume != nullptr)
|
||||
{
|
||||
*currentTrafficVolume = traffic;
|
||||
}
|
||||
|
||||
return page.toUtf8();
|
||||
}
|
||||
|
||||
@@ -164,8 +170,21 @@ void WebPage::lastDestinations(QString &document)
|
||||
document.replace(g::c::HA_LAST_DESTINATIONS_LIST, destinationsBlock);
|
||||
}
|
||||
|
||||
void WebPage::trafficBlock(QString &document)
|
||||
quint64 WebPage::trafficBlock(QString &document, quint64 lastTrafficVolume)
|
||||
{
|
||||
quint64 currentVolume = Statistics::totalUpload() + Statistics::totalDownload();
|
||||
if (lastTrafficVolume != 0 and currentVolume > lastTrafficVolume)
|
||||
{
|
||||
quint64 difference = currentVolume - lastTrafficVolume;
|
||||
auto differenceHr = bytesToHumanReadableString(difference);
|
||||
document.replace(g::c::HA_TRAFFIC_SECTION_TITLE, "Since your last visit: " +
|
||||
differenceHr.integer+"."+differenceHr.decimal+" "+differenceHr.measure);
|
||||
}
|
||||
else
|
||||
{
|
||||
document.replace(g::c::HA_TRAFFIC_SECTION_TITLE, "Traffic");
|
||||
}
|
||||
|
||||
auto dailyUpload = bytesToHumanReadableString( Statistics::dailyUpload() );
|
||||
document.replace(g::c::HA_DAILY_UPLOAD, dailyUpload.integer);
|
||||
document.replace(g::c::HA_DAILY_UPLOAD_DECIMAL, dailyUpload.decimal);
|
||||
@@ -181,6 +200,8 @@ void WebPage::trafficBlock(QString &document)
|
||||
|
||||
auto totalDownload = bytesToHumanReadableString( Statistics::totalDownload() );
|
||||
document.replace(g::c::HA_TOTAL_DOWNLOAD, totalDownload.integer + " " + totalDownload.measure);
|
||||
|
||||
return currentVolume;
|
||||
}
|
||||
|
||||
void WebPage::topDestinations(QString &document)
|
||||
|
||||
+2
-2
@@ -28,13 +28,13 @@ class WebPage
|
||||
{
|
||||
public:
|
||||
WebPage() = delete;
|
||||
static QByteArray document();
|
||||
static QByteArray document(quint64 lastTrafficVolume, quint64* currentTrafficVolume = nullptr);
|
||||
|
||||
private:
|
||||
static void setTitle(QString& document);
|
||||
static void customStyles(QString& document);
|
||||
static void lastDestinations(QString& document);
|
||||
static void trafficBlock(QString& document);
|
||||
static quint64 trafficBlock(QString& document, quint64 lastTrafficVolume);
|
||||
static void topDestinations(QString& document);
|
||||
static void blockedDestinations(QString& document);
|
||||
static void informationBlock(QString& document);
|
||||
|
||||
Reference in New Issue
Block a user