Merge "Added support for using oauth token for Kafka"
[nonrtric/plt/ranpm.git] / datafilecollector / src / main / java / org / onap / dcaegen2 / collectors / datafile / model / Counters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.model;
22
23 import java.time.Instant;
24 import java.util.concurrent.atomic.AtomicInteger;
25
26 /**
27  * Various counters that can be shown via a REST API.
28  *
29  */
30 public class Counters {
31
32     private long noOfCollectedFiles = 0;
33     private long noOfFailedFtpAttempts = 0;
34     private long noOfFailedHttpAttempts = 0;
35     private long noOfFailedFtp = 0;
36     private long noOfFailedHttp = 0;
37     private long noOfFailedPublishAttempts = 0;
38     private long totalPublishedFiles = 0;
39     private long noOfFailedPublish = 0;
40     private Instant lastPublishedTime = Instant.MIN;
41     private long totalReceivedEvents = 0;
42     private Instant lastEventTime = Instant.MIN;
43
44     public final AtomicInteger threadPoolQueueSize = new AtomicInteger();
45
46     public synchronized void incNoOfReceivedEvents() {
47         totalReceivedEvents++;
48         lastEventTime = Instant.now();
49     }
50
51     public synchronized void incNoOfCollectedFiles() {
52         noOfCollectedFiles++;
53     }
54
55     public synchronized void incNoOfFailedFtpAttempts() {
56         noOfFailedFtpAttempts++;
57     }
58
59     public synchronized void incNoOfFailedHttpAttempts() {
60         noOfFailedHttpAttempts++;
61     }
62
63     public synchronized void incNoOfFailedFtp() {
64         noOfFailedFtp++;
65     }
66
67     public synchronized void incNoOfFailedHttp() {
68         noOfFailedHttp++;
69     }
70
71     public synchronized void incNoOfFailedPublishAttempts() {
72         noOfFailedPublishAttempts++;
73     }
74
75     public synchronized void incTotalPublishedFiles() {
76         totalPublishedFiles++;
77         lastPublishedTime = Instant.now();
78     }
79
80     public synchronized void incNoOfFailedPublish() {
81         noOfFailedPublish++;
82     }
83
84     @Override
85     public synchronized String toString() {
86         StringBuilder str = new StringBuilder();
87         str.append(format("totalReceivedEvents", totalReceivedEvents));
88         str.append(format("lastEventTime", lastEventTime));
89         str.append("\n");
90         str.append(format("collectedFiles", noOfCollectedFiles));
91         str.append(format("failedFtpAttempts", noOfFailedFtpAttempts));
92         str.append(format("failedHttpAttempts", noOfFailedHttpAttempts));
93         str.append(format("failedFtp", noOfFailedFtp));
94         str.append(format("failedHttp", noOfFailedHttp));
95         str.append("\n");
96         str.append(format("totalPublishedFiles", totalPublishedFiles));
97         str.append(format("lastPublishedTime", lastPublishedTime));
98
99         str.append(format("failedPublishAttempts", noOfFailedPublishAttempts));
100         str.append(format("noOfFailedPublish", noOfFailedPublish));
101
102         return str.toString();
103     }
104
105     private static String format(String name, Object value) {
106         String header = name + ":";
107         return String.format("%-24s%-22s%n", header, value);
108     }
109
110     public long getNoOfCollectedFiles() {
111         return noOfCollectedFiles;
112     }
113
114     public long getNoOfFailedFtpAttempts() {
115         return noOfFailedFtpAttempts;
116     }
117
118     public long getNoOfFailedHttpAttempts() {
119         return noOfFailedHttpAttempts;
120     }
121
122     public long getNoOfFailedFtp() {
123         return noOfFailedFtp;
124     }
125
126     public long getNoOfFailedHttp() {
127         return noOfFailedHttp;
128     }
129
130     public long getNoOfFailedPublishAttempts() {
131         return noOfFailedPublishAttempts;
132     }
133
134     public long getTotalPublishedFiles() {
135         return totalPublishedFiles;
136     }
137
138     public long getNoOfFailedPublish() {
139         return noOfFailedPublish;
140     }
141
142     public long getTotalReceivedEvents() {
143         return totalReceivedEvents;
144     }
145 }