Example usage for org.joda.time Duration Duration

List of usage examples for org.joda.time Duration Duration

Introduction

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

Prototype

public Duration(ReadableInstant start, ReadableInstant end) 

Source Link

Document

Creates a duration from the given interval endpoints.

Usage

From source file:es.jpons.persistence.TemporalPersistenceManager.java

License:Open Source License

/**
 * Function to close a vtp from another// w ww .j a  v a2  s . com
 *
 * @param toClose The vtp to close
 * @param newVtp The other vtp to start
 * @return A copy of the object toClose closed to the left.
 * @throws TemporalException If the closure of the vtp can not be computed.
 */
public PossibilisticVTP closeR(PossibilisticVTP toClose, PossibilisticVTP newVtp) throws TemporalException {
    if (toClose.getSide() != null && toClose.getSide().compareTo(OpenInterval.UC) == 0) {
        DateTime startmp = new DateTime(toClose.getStartMP());
        //            DateTime leftmp = startmp.minus(toClose.getStartLeft());
        DateTime rightmp = startmp.plus(toClose.getStartRight());

        DateTime newmp = new DateTime(newVtp.getStartMP());
        DateTime newleft = newmp.minus(newVtp.getStartLeft());
        //            DateTime newright = newmp.plus(newVtp.getStartRight());

        if (rightmp.isBefore(newleft)) {
            log.trace("Closing ending point");
            Duration d = new Duration(startmp, newmp);
            Duration d1 = new Duration(d.getMillis() / 2);

            DateTime closeMp = new DateTime(startmp);
            closeMp = closeMp.plus(d1);

            Duration left = new Duration(startmp, closeMp);
            Duration right = new Duration(closeMp, newleft);

            toClose.setEndMP(closeMp.getMillis());
            toClose.setEndLeft(left.getMillis());
            toClose.setEndRight(right.getMillis());

            toClose.setSide(null);

        } else {
            log.error("The point cannot be closed");
            throw new TemporalException("The point cannot be closed");
        }

        //            
        //            DateTime lefts = startmp.plus( new Instant(newVtp.getStartLeft()));
        //            if(newVtp.getStartMP()> )
    } else {
        log.error("The point is not open");
        throw new TemporalException("The point is not open");
    }

    return toClose;
}

From source file:es.jpons.temporal.types.PossibilisticVTP.java

License:Open Source License

/**
 * Constructor//from   ww  w. j av  a  2s.c  o  m
 *
 * @param sday Starting day of month
 * @param smonth Starting month of year
 * @param syear Starting year
 * @param sLeftDays Number of days of the left starting margin.
 * @param sRightDays Number of days of the right starting margin.
 * @param eday Ending day of month.
 * @param emonth Ending month of year.
 * @param eyear Ending year.
 * @param eLeftDays Number of days of the ending margin.
 * @param eRightDays Number of days of the right ending margin.
 */
