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.stats;
018
019import java.time.*;
020import java.util.*;
021
022import org.apache.juneau.annotation.*;
023import org.apache.juneau.rest.*;
024import org.apache.juneau.swaps.*;
025
026/**
027 * A snapshot of execution statistics for REST resource classes.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ExecutionStatistics">REST method execution statistics</a>
031 * </ul>
032 */
033@Bean(properties="startTime,upTime,methodStats")
034public class RestContextStats {
035   private final Instant startTime;
036   private final List<MethodExecStats> methodStats;
037
038   /**
039    * Constructor.
040    *
041    * @param startTime The start time of the {@link RestContext} object.
042    * @param methodStats The execution statistics beans for the context.
043    */
044   public RestContextStats(Instant startTime, List<MethodExecStats> methodStats) {
045      this.startTime = startTime;
046      this.methodStats = methodStats;
047   }
048
049   /**
050    * Returns the time this REST resource class was started.
051    *
052    * @return The time this REST resource class was started.
053    */
054   @Swap(TemporalSwap.IsoInstant.class)
055   public Instant getStartTime() {
056      return startTime;
057   }
058
059   /**
060    * Returns the time in milliseconds that this REST resource class has been running.
061    *
062    * @return The time in milliseconds that this REST resource class has been running.
063    */
064   public String getUpTime() {
065      long s = Duration.between(startTime, Instant.now()).getSeconds();
066      return String.format("%dh:%02dm:%02ds", s / 3600, (s % 3600) / 60, (s % 60));
067   }
068
069   /**
070    * Returns statistics on all method executions.
071    *
072    * @return Statistics on all method executions.
073    */
074   public Collection<MethodExecStats> getMethodStats() {
075      return methodStats;
076   }
077}