001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.server.stats;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.time.*;
022import java.util.*;
023
024import org.apache.juneau.commons.bean.*;
025import org.apache.juneau.marshall.*;
026import org.apache.juneau.rest.server.*;
027
028/**
029 * A snapshot of execution statistics for REST resource classes.
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ExecutionStatistics">REST method execution statistics</a>
033 * </ul>
034 */
035@BeanType(properties = "startTime,upTime,methodStats")
036public class RestContextStats {
037   private final Instant startTime;
038   private final List<MethodExecStats> methodStats;
039
040   /**
041    * Constructor.
042    *
043    * @param startTime The start time of the {@link RestContext} object.
044    * @param methodStats The execution statistics beans for the context.
045    */
046   public RestContextStats(Instant startTime, List<MethodExecStats> methodStats) {
047      this.startTime = startTime;
048      this.methodStats = methodStats;
049   }
050
051   /**
052    * Returns statistics on all method executions.
053    *
054    * @return Statistics on all method executions.
055    */
056   public Collection<MethodExecStats> getMethodStats() { return methodStats; }
057
058   /**
059    * Returns the time this REST resource class was started.
060    *
061    * @return The time this REST resource class was started.
062    */
063   @MarshalledProp(temporalFormat = TemporalFormat.ISO_INSTANT)
064   public Instant getStartTime() { return startTime; }
065
066   /**
067    * Returns the time in milliseconds that this REST resource class has been running.
068    *
069    * @return The time in milliseconds that this REST resource class has been running.
070    */
071   public String getUpTime() {
072      long s = Duration.between(startTime, Instant.now()).getSeconds();
073      return f("%dh:%02dm:%02ds", s / 3600, (s % 3600) / 60, (s % 60));
074   }
075}