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