Skip to content

Commit

Permalink
Moved some hard coded statistics settings to config file. Parameteriz…
Browse files Browse the repository at this point in the history
…ed DB queries to prevent SQL injection. Added method that attempts to cut db siz einstead of full reset when siz elimit exceeded (Not enabled until tested). UI support for new config settings (excluding stats record interval until discussed).
  • Loading branch information
hybridview committed Feb 6, 2015
1 parent 1bac78d commit 0a59fe0
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 26 deletions.
13 changes: 13 additions & 0 deletions BaseFiles/Common/html/js/api/homegenie.system.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ HG.System.WebCacheIsEnabled = function (callback) {
});
};

// Should this be added to a new namespace like "HG.System.Statistics"? It's a setting, so thought it might not belong in homegenie.statstics.js... Opinions?
HG.System.SetStatisticsDatabaseMaximumSize = function (mb, callback) {
$.ajax({
url: '/' + HG.WebApp.Data.ServiceKey + '/' + HG.WebApp.Data.ServiceDomain + '/Config/System.Configure/Statistics.SetStatisticsDatabaseMaximumSize/' + mb + '/' + (new Date().getTime()),
type: "POST",
data: "{ dummy: 'dummy' }",
dataType: "text",
success: function (data) {
if (callback != null) callback(data);
}
});
};


HG.System.UpdateManager = HG.System.UpdateManager || {};
HG.System.UpdateManager.UpdateCheck = function (callback) {
Expand Down
10 changes: 9 additions & 1 deletion BaseFiles/Common/html/pages/analyze/_statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ HG.WebApp.Statistics._RefreshIntervalObject = null;
HG.WebApp.Statistics._RefreshInterval = 2 * 60000; // stats refresh interval = 2 minutes
//
HG.WebApp.Statistics.InitializePage = function () {

HG.WebApp.Statistics.InitConfiguration();
$('#page_analyze_source').on('change', function () {
var selected = $(this).find('option:selected');
var filter = selected.attr('data-context-domain') + ':' + selected.attr('data-context-address');
Expand Down Expand Up @@ -162,6 +162,14 @@ HG.WebApp.Statistics.SetAutoRefresh = function (autorefresh) {
}
};

HG.WebApp.Statistics.InitConfiguration = function () {
HG.Statistics.ServiceCall('Configuration.Get', '', '', function (configs) {
var setting = eval(configs)[0];
var sec = (setting.StatisticsUIRefreshSeconds * 1);
HG.WebApp.Statistics._RefreshInterval = sec * 1000; //2 * 60000;
HG.WebApp.Statistics.SetAutoRefresh(true);
});
}

HG.WebApp.Statistics.RefreshParameters = function (filter) {
var cval = $('#page_analyze_param').val();
Expand Down
21 changes: 21 additions & 0 deletions BaseFiles/Common/html/pages/configure/maintenance/_maintenance.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ HG.WebApp.Maintenance.InitializePage = function () {
$.mobile.loading('hide');
});
});

$('#systemsettings_databasemaxsize_change').bind('click', function () {
var sizemb = $('#systemsettings_databasemaxsizechange_size').val();
$.mobile.loading('show');
HG.System.SetStatisticsDatabaseMaximumSize(sizemb, function (data) {
$.mobile.loading('hide');
setTimeout(function () {
HG.WebApp.Maintenance.LoadStatisticsSettings();
}, 1000);
});
});

});
};
Expand All @@ -285,6 +296,7 @@ HG.WebApp.Maintenance.LoadSettings = function () {
$.mobile.loading('hide');
});
//
HG.WebApp.Maintenance.LoadStatisticsSettings();
HG.WebApp.Maintenance.LoadSecuritySettings();
HG.WebApp.Maintenance.LoadUpdateCheckSettings();
//
Expand Down Expand Up @@ -336,6 +348,15 @@ HG.WebApp.Maintenance.LoadSecuritySettings = function () {
});
};

HG.WebApp.Maintenance.LoadStatisticsSettings = function () {
$.mobile.loading('show');
HG.Configure.System.ServiceCall("Statistics.GetStatisticsDatabaseMaximumSize", function (data) {
$('#systemsettings_databasemaxsizechange_size').val(data);
$('#systemsettings_databasemaxsize_text').html(data);
$.mobile.loading('hide');
});
};

HG.WebApp.Maintenance.RefreshModulesList = function () {
$('#configure_interfaces_modules_list').empty();
var cdomain = '';
Expand Down
108 changes: 102 additions & 6 deletions BaseFiles/Common/html/pages/configure/maintenance/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,30 @@
</div>
</div>
</li>
<!--
<li>
<div class="ui-grid-a">
<div class="ui-block-a">
<div>
<label data-locale-id="configure_system_statistics">Statistics</label><br />
</div>
</div>
<div class="ui-block-b" style="float:right;width:150px">
<a class="ui-btn ui-corner-all" data-locale-id="configure_system_databasereset" href="javascript:HG.Statistics.Database.Reset()">Database Reset</a>
</div>
</div>
</li>
-->
<li>
<div class="ui-grid-a">
<div class="ui-block-a">
<div>
<label data-locale-id="configure_system_statistics">Statistics</label><br />
<label data-locale-id="configure_system_modulescleanup">Modules Cleanup</label><br />
</div>
</div>
<div class="ui-block-b" style="float:right;width:150px">
<a class="ui-btn ui-corner-all" data-locale-id="configure_system_databasereset" href="javascript:HG.Statistics.Database.Reset()">Database Reset</a>
<a href="#systemsettings_modulesdelete" class="ui-btn ui-corner-all" data-locale-id="configure_system_modulesselect" data-rel="popup">Select Modules</a>
</div>
</div>

Expand All @@ -136,11 +151,25 @@
<div class="ui-grid-a">
<div class="ui-block-a">
<div>
<label data-locale-id="configure_system_modulescleanup">Modules Cleanup</label><br />
<label data-locale-id="configure_system_remotemodules">Remote Modules Routing</label><br />
</div>
</div>
<div class="ui-block-b" style="float:right;width:150px">
<a href="#systemsettings_modulesdelete" class="ui-btn ui-corner-all" data-locale-id="configure_system_modulesselect" data-rel="popup">Select Modules</a>
<a href="javascript:HG.Configure.Modules.RoutingReset()" class="ui-btn ui-corner-all" data-locale-id="configure_system_modsroutingreset">Reset</a>
</div>
</div>

</li>

<li data-locale-id="configure_system_statistics" data-role="list-divider">Statistics</li>
<li>
<div class="ui-grid-a">
<div>
<label data-locale-id="configure_system_statistics_clear">Clear All Statistics Data</label><br />
</div>
<div class="ui-block-b" style="float:right;width:150px">
<!-- TODO: Confirmation for this?-->
<a class="ui-btn ui-corner-all" data-locale-id="configure_system_databasereset" href="javascript:HG.Statistics.Database.Reset()">Reset</a>
</div>
</div>

Expand All @@ -149,15 +178,34 @@
<div class="ui-grid-a">
<div class="ui-block-a">
<div>
<label data-locale-id="configure_system_remotemodules">Remote Modules Routing</label><br />
<label><span data-locale-id="configure_system_statistics_databasemaxsize">Maximum Database Size is </span><span id="systemsettings_databasemaxsize_text"></span> MB</label><br />
</div>
</div>
<div class="ui-block-b" style="float:right;width:150px">
<a href="javascript:HG.Configure.Modules.RoutingReset()" class="ui-btn ui-corner-all" data-locale-id="configure_system_modsroutingreset">Reset</a>
<a href="#systemsettings_databasemaxsizechange_popup" data-locale-id="configure_system_statistics_changedatabasemaxsize" data-rel="popup" class="ui-btn ui-corner-all">Change</a>
</div>
</div>

</li>


<!-- Might be dangerous for average user without constraints? Hiding for now...
<li>
<div class="ui-grid-a">
<div class="ui-block-a">
<div>
<label><span data-locale-id="configure_system_statistics_timeresolution">Record statistics every</span> <span id="systemsettings_statistics_timeresolutionseconds_text"></span> <span data-locale-id="configure_system_statistics_timeresolution_units">seconds</span></label><br />
</div>
</div>
<div class="ui-block-b" style="float:right;width:150px">
<a href="#systemsettings_databasemaxsizechange_popup" data-locale-id="configure_system_statistics_changetimeresolution" data-rel="popup" class="ui-btn ui-corner-all">Change Size</a>
</div>
</div>
</li>
-->


<li data-locale-id="configure_system_userinterface" data-role="list-divider">User Interface</li>
<li>
<span data-locale-id="configure_system_theme">Theme</span>
Expand Down Expand Up @@ -341,6 +389,54 @@ <h3 data-locale-id="configure_system_updateinstall_warning" class="ui-title">It
</div>
</div>
</div>

<div id="systemsettings_databasemaxsizechange_popup" class="ui-corner-all hg-popup-a" data-role="popup" data-position-to="window" data-transition="pop" data-overlay-theme="b">
<div data-role="header" class="ui-corner-top">
<h1 data-locale-id="configure_system_statistics_databasemaxsizechange">Maximum Size (MB)</h1>
</div>

<div class="ui-corner-bottom ui-content">
<h3 data-locale-id="configure_system_statistics_databasemaxsizechange_note" class="ui-title">Note: after changing size, HomeGenie must be restarted to make changes effective!</h3>
<p>
<label for="systemsettings_databasemaxsizechange_size" />
<input id="systemsettings_databasemaxsizechange_size" type="number" />
</p>
<div class="ui-grid-a">
<div class="ui-block-a">
<a href="#" class="ui-btn ui-corner-all" data-locale-id="configure_system_statistics_databasemaxsizechange_cancel" data-rel="back">Cancel</a>
</div>
<div class="ui-block-b">
<a id="systemsettings_databasemaxsize_change" href="#" class="ui-btn ui-corner-all" data-locale-id="configure_system_statistics_databasemaxsizechange_commit" data-rel="back" data-transition="flow">Change</a>
</div>
</div>
</div>
</div>

<!--
<div id="systemsettings_statistics_timeresolutionseconds_popup" class="ui-corner-all hg-popup-a" data-role="popup" data-position-to="window" data-transition="pop" data-overlay-theme="b">
<div data-role="header" class="ui-corner-top">
<h1 data-locale-id="configure_system_statistics_timeresolutionchange">Statistics Record Interval</h1>
</div>
<div class="ui-corner-bottom ui-content">
<h3 data-locale-id="configure_system_statistics_timeresolutionchange_note" class="ui-title">Note: after changing interval, HomeGenie must be restarted to make changes effective!</h3>
<p>
<label for="systemsettings_statistics_timeresolutionchange_seconds" />
<input id="systemsettings_statistics_timeresolutionchange_seconds" type="number" />
</p>
<div class="ui-grid-a">
<div class="ui-block-a">
<a href="#" class="ui-btn ui-corner-all" data-locale-id="configure_system_statistics_timeresolutionchange_cancel" data-rel="back">Cancel</a>
</div>
<div class="ui-block-b">
<a id="systemsettings_statistics_timeresolution_change" href="#" class="ui-btn ui-corner-all" data-locale-id="configure_system_statistics_timeresolutionchange_commit" data-rel="back" data-transition="flow">Change</a>
</div>
</div>
</div>
</div>
-->



</div>
<div data-role="footer" data-position="fixed" data-tap-toggle="false">
Expand Down
52 changes: 52 additions & 0 deletions HomeGenie/Data/SystemConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,58 @@ public class HomeGenieConfiguration
public string GUID { get; set; }

public string EnableLogFile { get; set; }

public StatisticsConfiguration Statistics = new StatisticsConfiguration();

[Serializable()]
public class StatisticsConfiguration
{

[XmlAttribute]
public int MaxDatabaseSizeMBytes { get; set; }

[XmlAttribute]
public int StatisticsTimeResolutionSeconds { get; set; }

[XmlAttribute]
public int StatisticsUIRefreshSeconds { get; set; }

public StatisticsConfiguration()
{

MaxDatabaseSizeMBytes = 10; // 10MB default.
StatisticsTimeResolutionSeconds = 5 * 60; // 5 minute default.
StatisticsUIRefreshSeconds = 2 * 60; // 2 minute default.
}

/// <summary>
/// Set constraints to protect the system. These are absolute constraints to protect the user experience (locked browser/server), but are not
/// RECOMMENDED constraints. For example, StatisticsTimeResolutionSeconds less than 5*60 starts to make the graph
/// look messy, but we still allow anything above 30 seconds in case advanced user wants it. Might want to keep
/// recommended values reference later.
///
/// Should later throw error so UI can notify user?
/// </summary>
public void Validate()
{
//
if (MaxDatabaseSizeMBytes < 1)
{
MaxDatabaseSizeMBytes = 1;
}
// Current design would make < 30 seconds a poor setting. In full day view, if this is anything less than a few minutes, day detail line is smashed.
if (StatisticsTimeResolutionSeconds < 30)
{
StatisticsTimeResolutionSeconds = 30;
}
if (StatisticsUIRefreshSeconds < 5)
{
StatisticsUIRefreshSeconds = 5;
}

}

}
}

}
Expand Down
15 changes: 15 additions & 0 deletions HomeGenie/Service/Handlers/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migComm
{
}
}
else if (migCommand.GetOption(0) == "Statistics.GetStatisticsDatabaseMaximumSize")
{
migCommand.Response = JsonHelper.GetSimpleResponse(homegenie.SystemConfiguration.HomeGenie.Statistics.MaxDatabaseSizeMBytes.ToString());
}
else if (migCommand.GetOption(0) == "Statistics.SetStatisticsDatabaseMaximumSize")
{
try
{
homegenie.SystemConfiguration.HomeGenie.Statistics.MaxDatabaseSizeMBytes = int.Parse(migCommand.GetOption(1));
homegenie.SystemConfiguration.Update();
}
catch
{
}
}
else if (migCommand.GetOption(0) == "SystemLogging.DownloadCsv")
{
string csvlog = "";
Expand Down
5 changes: 4 additions & 1 deletion HomeGenie/Service/Handlers/Statistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migComm
case "Database.Reset":
homegenie.Statistics.DatabaseReset();
break;

case "Configuration.Get":
// Just one at the moment.
migCommand.Response = "[{ StatisticsUIRefreshSeconds : '" + homegenie.SystemConfiguration.HomeGenie.Statistics.StatisticsUIRefreshSeconds + "' }]";
break;
case "Parameter.List":
filterList = migCommand.GetOption(0).Split(':');
if (filterList.Length == 2)
Expand Down
Loading

0 comments on commit 0a59fe0

Please sign in to comment.