001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020package org.nuxeo.ecm.automation.core.operations.management; 021 022import java.io.IOException; 023import java.security.Principal; 024import java.util.ArrayList; 025import java.util.Arrays; 026import java.util.Collections; 027import java.util.LinkedHashMap; 028import java.util.List; 029import java.util.Map; 030 031import org.nuxeo.ecm.automation.OperationContext; 032import org.nuxeo.ecm.automation.core.Constants; 033import org.nuxeo.ecm.automation.core.annotations.Context; 034import org.nuxeo.ecm.automation.core.annotations.Operation; 035import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 036import org.nuxeo.ecm.automation.core.annotations.Param; 037import org.nuxeo.ecm.automation.core.util.StringList; 038import org.nuxeo.ecm.core.api.Blob; 039import org.nuxeo.ecm.core.api.Blobs; 040import org.nuxeo.ecm.core.api.NuxeoPrincipal; 041import org.nuxeo.runtime.api.Framework; 042import org.nuxeo.runtime.management.counters.CounterHistoryStack; 043import org.nuxeo.runtime.management.counters.CounterManager; 044 045/** 046 * Return the data collected by one or more Counters For each counter 3 series are returned , bare values, delta and 047 * speed 048 * 049 * @author Tiry (tdelprat@nuxeo.com) 050 */ 051@Operation(id = GetCounters.ID, category = Constants.CAT_SERVICES, label = "Retrieve counters values", description = "Retrieve data collected by one or more Counters", addToStudio = false) 052public class GetCounters { 053 054 public static final String ID = "Counters.GET"; 055 056 @Context 057 protected OperationContext ctx; 058 059 @Param(name = "counterNames", required = true) 060 protected StringList counterNames; 061 062 @OperationMethod 063 public Blob run() throws IOException { 064 065 CounterManager cm = Framework.getService(CounterManager.class); 066 067 Map<String, Object> collection = new LinkedHashMap<>(); 068 069 NuxeoPrincipal nuxeoUser = ctx.getPrincipal(); 070 // Only Administrators can access the counters 071 if (nuxeoUser.isAdministrator()) { 072 for (String counterName : counterNames) { 073 CounterHistoryStack stack = cm.getCounterHistory(counterName); 074 075 // copy and reverse the list 076 List<long[]> valueList = new ArrayList<>(stack.getAsList()); 077 Collections.reverse(valueList); 078 079 // bare values [ [t0,v0], [t1,v1] ...] 080 List<List<Number>> valueSerie = new ArrayList<>(); 081 // delta values [ [t1,v1-v0], [t2,v2-v3] ...] 082 List<List<Number>> deltaSerie = new ArrayList<>(); 083 // speed values [ [t1,v1-v0/t1-t0], ...] 084 List<List<Number>> speedSerie = new ArrayList<>(); 085 086 float lastTS = 0; 087 float lastValue = 0; 088 long now = System.currentTimeMillis(); 089 for (long[] values : valueList) { 090 091 // use seconds 092 long ts = values[0]; 093 float t = (now - ts) / 1000; 094 float value = values[1]; 095 Float tFloat = Float.valueOf(ts); 096 097 // bare values 098 Float bareValue = Float.valueOf(value); 099 valueSerie.add(Arrays.asList(tFloat, bareValue)); 100 101 // delta values 102 Float deltaValue = Float.valueOf(value - lastValue); 103 deltaSerie.add(Arrays.asList(tFloat, deltaValue)); 104 105 if (lastTS > 0) { 106 // speed values 107 float tdelta = lastTS - t; 108 if (tdelta == 0) { 109 tdelta = 1; 110 } 111 Float speedValue = Float.valueOf(60 * (value - lastValue) / (tdelta)); 112 speedSerie.add(Arrays.asList(tFloat, speedValue)); 113 } 114 lastTS = t; 115 lastValue = value; 116 } 117 118 Map<String, Object> counter = new LinkedHashMap<>(); 119 counter.put("values", valueSerie); 120 counter.put("deltas", deltaSerie); 121 counter.put("speed", speedSerie); 122 123 collection.put(counterName, counter); 124 } 125 } 126 127 return Blobs.createJSONBlobFromValue(collection); 128 } 129 130}