Example usage for org.joda.time DateTimeZone UTC

List of usage examples for org.joda.time DateTimeZone UTC

Introduction

In this page you can find the example usage for org.joda.time DateTimeZone UTC.

Prototype

DateTimeZone UTC

To view the source code for org.joda.time DateTimeZone UTC.

Click Source Link

Document

The time zone for Universal Coordinated Time

Usage

From source file:com.netflix.ice.basic.BasicWeeklyCostEmailService.java

License:Apache License

private void sendEmail(boolean test, AmazonSimpleEmailServiceClient emailService, String email,
        List<ApplicationGroup> appGroups) throws IOException, MessagingException {

    StringBuilder body = new StringBuilder();
    body.append(//from   w  w w .ja  v  a2s  .  c o m
            "<html><head><style type=\"text/css\">a:link, a:visited{color:#006DBA;}a:link, a:visited, a:hover {\n"
                    + "text-decoration: none;\n" + "}\n" + "body {\n" + "color: #333;\n" + "}"
                    + "</style></head>");
    List<MimeBodyPart> mimeBodyParts = Lists.newArrayList();
    int index = 0;
    String subject = "";
    for (ApplicationGroup appGroup : appGroups) {
        boolean hasData = false;
        for (String prodName : appGroup.data.keySet()) {
            if (config.productService.getProductByName(prodName) == null)
                continue;
            hasData = appGroup.data.get(prodName) != null && appGroup.data.get(prodName).size() > 0;
            if (hasData)
                break;
        }
        if (!hasData)
            continue;

        try {
            MimeBodyPart mimeBodyPart = constructEmail(index, appGroup, body);
            index++;
            if (mimeBodyPart != null) {
                mimeBodyParts.add(mimeBodyPart);
                subject = subject + (subject.length() > 0 ? ", " : "") + appGroup.getDisplayName();
            }
        } catch (Exception e) {
            logger.error("Error contructing email", e);
        }
    }
    body.append("</html>");

    if (mimeBodyParts.size() == 0)
        return;

    DateTime end = new DateTime(DateTimeZone.UTC).withDayOfWeek(1).withMillisOfDay(0);
    subject = String.format("%s Weekly AWS Costs (%s - %s)", subject, formatter.print(end.minusWeeks(1)),
            formatter.print(end));
    String toEmail = test ? testEmail : email;
    Session session = Session.getInstance(new Properties());
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setSubject(subject);
    mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, toEmail);
    if (!test && !StringUtils.isEmpty(bccEmail)) {
        mimeMessage.addRecipients(Message.RecipientType.BCC, bccEmail);
    }
    MimeMultipart mimeMultipart = new MimeMultipart();
    BodyPart p = new MimeBodyPart();
    p.setContent(body.toString(), "text/html");
    mimeMultipart.addBodyPart(p);

    for (MimeBodyPart mimeBodyPart : mimeBodyParts)
        mimeMultipart.addBodyPart(mimeBodyPart);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    mimeMessage.setContent(mimeMultipart);
    mimeMessage.writeTo(outputStream);
    RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));

    SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
    rawEmailRequest.setDestinations(Lists.<String>newArrayList(toEmail));
    rawEmailRequest.setSource(fromEmail);
    logger.info("sending email to " + toEmail + " " + body.toString());
    emailService.sendRawEmail(rawEmailRequest);
}

From source file:com.netflix.ice.common.Config.java

License:Apache License

/**
 *
 * @param properties (required)//w ww . j av a  2 s .c o m
 * @param credentialsProvider (required)
 * @param accountService (required)
 * @param productService (required)
 * @param resourceService (optional)
 */
