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 8D4C61087A for ; Tue, 15 Sep 2015 03:24:00 +0000 (UTC) Received: (qmail 95948 invoked by uid 500); 15 Sep 2015 03:23:50 -0000 Delivered-To: apmail-cloudstack-dev-archive@cloudstack.apache.org Received: (qmail 95894 invoked by uid 500); 15 Sep 2015 03:23:50 -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 95883 invoked by uid 99); 15 Sep 2015 03:23:50 -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; Tue, 15 Sep 2015 03:23:50 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 4A8F3E0414; Tue, 15 Sep 2015 03:23:50 +0000 (UTC) From: jburwell To: dev@cloudstack.apache.org Reply-To: dev@cloudstack.apache.org References: In-Reply-To: Subject: [GitHub] cloudstack pull request: Quota Content-Type: text/plain Message-Id: <20150915032350.4A8F3E0414@git1-us-west.apache.org> Date: Tue, 15 Sep 2015 03:23:50 +0000 (UTC) Github user jburwell commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/768#discussion_r39471234 --- Diff: plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java --- @@ -0,0 +1,422 @@ +//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.api.response; + +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.user.Account; +import com.cloud.user.AccountVO; +import com.cloud.user.User; +import com.cloud.user.dao.AccountDao; +import com.cloud.user.dao.UserDao; +import com.cloud.utils.db.TransactionLegacy; + +import org.apache.cloudstack.api.command.QuotaBalanceCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd; +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.quota.QuotaService; +import org.apache.cloudstack.quota.constant.QuotaConfig; +import org.apache.cloudstack.quota.constant.QuotaTypes; +import org.apache.cloudstack.quota.dao.QuotaBalanceDao; +import org.apache.cloudstack.quota.dao.QuotaCreditsDao; +import org.apache.cloudstack.quota.dao.QuotaEmailTemplatesDao; +import org.apache.cloudstack.quota.dao.QuotaTariffDao; +import org.apache.cloudstack.quota.vo.QuotaBalanceVO; +import org.apache.cloudstack.quota.vo.QuotaCreditsVO; +import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO; +import org.apache.cloudstack.quota.vo.QuotaTariffVO; +import org.apache.cloudstack.quota.vo.QuotaUsageVO; +import org.apache.cloudstack.region.RegionManager; +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; + +import javax.ejb.Local; +import javax.inject.Inject; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +@Component +@Local(value = QuotaResponseBuilderImpl.class) +public class QuotaResponseBuilderImpl implements QuotaResponseBuilder { + private static final Logger s_logger = Logger.getLogger(QuotaResponseBuilderImpl.class.getName()); + + @Inject + private QuotaTariffDao _quotaTariffDao; + @Inject + private QuotaBalanceDao _quotaBalanceDao; + @Inject + private QuotaCreditsDao _quotaCreditsDao; + @Inject + private QuotaEmailTemplatesDao _quotaEmailTemplateDao; + + @Inject + private UserDao _userDao; + @Inject + private QuotaService _quotaService; + @Inject + AccountDao _accountDao; + @Inject + private RegionManager _regionMgr; + + @Override + public QuotaTariffResponse createQuotaTariffResponse(QuotaTariffVO tariff) { + final QuotaTariffResponse response = new QuotaTariffResponse(); + response.setUsageType(tariff.getUsageType()); + response.setUsageName(tariff.getUsageName()); + response.setUsageUnit(tariff.getUsageUnit()); + response.setUsageDiscriminator(tariff.getUsageDiscriminator()); + response.setTariffValue(tariff.getCurrencyValue()); + response.setEffectiveOn(tariff.getEffectiveOn()); + response.setDescription(tariff.getDescription()); + response.setCurrency(QuotaConfig.QuotaCurrencySymbol.value()); + return response; + } + + @Override + public QuotaBalanceResponse createQuotaBalanceResponse(List quotaBalance, Date startDate, Date endDate) { + if (quotaBalance == null || quotaBalance.size() == 0) { + new InvalidParameterValueException("The request period does not contain balance entries."); + } + Collections.sort(quotaBalance, new Comparator() { + public int compare(QuotaBalanceVO o1, QuotaBalanceVO o2) { + return o2.getUpdatedOn().compareTo(o1.getUpdatedOn()); // desc + } + }); + + int quota_activity = quotaBalance.size(); + QuotaBalanceResponse resp = new QuotaBalanceResponse(); + BigDecimal lastCredits = new BigDecimal(0); + boolean consecutive = true; + for (Iterator it = quotaBalance.iterator(); it.hasNext();) { + QuotaBalanceVO entry = it.next(); + if (s_logger.isDebugEnabled()){ + s_logger.debug("createQuotaBalanceResponse: Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId()); + } + if (entry.getCreditsId() > 0) { + if (consecutive) { + lastCredits = lastCredits.add(entry.getCreditBalance()); + } + resp.addCredits(entry); + it.remove(); + } else { + consecutive = false; + } + } + + if (quota_activity > 0 && quotaBalance.size() > 0) { + // order is desc last item is the start item + QuotaBalanceVO startItem = quotaBalance.get(quotaBalance.size() - 1); + QuotaBalanceVO endItem = quotaBalance.get(0); + resp.setStartDate(startDate); + resp.setStartQuota(startItem.getCreditBalance()); + resp.setEndDate(endDate); + resp.setEndQuota(endItem.getCreditBalance().add(lastCredits)); + } else if (quota_activity > 0) { + // order is desc last item is the start item + resp.setStartDate(startDate); + resp.setStartQuota(new BigDecimal(0)); + resp.setEndDate(endDate); + resp.setEndQuota(new BigDecimal(0).add(lastCredits)); + } else { + resp.setStartQuota(new BigDecimal(0)); + resp.setEndQuota(new BigDecimal(0)); + } + resp.setCurrency(QuotaConfig.QuotaCurrencySymbol.value()); + resp.setObjectName("balance"); + return resp; + } + + @Override + public QuotaStatementResponse createQuotaStatementResponse(final List quotaUsage) { + if (quotaUsage == null || quotaUsage.size() == 0) { + throw new InvalidParameterValueException("There is no usage data found for period mentioned."); + } + final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); + TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); + QuotaStatementResponse statement = new QuotaStatementResponse(); + + HashMap quotaTariffMap = new HashMap(); + List result = _quotaTariffDao.listAll(); + + for (QuotaTariffVO quotaTariff : result) { + quotaTariffMap.put(quotaTariff.getUsageType(), quotaTariff); + // add dummy record for each usage type + QuotaUsageVO dummy = new QuotaUsageVO(quotaUsage.get(0)); + dummy.setUsageType(quotaTariff.getUsageType()); + dummy.setQuotaUsed(new BigDecimal(0)); + quotaUsage.add(dummy); + } + + Collections.sort(quotaUsage, new Comparator() { + public int compare(QuotaUsageVO o1, QuotaUsageVO o2) { + if (o1.getUsageType() == o2.getUsageType()) + return 0; + return o1.getUsageType() < o2.getUsageType() ? -1 : 1; + } + }); + + List items = new ArrayList(); + QuotaStatementItemResponse lineitem; + int type = -1; + BigDecimal usage = new BigDecimal(0); + BigDecimal totalUsage = new BigDecimal(0); + quotaUsage.add(new QuotaUsageVO());// boundary + QuotaUsageVO prev = quotaUsage.get(0); + if (s_logger.isDebugEnabled()){ + s_logger.debug("createQuotaStatementResponse record count=" + quotaUsage.size()); + } + for (final QuotaUsageVO quotaRecord : quotaUsage) { + if (s_logger.isDebugEnabled()){ + s_logger.debug("createQuotaStatementResponse Type=" + quotaRecord.getUsageType() + " usage=" + usage + " name" + quotaRecord.getUsageItemId()); + } + if (type != quotaRecord.getUsageType()) { + if (type != -1) { + lineitem = new QuotaStatementItemResponse(); + lineitem.setUsageType(type); + lineitem.setQuotaUsed(usage); + lineitem.setAccountId(prev.getAccountId()); + lineitem.setDomainId(prev.getDomainId()); + lineitem.setStartDate(prev.getStartDate()); + lineitem.setEndDate(prev.getEndDate()); + lineitem.setUsageUnit(quotaTariffMap.get(type).getUsageUnit()); + lineitem.setUsageName(quotaTariffMap.get(type).getUsageName()); + lineitem.setObjectName("quotausage"); + items.add(lineitem); + totalUsage = totalUsage.add(usage); + usage = new BigDecimal(0); + } + type = quotaRecord.getUsageType(); + } + prev = quotaRecord; + usage = usage.add(quotaRecord.getQuotaUsed()); + } + TransactionLegacy.open(opendb).close(); + + statement.setLineItem(items); + statement.setTotalQuota(totalUsage); + statement.setCurrency(QuotaConfig.QuotaCurrencySymbol.value()); + statement.setObjectName("statement"); + return statement; + } + + @Override + public List listQuotaTariffPlans(final QuotaTariffListCmd cmd) { + List result = new ArrayList(); + Date effectiveDate = cmd.getEffectiveDate() == null ? new Date() : cmd.getEffectiveDate(); + Date adjustedEffectiveDate = _quotaService.computeAdjustedTime(effectiveDate); + if (s_logger.isDebugEnabled()){ + s_logger.debug("Effective datec=" + effectiveDate + " quotatype=" + cmd.getUsageType() + " Adjusted date=" + adjustedEffectiveDate); + } + if (cmd.getUsageType() != null) { + QuotaTariffVO tariffPlan = _quotaTariffDao.findTariffPlanByUsageType(cmd.getUsageType(), adjustedEffectiveDate); + if (tariffPlan != null) { + result.add(tariffPlan); + } + } else { + result = _quotaTariffDao.listAllTariffPlans(adjustedEffectiveDate); + } + return result; + } + + @Override + public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) { + final int quotaType = cmd.getUsageType(); + final BigDecimal quotaCost = new BigDecimal(cmd.getValue()); + final Date effectiveDate = _quotaService.computeAdjustedTime(cmd.getStartDate()); + final Date now = _quotaService.computeAdjustedTime(new Date()); + // if effective date is in the past return error + if (effectiveDate.compareTo(now) < 0) { + throw new InvalidParameterValueException("Incorrect effective date for tariff " + effectiveDate + " is less than now " + now); + } + QuotaTypes quotaConstant = QuotaTypes.listQuotaTypes().get(quotaType); + if (quotaConstant == null) { + throw new InvalidParameterValueException("Quota type does not exists " + quotaType); + } + final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); + TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); + QuotaTariffVO result = null; + try { + result = new QuotaTariffVO(); + result.setUsageType(quotaType); + result.setUsageName(quotaConstant.getQuotaName()); + result.setUsageUnit(quotaConstant.getQuotaUnit()); + result.setUsageDiscriminator(quotaConstant.getDiscriminator()); + result.setCurrencyValue(quotaCost); + result.setEffectiveOn(effectiveDate); + result.setUpdatedOn(now); + result.setUpdatedBy(cmd.getEntityOwnerId()); + + s_logger.debug(String.format("Updating Quota Tariff Plan: New value=%s for resource type=%d effective on date=%s", quotaCost, quotaType, effectiveDate)); --- End diff -- This ``debug`` statement should be wrapped in an ``if (s_logger.isDebugEnabled) { }`` block. --- 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. ---