1 package org.apache.helix.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.helix.HelixProperty;
27 import org.apache.helix.ZNRecord;
28 import org.apache.helix.alerts.ExpressionParser;
29 import org.apache.helix.alerts.StatsHolder;
30 import org.apache.helix.model.Message.Attributes;
31 import org.apache.log4j.Logger;
32
33
34 public class HealthStat extends HelixProperty
35 {
36 public enum HealthStatProperty
37 {
38 FIELDS
39 }
40 private static final Logger _logger = Logger.getLogger(HealthStat.class.getName());
41
42 public HealthStat(String id)
43 {
44 super(id);
45 }
46
47 public HealthStat(ZNRecord record)
48 {
49 super(record);
50 if(getCreateTimeStamp() == 0)
51 {
52 _record.setSimpleField(Attributes.CREATE_TIMESTAMP.toString(), ""
53 + new Date().getTime());
54 }
55 }
56
57 public long getLastModifiedTimeStamp()
58 {
59 return _record.getModifiedTime();
60 }
61
62 public long getCreateTimeStamp()
63 {
64 if (_record.getSimpleField(Attributes.CREATE_TIMESTAMP.toString()) == null)
65 {
66 return 0;
67 }
68 try
69 {
70 return Long.parseLong(_record.getSimpleField(Attributes.CREATE_TIMESTAMP.toString()));
71 } catch (Exception e)
72 {
73 return 0;
74 }
75 }
76
77 public String getTestField()
78 {
79 return _record.getSimpleField("requestCountStat");
80 }
81
82 public void setHealthFields(Map<String, Map<String, String>> healthFields)
83 {
84 _record.setMapFields(healthFields);
85 }
86
87 public String buildCompositeKey(String instance, String parentKey, String statName ) {
88 String delim = ExpressionParser.statFieldDelim;
89 return instance+delim+parentKey+delim+statName;
90 }
91
92 public Map<String, Map<String, String>> getHealthFields(String instanceName)
93
94
95 {
96
97
98 Map<String, Map<String, String>> currMapFields = _record.getMapFields();
99 Map<String, Map<String, String>> convertedMapFields = new HashMap<String, Map<String, String>>();
100 for (String key : currMapFields.keySet())
101 {
102 Map<String, String> currMap = currMapFields.get(key);
103 String timestamp = "-1";
104 if (_record.getSimpleFields().keySet().contains(StatsHolder.TIMESTAMP_NAME))
105 {
106 timestamp = _record.getSimpleField(StatsHolder.TIMESTAMP_NAME);
107 }
108 for (String subKey : currMap.keySet())
109 {
110 if (subKey.equals("StatsHolder.TIMESTAMP_NAME"))
111 {
112 continue;
113 }
114 String compositeKey = buildCompositeKey(instanceName, key, subKey);
115 String value = currMap.get(subKey);
116 Map<String, String> convertedMap = new HashMap<String, String>();
117 convertedMap.put(StatsHolder.VALUE_NAME, value);
118 convertedMap.put(StatsHolder.TIMESTAMP_NAME, timestamp);
119 convertedMapFields.put(compositeKey, convertedMap);
120 }
121 }
122 return convertedMapFields;
123 }
124
125 @Override
126 public boolean isValid() {
127
128 return true;
129 }
130 }