Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.influxdbmetrics; |
| 17 | |
Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 20 | import java.time.Instant; |
| 21 | import java.time.format.DateTimeFormatter; |
| 22 | |
Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Default implementation of influx metric. |
| 25 | */ |
| 26 | public final class DefaultInfluxMetric implements InfluxMetric { |
| 27 | |
| 28 | private double oneMinRate; |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 29 | private Instant time; |
Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 30 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 31 | private DefaultInfluxMetric(double oneMinRate, Instant time) { |
Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 32 | this.oneMinRate = oneMinRate; |
| 33 | this.time = time; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public double oneMinRate() { |
| 38 | return oneMinRate; |
| 39 | } |
| 40 | |
| 41 | @Override |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 42 | public Instant time() { |
Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 43 | 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() { |
Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 70 | checkNotNull(timestamp, TIMESTAMP_MSG); |
| 71 | |
| 72 | return new DefaultInfluxMetric(oneMinRate, parseTime(timestamp)); |
| 73 | } |
| 74 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 75 | private Instant parseTime(String time) { |
| 76 | return DateTimeFormatter.ISO_DATE_TIME.parse(time, Instant::from); |
Jian Li | 65f5aa2 | 2016-03-22 11:49:42 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |