// Copyright (c) 2011, Outercurve Foundation. // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // - Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // - Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // - Neither the name of the Outercurve Foundation nor the names of its // contributors may be used to endorse or promote products derived from this // software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using System; using System.Collections.Generic; using System.Text; using System.Data; namespace WebsitePanel.EnterpriseServer.Base.Reports { public partial class OverusageReport { #region BandwidthOverusageRow Extension Functions public partial class BandwidthOverusageRow { /// /// Creates a using the /// DataSet from a row that contains information about bandwidth usage. /// /// Instance of class. /// DataRow with bandwidth information. /// . /// When or either is null. public static BandwidthOverusageRow CreateFromHostingSpaceDataRow(OverusageReport report, DataRow hostingSpaceDataRow) { if (report == null) { throw new ArgumentNullException("report"); } if (hostingSpaceDataRow == null) { throw new ArgumentNullException("hostingSpaceDataRow"); } BandwidthOverusageRow row = report.BandwidthOverusage.NewBandwidthOverusageRow(); row.HostingSpaceId = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceDataRow["PackageID"].ToString(), 0); row.Allocated = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceDataRow["QuotaValue"].ToString(), 0); row.Used = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceDataRow["Bandwidth"].ToString(), 0); row.Usage = (row.Used - row.Allocated); return row; } /// /// Creates using as a data source. /// /// Current dataset. /// instance. /// instance. /// When or either is null. public static BandwidthOverusageRow CreateFromPackageInfo(OverusageReport report, PackageInfo packageInfo) { if (report == null) { throw new ArgumentNullException("report"); } if (packageInfo == null) { throw new ArgumentNullException("packageInfo"); } BandwidthOverusageRow row = report.BandwidthOverusage.NewBandwidthOverusageRow(); row.HostingSpaceId = packageInfo.PackageId; row.Allocated = packageInfo.BandWidthQuota; row.Used = packageInfo.BandWidth; row.Usage = (row.Used - row.Allocated); return row; } } #endregion #region OverusageDetailsRow Extension methods public partial class OverusageDetailsRow { /// /// Creates using the information about Hosting Space disk space. /// /// data set. Current report. /// containing the Hosting Space disk space information. /// Hosting Space id. /// Type of overusage, can be Diskspace, Bandwidth, etc. /// An instance of . /// When , or is null. /// When less then 1. public static OverusageDetailsRow CreateFromPackageDiskspaceRow(OverusageReport report, DataRow packageDiskspaceRow, long hostingSpaceId, string overusageType) { if (report == null) { throw new ArgumentNullException("report"); } if (packageDiskspaceRow == null) { throw new ArgumentNullException("packageDiskspaceRow"); } if (String.IsNullOrEmpty(overusageType)) { throw new ArgumentNullException("overusageType"); } if (hostingSpaceId < 1) { throw new ArgumentOutOfRangeException( "hostingSpaceId" , String.Format( "Hosting Space Id cannot be less then 1, however it is {0}." , hostingSpaceId ) ); } OverusageDetailsRow row = report.OverusageDetails.NewOverusageDetailsRow(); row.HostingSpaceId = hostingSpaceId; row.OverusageType = overusageType; row.ResourceGroupName = OverusageReportUtils.GetStringOrDefault(packageDiskspaceRow, "GroupName", "Files"); row.Used = OverusageReportUtils.GetLongValueOrDefault(packageDiskspaceRow["Diskspace"].ToString(), 0); row.Allowed = 0; return row; } /// /// Creates using information about Hosting Space bandwidth. /// /// Current instance. /// containing information about Hosting Space bandwidth. /// Hosting Space Id. /// Type of overusage. Diskspace, Bandwidth, etc. /// filled from . /// When , or is null. /// When less then 1. public static OverusageDetailsRow CreateFromBandwidthRow(OverusageReport report, DataRow bandwidthRow, long hostingSpaceId, string overusageType) { if (report == null) { throw new ArgumentNullException("report"); } if (bandwidthRow == null) { throw new ArgumentNullException("bandwidthRow"); } if (String.IsNullOrEmpty(overusageType)) { throw new ArgumentNullException("overusageType"); } if (hostingSpaceId < 1) { throw new ArgumentOutOfRangeException( "hostingSpaceId" , String.Format( "Hosting Space Id cannot be less then 1, however it is {0}." , hostingSpaceId ) ); } OverusageDetailsRow row = report.OverusageDetails.NewOverusageDetailsRow(); row.HostingSpaceId = hostingSpaceId; row.OverusageType = overusageType; row.ResourceGroupName = OverusageReportUtils.GetStringOrDefault(bandwidthRow, "GroupName", "Files"); row.Used = OverusageReportUtils.GetLongValueOrDefault(bandwidthRow["MegaBytesSent"].ToString(), 0); row.AdditionalField = OverusageReportUtils.GetLongValueOrDefault(bandwidthRow["MegaBytesReceived"].ToString(), 0); row.Allowed = 0; return row; } } #endregion #region HostingSpaceRow Extension functions public partial class HostingSpaceRow { /// /// Returns a package id /// /// containing information about Hosting Space /// Hosting space ID. /// When DataRow does not contain PackageID field or this field contains value less then 1. public static long GetPackageId(DataRow hostingSpaceRow) { long hostingSpaceId = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceRow["PackageID"].ToString(), 0); if (hostingSpaceId < 1) { throw new ArgumentOutOfRangeException( "hostingSpaceId" , String.Format( "PackageID field is either empty or contains incorrect value. Received package ID value: {0}." , hostingSpaceId ) ); } return hostingSpaceId; } /// /// Creates using . /// /// Current . /// Information about Hosting Space /// Information about user belongs to. /// Information about server. Physical storage of a hosting space described in /// Indicates whether disk space is overused. /// Indicates whether bandwidht is overused. /// File system -like path of the location of a hosting space described in /// instance. /// When , , or is null. public static HostingSpaceRow CreateFromPackageInfo(OverusageReport report, PackageInfo packageInfo, UserInfo userInfo, ServerInfo serverInfo,bool isDiskspaceOverused, bool isBandwidthOverused, string packageFullTree) { if (report == null) { throw new ArgumentNullException("report"); } if (packageInfo == null) { throw new ArgumentNullException("packageInfo"); } if (userInfo == null) { throw new ArgumentNullException("userInfo"); } if (serverInfo == null) { throw new ArgumentNullException("serverInfo"); } HostingSpaceRow row = report.HostingSpace.NewHostingSpaceRow(); row.IsBandwidthOverused = isBandwidthOverused; row.IsDiskspaceOverused = isDiskspaceOverused; row.HostingSpaceName = packageInfo.PackageName; row.UserEmail = userInfo.Username; row.UserName = userInfo.Email; row.ChildSpaceQuantity = 1; row.UserId = packageInfo.UserId; row.HostingSpaceId = packageInfo.PackageId; row.Status = packageInfo.StatusId.ToString(); row.HostingSpaceFullTreeName = packageFullTree; row.HostingSpaceCreationDate = packageInfo.PurchaseDate; row.Location = serverInfo.ServerName; return row; } /// /// Creating from DataRow containing data about Hosting Space /// /// Current /// containing information about Hosting Space. /// Indicates whether disk space is overused. /// Indicates whether bandwidth is overusaed. /// File system -like path to hosting space described by /// instance. /// When or is null. public static HostingSpaceRow CreateFromHostingSpacesRow(OverusageReport report, DataRow hostingSpaceRow, bool isDiskspaceOverused, bool isBandwidthOverused, string packageFullTree) { if (report == null) { throw new ArgumentNullException("report"); } if (hostingSpaceRow == null) { throw new ArgumentNullException("hostingSpaceRow"); } HostingSpaceRow row = report.HostingSpace.NewHostingSpaceRow(); row.IsBandwidthOverused = isBandwidthOverused; row.IsDiskspaceOverused = isDiskspaceOverused; row.HostingSpaceName = OverusageReportUtils.GetStringOrDefault(hostingSpaceRow, "PackageName", String.Empty); row.UserEmail = OverusageReportUtils.GetStringOrDefault(hostingSpaceRow, "Email", String.Empty); row.UserName = OverusageReportUtils.GetStringOrDefault(hostingSpaceRow, "Username", String.Empty); row.UserId = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceRow["UserId"].ToString(), 0); row.HostingSpaceId = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceRow["PackageID"].ToString(), 0); row.ChildSpaceQuantity = (int)OverusageReportUtils.GetLongValueOrDefault(hostingSpaceRow["PackagesNumber"].ToString(), 0); row.Status = hostingSpaceRow["StatusID"].ToString(); row.HostingSpaceFullTreeName = packageFullTree; return row; } /// /// Verify whether contains child spaces. /// /// containing information about Hosting Space. /// True it Hosting space contains child spaces. Otherwise, False. /// When is null. public static bool IsContainChildSpaces(DataRow hostingSpacesRow) { if (hostingSpacesRow == null) { throw new ArgumentNullException("hostingSpacesRow"); } return OverusageReportUtils.GetLongValueOrDefault(hostingSpacesRow["PackagesNumber"].ToString(), 0) > 0; } /// /// Verify is disk space is overused by a Hosting Space. /// /// containing information about Hosting Space. /// True, if Hosting Space overuses disk space quota. Otherwise, False. /// When is null. public static bool VerifyIfDiskspaceOverused(DataRow hostingSpacesRow) { if (hostingSpacesRow == null) { throw new ArgumentNullException("hostingSpacesRow"); } long allocated = OverusageReportUtils.GetLongValueOrDefault(hostingSpacesRow["QuotaValue"].ToString(), 0), used = OverusageReportUtils.GetLongValueOrDefault(hostingSpacesRow["Diskspace"].ToString(), 0); return (used > allocated); } /// /// Vefiry if bandwidth is overused by Hosting Space /// /// containing bandwidth information about Hosting Space. /// True, if bandwidth is overused by a Hosting Space. Otherwise, False. /// When is null. public static bool VerifyIfBandwidthOverused(DataRow hostingSpacesRow) { if (hostingSpacesRow == null) { throw new ArgumentNullException("hostingSpacesRow"); } long allocated = OverusageReportUtils.GetLongValueOrDefault(hostingSpacesRow["QuotaValue"].ToString(), 0), used = OverusageReportUtils.GetLongValueOrDefault(hostingSpacesRow["Bandwidth"].ToString(), 0); return (used > allocated); } } #endregion #region DiskspaceOverusageRow extension functions public partial class DiskspaceOverusageRow { /// /// Creates from Hosting Space row. /// /// Current /// containing Hosting Space information. /// instance. /// When or is null. public static DiskspaceOverusageRow CreateFromHostingSpacesRow(OverusageReport report, DataRow hostingSpaceRow) { if (report == null) { throw new ArgumentNullException("report"); } if (hostingSpaceRow == null) { throw new ArgumentNullException("hostingSpaceRow"); } DiskspaceOverusageRow row = report.DiskspaceOverusage.NewDiskspaceOverusageRow(); row.HostingSpaceId = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceRow["PackageID"].ToString(), 0); row.Allocated = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceRow["QuotaValue"].ToString(), 0); row.Used = OverusageReportUtils.GetLongValueOrDefault(hostingSpaceRow["Diskspace"].ToString(), 0); row.Usage = (row.Used - row.Allocated); return row; } /// /// Creates using information. /// /// Current /// Hosting Space information. /// instance. /// When or is null. public static DiskspaceOverusageRow CreateFromPackageInfo(OverusageReport report, PackageInfo packageInfo) { if (report == null) { throw new ArgumentNullException("report"); } if (packageInfo == null) { throw new ArgumentNullException("packageInfo"); } DiskspaceOverusageRow row = report.DiskspaceOverusage.NewDiskspaceOverusageRow(); row.HostingSpaceId = packageInfo.PackageId; row.Allocated = packageInfo.DiskSpaceQuota; row.Used = packageInfo.DiskSpace; row.Usage = (row.Used - row.Allocated); return row; } } #endregion } }