From 1454557bc2b8120ac52625c0c71302b8250ffadb Mon Sep 17 00:00:00 2001 From: Raymond Wagner Date: Sat, 1 Jun 2013 20:53:30 -0400 Subject: [PATCH] Add housekeeping task for hardware profiler (smolt). This adds a housekeeping task to allow the hardware profiler to be run periodically through the housekeeper, rather than a single check performed during frontend startup. This also adds a schema update to reorganize a few settings related to it. --- mythtv/libs/libmythbase/hardwareprofile.cpp | 57 +++++++++++++++++++-- mythtv/libs/libmythbase/mythversion.h | 2 +- mythtv/libs/libmythtv/dbcheck.cpp | 27 ++++++++++ 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/mythtv/libs/libmythbase/hardwareprofile.cpp b/mythtv/libs/libmythbase/hardwareprofile.cpp index 3c55c04dca6..34dba5cea6f 100644 --- a/mythtv/libs/libmythbase/hardwareprofile.cpp +++ b/mythtv/libs/libmythbase/hardwareprofile.cpp @@ -19,19 +19,44 @@ const QString SMOLT_TOKEN = QString("smolt_token-smolt.mythtv.org"); HardwareProfile::HardwareProfile() : + m_enabled(false), m_uuid(QString()), m_publicuuid(QString()), m_lastUpdate(QDateTime()), m_hardwareProfile(QString()) { + m_enabled = (gCoreContext->GetNumSetting("HardwareProfileEnabled", 0) == 1); m_uuid = gCoreContext->GetSetting("HardwareProfileUUID"); m_publicuuid = gCoreContext->GetSetting("HardwareProfilePublicUUID"); - QString lastupdate = gCoreContext->GetSetting("HardwareProfileLastUpdated"); - m_lastUpdate = MythDate::fromString(lastupdate); + + if (m_enabled) + { + MSqlQuery query(MSqlQuery::InitCon()); + + query.prepare("SELECT lastrun FROM housekeeping" + " WHERE tag = 'HardwareProfiler'" + " AND hostname = :HOST"); + query.bindValue(":HOST", gCoreContext->GetHostName()); + if (query.exec() && query.next()) + m_lastUpdate = MythDate::as_utc(query.value(0).toDateTime()); + } } HardwareProfile::~HardwareProfile() { } +void HardwareProfile::Enable(void) +{ + if (m_uuid.isEmpty()) + return; + + gCoreContext->SaveSettingOnHost("HardwareProfileEnabled", "1", ""); +} + +void HardwareProfile::Disable(void) +{ + gCoreContext->SaveSettingOnHost("HardwareProfileEnabled", "0", ""); +} + void HardwareProfile::GenerateUUIDs(void) { QString fileprefix = GetConfDir() + "/HardwareProfile"; @@ -169,11 +194,14 @@ bool HardwareProfile::NeedsUpdate(void) const return false; } -bool HardwareProfile::SubmitProfile(void) +bool HardwareProfile::SubmitProfile(bool updateTime) { if (m_uuid.isEmpty()) return false; + if (!m_enabled) + Enable(); + if (!m_hardwareProfile.isEmpty()) LOG(VB_GENERAL, LOG_INFO, QString("Submitting the following hardware profile: %1") @@ -191,8 +219,13 @@ bool HardwareProfile::SubmitProfile(void) GenerateUUIDs(); gCoreContext->SaveSetting("HardwareProfileUUID", GetPrivateUUID()); gCoreContext->SaveSetting("HardwareProfilePublicUUID", GetPublicUUID()); - gCoreContext->SaveSetting("HardwareProfileLastUpdated", - MythDate::current_iso_string()); + + if (updateTime) + { + HardwareProfileTask task; + task.UpdateLastRun(MythDate::current()); + } + return true; } else @@ -252,3 +285,17 @@ QString HardwareProfile::GetHardwareProfile() const system.Wait(); return system.ReadAll(); } + +bool HardwareProfileTask::DoCheckRun(QDateTime now) +{ + if (GetLastRun().secsTo(now) > 2592000 && // 60*60*24*30 + (gCoreContext->GetNumSetting("HardwareProfileEnabled", 0) == 1)) + return true; + return false; +} + +bool HardwareProfileTask::DoRun(void) +{ + HardwareProfile hp; + return hp.SubmitProfile(false); +} diff --git a/mythtv/libs/libmythbase/mythversion.h b/mythtv/libs/libmythbase/mythversion.h index d659756272c..dd6e78fb423 100644 --- a/mythtv/libs/libmythbase/mythversion.h +++ b/mythtv/libs/libmythbase/mythversion.h @@ -61,7 +61,7 @@ * mythtv/bindings/php/MythBackend.php #endif -#define MYTH_DATABASE_VERSION "1311" +#define MYTH_DATABASE_VERSION "1312" MBASE_PUBLIC const char *GetMythSourceVersion(); diff --git a/mythtv/libs/libmythtv/dbcheck.cpp b/mythtv/libs/libmythtv/dbcheck.cpp index 53aa520e5b6..6ec3a1ed12d 100644 --- a/mythtv/libs/libmythtv/dbcheck.cpp +++ b/mythtv/libs/libmythtv/dbcheck.cpp @@ -2368,6 +2368,33 @@ NULL return false; } + if (dbver == "1311") + { + const char *updates[] = { +// Create a global enable/disable instead of one per-host +// Any hosts previously running it mean all hosts do now +"INSERT INTO `settings` (`value`, `hostname`, `data`)," +" SELECT 'HardwareProfileEnaled'," +" NULL," +" IF((SELECT COUNT(1)" +" FROM `settings`" +" WHERE `value` = 'HardwareProfileLastUpdated' > 0)," +" 1, 0);", +// Create 'lastrun' times using existing data in settings +"INSERT INTO `housekeeper` (`tag`, `hostname`, `lastrun`)" +" SELECT 'HardwareProfiler'," +" `hostname`," +" `data`" +" FROM `settings`" +" WHERE `value` = 'HardwareProfileLastUpdated';", +// Clear out old settings +"DELETE FROM `settings` WHERE `value` = 'HardwareProfileLastUpdated';", +NULL +}; + if (!performActualUpdate(&updates[0], "1312", dbver)) + return false; + } + return true; }