Ref T149, URL log list/class to trace network calls

* value class
* list
* metadata registration
This commit is contained in:
Klaus Basan
2017-09-10 23:24:40 +02:00
committed by Mathew Sutcliffe
parent 099afba8a7
commit 1c57ce87a2
9 changed files with 466 additions and 1 deletions

View File

@@ -0,0 +1,96 @@
/* Copyright (C) 2017
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackmisc/network/urllog.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/propertyindex.h"
#include <QJsonValue>
#include <QPair>
#include <QtGlobal>
#include <atomic>
using namespace BlackMisc::Db;
namespace BlackMisc
{
namespace Network
{
CUrlLog::CUrlLog(const CUrl &url) :
ITimestampBased(), m_id(uniqueId()), m_url(url)
{
ITimestampBased::setCurrentUtcTime();
}
void CUrlLog::setResponseTimestampToNow()
{
m_responseTimeMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
m_responseTimeMs = m_responseTimeMSecsSinceEpoch - ITimestampBased::getMSecsSinceEpoch();
}
bool CUrlLog::isPending() const
{
return m_responseTimeMs < 0;
}
CVariant CUrlLog::propertyByIndex(const CPropertyIndex &index) const
{
if (index.isMyself()) { return CVariant::from(*this); }
if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::propertyByIndex(index); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexId: return CVariant::from(m_id);
case IndexSuccess: return CVariant::from(m_success);
case IndexUrl: return this->m_url.propertyByIndex(index.copyFrontRemoved());
case IndexResponseTimestamp: return CVariant::fromValue(this->getResponseTimestamp());
case IndexResponseTime: return CVariant::fromValue(m_responseTimeMs);
default: return CValueObject::propertyByIndex(index);
}
}
void CUrlLog::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{
if (index.isMyself()) { (*this) = variant.to<CUrlLog>(); return; }
if (ITimestampBased::canHandleIndex(index)) { ITimestampBased::setPropertyByIndex(index, variant); return; }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexId: m_id = variant.toInt(); break;
case IndexSuccess: m_success = variant.toBool(); break;
case IndexUrl: m_url.setPropertyByIndex(index.copyFrontRemoved(), variant); break;
case IndexResponseTime: this->setResponseTimestampToNow(); break; // a bit unusual
default: CValueObject::setPropertyByIndex(index, variant); break;
}
}
QString CUrlLog::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
static const QString s("Id: %1, success: %2 response: %3ms, started: %4 ended: %5");
return s.arg(m_id).arg(boolToYesNo(m_success)).arg(m_responseTimeMs).arg(this->getMSecsSinceEpoch()).arg(m_responseTimeMSecsSinceEpoch);
}
const char *CUrlLog::propertyNameId()
{
static const QByteArray p(QString("urlLogId").toLatin1());
return p.constData();
}
int CUrlLog::uniqueId()
{
static std::atomic_int s_id {1}; // 0 means default in property system, so I start with 1
const int id = s_id++;
return id;
}
} // namespace
} // namespace