public PossibilisticVTP(Integer sday, Integer smonth, Integer syear, Integer sLeftDays, Integer sRightDays,
        Integer eday, Integer emonth, Integer eyear, Integer eLeftDays, Integer eRightDays) {
    DateTime dateStartingMainPoint, dateStartingLeft, dateStartingRight;
    DateTime dateEndingMainPoint, dateEndingLeft, dateEndingRight;

    dateStartingMainPoint = new DateTime(syear, smonth, sday, 0, 0);

    dateStartingLeft = dateStartingMainPoint.minusDays(sLeftDays);
    dateStartingRight = dateStartingMainPoint.plusDays(sRightDays);

    Duration d = new Duration(dateStartingLeft, dateStartingMainPoint);
    Duration d1 = new Duration(dateStartingMainPoint, dateStartingRight);

    dateEndingMainPoint = new DateTime(eyear, emonth, eday, 0, 0);
    dateEndingLeft = dateEndingMainPoint.minusDays(eLeftDays);
    dateEndingRight = dateEndingMainPoint.plusDays(eRightDays);

    Duration d2 = new Duration(dateEndingLeft, dateEndingMainPoint);
    Duration d3 = new Duration(dateEndingMainPoint, dateEndingRight);

    //            PossibilisticTP start, end;
    PossibilisticVTP pvp;

    //            
    //            start = new PossibilisticTP(
    //                    dateStartingMainPoint.toDate(),
    //                    dateStartingLeft.toDate(),
    //                    dateStartingRight.toDate());
    //            end = new PossibilisticTP(   
    //                    dateEndingMainPoint.toDate(),
    //                    dateEndingLeft.toDate(),
    //                    dateEndingRight.toDate());

    //PossibilisticVTP(dateStartingMainPoint.toDate(),d.getMillis(),d1.getMillis(),dateEndingMainPoint.toDate(),d2.getMillis(),d3.getMillis());
    this.startMP = dateStartingMainPoint.getMillis();
    this.startLeft = d.getMillis();
    this.startRight = d1.getMillis();
    this.endMP = dateEndingMainPoint.getMillis();
    this.endLeft = d2.getMillis();
    this.endRight = d3.getMillis();
    this.startDate = dateStartingMainPoint.toDate();
    this.endDate = dateEndingMainPoint.toDate();

    convertToTM();

}

From source file:es.jpons.temporal.types.PossibilisticVTP.java

License:Open Source License

public PossibilisticVTP(Integer day, Integer month, Integer year, Integer leftDays, Integer rightDays,
        OpenInterval side) {/*from  w w  w  .j  a  v  a 2 s.c o m*/
    DateTime dateMainPoint, dateLeft, dateRight;

    dateMainPoint = new DateTime(year, month, day, 0, 0);

    dateLeft = dateMainPoint.minusDays(leftDays);
    dateRight = dateMainPoint.plusDays(rightDays);

    Duration d = new Duration(dateLeft, dateMainPoint);
    Duration d1 = new Duration(dateMainPoint, dateRight);

    switch (side) {
    case UC:
        this.startMP = dateMainPoint.getMillis();
        this.startLeft = d.getMillis();
        this.startRight = d1.getMillis();
        this.startDate = dateMainPoint.toDate();
        this.side = OpenInterval.UC;
        break;
    case FB:
        this.endMP = dateMainPoint.getMillis();
        this.endLeft = d.getMillis();
        this.endRight = d1.getMillis();
        this.endDate = dateMainPoint.toDate();
        this.side = OpenInterval.FB;
        break;
    }

}

From source file:es.ugr.decsai.jpons.testapplication.Main.java

License:Open Source License

protected static PossibilisticVTP createVTP(Integer sday, Integer smonth, Integer syear, Integer eday,
        Integer emonth, Integer eyear) {
    DateTime dateStartingMainPoint, dateStartingLeft, dateStartingRight;
    DateTime dateEndingMainPoint, dateEndingLeft, dateEndingRight;

    dateStartingMainPoint = new DateTime(syear, smonth, sday, 0, 0);

    dateStartingLeft = dateStartingMainPoint.minusDays(3);
    dateStartingRight = dateStartingMainPoint.plusDays(3);

    Duration d = new Duration(dateStartingLeft, dateStartingMainPoint);
    Duration d1 = new Duration(dateStartingMainPoint, dateStartingRight);

    dateEndingMainPoint = new DateTime(eyear, emonth, eday, 0, 0);
    dateEndingLeft = dateEndingMainPoint.minusDays(3);
    dateEndingRight = dateEndingMainPoint.plusDays(3);

    Duration d2 = new Duration(dateEndingLeft, dateEndingMainPoint);
    Duration d3 = new Duration(dateEndingMainPoint, dateEndingRight);

    PossibilisticTP start, end;/*from  w w w  .  jav  a2  s .  c om*/
    PossibilisticVTP pvp;

    //            start = new PossibilisticTP(
    //                    dateStartingMainPoint.toDate(),
    //                    dateStartingLeft.toDate(),
    //                    dateStartingRight.toDate());
    //            end = new PossibilisticTP(   
    //                    dateEndingMainPoint.toDate(),
    //                    dateEndingLeft.toDate(),
    //                    dateEndingRight.toDate());

    pvp = new PossibilisticVTP(dateStartingMainPoint.toDate(), d.getMillis(), d1.getMillis(),
            dateEndingMainPoint.toDate(), d2.getMillis(), d3.getMillis());
    return pvp;
}

