blob: bd56778b34c9630c119625a4ef77a1357c364afe [file] [log] [blame]
Jian Li65f5aa22016-03-22 11:49:42 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
18import org.apache.commons.lang.StringUtils;
19import org.joda.time.DateTime;
20import org.joda.time.format.DateTimeFormat;
21import org.joda.time.format.DateTimeFormatter;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Default implementation of influx metric.
27 */
28public final class DefaultInfluxMetric implements InfluxMetric {
29
30 private double oneMinRate;
31 private DateTime time;
32
33 private DefaultInfluxMetric(double oneMinRate, DateTime time) {
34 this.oneMinRate = oneMinRate;
35 this.time = time;
36 }
37
38 @Override
39 public double oneMinRate() {
40 return oneMinRate;
41 }
42
43 @Override
44 public DateTime time() {
45 return time;
46 }
47
48 public static final class Builder implements InfluxMetric.Builder {
49
50 private double oneMinRate;
51 private String timestamp;
52 private static final String TIMESTAMP_MSG = "Must specify a timestamp.";
53 private static final String ONE_MIN_RATE_MSG = "Must specify one minute rate.";
54
55 public Builder() {
56 }
57
58 @Override
59 public InfluxMetric.Builder oneMinRate(double rate) {
60 this.oneMinRate = rate;
61 return this;
62 }
63
64 @Override
65 public InfluxMetric.Builder time(String time) {
66 this.timestamp = time;
67 return this;
68 }
69
70 @Override
71 public InfluxMetric build() {
72 checkNotNull(oneMinRate, ONE_MIN_RATE_MSG);
73 checkNotNull(timestamp, TIMESTAMP_MSG);
74
75 return new DefaultInfluxMetric(oneMinRate, parseTime(timestamp));
76 }
77
78 private DateTime parseTime(String time) {
79 String reformatTime = StringUtils.replace(StringUtils.replace(time, "T", " "), "Z", "");
80 DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
81 return formatter.parseDateTime(reformatTime);
82 }
83 }
84}