public Config(Properties properties, AWSCredentialsProvider credentialsProvider, AccountService accountService,
        ProductService productService, ResourceService resourceService) {
    if (properties.getProperty(IceOptions.START_MILLIS) == null)
        throw new IllegalArgumentException("IceOptions.START_MILLIS must be specified");
    if (properties == null)
        throw new IllegalArgumentException("properties must be specified");
    if (credentialsProvider == null)
        throw new IllegalArgumentException("credentialsProvider must be specified");
    if (accountService == null)
        throw new IllegalArgumentException("accountService must be specified");
    if (productService == null)
        throw new IllegalArgumentException("productService must be specified");

    DateTime startDate = new DateTime(Long.parseLong(properties.getProperty(IceOptions.START_MILLIS)),
            DateTimeZone.UTC);
    workS3BucketName = properties.getProperty(IceOptions.WORK_S3_BUCKET_NAME);
    workS3BucketPrefix = properties.getProperty(IceOptions.WORK_S3_BUCKET_PREFIX, "ice/");
    localDir = properties.getProperty(IceOptions.LOCAL_DIR, "/mnt/ice");

    if (workS3BucketName == null)
        throw new IllegalArgumentException("IceOptions.WORK_S3_BUCKET_NAME must be specified");

    this.credentialsProvider = credentialsProvider;
    this.startDate = startDate;
    this.accountService = accountService;
    this.productService = productService;
    this.resourceService = resourceService;

    AwsUtils.init(credentialsProvider);
}

From source file:com.netflix.ice.processor.BillingFileProcessor.java

License:Apache License