From source file:es.ugr.decsai.jpons.testapplication.Main.java

License:Open Source License

public static void main(String[] args) {
    Main pp = new Main();
    log.trace("Initializing hibernate session");

    try {/*from  w w  w.j  a v  a2s.c o m*/
        Properties prop = new Properties();
        prop.load(pp.getClass().getResourceAsStream("hibernate.properties"));
        List<Class> annotatedClasses = new ArrayList<Class>();
        annotatedClasses.add(Employee.class);
        //            annotatedClasses.add(TemporalPK.class);
        //            annotatedClasses.add(PossibilisticTP.class);
        //            annotatedClasses.add(PossibilisticVTP.class);

        //            Session session = TemporalHibernateUtil.getSession(prop, annotatedClasses);
        //        }catch(Exception ex){
        //            log.error(ex);
        //        }
        //            EntityManagerFactory emf = Persistence.createEntityManagerFactory("TemporalPU");
        //            EntityManager em = emf.createEntityManager();
        // 
        //            
        //           TemporalWebEntityManager manager = pp.configure();
        //            TemporalEntityManagerInterface entityManager = TemporalWebEntityManager.getEntityManager();
        //            

        TemporalPersistenceManager manager = TemporalPersistenceManager.getInstance(prop, annotatedClasses);

        //        try {
        ////            TemporalEntityManager manager = TemporalEntityManagerFactory.getInstance().createTemporalEntityManager();
        //        } catch (NamingException ex) {
        //            log.error(ex);
        //        }

        DateTime dateStartingMainPoint, dateStartingLeft, dateStartingRight;
        DateTime dateEndingMainPoint, dateEndingLeft, dateEndingRight;

        dateStartingMainPoint = new DateTime(2012, 3, 15, 0, 0);

        dateStartingLeft = new DateTime(2012, 3, 1, 0, 0);
        dateStartingRight = new DateTime(2012, 3, 21, 0, 0);

        Duration d = new Duration(dateStartingLeft, dateStartingMainPoint);
        Duration d1 = new Duration(dateStartingMainPoint, dateStartingRight);

        dateEndingMainPoint = new DateTime(2012, 3, 30, 0, 0);
        dateEndingLeft = new DateTime(2012, 3, 27, 0, 0);
        dateEndingRight = new DateTime(2012, 4, 3, 0, 0);

        Duration d2 = new Duration(dateEndingLeft, dateEndingMainPoint);
        Duration d3 = new Duration(dateEndingMainPoint, dateEndingRight);

        PossibilisticTP start, end;
        PossibilisticVTP pvp;

        start = new PossibilisticTP(dateStartingMainPoint.toDate(), dateStartingLeft.toDate(),
                dateStartingRight.toDate());
        end = new PossibilisticTP(dateEndingMainPoint.toDate(), dateEndingLeft.toDate(),
                dateEndingRight.toDate());

        pvp = new PossibilisticVTP(dateStartingMainPoint.toDate(), d.getMillis(), d1.getMillis(),
                dateEndingMainPoint.toDate(), d2.getMillis(), d3.getMillis());

        //new date to compare to
        DateTime dateStartingMainPoint1, dateStartingLeft1, dateStartingRight1;
        DateTime dateEndingMainPoint1, dateEndingLeft1, dateEndingRight1;

        dateStartingMainPoint1 = new DateTime(2011, 3, 1, 0, 0);

        dateStartingLeft1 = new DateTime(2011, 3, 1, 0, 0);
        dateStartingRight1 = new DateTime(2011, 3, 21, 0, 0);

        Duration d4 = new Duration(dateStartingLeft1, dateStartingMainPoint1);
        Duration d5 = new Duration(dateStartingMainPoint1, dateStartingRight1);

        dateEndingMainPoint1 = new DateTime(2011, 3, 27, 0, 0);
        dateEndingLeft1 = new DateTime(2011, 3, 27, 0, 0);
        dateEndingRight1 = new DateTime(2011, 4, 3, 0, 0);

        Duration d6 = new Duration(dateEndingLeft1, dateEndingMainPoint1);
        Duration d7 = new Duration(dateEndingMainPoint1, dateEndingRight1);

        PossibilisticTP start1, end1;
        PossibilisticVTP pvp1;

        start1 = new PossibilisticTP(dateStartingMainPoint1.toDate(), dateStartingLeft1.toDate(),
                dateStartingRight1.toDate());
        end1 = new PossibilisticTP(dateEndingMainPoint1.toDate(), dateEndingLeft1.toDate(),
                dateEndingRight1.toDate());

        pvp1 = new PossibilisticVTP(dateStartingMainPoint1.toDate(), d4.getMillis(), d5.getMillis(),
                dateEndingMainPoint1.toDate(), d6.getMillis(), d7.getMillis());

        PossibilisticVTP pvp2, pvp3, pvp4;

        pvp2 = createVTP(10, 3, 2012, 30, 4, 2012);
        pvp3 = createVTP(1, 5, 2012, 27, 5, 2012);
        pvp4 = createVTP(30, 3, 2012, 25, 4, 2012);

        //            overlapsTM(pvp1, pvp);
        //            overlapsTM(pvp, pvp1);
        //            
        //            overlapsTM(pvp, pvp2);
        //            overlapsTM(pvp, pvp3);
        //            overlapsTM(pvp, pvp4);
        //            System.out.println("These two do overlap");
        //            overlapsTM(pvp2, pvp4);

        //ejemplo
        pvp4.setSide(OpenInterval.UC);
        pvp4.setEndLeft(null);
        pvp4.setEndMP(null);
        pvp4.setEndRight(null);
        //            try{
        //            manager.closeR(pvp4, pvp); // an error
        //            }catch(Exception e){
        //                log.error(e);
        //            }
        //            PossibilisticVTP closed = manager.closeR(pvp4, pvp3);
        //            System.out.println(" Closed interval: " + closed);
        //            

        Employee e1 = new Employee(new TemporalPK(23, 1), "emp3");
        pvp.setSide(OpenInterval.UC);
        e1.setPvp(pvp2);

        Employee e2 = new Employee(new TemporalPK(2, 2), "emp4");
        e2.setPvp(pvp4);

        Employee e3 = new Employee(new TemporalPK(2, 3), "emp4");
        e3.setPvp(pvp3);

        //            Employee e1 = new Employee(22, null, "emp2");
        //        try {
        //            Transaction t =.beginTransaction();
        Transaction beginTransaction = manager.beginTransaction();
        //            beginTransaction.begin();

        manager.save(e1);
        manager.save(e2);
        manager.save(e3);
        //  manager.update(e3);
        beginTransaction.commit();

        TemporalQuery tq = manager.createTemporalQuery(e3);
        tq.setAllenRelation(AllenRelation.before, pvp3);
        List<QueryResult> list = tq.getList();

        for (QueryResult qr : list) {
            System.out
                    .println("aggregation: " + qr.getAggregation() + " temporal " + qr.getTemporalSatisfaction()
                            + " endmp: " + qr.getVtp().getEndMP() + " vid: " + qr.getKey());
        }

        PossibilisticVTP pvpafter = createVTP(28, 4, 2012, 30, 4, 2013);
        TemporalQuery tq1 = manager.createTemporalQuery(e3);
        tq1.setAllenRelation(AllenRelation.meets, pvpafter);
        List<QueryResult> list1 = tq1.getList();

        for (QueryResult qr : list1) {
            System.out
                    .println("aggregation: " + qr.getAggregation() + " temporal " + qr.getTemporalSatisfaction()
                            + " endmp: " + qr.getVtp().getEndMP() + " vid: " + qr.getKey());
        }

        //test for the cartesian product:
        tq1.createTemporalQuery(e1);
        System.out.println("---- CARTESIAN PRODUCT ----");
        List<QueryResult> list2 = tq1.cartesianProduct();

        for (QueryResult qr : list2) {
            System.out
                    .println("aggregation: " + qr.getAggregation() + " temporal " + qr.getTemporalSatisfaction()
                            + " endmp: " + qr.getVtp().getEndMP() + " vid: " + qr.getKeys());
        }

        //            String tableName = e3.getClass().getSimpleName();
        //            TemporalPersistenceManager.getSession().createQuery("Delete from "+ tableName + " where tid.id = :idval" ).setParameter("idval", e3.getTid().getId()).executeUpdate();
        ////            //query:
        //            Session s = TemporalPersistenceManager.getSession();
        //            Date startdate = new DateTime(2012, 3, 10, 0, 0).toDate();
        //            Date enddate = new DateTime(2012,3,21,1,0,0).toDate();
        //            List list = s.createQuery("from Employee e Where (:startdate < e.pvp.startMP ) and ((:enddate > e.pvp.startMP and :enddate < e.pvp.endMP ) or (:enddate > e.pvp.endMP)) ").
        //                    setParameter("startdate", startdate).
        //                    setParameter("enddate", enddate).
        //                    list();
        //            
        //            if(list!=null){
        //                for(int i=0;i<list.size();i++){
        //                    Employee e = (Employee) list.get(i);
        //                    System.out.println("Time: " + e.getPvp().getStartMP() + " , "+e.getPvp().getEndMP());
        //                }
        //            }
        //            
        //            beginTransaction.commit();
        manager.close();
        //            t.commit();
        //       em.getTransaction().begin();
        ////        
        //        em.persist(e1);
        ////        
        //        em.getTransaction().commit();
        //        
        //            session.close();
    } catch (Throwable t) {
        log.error(t);
    }

}

