Subversion Repositories SmartDukaan

Rev

Rev 3516 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2729 rajveer 1
package in.shop2020.ui.util;
2
 
3
import in.shop2020.metamodel.core.EntityState;
4
import in.shop2020.metamodel.util.CreationUtils;
8258 amit.gupta 5
import in.shop2020.util.Utils;
2729 rajveer 6
 
7
import java.io.BufferedReader;
8258 amit.gupta 8
import java.io.File;
2729 rajveer 9
import java.io.InputStreamReader;
2737 rajveer 10
import java.io.Serializable;
2729 rajveer 11
import java.net.URL;
12
import java.net.URLConnection;
3516 rajveer 13
import java.util.Calendar;
2729 rajveer 14
import java.util.Comparator;
15
import java.util.HashMap;
16
import java.util.LinkedHashMap;
17
import java.util.Map;
18
import java.util.Map.Entry;
19
import java.util.TreeMap;
20
 
8258 amit.gupta 21
import org.apache.commons.io.FileUtils;
2729 rajveer 22
import org.json.JSONException;
23
import org.json.JSONObject;
24
 
2739 rajveer 25
public class ComparisonStatsFetcher implements Serializable{
2799 rajveer 26
	private static final long serialVersionUID = -326425176723602170L;
2729 rajveer 27
	public static final String GOOGLE_COMPARE_DATA_URL = "http://saholic-datastore.appspot.com/show-comparisons";
28
 
2737 rajveer 29
	class ValueComparator implements Comparator, Serializable {
2797 rajveer 30
		private static final long serialVersionUID = 6060119992669086920L;
2795 rajveer 31
	    Map base;
32
        public ValueComparator(Map base) {
33
	    this.base = base;
34
	  }
35
	  public int compare(Object a, Object b) {
36
	    if((Long)base.get(a) < (Long)base.get(b)) {
37
	      return 1;
38
	    } else {
39
	      return -1;
2729 rajveer 40
	    }
2795 rajveer 41
	  }
42
	}
2729 rajveer 43
 
44
 
3516 rajveer 45
	public void fetchAndStoreComparisonStats(){
46
		String data = ComparisonStatsFetcher.sendGetRequest(GOOGLE_COMPARE_DATA_URL, null);
2729 rajveer 47
		try {
48
			JSONObject json = new JSONObject(data);
49
			Map<Long, Map<Long, Long>> comparisonStats = new HashMap<Long, Map<Long, Long>>();
50
 
51
			for (String itemId : JSONObject.getNames(json)) {
52
				Map<Long, Long> freq = new HashMap<Long, Long>();
53
				JSONObject items = (JSONObject) json.get(itemId);
54
				for (String comparedItemId : JSONObject.getNames(items)) {
55
					Integer frequency = (Integer) items.get(comparedItemId);
56
					freq.put(Long.parseLong(comparedItemId), frequency.longValue());
57
				}
58
				comparisonStats.put(Long.parseLong(itemId), freq);
59
			}
60
 
61
			if(comparisonStats != null){
62
				Map<Long, Map<Long, Long>> sortedComparisonStats = new HashMap<Long, Map<Long, Long>>();
63
				for(Entry<Long, Map<Long, Long>> entry: comparisonStats.entrySet()){
64
					ValueComparator bvc =  new ValueComparator(entry.getValue());
65
					TreeMap<Long, Long> sortedMap = new TreeMap(bvc);
66
					sortedMap.putAll(entry.getValue());
67
					sortedComparisonStats.put(entry.getKey(), sortedMap);
68
				}
69
				CreationUtils.storeComparisonStats(sortedComparisonStats);
70
			}
71
		} catch (JSONException e) {
72
			// TODO Auto-generated catch block
73
			e.printStackTrace();
74
		} catch (Exception e) {
75
			// TODO Auto-generated catch block
76
			e.printStackTrace();
77
		}
78
	}
79
 
80
	private static Map<String, Long> getMostComparedPhones(Map<Long, Map<Long, Long>> comparisonStats){
81
		Map<String, Long> allCounts = new HashMap<String, Long>(); 
82
		for(Long key: comparisonStats.keySet()){
83
			for(Long key1: comparisonStats.get(key).keySet()){
84
				String newKey;
85
				if(key<key1){
86
					newKey = key + " : " + key;
87
					//newKey = key + "\t" + ComparisonStatsFetcher.getProductName(es) + " : " + key1 + "\t" + ComparisonStatsFetcher.getProductName(es1);   
88
				}else{
89
					//newKey = key1 + "\t" + ComparisonStatsFetcher.getProductName(es1) + " : " + key + "\t" + ComparisonStatsFetcher.getProductName(es);
90
					newKey = key1 + " : " + key;
91
				}
92
				allCounts.put(newKey, comparisonStats.get(key).get(key1));
93
			}
94
		}
95
		return allCounts;
96
	}
97
 
98
	private static Map<Long, Long> getTopComparedPhones(Map<Long, Map<Long, Long>> comparisonStats){
99
		Map<Long, Long> mostCompared = new LinkedHashMap<Long, Long>(); 
100
		for(Long key: comparisonStats.keySet()){
101
			long count = 0;
102
			for(Long key1: comparisonStats.get(key).keySet()){
103
				count += comparisonStats.get(key).get(key1);
104
			}
105
			mostCompared.put(key, new Long(count));
106
		}
107
		return mostCompared;
108
	}
109
 
110
	public static void main(String[] args) throws Exception{
3516 rajveer 111
		Calendar cal = Calendar.getInstance();
112
        //int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
113
        //if(dayOfWeek == Calendar.SUNDAY){
114
    		long toDate = cal.getTime().getTime();
115
    		cal.add(Calendar.MONTH, -1);
116
    		long fromDate = cal.getTime().getTime();
117
        	ComparisonStatsFetcher csf = new ComparisonStatsFetcher();
118
        	csf.fetchAndStoreComparisonStats();
2729 rajveer 119
	}
120
 
121
 
122
	/**
123
	 * 
124
	 * @param endpoint
125
	 * @param requestParameters
126
	 * @return
127
	 */
128
	 public static String sendGetRequest(String endpoint, String requestParameters)
129
     {
130
             String result = null;
131
             try
132
             {
133
                     String urlStr = endpoint;
134
                     if (requestParameters != null && requestParameters.length () > 0){
135
                             urlStr += "?" + requestParameters;
136
                     }
137
                     URL url = new URL(urlStr);
138
                     URLConnection conn = url.openConnection ();
139
 
140
                     // Get the response
141
                     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
142
                     StringBuffer sb = new StringBuffer();
143
                     String line;
144
                     while ((line = rd.readLine()) != null)
145
                     {
146
                             sb.append(line);
147
                     }
148
                     rd.close();
149
                     result = sb.toString();
150
             } catch (Exception e){
8258 amit.gupta 151
                     String filename = Utils.EXPORT_PATH + "comparestats.json";
152
                     try { 
153
                    	result = FileUtils.readFileToString(new File(filename));
154
                     }catch (Exception e1){
155
                    	e1.printStackTrace(); 
156
                     }
2729 rajveer 157
             }
158
 
159
             return result;
160
     }
161
 
162
		/**
163
	     * Get the name of the product from entity. It considers null values also
164
	     * @param EntityState
165
	     * @return
166
	     */
167
		public static String getProductName(EntityState EntityState){
168
			//FIXME Use stringbuilder
169
			String productName = ((EntityState.getBrand() != null) ? EntityState.getBrand().trim() + " " : "")
170
			+ ((EntityState.getModelName() != null) ? EntityState.getModelName().trim() + " " : "")
171
			+ (( EntityState.getModelNumber() != null ) ? EntityState.getModelNumber().trim() : "" );	
172
			return productName;
173
		}
174
}