Return-Path: X-Original-To: apmail-cloudstack-dev-archive@www.apache.org Delivered-To: apmail-cloudstack-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4345C172B4 for ; Wed, 26 Aug 2015 06:38:55 +0000 (UTC) Received: (qmail 2575 invoked by uid 500); 26 Aug 2015 06:38:54 -0000 Delivered-To: apmail-cloudstack-dev-archive@cloudstack.apache.org Received: (qmail 2515 invoked by uid 500); 26 Aug 2015 06:38:54 -0000 Mailing-List: contact dev-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list dev@cloudstack.apache.org Received: (qmail 2503 invoked by uid 99); 26 Aug 2015 06:38:54 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Aug 2015 06:38:54 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 378C3E1145; Wed, 26 Aug 2015 06:38:54 +0000 (UTC) From: abhinandanprateek To: dev@cloudstack.apache.org Reply-To: dev@cloudstack.apache.org References: In-Reply-To: Subject: [GitHub] cloudstack pull request: Quota master Content-Type: text/plain Message-Id: <20150826063854.378C3E1145@git1-us-west.apache.org> Date: Wed, 26 Aug 2015 06:38:54 +0000 (UTC) Github user abhinandanprateek commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/689#discussion_r37950304 --- Diff: plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java --- @@ -0,0 +1,296 @@ +//Licensed to the Apache Software Foundation (ASF) under one +//or more contributor license agreements. See the NOTICE file +//distributed with this work for additional information +//regarding copyright ownership. The ASF licenses this file +//to you under the Apache License, Version 2.0 (the +//"License"); you may not use this file except in compliance +//with the License. You may obtain a copy of the License at +// +//http://www.apache.org/licenses/LICENSE-2.0 +// +//Unless required by applicable law or agreed to in writing, +//software distributed under the License is distributed on an +//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +//KIND, either express or implied. See the License for the +//specific language governing permissions and limitations +//under the License. +package org.apache.cloudstack.quota; + +import com.cloud.configuration.Config; +import com.cloud.domain.dao.DomainDao; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.PermissionDeniedException; +import com.cloud.user.Account; +import com.cloud.user.AccountVO; +import com.cloud.user.dao.AccountDao; +import com.cloud.utils.component.ManagerBase; +import com.cloud.utils.db.Filter; +import com.cloud.utils.db.TransactionLegacy; + +import org.apache.cloudstack.api.command.QuotaBalanceCmd; +import org.apache.cloudstack.api.command.QuotaCreditsCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd; +import org.apache.cloudstack.api.command.QuotaStatementCmd; +import org.apache.cloudstack.api.command.QuotaTariffListCmd; +import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd; +import org.apache.cloudstack.api.response.QuotaResponseBuilder; +import org.apache.cloudstack.context.CallContext; +import org.apache.cloudstack.framework.config.ConfigKey; +import org.apache.cloudstack.framework.config.Configurable; +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; +import org.apache.cloudstack.quota.constant.QuotaConfig; +import org.apache.cloudstack.quota.dao.QuotaAccountDao; +import org.apache.cloudstack.quota.dao.QuotaBalanceDao; +import org.apache.cloudstack.quota.dao.QuotaUsageDao; +import org.apache.cloudstack.quota.vo.QuotaAccountVO; +import org.apache.cloudstack.quota.vo.QuotaBalanceVO; +import org.apache.cloudstack.quota.vo.QuotaUsageVO; +import org.apache.cloudstack.utils.usage.UsageUtils; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; + +import javax.ejb.Local; +import javax.inject.Inject; +import javax.naming.ConfigurationException; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; + +@Component +@Local(value = QuotaService.class) +public class QuotaServiceImpl extends ManagerBase implements QuotaService, Configurable, QuotaConfig { + private static final Logger s_logger = Logger.getLogger(QuotaServiceImpl.class.getName()); + + @Inject + private AccountDao _accountDao; + @Inject + private QuotaAccountDao _quotaAcc; + @Inject + private QuotaUsageDao _quotaUsageDao; + @Inject + private DomainDao _domainDao; + @Inject + private ConfigurationDao _configDao; + @Inject + private QuotaBalanceDao _quotaBalanceDao; + @Inject + private QuotaResponseBuilder _respBldr; + + private TimeZone _usageTimezone; + private int _aggregationDuration = 0; + + final static BigDecimal s_hoursInMonth = new BigDecimal(30 * 24); + final static BigDecimal s_minutesInMonth = new BigDecimal(30 * 24 * 60); + final static BigDecimal s_gb = new BigDecimal(1024 * 1024 * 1024); + + public QuotaServiceImpl() { + super(); + } + + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + super.configure(name, params); + String timeZoneStr = _configDao.getValue(Config.UsageAggregationTimezone.toString()); + String aggregationRange = _configDao.getValue(Config.UsageStatsJobAggregationRange.toString()); + if (timeZoneStr == null) { + timeZoneStr = "GMT"; + } + _usageTimezone = TimeZone.getTimeZone(timeZoneStr); + + _aggregationDuration = Integer.parseInt(aggregationRange); + if (_aggregationDuration < UsageUtils.USAGE_AGGREGATION_RANGE_MIN) { + s_logger.warn("Usage stats job aggregation range is to small, using the minimum value of " + UsageUtils.USAGE_AGGREGATION_RANGE_MIN); + _aggregationDuration = UsageUtils.USAGE_AGGREGATION_RANGE_MIN; + } + s_logger.info("Usage timezone = " + _usageTimezone + " AggregationDuration=" + _aggregationDuration); + return true; + } + + @Override + public List> getCommands() { + final List> cmdList = new ArrayList>(); + if (!isQuotaServiceEnabled()) { + return cmdList; + } + cmdList.add(QuotaStatementCmd.class); + cmdList.add(QuotaBalanceCmd.class); + cmdList.add(QuotaTariffListCmd.class); + cmdList.add(QuotaTariffUpdateCmd.class); + cmdList.add(QuotaCreditsCmd.class); + cmdList.add(QuotaEmailTemplateListCmd.class); + cmdList.add(QuotaEmailTemplateUpdateCmd.class); + return cmdList; + } + + @Override + public String getConfigComponentName() { + return "QUOTA-PLUGIN"; + } + + @Override + public ConfigKey[] getConfigKeys() { + return new ConfigKey[] { QuotaPluginEnabled, QuotaEnableEnforcement, QuotaCurrencySymbol, QuotaSmtpHost, QuotaSmtpPort, QuotaSmtpTimeout, QuotaSmtpUser, + QuotaSmtpPassword, QuotaSmtpAuthType, QuotaSmtpSender }; + } + + public Boolean isQuotaServiceEnabled() { + return QuotaPluginEnabled.value(); + } + + @Override + public List findQuotaBalanceVO(Long accountId, String accountName, Long domainId, Date startDate, Date endDate) { + final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); + TransactionLegacy.open(TransactionLegacy.CLOUD_DB).close(); + + Account userAccount = null; + Account caller = CallContext.current().getCallingAccount(); + + // if accountId is not specified, use accountName and domainId + if ((accountId == null) && (accountName != null) && (domainId != null)) { + if (_domainDao.isChildDomain(caller.getDomainId(), domainId)) { + Filter filter = new Filter(AccountVO.class, "id", Boolean.FALSE, null, null); + List accounts = _accountDao.listAccounts(accountName, domainId, filter); + if (accounts.size() > 0) { + userAccount = accounts.get(0); + } + if (userAccount != null) { + accountId = userAccount.getId(); + } else { + throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + } + } else { + throw new PermissionDeniedException("Invalid Domain Id or Account"); + } + } + TransactionLegacy.open(opendb).close(); + + startDate = startDate == null ? new Date() : startDate; + + if (endDate == null) { + // adjust start date to end of day as there is no end date + Date adjustedStartDate = computeAdjustedTime(_respBldr.startOfNextDay(startDate)); + s_logger.debug("getQuotaBalance1: Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", on or before " + adjustedStartDate); + List qbrecords = _quotaBalanceDao.lastQuotaBalanceVO(accountId, domainId, adjustedStartDate); + s_logger.info("Found records size=" + qbrecords.size()); + if (qbrecords.size() == 0) { + throw new InvalidParameterValueException("Incorrect Date there are no quota records before this date " + adjustedStartDate); + } else { + return qbrecords; + } + } else { + Date adjustedStartDate = computeAdjustedTime(startDate); + if (endDate.after(_respBldr.startOfNextDay())) { + throw new InvalidParameterValueException("Incorrect Date Range. End date:" + endDate + " should not be in future. "); + } else if (startDate.before(endDate)) { + Date adjustedEndDate = computeAdjustedTime(endDate); + s_logger.debug("getQuotaBalance2: Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + adjustedEndDate); + List qbrecords = _quotaBalanceDao.findQuotaBalance(accountId, domainId, adjustedStartDate, adjustedEndDate); + s_logger.info("getQuotaBalance3: Found records size=" + qbrecords.size()); + if (qbrecords.size() == 0) { + throw new InvalidParameterValueException("Incorrect Date range there are no quota records between these dates start date " + adjustedStartDate + " and end date:" + endDate); + } else { + return qbrecords; + } + } else { + throw new InvalidParameterValueException("Incorrect Date Range. Start date: " + startDate + " is after end date:" + endDate); + } + } + + } + + @Override + public List getQuotaUsage(Long accountId, String accountName, Long domainId, Integer usageType, Date startDate, Date endDate) { + final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); + TransactionLegacy.open(TransactionLegacy.CLOUD_DB).close(); + Account userAccount = null; + Account caller = CallContext.current().getCallingAccount(); + + // if accountId is not specified, use accountName and domainId + if ((accountId == null) && (accountName != null) && (domainId != null)) { + if (_domainDao.isChildDomain(caller.getDomainId(), domainId)) { + Filter filter = new Filter(AccountVO.class, "id", Boolean.FALSE, null, null); + List accounts = _accountDao.listAccounts(accountName, domainId, filter); + if (accounts.size() > 0) { + userAccount = accounts.get(0); + } + if (userAccount != null) { + accountId = userAccount.getId(); + } else { + throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + } + } else { + throw new PermissionDeniedException("Invalid Domain Id or Account"); + } + } + TransactionLegacy.open(opendb).close(); --- End diff -- LegacyTranscaction is in use in cloud almost everywhere. While this is not a reason to continue its use. My thinking is that this should be delinked from Quota changes and looked at in the right perspective. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---