From source file:es.usc.citius.servando.calendula.pinlock.UnlockStateManager.java

License:Open Source License

/**
 * Checks if unlock is set and not expired
 *
 * @return <code>true</code> if unlocked, false otherwise.
 *//*w  w  w  .  j av a  2  s  .  c o m*/
public boolean isUnlocked() {
    LogUtil.v(TAG, "isUnlocked() called");
    if (unlockTimestamp != null) {
        Duration diff = new Duration(unlockTimestamp, DateTime.now());
        // need to be a string to use a ListPreference
        final String maxDurationStr = PreferenceUtils.getString(PreferenceKeys.UNLOCK_PIN_TIMEOUT,
                DEFAULT_UNLOCK_DURATION_SECONDS);
        Duration maxDuration = Duration.standardSeconds(Integer.parseInt(maxDurationStr));
        LogUtil.d(TAG, "Max unlock duration is " + maxDuration.toString());
        if (diff.compareTo(maxDuration) <= 0) {
            LogUtil.d(TAG, "isUnlocked() returned: " + true);
            return true;
        } else {
            LogUtil.d(TAG, "isUnlocked: unlock has expired, unsetting and returning null");
            unlockTimestamp = null;
            return false;
        }
    } else {
        LogUtil.d(TAG, "isUnlocked: timestamp is null");
    }
    return false;
}