@Override
protected void poll() throws Exception {

    TreeMap<DateTime, List<BillingFile>> filesToProcess = Maps.newTreeMap();
    Map<DateTime, List<BillingFile>> monitorFilesToProcess = Maps.newTreeMap();

    // list the tar.gz file in billing file folder
    for (int i = 0; i < config.billingS3BucketNames.length; i++) {
        String billingS3BucketName = config.billingS3BucketNames[i];
        String billingS3BucketPrefix = config.billingS3BucketPrefixes.length > i
                ? config.billingS3BucketPrefixes[i]
                : "";
        String accountId = config.billingAccountIds.length > i ? config.billingAccountIds[i] : "";
        String billingAccessRoleName = config.billingAccessRoleNames.length > i
                ? config.billingAccessRoleNames[i]
                : "";
        String billingAccessExternalId = config.billingAccessExternalIds.length > i
                ? config.billingAccessExternalIds[i]
                : "";

        logger.info("trying to list objects in billing bucket " + billingS3BucketName
                + " using assume role, and external id " + billingAccessRoleName + " "
                + billingAccessExternalId);
        List<S3ObjectSummary> objectSummaries = AwsUtils.listAllObjects(billingS3BucketName,
                billingS3BucketPrefix, accountId, billingAccessRoleName, billingAccessExternalId);
        logger.info("found " + objectSummaries.size() + " in billing bucket " + billingS3BucketName);
        TreeMap<DateTime, S3ObjectSummary> filesToProcessInOneBucket = Maps.newTreeMap();
        Map<DateTime, S3ObjectSummary> monitorFilesToProcessInOneBucket = Maps.newTreeMap();

        // for each file, download&process if not needed
        for (S3ObjectSummary objectSummary : objectSummaries) {

            String fileKey = objectSummary.getKey();
            DateTime dataTime = AwsUtils.getDateTimeFromFileNameWithTags(fileKey);
            boolean withTags = true;
            if (dataTime == null) {
                dataTime = AwsUtils.getDateTimeFromFileName(fileKey);
                withTags = false;/*  w  w w . j  a va2s. c o m*/
            }

            if (dataTime != null && !dataTime.isBefore(config.startDate)) {
                if (!filesToProcessInOneBucket.containsKey(dataTime)
                        || withTags && config.resourceService != null
                        || !withTags && config.resourceService == null)
                    filesToProcessInOneBucket.put(dataTime, objectSummary);
                else
                    logger.info("ignoring file " + objectSummary.getKey());
            } else {
                logger.info("ignoring file " + objectSummary.getKey());
            }
        }

        for (S3ObjectSummary objectSummary : objectSummaries) {
            String fileKey = objectSummary.getKey();
            DateTime dataTime = AwsUtils.getDateTimeFromFileNameWithMonitoring(fileKey);

            if (dataTime != null && !dataTime.isBefore(config.startDate)) {
                monitorFilesToProcessInOneBucket.put(dataTime, objectSummary);
            }
        }

        for (DateTime key : filesToProcessInOneBucket.keySet()) {
            List<BillingFile> list = filesToProcess.get(key);
            if (list == null) {
                list = Lists.newArrayList();
                filesToProcess.put(key, list);
            }
            list.add(new BillingFile(filesToProcessInOneBucket.get(key), accountId, billingAccessRoleName,
                    billingAccessExternalId, billingS3BucketPrefix));
        }

        for (DateTime key : monitorFilesToProcessInOneBucket.keySet()) {
            List<BillingFile> list = monitorFilesToProcess.get(key);
            if (list == null) {
                list = Lists.newArrayList();
                monitorFilesToProcess.put(key, list);
            }
            list.add(new BillingFile(monitorFilesToProcessInOneBucket.get(key), accountId,
                    billingAccessRoleName, billingAccessExternalId, billingS3BucketPrefix));
        }
    }

    for (DateTime dataTime : filesToProcess.keySet()) {
        startMilli = endMilli = dataTime.getMillis();
        init();

        boolean hasNewFiles = false;
        boolean hasTags = false;
        long lastProcessed = lastProcessTime(AwsUtils.monthDateFormat.print(dataTime));

        for (BillingFile billingFile : filesToProcess.get(dataTime)) {
            S3ObjectSummary objectSummary = billingFile.s3ObjectSummary;
            if (objectSummary.getLastModified().getTime() < lastProcessed) {
                logger.info("data has been processed. ignoring " + objectSummary.getKey() + "...");
                continue;
            }
            hasNewFiles = true;
        }

        if (!hasNewFiles) {
            logger.info("data has been processed. ignoring all files at "
                    + AwsUtils.monthDateFormat.print(dataTime));
            continue;
        }

        long processTime = new DateTime(DateTimeZone.UTC).getMillis();
        for (BillingFile billingFile : filesToProcess.get(dataTime)) {

            S3ObjectSummary objectSummary = billingFile.s3ObjectSummary;
            String fileKey = objectSummary.getKey();

            File file = new File(config.localDir, fileKey.substring(billingFile.prefix.length()));
            logger.info("trying to download " + fileKey + "...");
            boolean downloaded = AwsUtils.downloadFileIfChangedSince(objectSummary.getBucketName(),
                    billingFile.prefix, file, lastProcessed, billingFile.accountId, billingFile.accessRoleName,
                    billingFile.externalId);
            if (downloaded)
                logger.info("downloaded " + fileKey);
            else {
                logger.info("file already downloaded " + fileKey + "...");
            }

            logger.info("processing " + fileKey + "...");
            boolean withTags = fileKey.contains("with-resources-and-tags");
            hasTags = hasTags || withTags;
            processingMonitor = false;
            processBillingZipFile(file, withTags);
            logger.info("done processing " + fileKey);
        }

        if (monitorFilesToProcess.get(dataTime) != null) {
            for (BillingFile monitorBillingFile : monitorFilesToProcess.get(dataTime)) {

                S3ObjectSummary monitorObjectSummary = monitorBillingFile.s3ObjectSummary;
                if (monitorObjectSummary != null) {
                    String monitorFileKey = monitorObjectSummary.getKey();
                    logger.info("processing " + monitorFileKey + "...");
                    File monitorFile = new File(config.localDir,
                            monitorFileKey.substring(monitorFileKey.lastIndexOf("/") + 1));
                    logger.info("trying to download " + monitorFileKey + "...");
                    boolean downloaded = AwsUtils.downloadFileIfChangedSince(
                            monitorObjectSummary.getBucketName(), monitorBillingFile.prefix, monitorFile,
                            lastProcessed, monitorBillingFile.accountId, monitorBillingFile.accessRoleName,
                            monitorBillingFile.externalId);
                    if (downloaded)
                        logger.info("downloaded " + monitorFile);
                    else
                        logger.warn(monitorFile + "already downloaded...");
                    FileInputStream in = new FileInputStream(monitorFile);
                    try {
                        processingMonitor = true;
                        processBillingFile(monitorFile.getName(), in, true);
                    } catch (Exception e) {
                        logger.error("Error processing " + monitorFile, e);
                    } finally {
                        in.close();
                    }
                }
            }
        }

        if (dataTime.equals(filesToProcess.lastKey())) {
            int hours = (int) ((endMilli - startMilli) / 3600000L);
            logger.info("cut hours to " + hours);
            cutData(hours);
        }

        // now get reservation capacity to calculate upfront and un-used cost
        for (Ec2InstanceReservationPrice.ReservationUtilization utilization : Ec2InstanceReservationPrice.ReservationUtilization
                .values())
            processReservations(utilization);

        if (hasTags && config.resourceService != null)
            config.resourceService.commit();

        logger.info("archiving results for " + dataTime + "...");
        archive();
        logger.info("done archiving " + dataTime);

        updateProcessTime(AwsUtils.monthDateFormat.print(dataTime), processTime);
        if (dataTime.equals(filesToProcess.lastKey())) {
            sendOndemandCostAlert();
        }
    }

    logger.info("AWS usage processed.");
}

