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;
014
015import java.time.*;
016import java.util.*;
017
018import org.apache.juneau.annotation.*;
019import org.apache.juneau.mstat.*;
020import org.apache.juneau.transforms.*;
021
022/**
023 * A snapshot of execution statistics for REST resource classes.
024 */
025@Bean(bpi="startTime,upTime,methodStats")
026public class RestContextStats {
027   private final Instant startTime;
028   private final List<MethodExecStats> methodStats;
029
030   RestContextStats(Instant startTime, List<MethodExecStats> methodStats) {
031      this.startTime = startTime;
032      this.methodStats = methodStats;
033   }
034
035   /**
036    * Returns the time this REST resource class was started.
037    *
038    * @return The time this REST resource class was started.
039    */
040   @Swap(TemporalSwap.IsoInstant.class)
041   public Instant getStartTime() {
042      return startTime;
043   }
044
045   /**
046    * Returns the time in milliseconds that this REST resource class has been running.
047    *
048    * @return The time in milliseconds that this REST resource class has been running.
049    */
050   public String getUpTime() {
051      long s = Duration.between(startTime, Instant.now()).getSeconds();
052      return String.format("%dh:%02dm:%02ds", s / 3600, (s % 3600) / 60, (s % 60));
053   }
054
055   /**
056    * Returns statistics on all method executions.
057    *
058    * @return Statistics on all method executions.
059    */
060   public Collection<MethodExecStats> getMethodStats() {
061      return methodStats;
062   }
063}