Example usage for org.joda.time DateTime getWeekOfWeekyear

List of usage examples for org.joda.time DateTime getWeekOfWeekyear

Introduction

In this page you can find the example usage for org.joda.time DateTime getWeekOfWeekyear.

Prototype

public int getWeekOfWeekyear() 

Source Link

Document

Get the week of weekyear field value.

Usage

From source file:org.apache.phoenix.expression.function.WeekFunction.java

License:Apache License

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression expression = getChildExpression();
    if (!expression.evaluate(tuple, ptr)) {
        return false;
    }/*from ww  w  .  j  ava2s  . c o m*/
    if (ptr.getLength() == 0) {
        return true; //means null
    }
    long dateTime = inputCodec.decodeLong(ptr, expression.getSortOrder());
    DateTime dt = new DateTime(dateTime);
    int week = dt.getWeekOfWeekyear();
    PDataType returnType = getDataType();
    byte[] byteValue = new byte[returnType.getByteSize()];
    returnType.getCodec().encodeInt(week, byteValue, 0);
    ptr.set(byteValue);
    return true;
}

From source file:org.entcore.feeder.timetable.udt.UDTImporter.java

License:Open Source License

void initSchoolYear(JsonObject currentEntity) {
    startDateWeek1 = startDateStudents//w  w  w.  j  a  va2  s . c o  m
            .withWeekOfWeekyear(Integer.parseInt(currentEntity.getString("premiere_semaine_ISO")))
            .withDayOfWeek(1);
    slotDuration = Integer.parseInt(currentEntity.getString("duree_seq")) / 2;
    DateTime h = startDateWeek1;
    while (h.isBefore(startDateStudents)) {
        holidays.add(h);
        h = h.plusDays(1);
        holidaysWeeks.add(h.getWeekOfWeekyear());
    }
}

From source file:org.entcore.feeder.timetable.udt.UDTImporter.java

License:Open Source License

void initHolidays(JsonObject currentEntity) {
    DateTime s = DateTime.parse(currentEntity.getString("debut"), DateTimeFormat.forPattern(DATE_FORMAT));
    DateTime e = DateTime.parse(currentEntity.getString("fin"), DateTimeFormat.forPattern(DATE_FORMAT));
    while (s.isBefore(e)) {
        holidays.add(s);//ww  w  .  ja  va  2s.  co m
        s = s.plusDays(1);
        holidaysWeeks.add(s.getWeekOfWeekyear());
    }
    holidays.add(e);
}

From source file:org.hawkular.metrics.core.impl.DateTimeService.java

License:Apache License

public DateTime getTimeSlice(DateTime dt, Duration duration) {
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }//from  ww w  . j ava 2  s. co  m
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.hawkular.metrics.datetime.DateTimeService.java

License:Apache License

public static DateTime getTimeSlice(DateTime dt, Duration duration) {
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }// w  w w.ja v  a  2 s .  c  o m
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.hawkular.metrics.tasks.api.AbstractTrigger.java

License:Apache License

protected DateTime getExecutionTime(long time, Duration duration) {
    DateTime dt = new DateTime(time);
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }//w  w  w . ja  v  a  2 s  .c om
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.jimcat.gui.histogram.image.DateTakenDimension.java

License:Open Source License

/**
 * get label text for given index/*from ww  w  . j  a  v  a2 s  . c  o m*/
 * 
 * @param index
 * @return the label text for the given index
 */
private String getWeekLabel(int index) {
    DateTime date = indexToDate(WEEKS, index);
    int week = date.getWeekOfWeekyear();
    if (week == 1 || week % 10 == 0) {
        return WEEK_LABEL_FORMATTER.print(date);
    }
    return null;
}

From source file:org.jimcat.gui.histogram.image.DateTakenDimension.java

License:Open Source License

/**
 * get mark for index/*from  w  ww  .j  a va2 s  .  c  om*/
 * 
 * @see org.jimcat.gui.histogram.image.Dimension#getMarkFor(int, int)
 */
