blob: f3ce81ab79612b5b1a40ade0d80b15616dbdd601 [file] [log] [blame]
Jian Li65f5aa22016-03-22 11:49:42 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Li65f5aa22016-03-22 11:49:42 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.influxdbmetrics;
17
Jian Li65f5aa22016-03-22 11:49:42 -070018import static com.google.common.base.Preconditions.checkNotNull;
19
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070020import java.time.Instant;
21import java.time.format.DateTimeFormatter;
22
Jian Li65f5aa22016-03-22 11:49:42 -070023/**
24 * Default implementation of influx metric.
25 */
26public final class DefaultInfluxMetric implements InfluxMetric {
27
28 private double oneMinRate;
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070029 private Instant time;
Jian Li65f5aa22016-03-22 11:49:42 -070030
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070031 private DefaultInfluxMetric(double oneMinRate, Instant time) {
Jian Li65f5aa22016-03-22 11:49:42 -070032 this.oneMinRate = oneMinRate;
33 this.time = time;
34 }
35
36 @Override
37 public double oneMinRate() {
38 return oneMinRate;
39 }
40
41 @Override
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070042 public Instant time() {
Jian Li65f5aa22016-03-22 11:49:42 -070043 return time;
44 }
45
46 public static final class Builder implements InfluxMetric.Builder {
47
48 private double oneMinRate;
49 private String timestamp;
50 private static final String TIMESTAMP_MSG = "Must specify a timestamp.";
51 private static final String ONE_MIN_RATE_MSG = "Must specify one minute rate.";
52
53 public Builder() {
54 }
55
56 @Override
57 public InfluxMetric.Builder oneMinRate(double rate) {
58 this.oneMinRate = rate;
59 return this;
60 }
61
62 @Override
63 public InfluxMetric.Builder time(String time) {
64 this.timestamp = time;
65 return this;
66 }
67
68 @Override
69 public InfluxMetric build() {
70 checkNotNull(oneMinRate, ONE_MIN_RATE_MSG);
71 checkNotNull(timestamp, TIMESTAMP_MSG);
72
73 return new DefaultInfluxMetric(oneMinRate, parseTime(timestamp));
74 }
75
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070076 private Instant parseTime(String time) {
77 return DateTimeFormatter.ISO_DATE_TIME.parse(time, Instant::from);
Jian Li65f5aa22016-03-22 11:49:42 -070078 }
79 }
80}