From source file:com.netflix.ice.processor.BillingFileProcessor.java

License:Apache License

private void archiveHourly(Map<Product, ReadWriteData> dataMap, String prefix) throws Exception {
    DateTime monthDateTime = new DateTime(startMilli, DateTimeZone.UTC);
    for (Product product : dataMap.keySet()) {
        String prodName = product == null ? "all" : product.name;
        DataWriter writer = new DataWriter(
                prefix + "hourly_" + prodName + "_" + AwsUtils.monthDateFormat.print(monthDateTime), false);
        writer.archive(dataMap.get(product));
    }//w w w .j  a v  a  2s .  c  o m
}

From source file:com.netflix.ice.processor.BillingFileProcessor.java

License:Apache License

private void archiveSummary(Map<Product, ReadWriteData> dataMap, String prefix) throws Exception {

    DateTime monthDateTime = new DateTime(startMilli, DateTimeZone.UTC);

    for (Product product : dataMap.keySet()) {

        String prodName = product == null ? "all" : product.name;
        ReadWriteData data = dataMap.get(product);
        Collection<TagGroup> tagGroups = data.getTagGroups();

        // init daily, weekly and monthly
        List<Map<TagGroup, Double>> daily = Lists.newArrayList();
        List<Map<TagGroup, Double>> weekly = Lists.newArrayList();
        List<Map<TagGroup, Double>> monthly = Lists.newArrayList();

        // get last month data
        ReadWriteData lastMonthData = new DataWriter(prefix + "hourly_" + prodName + "_"
                + AwsUtils.monthDateFormat.print(monthDateTime.minusMonths(1)), true).getData();

        // aggregate to daily, weekly and monthly
        int dayOfWeek = monthDateTime.getDayOfWeek();
        int daysFromLastMonth = dayOfWeek - 1;
        int lastMonthNumHours = monthDateTime.minusMonths(1).dayOfMonth().getMaximumValue() * 24;
        for (int hour = 0 - daysFromLastMonth * 24; hour < data.getNum(); hour++) {
            if (hour < 0) {
                // handle data from last month, add to weekly
                Map<TagGroup, Double> prevData = lastMonthData.getData(lastMonthNumHours + hour);
                for (TagGroup tagGroup : tagGroups) {
                    Double v = prevData.get(tagGroup);
                    if (v != null && v != 0) {
                        addValue(weekly, 0, tagGroup, v);
                    }//from ww  w  .  j av a2 s.  c  o  m
                }
            } else {
                // this month, add to weekly, monthly and daily
                Map<TagGroup, Double> map = data.getData(hour);

                for (TagGroup tagGroup : tagGroups) {
                    Double v = map.get(tagGroup);
                    if (v != null && v != 0) {
                        addValue(monthly, 0, tagGroup, v);
                        addValue(daily, hour / 24, tagGroup, v);
                        addValue(weekly, (hour + daysFromLastMonth * 24) / 24 / 7, tagGroup, v);
                    }
                }
            }
        }

        // archive daily
        int year = monthDateTime.getYear();
        DataWriter writer = new DataWriter(prefix + "daily_" + prodName + "_" + year, true);
        ReadWriteData dailyData = writer.getData();
        dailyData.setData(daily, monthDateTime.getDayOfYear() - 1, false);
        writer.archive();

        // archive monthly
        writer = new DataWriter(prefix + "monthly_" + prodName, true);
        ReadWriteData monthlyData = writer.getData();
        monthlyData.setData(monthly, Months.monthsBetween(config.startDate, monthDateTime).getMonths(), false);
        writer.archive();

        // archive weekly
        writer = new DataWriter(prefix + "weekly_" + prodName, true);
        ReadWriteData weeklyData = writer.getData();
        DateTime weekStart = monthDateTime.withDayOfWeek(1);
        int index;
        if (!weekStart.isAfter(config.startDate))
            index = 0;
        else
            index = Weeks.weeksBetween(config.startDate, weekStart).getWeeks()
                    + (config.startDate.dayOfWeek() == weekStart.dayOfWeek() ? 0 : 1);
        weeklyData.setData(weekly, index, true);
        writer.archive();
    }
}

