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;
27 import javax.persistence.Column;
28 import javax.persistence.EmbeddedId;
29 import javax.persistence.Entity;
30 import javax.persistence.Table;
31 import javax.persistence.Transient;
32 import javax.xml.bind.annotation.XmlAccessType;
33 import javax.xml.bind.annotation.XmlAccessorType;
34 import javax.xml.bind.annotation.XmlElement;
35 import javax.xml.bind.annotation.XmlRootElement;
36 import javax.xml.bind.annotation.XmlType;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
47 import org.onap.policy.common.utils.validation.Assertions;
50 * This class is a java bean that is used to record statistics on Apex engines as they execute. Statistics on the number
51 * of events, the amount of time taken to execute the last policy, the average policy execution time, the up time of the
52 * engine, and the time stamp of the last engine start are recorded.
56 @Table(name = "AxEngineStats")
58 @XmlAccessorType(XmlAccessType.FIELD)
59 @XmlRootElement(name = "apexEngineStats", namespace = "http://www.onap.org/policy/apex-pdp")
60 @XmlType(name = "AxEngineStats", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = {"key", "timeStamp",
61 "eventCount", "lastExecutionTime", "averageExecutionTime", "upTime", "lastStart"})
62 public class AxEngineStats extends AxConcept {
63 private static final long serialVersionUID = -6981129081962785368L;
64 private static final int HASH_CODE_PRIME = 32;
67 @XmlElement(name = "key", required = true)
68 private AxReferenceKey key;
71 @XmlElement(required = true)
72 private long timeStamp;
75 @XmlElement(required = true)
76 private long eventCount;
79 @XmlElement(required = true)
80 private long lastExecutionTime;
83 @XmlElement(required = true)
84 private double averageExecutionTime;
87 @XmlElement(required = true)
92 private transient long lastEnterTime;
95 @XmlElement(required = true)
96 private long lastStart;
99 * The Default Constructor creates an engine statistics instance with a null key and with all values cleared.
101 public AxEngineStats() {
102 this(new AxReferenceKey());
105 lastExecutionTime = 0;
106 averageExecutionTime = 0;
115 * @param copyConcept the concept to copy from
117 public AxEngineStats(final AxEngineStats copyConcept) {
122 * The Keyed Constructor creates an engine statistics instance with the given key and all values cleared.
126 public AxEngineStats(final AxReferenceKey key) {
127 this(key, 0, 0, 0, 0, 0, 0);
131 * This Constructor creates an engine statistics instance with all its fields set.
133 * @param key the engine statistics key
134 * @param timeStamp the time stamp at which the statistics were recorded
135 * @param eventCount the number of events processed by the engine
136 * @param lastExecutionTime the amount of time taken to execute the last policy
137 * @param averageExecutionTime the average amount of time taken to execute a policy
138 * @param upTime the time that has elapsed since the policy engine was started
139 * @param lastStart the time at which the policy engine was last started
141 public AxEngineStats(final AxReferenceKey key, final long timeStamp, final long eventCount,
142 final long lastExecutionTime, final double averageExecutionTime, final long upTime, final long lastStart) {
144 Assertions.argumentNotNull(key, "key may not be null");
147 this.timeStamp = timeStamp;
148 this.eventCount = eventCount;
149 this.lastExecutionTime = lastExecutionTime;
150 this.averageExecutionTime = averageExecutionTime;
151 this.upTime = upTime;
152 this.lastStart = lastStart;
159 public AxReferenceKey getKey() {
167 public List<AxKey> getKeys() {
168 return key.getKeys();
172 * Sets the engine statistics key.
174 * @param key the engine statistics key
176 public void setKey(final AxReferenceKey key) {
177 Assertions.argumentNotNull(key, "key may not be null");
182 * Gets the time stamp at which the statistics were recorded.
184 * @return the time stamp at which the statistics were recorded
186 public long getTimeStamp() {
191 * Gets the time stamp at which the statistics were recorded as a string.
193 * @return the time stamp at which the statistics were recorded as a string
195 public String getTimeStampString() {
196 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timeStamp);
200 * Sets the time stamp at which the statistics were recorded.
202 * @param timeStamp the time stamp at which the statistics were recorded
204 public void setTimeStamp(final long timeStamp) {
205 this.timeStamp = timeStamp;
209 * Gets the number of events processed by the engine.
211 * @return the number of events processed by the engine
213 public long getEventCount() {
218 * Sets the number of events processed by the engine.
220 * @param eventCount the number of events processed by the engine
222 public void setEventCount(final long eventCount) {
223 this.eventCount = eventCount;
227 * Gets the amount of time taken to execute the last policy.
229 * @return the amount of time taken to execute the last policy
231 public long getLastExecutionTime() {
232 return lastExecutionTime;
236 * Sets the amount of time taken to execute the last policy.
238 * @param lastExecutionTime the amount of time taken to execute the last policy
240 public void setLastExecutionTime(final long lastExecutionTime) {
241 this.lastExecutionTime = lastExecutionTime;
245 * Gets the average amount of time taken to execute a policy.
247 * @return the average amount of time taken to execute a policy
249 public double getAverageExecutionTime() {
250 return averageExecutionTime;
254 * Sets the average amount of time taken to execute a policy.
256 * @param averageExecutionTime the average amount of time taken to execute a policy
258 public void setAverageExecutionTime(final double averageExecutionTime) {
259 this.averageExecutionTime = averageExecutionTime;
263 * Gets the time that has elapsed since the policy engine was started.
265 * @return the time that has elapsed since the policy engine was started
267 public long getUpTime() {
268 if (this.getLastStart() != 0) {
269 return upTime + (timeStamp - this.getLastStart());
275 * Sets the time that has elapsed since the policy engine was started.
277 * @param upTime the time that has elapsed since the policy engine was started
279 public void setUpTime(final long upTime) {
280 this.upTime = upTime;
284 * Sets the time at which the policy engine was last started.
286 * @param lastStart the time at which the policy engine was last started
288 private void setLastStart(final long lastStart) {
289 this.lastStart = lastStart;
293 * Gets the time at which the policy engine was last started.
295 * @return the time at which the policy engine was last started
297 public long getLastStart() {
302 * Resets all the statistic values to zero.
304 public synchronized void reset() {
307 lastExecutionTime = 0;
308 averageExecutionTime = 0;
315 * Updates the statistics when called, used by the Apex engine when it starts executing a policy.
317 * @param eventkey the key of the event that is being executed
319 public synchronized void executionEnter(final AxArtifactKey eventkey) {
320 final long now = System.currentTimeMillis();
322 if (eventCount < 0) {
330 * Updates the statistics when called, used by the Apex engine when it completes executing a policy.
332 public synchronized void executionExit() {
333 final long now = System.currentTimeMillis();
334 lastExecutionTime = now - lastEnterTime;
336 averageExecutionTime = ((averageExecutionTime * (eventCount - 1.0)) + lastExecutionTime) / eventCount;
338 timeStamp = System.currentTimeMillis();
342 * Updates the statistics when called, used by the Apex engine when it is started.
344 public synchronized void engineStart() {
345 final long now = System.currentTimeMillis();
347 this.setLastStart(now);
351 * Updates the statistics when called, used by the Apex engine when it is stopped.
353 public synchronized void engineStop() {
354 final long now = System.currentTimeMillis();
356 upTime += (timeStamp - this.getLastStart());
357 this.setLastStart(0);
364 public AxValidationResult validate(final AxValidationResult result) {
365 if (key.equals(AxReferenceKey.getNullKey())) {
366 result.addValidationMessage(
367 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
370 return key.validate(result);
377 public void clean() {
385 public String toString() {
386 final StringBuilder builder = new StringBuilder();
387 builder.append(this.getClass().getSimpleName());
388 builder.append(":(");
389 builder.append("engineKey=");
391 builder.append(",timeStamp=");
392 builder.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timeStamp));
393 builder.append(",eventCount=");
394 builder.append(eventCount);
395 builder.append(",lastExecutionTime=");
396 builder.append(lastExecutionTime);
397 builder.append(",averageExecutionTime=");
398 builder.append(averageExecutionTime);
399 builder.append(",upTime=");
400 builder.append(getUpTime());
402 return builder.toString();
409 public AxConcept copyTo(final AxConcept targetObject) {
410 Assertions.argumentNotNull(targetObject, "target may not be null");
412 final Object copyObject = targetObject;
413 Assertions.instanceOf(copyObject, AxEngineStats.class);
415 final AxEngineStats copy = ((AxEngineStats) copyObject);
416 copy.setKey(new AxReferenceKey(key));
417 copy.setTimeStamp(timeStamp);
418 copy.setEventCount(eventCount);
419 copy.setLastExecutionTime(lastExecutionTime);
420 copy.setAverageExecutionTime(averageExecutionTime);
421 copy.setUpTime(upTime);
422 copy.setLastStart(lastStart);
431 public int hashCode() {
432 final int prime = 31;
434 result = prime * result + key.hashCode();
435 result = prime * result + (int) (timeStamp ^ (timeStamp >>> HASH_CODE_PRIME));
436 result = prime * result + (int) (eventCount ^ (eventCount >>> HASH_CODE_PRIME));
437 result = prime * result + (int) (lastExecutionTime ^ (lastExecutionTime >>> HASH_CODE_PRIME));
438 result = prime * result + ((int) averageExecutionTime ^ ((int) averageExecutionTime >>> HASH_CODE_PRIME));
439 result = prime * result + (int) (upTime ^ (upTime >>> HASH_CODE_PRIME));
440 result = prime * result + (int) (getLastStart() ^ (getLastStart() >>> HASH_CODE_PRIME));
448 public boolean equals(final Object obj) {
456 if (getClass() != obj.getClass()) {
460 final AxEngineStats other = (AxEngineStats) obj;
461 if (!key.equals(other.key)) {
464 if (timeStamp != other.timeStamp) {
467 if (eventCount != other.eventCount) {
470 if (lastExecutionTime != other.lastExecutionTime) {
473 if (Double.compare(averageExecutionTime, other.averageExecutionTime) != 0) {
476 if (upTime != other.upTime) {
479 return getLastStart() == other.getLastStart();
486 public int compareTo(final AxConcept otherObj) {
487 if (otherObj == null) {
490 if (this == otherObj) {
493 if (getClass() != otherObj.getClass()) {
494 return this.hashCode() - otherObj.hashCode();
497 final AxEngineStats other = (AxEngineStats) otherObj;
498 if (!key.equals(other.key)) {
499 return key.compareTo(other.key);
501 if (timeStamp != other.timeStamp) {
502 return (int) (timeStamp - other.timeStamp);
504 if (eventCount != other.eventCount) {
505 return (int) (eventCount - other.eventCount);
507 if (lastExecutionTime != other.lastExecutionTime) {
508 return (int) (lastExecutionTime - other.lastExecutionTime);
510 final int result = Double.compare(averageExecutionTime, other.averageExecutionTime);
514 if (upTime != other.upTime) {
515 return (int) (upTime - other.upTime);
518 return Long.compare(lastStart, other.lastStart);