From source file:eu.aylett.skyscanner.logparse.LogGlobalAggregator.java

License:Apache License

@JsonProperty
public long getDurationInMinutes() {
    return new Duration(earliest, latest.plus(Duration.standardMinutes(1))).getStandardMinutes();
}

From source file:eu.hydrologis.libs.peakflow.core.discharge.QReal.java

License:Open Source License

/**
 * Calculate the discharge with rainfall data.
 *//*from  w w  w . java 2s  .c o m*/
// public QReal( ParameterBox fixedParameters, double[][] iuhdata, double[][] jeffdata ) {
// fixedParams = fixedParameters;
//
// jeff = jeffdata;
// ampi = iuhdata;
//
// System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAA: ampilength " + ampi[ampi.length - 1][0]);
// }

/*
 * (non-Javadoc)
 * @see bsh.commands.h.peakflow.core.discharge.DischargeCalculator#calculateQ()
 */
public double[][] calculateQ() {
    double timestep = fixedParams.getTimestep();
    double area_super = fixedParams.getArea();
    double area_sub = fixedParams.getArea_sub();
    double area_tot = 0f;

    double raintimestep = jeffC.getRain_timestep();
    DateTime firstDate = jeffC.getFirstDate();

    /*
     * The maximum rain time has no sense with the real precipitations. In this case it will use
     * the rain timestep for tp.
     */
    double tcorr = ampi[ampi.length - 1][0];
    tpmax = (double) raintimestep;
    int rainLength = jeff.size();
    double[][] totalQshiftMatrix = new double[rainLength][(int) (Math.floor((tcorr + tpmax) / timestep) + 1
            + rainLength * raintimestep / timestep)];
    double[][] Q = new double[(int) Math.floor((tcorr + tpmax) / timestep) + 1][3];

    if (area_sub != -9999.0) {
        area_tot = area_sub + area_super;
    } else {
        area_tot = area_super;
    }

    Set<DateTime> dates = jeff.keySet();
    PrintStreamProgressMonitor pm = new PrintStreamProgressMonitor(out);
    pm.beginTask("Calculating discharge...", dates.size());
    int i = 0;
    for (DateTime dateTime : dates) {
        double J = jeff.get(dateTime);
        /*
         * calculate the discharge for t < tcorr
         */
        int j = 0;
        for (int t = 1; t < tcorr; t += timestep) {
            j = (int) Math.floor((t) / timestep);

            if (t <= tpmax) {
                Q[j][0] = t;
                double widthInterpolate = FluidUtils.width_interpolate(ampi, t, 0, 2);
                Q[j][1] = (double) (J * area_tot * widthInterpolate);
                Q[j][2] = Q[j - 1][2] + Q[j][1];
            } else {
                Q[j][0] = t;
                Q[j][1] = (double) (J * area_tot * (FluidUtils.width_interpolate(ampi, t, 0, 2)
                        - FluidUtils.width_interpolate(ampi, t - tpmax, 0, 2)));
                Q[j][2] = Q[j - 1][2] + Q[j][1];
            }
        }

        /*
         * calculate the discharge for t > tcorr
         */

        for (double t = tcorr; t < (tcorr + tpmax); t += timestep) {
            j = (int) Math.floor(((int) t) / timestep);
            Q[j][0] = t;
            Q[j][1] = (double) (J * area_tot
                    * (ampi[ampi.length - 1][2] - FluidUtils.width_interpolate(ampi, t - tpmax, 0, 2)));
            Q[j][2] = Q[j - 1][2] + Q[j][1];
        }

        /*
         * calculate the volumes
         */
        double vol = Q[Q.length - 2][2] * timestep;
        double vol2 = (double) (area_tot * J * raintimestep);

        /*
         * calculate zero padding before first value Note that jeff contains already the
         * progressive time of the rainfile.
         */
        int totalshiftmatrixindex = 0;
        int initalshiftmatrixindex = 0;
        // FIXME time in ???
        Duration duration = new Duration(firstDate, dateTime);
        long intervalSeconds = duration.getStandardSeconds();

        int paddingnumber = (int) (intervalSeconds / timestep);
        for (int m = 0; m < paddingnumber; m++) {
            totalQshiftMatrix[i][m] = 0;
            totalshiftmatrixindex++;
        }
        initalshiftmatrixindex = totalshiftmatrixindex;
        for (int k = initalshiftmatrixindex; k < Q.length + initalshiftmatrixindex; k++) {
            totalQshiftMatrix[i][k] = Q[k - initalshiftmatrixindex][1];
            totalshiftmatrixindex++;
        }
        for (int k = Q.length + totalshiftmatrixindex; k < totalQshiftMatrix[0].length; k++) {
            totalQshiftMatrix[i][k] = 0;
        }
        i++;
        pm.worked(1);
    }
    pm.done();

    /*
     * sum the discharge contributes
     */
    Qtot = new double[totalQshiftMatrix[0].length][2];
    double tottime = 0f;
    for (int k = 0; k < Qtot.length; k++) {
        double sum = 0f;
        for (int j = 0; j < totalQshiftMatrix.length; j++) {
            sum = sum + totalQshiftMatrix[j][k];
        }

        tottime = tottime + timestep;

        Qtot[k][1] = sum;
        Qtot[k][0] = tottime;
    }

    double total_vol = 0f;
    for (int k = 0; k < Qtot.length; k++) {
        total_vol = total_vol + Qtot[k][1];
    }
    double total_rain = 0.0;
    for (DateTime dateTime : dates) {
        double J = jeff.get(dateTime);
        total_rain = total_rain + J;
    }
    total_rain = total_rain * area_tot * raintimestep;

    return Qtot;
}