@Override
public ScaleMark getMarkFor(int resolution, int index) {
    ScaleMark result = ScaleMark.SMALL;

    // get corresponding date
    DateTime date = indexToDate(resolution, index);
    if (date == null) {
        // no data available
        return ScaleMark.NONE;
    }

    if (resolution == DAYS) {
        int day = date.getDayOfMonth();
        if (day == 1 || day == 15) {
            result = ScaleMark.LABEL;
        }
    } else if (resolution == WEEKS) {
        int week = date.getWeekOfWeekyear();
        if (week == 1 || week % 10 == 0) {
            result = ScaleMark.LABEL;
        }
    } else if (resolution == MONTHS) {
        int month = date.getMonthOfYear();
        if (month % 3 == 0) {
            result = ScaleMark.LABEL;
        }
    }

    return result;
}

From source file:org.jmxtrans.embedded.samples.graphite.CocktailAppMetricsSimulator.java

License:Open Source License

public void generateLoad(GraphiteDataInjector graphiteDataInjector) throws Exception {

    TimeSeries rawIntegratedTimeSeries = new TimeSeries("sales.integrated.raw");
    TimeSeries rawTimeSeries = new TimeSeries("sales.raw");

    DateTime now = new DateTime();
    DateTime end = now.plusDays(3);/*from  ww w  .  j a  v a 2 s.c om*/

    DateTime date = now.minusDays(15);
    DateTime twoDaysAfterBegin = date.plusDays(2);
    double serverFairness = 1.05;

    int integratedValue = 0;

    MathContext mathContext = new MathContext(1, RoundingMode.CEILING);

    int randomFactor = 0;

    while (date.isBefore(end)) {
        if (rawIntegratedTimeSeries.getItemCount() % 120 == 0) {
            randomFactor = 10 + random.nextInt(2);
        }
        int weekGrowthFactor = 6 - (now.getWeekOfWeekyear() - date.getWeekOfWeekyear());
        int value = new BigDecimal(randomFactor) // random factor
                .multiply(new BigDecimal(10)) // go to cents of USD
                .multiply(new BigDecimal(weekGrowthFactor))
                .multiply(new BigDecimal(hourlyDistribution[date.getHourOfDay()]))
                .multiply(new BigDecimal(weeklyDistribution[date.getDayOfWeek()]))
                .divide(new BigDecimal(20), mathContext).intValue(); // split hourly value in minutes

        integratedValue += value;
        for (int i1 = 0; i1 < 3; i1++) {
            Second period = new Second(date.toDate());
            rawTimeSeries.add(period, value);
            rawIntegratedTimeSeries.add(period, integratedValue);
            date = date.plusSeconds(30);
        }
    }

    rawIntegratedTimeSeries = MovingAverage.createMovingAverage(rawIntegratedTimeSeries,
            rawIntegratedTimeSeries.getKey().toString(), 60 * 7, 0);
    rawTimeSeries = MovingAverage.createMovingAverage(rawTimeSeries, rawTimeSeries.getKey().toString(), 60 * 7,
            0);

    // SALES - REVENUE

    TimeSeries salesRevenueInCentsCounter = new TimeSeries("sales.revenueInCentsCounter");
    TimeSeries salesRevenueInCentsCounterSrv1 = new TimeSeries("srv1.sales.revenueInCentsCounter");
    TimeSeries salesRevenueInCentsCounterSrv2 = new TimeSeries("srv2.sales.revenueInCentsCounter");
    int resetValue2ToZeroOffset = 0; // reset value 2 after 3 days of metrics
    for (int i = 0; i < rawIntegratedTimeSeries.getItemCount(); i++) {
        TimeSeriesDataItem dataItem = rawIntegratedTimeSeries.getDataItem(i);
        int value = dataItem.getValue().intValue();
        // value1 is 5% higher to value2 due to a 'weirdness' in the load balancing
        int value1 = Math.min((int) (value * serverFairness / 2), value);

        {
            // simulate srv2 restart
            DateTime currentDate = new DateTime(dataItem.getPeriod().getStart());
            boolean shouldResetValue2 = resetValue2ToZeroOffset == 0
                    && currentDate.getDayOfYear() == twoDaysAfterBegin.getDayOfYear();
            if (shouldResetValue2) {
                resetValue2ToZeroOffset = value - value1;
                System.out.println("reset value2 of " + resetValue2ToZeroOffset + " at " + currentDate);
            }
        }

        int value2 = value - value1 - resetValue2ToZeroOffset;
        salesRevenueInCentsCounter.add(dataItem.getPeriod(), value);
        salesRevenueInCentsCounterSrv1.add(dataItem.getPeriod(), value1);
        salesRevenueInCentsCounterSrv2.add(dataItem.getPeriod(), value2);
    }
    graphiteDataInjector.exportMetrics(salesRevenueInCentsCounter, salesRevenueInCentsCounterSrv1,
            salesRevenueInCentsCounterSrv2);

    // SALES - ITEMS
    TimeSeries salesItemsCounter = new TimeSeries("sales.itemsCounter");
    TimeSeries salesItemsCounterSrv1 = new TimeSeries("srv1.sales.itemsCounter");
    TimeSeries salesItemsCounterSrv2 = new TimeSeries("srv2.sales.itemsCounter");

    for (int i = 0; i < rawIntegratedTimeSeries.getItemCount(); i++) {
        RegularTimePeriod period = salesRevenueInCentsCounter.getDataItem(i).getPeriod();
        int ordersPriceInCents1 = salesRevenueInCentsCounterSrv1.getDataItem(i).getValue().intValue();
        int ordersPriceInCents2 = salesRevenueInCentsCounterSrv2.getDataItem(i).getValue().intValue();

        int value1 = ordersPriceInCents1 / 600;
        int value2 = ordersPriceInCents2 / 600;

        salesItemsCounter.add(period, value1 + value2);
        salesItemsCounterSrv1.add(period, value1);
        salesItemsCounterSrv2.add(period, value2);

    }

    graphiteDataInjector.exportMetrics(salesItemsCounter, salesItemsCounterSrv1, salesItemsCounterSrv2);

    // WEBSITE - VISITORS
    TimeSeries newVisitorsCounterSrv1 = new TimeSeries("srv1.website.visitors.newVisitorsCounter");
    TimeSeries newVisitorsCounterSrv2 = new TimeSeries("srv1.website.visitors.newVisitorsCounter");

    TimeSeries activeVisitorsGaugeSrv1 = new TimeSeries("srv1.website.visitors.activeGauge");
    TimeSeries activeVisitorsGaugeSrv2 = new TimeSeries("srv2.website.visitors.activeGauge");
    int integratedValue1 = 0;
    int integratedValue2 = 0;
    float activeVisitorsFactor = 1;
    for (int i = 0; i < rawTimeSeries.getItemCount(); i++) {

        TimeSeriesDataItem dataItem = rawTimeSeries.getDataItem(i);
        RegularTimePeriod period = dataItem.getPeriod();
        int value = dataItem.getValue().intValue() / 20;
        integratedValue += value;

        int value1 = Math.min((int) (value * serverFairness / 2), value);
        integratedValue1 += value1;

        int value2 = value - value1;
        integratedValue2 += value2;

        newVisitorsCounterSrv1.add(period, integratedValue1);
        newVisitorsCounterSrv2.add(period, integratedValue2);

        if (i % 120 == 0) {
            activeVisitorsFactor = (10 + random.nextInt(3)) / 10;
        }

        activeVisitorsGaugeSrv1.add(period, Math.floor(value1 * activeVisitorsFactor));
        activeVisitorsGaugeSrv2.add(period, Math.floor(value2 * activeVisitorsFactor));
    }

    graphiteDataInjector.exportMetrics(newVisitorsCounterSrv1, newVisitorsCounterSrv2, activeVisitorsGaugeSrv1,
            activeVisitorsGaugeSrv2);

}