From source file:com.netflix.ice.reader.ReaderConfig.java

License:Apache License

public void start() {

    Managers managers = ReaderConfig.getInstance().managers;
    Collection<Product> products = managers.getProducts();
    for (Product product : products) {
        TagGroupManager tagGroupManager = managers.getTagGroupManager(product);
        Interval interval = tagGroupManager.getOverlapInterval(new Interval(
                new DateTime(DateTimeZone.UTC).minusMonths(monthlyCacheSize), new DateTime(DateTimeZone.UTC)));
        if (interval == null)
            continue;
        for (ConsolidateType consolidateType : ConsolidateType.values()) {
            readData(product, managers.getCostManager(product, consolidateType), interval, consolidateType);
            readData(product, managers.getUsageManager(product, consolidateType), interval, consolidateType);
        }/*ww w  .  j a  v  a 2  s . co m*/
    }

    if (costEmailService != null)
        costEmailService.start();
}

From source file:com.netflix.iep.config.Strings.java

License:Apache License

public static DateTime parseDate(String v) {
    return parseDate(v, DateTimeZone.UTC);
}

From source file:com.netflix.iep.config.Strings.java

License:Apache License

public static DateTime parseDate(DateTime ref, String v, DateTimeZone tz) {
    Matcher m = IsoDate.matcher(v);
    if (m.matches())
        return isoDateFmt.withZone(tz).parseDateTime(m.group(1));
    m = RelativeDate.matcher(v);//from   w w  w.j  a  v  a2  s .c o m
    if (m.matches()) {
        String r = m.group(1);
        String op = m.group(2);
        String p = m.group(3);
        if (op.equals("-"))
            return parseRefVar(ref, r).minus(parsePeriod(p));
        else if (op.equals("+"))
            return parseRefVar(ref, r).plus(parsePeriod(p));
        else
            throw new IllegalArgumentException("invalid operation " + op);
    }
    m = NamedDate.matcher(v);
    if (m.matches())
        return parseRefVar(ref, m.group(1));
    m = UnixDate.matcher(v);
    if (m.matches())
        return new DateTime(Long.valueOf(m.group(1)) * 1000, DateTimeZone.UTC);
    throw new IllegalArgumentException("invalid date " + v);
}

From source file:com.netflix.raigad.backup.S3RepositorySettingsParams.java

License:Apache License

public String getS3RepositoryName() {
    DateTime dt = new DateTime();
    DateTime dtGmt = dt.withZone(DateTimeZone.UTC);
    return formatDate(dtGmt, S3_REPO_DATE_FORMAT);
}

From source file:com.nfsdb.journal.utils.Dates.java

License:Apache License

public static DateTime utc(int year, int month, int day, int hour, int minute) {
    return new DateTime(year, month, day, hour, minute, DateTimeZone.UTC);
}