From source file:eu.itesla_project.computation.mpi.CsvMpiStatistics.java

License:Mozilla Public License

public static void exportBusyCores(Path dbDir, String dbName) throws IOException {
    Objects.requireNonNull(dbDir);
    Objects.requireNonNull(dbName);

    Path csv = dbDir.resolve(dbName + ".csv");

    Path busyCoresCsv = dbDir.resolve("busy-cores.csv");
    LOGGER.info("Writing " + busyCoresCsv);

    final DateTime[] min = new DateTime[1];
    final DateTime[] max = new DateTime[1];
    try (StatisticsReader reader = new StatisticsReader(csv)) {
        reader.read(new StatisticsReader.AbstractHandler() {

            @Override/*from   w w w .ja v a 2 s  .c om*/
            public void onTaskEnd(StatisticsReader.TaskExecution task, StatisticsReader.JobExecution job) {
                if (min[0] == null || task.startTime.compareTo(min[0]) < 0) {
                    min[0] = task.startTime;
                }
                if (task.taskDuration != null) {
                    DateTime endTime = task.startTime.plusMillis((int) (long) task.taskDuration);
                    if (max[0] == null || endTime.compareTo(max[0]) > 0) {
                        max[0] = endTime;
                    }
                }
            }

        });
    }

    final int secs = (int) new Duration(min[0], max[0]).getStandardSeconds() + 1;
    final int[] busyCores = new int[secs];
    Arrays.fill(busyCores, 0);
    try (StatisticsReader reader = new StatisticsReader(csv)) {
        reader.read(new StatisticsReader.AbstractHandler() {

            @Override
            public void onTaskEnd(StatisticsReader.TaskExecution task, StatisticsReader.JobExecution job) {
                int s1 = (int) new Duration(min[0], task.startTime).getStandardSeconds();
                int s2;
                if (task.taskDuration != null) {
                    s2 = s1 + (int) ((float) task.taskDuration / 1000);
                } else {
                    s2 = secs - 1;
                }
                for (int s = s1; s <= s2; s++) {
                    busyCores[s]++;
                }
            }

        });
    }
    try (BufferedWriter writer = Files.newBufferedWriter(busyCoresCsv, StandardCharsets.UTF_8)) {
        writer.write("Second" + CSV_SEPARATOR + "Busy cores");
        writer.newLine();
        for (int s = 0; s < busyCores.length; s++) {
            writer.write(Integer.toString(s));
            writer.write(CSV_SEPARATOR);
            writer.write(Integer.toString(busyCores[s]));
            writer.newLine();
        }
    }
}

