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.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.LinkedHashMap;
027import java.util.List;
028import java.util.Map;
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 * @deprecated since 11.4, superseded by dropwizard metrics
049 * @author Tiry (tdelprat@nuxeo.com)
050 */
051@Deprecated(since = "11.4")
052@Operation(id = GetCounters.ID, category = Constants.CAT_SERVICES, label = "Retrieve counters values", description = "Retrieve data collected by one or more Counters", addToStudio = false, deprecatedSince = "11.4")
053public class GetCounters {
054
055    public static final String ID = "Counters.GET";
056
057    @Context
058    protected OperationContext ctx;
059
060    @Param(name = "counterNames", required = true)
061    protected StringList counterNames;
062
063    @OperationMethod
064    public Blob run() throws IOException {
065
066        CounterManager cm = Framework.getService(CounterManager.class);
067
068        Map<String, Object> collection = new LinkedHashMap<>();
069
070        NuxeoPrincipal nuxeoUser = ctx.getPrincipal();
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<>(stack.getAsList());
078                Collections.reverse(valueList);
079
080                // bare values [ [t0,v0], [t1,v1] ...]
081                List<List<Number>> valueSerie = new ArrayList<>();
082                // delta values [ [t1,v1-v0], [t2,v2-v3] ...]
083                List<List<Number>> deltaSerie = new ArrayList<>();
084                // speed values [ [t1,v1-v0/t1-t0], ...]
085                List<List<Number>> speedSerie = new ArrayList<>();
086
087                float lastTS = 0;
088                float lastValue = 0;
089                long now = System.currentTimeMillis();
090                for (long[] values : valueList) {
091
092                    // use seconds
093                    long ts = values[0];
094                    float t = (now - ts) / 1000; // NOSONAR (round)
095                    float value = values[1];
096                    Float tFloat = Float.valueOf(ts);
097
098                    // bare values
099                    Float bareValue = Float.valueOf(value);
100                    valueSerie.add(Arrays.asList(tFloat, bareValue));
101
102                    // delta values
103                    Float deltaValue = Float.valueOf(value - lastValue);
104                    deltaSerie.add(Arrays.asList(tFloat, deltaValue));
105
106                    if (lastTS > 0) {
107                        // speed values
108                        float tdelta = lastTS - t;
109                        if (tdelta == 0) {
110                            tdelta = 1;
111                        }
112                        Float speedValue = Float.valueOf(60 * (value - lastValue) / (tdelta));
113                        speedSerie.add(Arrays.asList(tFloat, speedValue));
114                    }
115                    lastTS = t;
116                    lastValue = value;
117                }
118
119                Map<String, Object> counter = new LinkedHashMap<>();
120                counter.put("values", valueSerie);
121                counter.put("deltas", deltaSerie);
122                counter.put("speed", speedSerie);
123
124                collection.put(counterName, counter);
125            }
126        }
127
128        return Blobs.createJSONBlobFromValue(collection);
129    }
130
131}