2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.enginemodel.concepts;
24 import java.text.SimpleDateFormat;
25 import java.util.List;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Table;
30 import javax.persistence.Transient;
31 import javax.xml.bind.annotation.XmlAccessType;
32 import javax.xml.bind.annotation.XmlAccessorType;
33 import javax.xml.bind.annotation.XmlElement;
34 import javax.xml.bind.annotation.XmlRootElement;
35 import javax.xml.bind.annotation.XmlType;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
44 import org.onap.policy.common.utils.validation.Assertions;
47 * This class is a java bean that is used to record statistics on Apex engines as they execute. Statistics on the number
48 * of events, the amount of time taken to execute the last policy, the average policy execution time, the up time of the
49 * engine, and the time stamp of the last engine start are recorded.
53 @Table(name = "AxEngineStats")
55 @XmlAccessorType(XmlAccessType.FIELD)
56 @XmlRootElement(name = "apexEngineStats", namespace = "http://www.onap.org/policy/apex-pdp")
57 @XmlType(name = "AxEngineStats", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = {"key", "timeStamp",
58 "eventCount", "lastExecutionTime", "averageExecutionTime", "upTime", "lastStart"})
59 public class AxEngineStats extends AxConcept {
60 private static final long serialVersionUID = -6981129081962785368L;
61 private static final int HASH_CODE_PRIME = 32;
64 @XmlElement(name = "key", required = true)
65 private AxReferenceKey key;
68 @XmlElement(required = true)
69 private long timeStamp;
72 @XmlElement(required = true)
73 private long eventCount;
76 @XmlElement(required = true)
77 private long lastExecutionTime;
80 @XmlElement(required = true)
81 private double averageExecutionTime;
84 @XmlElement(required = true)
89 private transient long lastEnterTime;
92 @XmlElement(required = true)
93 private long lastStart;
96 * The Default Constructor creates an engine statistics instance with a null key and with all values cleared.
98 public AxEngineStats() {
99 this(new AxReferenceKey());
102 lastExecutionTime = 0;
103 averageExecutionTime = 0;
112 * @param copyConcept the concept to copy from
114 public AxEngineStats(final AxEngineStats copyConcept) {
119 * The Keyed Constructor creates an engine statistics instance with the given key and all values cleared.
123 public AxEngineStats(final AxReferenceKey key) {
124 this(key, 0, 0, 0, 0, 0, 0);
128 * This Constructor creates an engine statistics instance with all its fields set.
130 * @param key the engine statistics key
131 * @param timeStamp the time stamp at which the statistics were recorded
132 * @param eventCount the number of events processed by the engine
133 * @param lastExecutionTime the amount of time taken to execute the last policy
134 * @param averageExecutionTime the average amount of time taken to execute a policy
135 * @param upTime the time that has elapsed since the policy engine was started
136 * @param lastStart the time at which the policy engine was last started
138 public AxEngineStats(final AxReferenceKey key, final long timeStamp, final long eventCount,
139 final long lastExecutionTime, final double averageExecutionTime, final long upTime, final long lastStart) {
141 Assertions.argumentNotNull(key, "key may not be null");
144 this.timeStamp = timeStamp;
145 this.eventCount = eventCount;
146 this.lastExecutionTime = lastExecutionTime;
147 this.averageExecutionTime = averageExecutionTime;
148 this.upTime = upTime;
149 this.lastStart = lastStart;
156 public AxReferenceKey getKey() {
164 public List<AxKey> getKeys() {
165 return key.getKeys();
169 * Sets the engine statistics key.
171 * @param key the engine statistics key
173 public void setKey(final AxReferenceKey key) {
174 Assertions.argumentNotNull(key, "key may not be null");
179 * Gets the time stamp at which the statistics were recorded.
181 * @return the time stamp at which the statistics were recorded
183 public long getTimeStamp() {
188 * Gets the time stamp at which the statistics were recorded as a string.
190 * @return the time stamp at which the statistics were recorded as a string
192 public String getTimeStampString() {
193 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timeStamp);
197 * Sets the time stamp at which the statistics were recorded.
199 * @param timeStamp the time stamp at which the statistics were recorded
201 public void setTimeStamp(final long timeStamp) {
202 this.timeStamp = timeStamp;
206 * Gets the number of events processed by the engine.
208 * @return the number of events processed by the engine
210 public long getEventCount() {
215 * Sets the number of events processed by the engine.
217 * @param eventCount the number of events processed by the engine
219 public void setEventCount(final long eventCount) {
220 this.eventCount = eventCount;
224 * Gets the amount of time taken to execute the last policy.
226 * @return the amount of time taken to execute the last policy
228 public long getLastExecutionTime() {
229 return lastExecutionTime;
233 * Sets the amount of time taken to execute the last policy.
235 * @param lastExecutionTime the amount of time taken to execute the last policy
237 public void setLastExecutionTime(final long lastExecutionTime) {
238 this.lastExecutionTime = lastExecutionTime;
242 * Gets the average amount of time taken to execute a policy.
244 * @return the average amount of time taken to execute a policy
246 public double getAverageExecutionTime() {
247 return averageExecutionTime;
251 * Sets the average amount of time taken to execute a policy.
253 * @param averageExecutionTime the average amount of time taken to execute a policy
255 public void setAverageExecutionTime(final double averageExecutionTime) {
256 this.averageExecutionTime = averageExecutionTime;
260 * Gets the time that has elapsed since the policy engine was started.
262 * @return the time that has elapsed since the policy engine was started
264 public long getUpTime() {
265 if (this.getLastStart() != 0) {
266 return upTime + (timeStamp - this.getLastStart());
272 * Sets the time that has elapsed since the policy engine was started.
274 * @param upTime the time that has elapsed since the policy engine was started
276 public void setUpTime(final long upTime) {
277 this.upTime = upTime;
281 * Sets the time at which the policy engine was last started.
283 * @param lastStart the time at which the policy engine was last started
285 private void setLastStart(final long lastStart) {
286 this.lastStart = lastStart;
290 * Gets the time at which the policy engine was last started.
292 * @return the time at which the policy engine was last started
294 public long getLastStart() {
299 * Resets all the statistic values to zero.
301 public synchronized void reset() {
304 lastExecutionTime = 0;
305 averageExecutionTime = 0;
312 * Updates the statistics when called, used by the Apex engine when it starts executing a policy.
314 * @param eventkey the key of the event that is being executed
316 public synchronized void executionEnter(final AxArtifactKey eventkey) {
317 final long now = System.currentTimeMillis();
319 if (eventCount < 0) {
327 * Updates the statistics when called, used by the Apex engine when it completes executing a policy.
329 public synchronized void executionExit() {
330 final long now = System.currentTimeMillis();
331 lastExecutionTime = now - lastEnterTime;
333 averageExecutionTime = ((averageExecutionTime * (eventCount - 1.0)) + lastExecutionTime) / eventCount;
335 timeStamp = System.currentTimeMillis();
339 * Updates the statistics when called, used by the Apex engine when it is started.
341 public synchronized void engineStart() {
342 final long now = System.currentTimeMillis();
344 this.setLastStart(now);
348 * Updates the statistics when called, used by the Apex engine when it is stopped.
350 public synchronized void engineStop() {
351 final long now = System.currentTimeMillis();
353 upTime += (timeStamp - this.getLastStart());
354 this.setLastStart(0);
361 public AxValidationResult validate(final AxValidationResult result) {
362 if (key.equals(AxReferenceKey.getNullKey())) {
363 result.addValidationMessage(
364 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
367 return key.validate(result);
374 public void clean() {
382 public String toString() {
383 final StringBuilder builder = new StringBuilder();
384 builder.append(this.getClass().getSimpleName());
385 builder.append(":(");
386 builder.append("engineKey=");
388 builder.append(",timeStamp=");
389 builder.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timeStamp));
390 builder.append(",eventCount=");
391 builder.append(eventCount);
392 builder.append(",lastExecutionTime=");
393 builder.append(lastExecutionTime);
394 builder.append(",averageExecutionTime=");
395 builder.append(averageExecutionTime);
396 builder.append(",upTime=");
397 builder.append(getUpTime());
399 return builder.toString();
406 public AxConcept copyTo(final AxConcept targetObject) {
407 Assertions.argumentNotNull(targetObject, "target may not be null");
409 final Object copyObject = targetObject;
410 Assertions.instanceOf(copyObject, AxEngineStats.class);
412 final AxEngineStats copy = ((AxEngineStats) copyObject);
413 copy.setKey(new AxReferenceKey(key));
414 copy.setTimeStamp(timeStamp);
415 copy.setEventCount(eventCount);
416 copy.setLastExecutionTime(lastExecutionTime);
417 copy.setAverageExecutionTime(averageExecutionTime);
418 copy.setUpTime(upTime);
419 copy.setLastStart(lastStart);
428 public int hashCode() {
429 final int prime = 31;
431 result = prime * result + key.hashCode();
432 result = prime * result + (int) (timeStamp ^ (timeStamp >>> HASH_CODE_PRIME));
433 result = prime * result + (int) (eventCount ^ (eventCount >>> HASH_CODE_PRIME));
434 result = prime * result + (int) (lastExecutionTime ^ (lastExecutionTime >>> HASH_CODE_PRIME));
435 result = prime * result + ((int) averageExecutionTime ^ ((int) averageExecutionTime >>> HASH_CODE_PRIME));
436 result = prime * result + (int) (upTime ^ (upTime >>> HASH_CODE_PRIME));
437 result = prime * result + (int) (getLastStart() ^ (getLastStart() >>> HASH_CODE_PRIME));
445 public boolean equals(final Object obj) {
453 if (getClass() != obj.getClass()) {
457 final AxEngineStats other = (AxEngineStats) obj;
458 if (!key.equals(other.key)) {
461 if (timeStamp != other.timeStamp) {
464 if (eventCount != other.eventCount) {
467 if (lastExecutionTime != other.lastExecutionTime) {
470 if (Double.compare(averageExecutionTime, other.averageExecutionTime) != 0) {
473 if (upTime != other.upTime) {
476 return getLastStart() == other.getLastStart();
483 public int compareTo(final AxConcept otherObj) {
484 if (otherObj == null) {
487 if (this == otherObj) {
490 if (getClass() != otherObj.getClass()) {
491 return this.hashCode() - otherObj.hashCode();
494 final AxEngineStats other = (AxEngineStats) otherObj;
495 if (!key.equals(other.key)) {
496 return key.compareTo(other.key);
498 if (timeStamp != other.timeStamp) {
499 return (int) (timeStamp - other.timeStamp);
501 if (eventCount != other.eventCount) {
502 return (int) (eventCount - other.eventCount);
504 if (lastExecutionTime != other.lastExecutionTime) {
505 return (int) (lastExecutionTime - other.lastExecutionTime);
507 final int result = Double.compare(averageExecutionTime, other.averageExecutionTime);
511 if (upTime != other.upTime) {
512 return (int) (upTime - other.upTime);
515 return Long.compare(lastStart, other.lastStart);