-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
482 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...tsdb-storm-topology/src/main/java/org/openkilda/wfm/topology/opentsdb/models/Storage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* Copyright 2023 Telstra Open Source | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openkilda.wfm.topology.opentsdb.models; | ||
|
||
import org.openkilda.messaging.info.Datapoint; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import lombok.ToString; | ||
import lombok.Value; | ||
|
||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
import java.util.SortedMap; | ||
import java.util.TreeMap; | ||
|
||
@ToString | ||
public class Storage implements Serializable { | ||
public static final String NULL_KEY = "null"; | ||
public static final char TAG_KEY_DELIMITER = '_'; | ||
public static final char TAG_VALUE_DELIMITER = ':'; | ||
public static final String NULL_TAG = "NULL_TAG"; | ||
|
||
private final Map<String, DatapointValue> map; | ||
|
||
public Storage() { | ||
this.map = new HashMap<>(); | ||
} | ||
|
||
public void add(Datapoint datapoint) { | ||
map.put(createKey(datapoint), createValue(datapoint)); | ||
} | ||
|
||
public DatapointValue get(Datapoint datapoint) { | ||
return map.get(createKey(datapoint)); | ||
} | ||
|
||
public void removeOutdated(long ttlInMillis) { | ||
long now = System.currentTimeMillis(); | ||
map.entrySet().removeIf(entry -> now - entry.getValue().getTime() > ttlInMillis); | ||
} | ||
|
||
public int size() { | ||
return map.size(); | ||
} | ||
|
||
@VisibleForTesting | ||
static String createKey(Datapoint datapoint) { | ||
if (datapoint == null) { | ||
return NULL_KEY; | ||
} | ||
StringBuilder key = new StringBuilder(); | ||
key.append(datapoint.getMetric()); | ||
|
||
if (datapoint.getTags() != null) { | ||
SortedMap<String, String> sortedTags = getSortedTags(datapoint); | ||
for (Entry<String, String> entry : sortedTags.entrySet()) { | ||
key.append(TAG_KEY_DELIMITER); | ||
key.append(entry.getKey()); | ||
key.append(TAG_VALUE_DELIMITER); | ||
key.append(entry.getValue()); | ||
} | ||
} | ||
return key.toString(); | ||
} | ||
|
||
private static DatapointValue createValue(Datapoint datapoint) { | ||
if (datapoint == null) { | ||
return null; | ||
} | ||
return new DatapointValue(datapoint.getValue(), datapoint.getTime()); | ||
} | ||
|
||
private static SortedMap<String, String> getSortedTags(Datapoint datapoint) { | ||
SortedMap<String, String> sortedTags = new TreeMap<>(); | ||
for (Entry<String, String> entry : datapoint.getTags().entrySet()) { | ||
String key = Optional.ofNullable(entry.getKey()).orElse(NULL_TAG); | ||
sortedTags.put(key, entry.getValue()); | ||
} | ||
return sortedTags; | ||
} | ||
|
||
@Value | ||
public static class DatapointValue implements Serializable { | ||
Number value; | ||
Long time; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.