From source file:org.jmxtrans.samples.graphite.GraphiteDataInjector.java

License:Open Source License

public void generateLoad() throws Exception {
    System.out.println("Inject data on Graphite server " + this.graphiteHost);

    TimeSeries timeSeries = new TimeSeries("shopping-cart.raw");

    DateTime now = new DateTime();
    DateTime end = now.plusDays(1);//from   w w  w. j  a v  a2s  .  co m

    DateTime date = now.minusDays(15);
    DateTime twoDaysAfterBegin = date.plusDays(2);

    int integratedValue = 0;

    MathContext mathContext = new MathContext(1, RoundingMode.CEILING);

    int randomFactor = 0;

    while (date.isBefore(end)) {
        if (timeSeries.getItemCount() % 120 == 0) {
            randomFactor = 10 + random.nextInt(2);
        }
        int weekGrowthFactor = 6 - (now.getWeekOfWeekyear() - date.getWeekOfWeekyear());
        int value = new BigDecimal(randomFactor) // random factor
                .multiply(new BigDecimal(10)) // go to cents of USD
                .multiply(new BigDecimal(weekGrowthFactor))
                .multiply(new BigDecimal(hourlyDistribution[date.getHourOfDay()]))
                .multiply(new BigDecimal(weeklyDistribution[date.getDayOfWeek()]))
                .divide(new BigDecimal(20), mathContext).intValue(); // split hourly value in minutes

        integratedValue += value;
        for (int i1 = 0; i1 < 3; i1++) {
            timeSeries.add(new Minute(date.toDate()), integratedValue);
            date = date.plusMinutes(1);
        }
    }

    TimeSeries ordersPriceInCentsTimeSeries = MovingAverage.createMovingAverage(timeSeries,
            "shopping-cart.OrdersPriceInCents", 60 * 7, 0);

    TimeSeries ordersPriceInCentsSrv1TimeSeries = new TimeSeries("srv1.shopping-cart.OrdersPriceInCents");
    TimeSeries ordersPriceInCentsSrv2TimeSeries = new TimeSeries("srv2.shopping-cart.OrdersPriceInCents");
    int resetValue2ToZeroOffset = 0; // reset value 2 after 3 days of metrics
    for (int i = 0; i < ordersPriceInCentsTimeSeries.getItemCount(); i++) {
        TimeSeriesDataItem dataItem = ordersPriceInCentsTimeSeries.getDataItem(i);
        int value = dataItem.getValue().intValue();
        // value1 is 5% higher to value2 due to a 'weirdness' in the load balancing
        int value1 = Math.min((int) (value * 1.05 / 2), value);

        {
            // simulate srv2 restart
            DateTime currentDate = new DateTime(dataItem.getPeriod().getStart());
            boolean shouldResetValue2 = resetValue2ToZeroOffset == 0
                    && currentDate.getDayOfYear() == twoDaysAfterBegin.getDayOfYear();
            if (shouldResetValue2) {
                resetValue2ToZeroOffset = value - value1;
                System.out.println("reset value2 of " + resetValue2ToZeroOffset + " at " + currentDate);
            }
        }

        int value2 = value - value1 - resetValue2ToZeroOffset;
        // System.out.println("value=" + value + ", value1=" + value1 + ", value2=" + value2);
        ordersPriceInCentsSrv1TimeSeries.add(dataItem.getPeriod(), value1);
        ordersPriceInCentsSrv2TimeSeries.add(dataItem.getPeriod(), value2);
    }

    TimeSeries orderItemsCountTimeSeries = new TimeSeries("shopping-cart.OrderItemsCount");
    TimeSeries orderItemsCountSrv1TimeSeries = new TimeSeries("srv1.shopping-cart.OrderItemsCount");
    TimeSeries orderItemsCountSrv2TimeSeries = new TimeSeries("srv2.shopping-cart.OrderItemsCount");

    for (int i = 0; i < ordersPriceInCentsTimeSeries.getItemCount(); i++) {
        RegularTimePeriod period = ordersPriceInCentsTimeSeries.getDataItem(i).getPeriod();
        int ordersPriceInCents1 = ordersPriceInCentsSrv1TimeSeries.getDataItem(i).getValue().intValue();
        int ordersPriceInCents2 = ordersPriceInCentsSrv2TimeSeries.getDataItem(i).getValue().intValue();

        int value1 = ordersPriceInCents1 / 600;
        int value2 = ordersPriceInCents2 / 600;

        orderItemsCountTimeSeries.add(period, value1 + value2);
        orderItemsCountSrv1TimeSeries.add(period, value1);
        orderItemsCountSrv2TimeSeries.add(period, value2);

    }

    exportMetrics(ordersPriceInCentsTimeSeries, ordersPriceInCentsSrv1TimeSeries,
            ordersPriceInCentsSrv2TimeSeries, ordersPriceInCentsTimeSeries, orderItemsCountTimeSeries,
            orderItemsCountSrv1TimeSeries, orderItemsCountSrv2TimeSeries);

    TimeSeries activeSrv1Visitors = new TimeSeries("srv1.visitors.currentActive");
    TimeSeries activeSrv2Visitors = new TimeSeries("srv1.visitors.currentActive");

}