From source file:eu.itesla_project.computation.mpi.MpiJobSchedulerImpl.java

License:Mozilla Public License

private void processCompletedTasks(MpiJob job, List<MpiTask> completedTasks) throws IOException {
    long t0 = System.currentTimeMillis();
    try {/*from ww  w.  j  av a  2  s. c o  m*/
        CommandExecution execution = job.getExecution();
        Command command = execution.getCommand();

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Tasks {} of job {} completed",
                    completedTasks.stream().map(MpiTask::getIndex).collect(Collectors.toList()), job.getId());
        }

        for (MpiTask task : completedTasks) {
            // duration of the task seen by the master in ms
            long taskDurationSeenByMaster = new Duration(task.getStartTime(), task.getEndTime()).getMillis();

            // decode task result messages
            Messages.TaskResult message = Messages.TaskResult.parseFrom(task.getResultMessage());

            // duration of the task seen by the slave in ms
            long taskDurationSeenBySlave = message.getTaskDuration(); // number of ms

            // write output files to working dir
            String stdOutGzFileName = job.getExecution().getCommand().getId() + "_" + task.getIndex()
                    + ".out.gz";
            for (Messages.TaskResult.OutputFile outputFile : message.getOutputFileList()) {
                // std out file special case directly unzip it on working dir
                if (outputFile.getName().equals(stdOutGzFileName)) {
                    Path path = job.getWorkingDir()
                            .resolve(outputFile.getName().substring(0, outputFile.getName().length() - 3));
                    try (InputStream is = new GZIPInputStream(outputFile.getData().newInput());
                            OutputStream os = Files.newOutputStream(path)) {
                        ByteStreams.copy(is, os);
                    }
                } else {
                    Path path = job.getWorkingDir().resolve(outputFile.getName());
                    try (InputStream is = outputFile.getData().newInput();
                            OutputStream os = Files.newOutputStream(path)) {
                        ByteStreams.copy(is, os);
                    }
                }

                // archive standard output of problematic tasks
                if (stdOutArchive != null) {
                    if (message.getExitCode() != 0) {
                        if (outputFile.getName().equals(stdOutGzFileName)) {
                            try (FileSystem archiveFileSystem = FileSystems.newFileSystem(
                                    URI.create("jar:file:" + stdOutArchive.toUri().getPath()), ZIP_FS_ENV)) {
                                Path dir = archiveFileSystem.getPath("/").resolve("job-" + job.getId());
                                Files.createDirectories(dir);
                                try (InputStream is = outputFile.getData().newInput();
                                        OutputStream os = Files
                                                .newOutputStream(dir.resolve(outputFile.getName()))) {
                                    ByteStreams.copy(is, os);
                                }
                            }
                        }
                    }
                }
            }

            if (message.getExitCode() != 0) {
                job.getErrors().add(new ExecutionError(command, task.getIndex(), message.getExitCode()));
            }

            // notify execution completion
            try {
                job.getListener().onExecutionCompletion(task.getIndex());
            } catch (Exception e) {
                LOGGER.error(e.toString(), e);
            }

            // update execution statistics
            statistics.logTaskEnd(task.getId(), taskDurationSeenByMaster, message.getCommandDurationList(),
                    taskDurationSeenByMaster - taskDurationSeenBySlave, task.getResultMessage().length,
                    message.getWorkingDataSize(), message.getExitCode());
        }

        job.getRunningTasks().removeAll(completedTasks);
        completedTasks.clear();
    } finally {
        processCompletedTasksTime += (System.currentTimeMillis() - t0);
    }
}