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        Principal principal = ctx.getPrincipal();
070        if (principal instanceof NuxeoPrincipal) {
071            NuxeoPrincipal nuxeoUser = (NuxeoPrincipal) principal;
072            // Only Administrators can access the counters
073            if (nuxeoUser.isAdministrator()) {
074                for (String counterName : counterNames) {
075                    CounterHistoryStack stack = cm.getCounterHistory(counterName);
076
077                    // copy and reverse the list
078                    List<long[]> valueList = new ArrayList<>(stack.getAsList());
079                    Collections.reverse(valueList);
080
081                    // bare values [ [t0,v0], [t1,v1] ...]
082                    List<List<Number>> valueSerie = new ArrayList<>();
083                    // delta values [ [t1,v1-v0], [t2,v2-v3] ...]
084                    List<List<Number>> deltaSerie = new ArrayList<>();
085                    // speed values [ [t1,v1-v0/t1-t0], ...]
086                    List<List<Number>> speedSerie = new ArrayList<>();
087
088                    float lastTS = 0;
089                    float lastValue = 0;
090                    long now = System.currentTimeMillis();
091                    for (long[] values : valueList) {
092
093                        // use seconds
094                        long ts = values[0];
095                        float t = (now - ts) / 1000;
096                        float value = values[1];
097                        Float tFloat = Float.valueOf(ts);
098
099                        // bare values
100                        Float bareValue = Float.valueOf(value);
101                        valueSerie.add(Arrays.asList(tFloat, bareValue));
102
103                        // delta values
104                        Float deltaValue = Float.valueOf(value - lastValue);
105                        deltaSerie.add(Arrays.asList(tFloat, deltaValue));
106
107                        if (lastTS > 0) {
108                            // speed values
109                            float tdelta = lastTS - t;
110                            if (tdelta == 0) {
111                                tdelta = 1;
112                            }
113                            Float speedValue = Float.valueOf(60 * (value - lastValue) / (tdelta));
114                            speedSerie.add(Arrays.asList(tFloat, speedValue));
115                        }
116                        lastTS = t;
117                        lastValue = value;
118                    }
119
120                    Map<String, Object> counter = new LinkedHashMap<>();
121                    counter.put("values", valueSerie);
122                    counter.put("deltas", deltaSerie);
123                    counter.put("speed", speedSerie);
124
125                    collection.put(counterName, counter);
126                }
127            }
128        }
129
130        return Blobs.createJSONBlobFromValue(collection);
131    }
132
133}