Coverage Report - com.thindeck.agents.BuryContainers
 
Classes in this File Line Coverage Branch Coverage Complexity
BuryContainers
11%
2/18
0%
0/4
5
 
 1  2
 /**
 2  
  * Copyright (c) 2014-2015, Thindeck.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the thindeck.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.thindeck.agents;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Tv;
 34  
 import com.jcabi.log.Logger;
 35  
 import com.jcabi.xml.XML;
 36  
 import com.thindeck.api.Agent;
 37  
 import java.io.IOException;
 38  
 import java.text.ParseException;
 39  
 import java.util.Collection;
 40  
 import java.util.Date;
 41  
 import java.util.concurrent.TimeUnit;
 42  
 import org.apache.commons.lang3.time.DateFormatUtils;
 43  
 import org.xembly.Directive;
 44  
 import org.xembly.Directives;
 45  
 
 46  
 /**
 47  
  * Mark DEAD containers as WASTE.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@teamed.io)
 50  
  * @version $Id$
 51  
  * @since 0.1
 52  
  */
 53  
 @Immutable
 54  1
 public final class BuryContainers implements Agent {
 55  
 
 56  
     @Override
 57  
     public Iterable<Directive> exec(final XML deck) throws IOException {
 58  0
         final Collection<XML> containers = deck.nodes(
 59  
             "/deck/containers/container[not(@waste) and @state='dead']"
 60  
         );
 61  0
         final Date today = new Date();
 62  0
         final String now = new Today().iso();
 63  0
         final Directives dirs = new Directives();
 64  0
         for (final XML ctr : containers) {
 65  
             final int age;
 66  
             try {
 67  0
                 age = (int) ((today.getTime()
 68  
                     - DateFormatUtils.ISO_DATETIME_FORMAT.parse(
 69  
                         ctr.xpath("@checked").get(0)
 70  
                     ).getTime()) / TimeUnit.MINUTES.toMillis(1L));
 71  0
             } catch (final ParseException ex) {
 72  0
                 throw new IOException(ex);
 73  0
             }
 74  0
             if (age < Tv.TEN) {
 75  0
                 continue;
 76  
             }
 77  0
             final String name = ctr.xpath("name/text()").get(0);
 78  0
             dirs.xpath(
 79  
                 String.format(
 80  
                     "/deck/containers/container[name='%s']",
 81  
                     name
 82  
                 )
 83  
             ).attr("waste", now);
 84  0
             Logger.info(
 85  
                 this, "Container %s is DEAD for over %d mins, it's a waste",
 86  
                 name, age
 87  
             );
 88  0
         }
 89  0
         return dirs;
 90  
     }
 91  
 
 92  
 }