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.security.Principal; 023import java.util.ArrayList; 024import java.util.Collections; 025import java.util.List; 026 027import net.sf.json.JSONArray; 028import net.sf.json.JSONObject; 029 030import org.nuxeo.ecm.automation.OperationContext; 031import org.nuxeo.ecm.automation.core.Constants; 032import org.nuxeo.ecm.automation.core.annotations.Context; 033import org.nuxeo.ecm.automation.core.annotations.Operation; 034import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 035import org.nuxeo.ecm.automation.core.annotations.Param; 036import org.nuxeo.ecm.automation.core.util.StringList; 037import org.nuxeo.ecm.core.api.Blob; 038import org.nuxeo.ecm.core.api.Blobs; 039import org.nuxeo.ecm.core.api.NuxeoPrincipal; 040import org.nuxeo.runtime.api.Framework; 041import org.nuxeo.runtime.management.counters.CounterHistoryStack; 042import org.nuxeo.runtime.management.counters.CounterManager; 043 044/** 045 * Return the data collected by one or more Counters For each counter 3 series are returned , bare values, delta and 046 * speed 047 * 048 * @author Tiry (tdelprat@nuxeo.com) 049 */ 050@Operation(id = GetCounters.ID, category = Constants.CAT_SERVICES, label = "Retrieve counters values", description = "Retrieve data collected by one or more Counters", addToStudio = false) 051public class GetCounters { 052 053 public static final String ID = "Counters.GET"; 054 055 @Context 056 protected OperationContext ctx; 057 058 @Param(name = "counterNames", required = true) 059 protected StringList counterNames; 060 061 @OperationMethod 062 public Blob run() { 063 064 CounterManager cm = Framework.getLocalService(CounterManager.class); 065 066 JSONObject collection = new JSONObject(); 067 068 Principal principal = ctx.getPrincipal(); 069 if (principal instanceof NuxeoPrincipal) { 070 NuxeoPrincipal nuxeoUser = (NuxeoPrincipal) principal; 071 // Only Administrators can access the counters 072 if (nuxeoUser.isAdministrator()) { 073 for (String counterName : counterNames) { 074 CounterHistoryStack stack = cm.getCounterHistory(counterName); 075 076 // copy and reverse the list 077 List<long[]> valueList = new ArrayList<long[]>(stack.getAsList()); 078 Collections.reverse(valueList); 079 080 JSONObject counter = new JSONObject(); 081 082 // bare values [ [t0,v0], [t1,v1] ...] 083 JSONArray valueSerie = new JSONArray(); 084 // delta values [ [t1,v1-v0], [t2,v2-v3] ...] 085 JSONArray deltaSerie = new JSONArray(); 086 // speed values [ [t1,v1-v0/t1-t0], ...] 087 JSONArray speedSerie = new JSONArray(); 088 089 float lastTS = 0; 090 float lastValue = 0; 091 long now = System.currentTimeMillis(); 092 for (long[] values : valueList) { 093 094 // use seconds 095 long ts = values[0]; 096 float t = (now - ts) / 1000; 097 float value = values[1]; 098 099 JSONArray valueArray = new JSONArray(); 100 JSONArray deltaArray = new JSONArray(); 101 JSONArray speedArray = new JSONArray(); 102 103 // bare values 104 valueArray.add(ts); 105 valueArray.add(value); 106 valueSerie.add(valueArray); 107 108 // delta values 109 deltaArray.add(ts); 110 deltaArray.add(value - lastValue); 111 deltaSerie.add(deltaArray); 112 113 if (lastTS > 0) { 114 // speed values 115 speedArray.add(ts); 116 float tdelta = lastTS - t; 117 if (tdelta == 0) { 118 tdelta = 1; 119 } 120 speedArray.add(60 * (value - lastValue) / (tdelta)); 121 speedSerie.add(speedArray); 122 } 123 lastTS = t; 124 lastValue = value; 125 } 126 127 counter.put("values", valueSerie); 128 counter.put("deltas", deltaSerie); 129 counter.put("speed", speedSerie); 130 131 collection.put(counterName, counter); 132 } 133 } 134 } 135 136 return Blobs.createBlob(collection.toString(), "application/json"); 137 } 138 139}