/*
 * Copyright 2015 the original author or authors.
 *
 * 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.springframework.github;

import java.util.Arrays;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.app.metrics.FieldValueCounterWriter;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class GithubDataListener {



    @Autowired
    private FieldValueCounterWriter fieldValueCounterWriter;

    @StreamListener(Sink.INPUT)
    public void listen(GithubData data) {
        processValue("repository", data.getRepository());
        processValue("username", data.getUsername());

    }


    protected void processValue(String counterName, Object value) {
        if ((value instanceof Collection) || ObjectUtils.isArray(value)) {
            Collection<?> c = (value instanceof Collection) ? (Collection<?>) value
                    : Arrays.asList(ObjectUtils.toObjectArray(value));
            for (Object val : c) {
                this.fieldValueCounterWriter.increment(counterName, val.toString(), 1.0);
            }
        }
        else if (value!=null) {
            this.fieldValueCounterWriter.increment(counterName, value.toString(), 1.0);
        }
    }

}

@@ -1100,22 +1100,17 @@
 ener %7B%0A%0A
-%0A%0A    
+%09
 @Autowir
@@ -1112,20 +1112,17 @@
 towired%0A
-    
+%09
 private 
@@ -1171,20 +1171,17 @@
 riter;%0A%0A
-    
+%09
 @StreamL
@@ -1200,20 +1200,17 @@
 .INPUT)%0A
-    
+%09
 public v
@@ -1239,24 +1239,18 @@
 data) %7B%0A
-        
+%09%09
 processV
@@ -1291,24 +1291,18 @@
 ory());%0A
-        
+%09%09
 processV
@@ -1343,21 +1343,98 @@
 ));%0A
-%0A    %7D%0A%0A%0A    
+%09%09processValue(%22type%22, data.getType());%0A%09%09processValue(%22action%22, data.getAction());%0A%0A%09%7D%0A%0A%09
 prot
@@ -1493,24 +1493,18 @@
 alue) %7B%0A
-        
+%09%09
 if ((val
@@ -1562,28 +1562,19 @@
 lue)) %7B%0A
-            
+%09%09%09
 Collecti
@@ -1641,28 +1641,13 @@
 lue%0A
-                    
+%09%09%09%09%09
 : Ar
@@ -1693,28 +1693,19 @@
 alue));%0A
-            
+%09%09%09
 for (Obj
@@ -1723,24 +1723,12 @@
 ) %7B%0A
-                
+%09%09%09%09
 this
@@ -1801,40 +1801,19 @@
 0);%0A
-            %7D%0A        %7D%0A        
+%09%09%09%7D%0A%09%09%7D%0A%09%09
 else
@@ -1826,18 +1826,20 @@
 alue
+ 
 !=
+ 
 null) %7B%0A
     
@@ -1838,20 +1838,11 @@
 ) %7B%0A
-            
+%09%09%09
 this
@@ -1917,22 +1917,13 @@
 0);%0A
-        %7D%0A    
+%09%09%7D%0A%09
 %7D%0A%0A%7D

Add new fields to metric store