11package org .baeldung .web .metric ;
22
3+ import java .text .SimpleDateFormat ;
4+ import java .util .Date ;
35import java .util .HashMap ;
6+ import java .util .Map .Entry ;
7+ import java .util .Set ;
48
59import org .springframework .stereotype .Service ;
610
@@ -9,12 +13,20 @@ public class MetricService {
913
1014 private static HashMap <String , HashMap <Integer , Integer >> metricMap = new HashMap <String , HashMap <Integer , Integer >>();
1115 private static HashMap <Integer , Integer > statusMetric = new HashMap <Integer , Integer >();
16+ private static HashMap <String , HashMap <Integer , Integer >> timeMap = new HashMap <String , HashMap <Integer , Integer >>();
17+ private static final SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm" );
1218
1319 public MetricService () {
1420 super ();
1521 }
1622
1723 public void increaseCount (final String request , final int status ) {
24+ increaseMainMetric (request , status );
25+ increaseStatusMetric (status );
26+ updateTimeMap (status );
27+ }
28+
29+ private void increaseMainMetric (final String request , final int status ) {
1830 HashMap <Integer , Integer > statusMap = metricMap .get (request );
1931 if (statusMap == null ) {
2032 statusMap = new HashMap <Integer , Integer >();
@@ -28,7 +40,9 @@ public void increaseCount(final String request, final int status) {
2840 }
2941 statusMap .put (status , count );
3042 metricMap .put (request , statusMap );
43+ }
3144
45+ private void increaseStatusMetric (final int status ) {
3246 final Integer statusCount = statusMetric .get (status );
3347 if (statusCount == null ) {
3448 statusMetric .put (status , 1 );
@@ -37,11 +51,58 @@ public void increaseCount(final String request, final int status) {
3751 }
3852 }
3953
54+ private void updateTimeMap (final int status ) {
55+ final String time = dateFormat .format (new Date ());
56+ HashMap <Integer , Integer > statusMap = timeMap .get (time );
57+ if (statusMap == null ) {
58+ statusMap = new HashMap <Integer , Integer >();
59+ }
60+
61+ Integer count = statusMap .get (status );
62+ if (count == null ) {
63+ count = 1 ;
64+ } else {
65+ count ++;
66+ }
67+ statusMap .put (status , count );
68+ timeMap .put (time , statusMap );
69+ }
70+
4071 public String getFullMetric () {
4172 return metricMap .entrySet ().toString ();
4273 }
4374
4475 public String getStatusMetric () {
4576 return statusMetric .entrySet ().toString ();
4677 }
78+
79+ public Object [][] getGraphData (){
80+ final int colCount = statusMetric .keySet ().size ()+1 ;
81+ final Set <Integer > allStatus = statusMetric .keySet ();
82+ final int rowCount = timeMap .keySet ().size ()+1 ;
83+
84+ final Object [][] result = new Object [rowCount ][colCount ];
85+ result [0 ][0 ] = "Time" ;
86+
87+ int j = 1 ;
88+ for (final int status : allStatus ) {
89+ result [0 ][j ] = status ;
90+ j ++;
91+ }
92+ int i = 1 ;
93+ HashMap <Integer , Integer > tempMap ;
94+ for (final Entry <String , HashMap <Integer , Integer >> entry : timeMap .entrySet ()) {
95+ result [i ][0 ] = entry .getKey ();
96+ tempMap = entry .getValue ();
97+ for (j = 1 ; j < colCount ; j ++) {
98+ result [i ][j ] = tempMap .get (result [0 ][j ]);
99+ if (result [i ][j ] == null ) {
100+ result [i ][j ] = 0 ;
101+ }
102+ }
103+ i ++;
104+ }
105+
106+ return result ;
107+ }
47108}
0 commit comments