admin
2023-04-12 f06a592dd1a7e995bf313ccb5efe7dff73ccfc4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.yeshi.buwan.util.log;
 
import com.google.gson.Gson;
import com.qcloud.cos.model.COSObjectSummary;
import com.yeshi.buwan.util.COSManager;
import com.yeshi.buwan.util.HttpUtil;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.TimeUtil;
import net.sf.json.JSONObject;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.zip.GZIPInputStream;
 
/**
 * @author hxh
 * @title: SearchKeyLogUtil
 * @description: 搜索关键字日志帮助类
 * @date 2022/11/10 17:28
 */
public class SearchKeyLogUtil {
    private final static String BUCKET_NAME = "log-1255749512";
 
    private static List<SearchKey> download(String url) throws IOException {
        InputStream inputStream = HttpUtil.getAsInputStream(url);
        GZIPInputStream gzin = new GZIPInputStream(inputStream);
 
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        int num;
        byte[] buf = new byte[1024];
        while ((num = gzin.read(buf, 0, buf.length)) != -1) {
            byteOutputStream.write(buf, 0, num);
        }
 
        List<SearchKey> searchKeyList = new ArrayList<>();
        String finalString
                = new String(byteOutputStream.toByteArray(),"UTF-8");
        Scanner scanner = new Scanner(finalString);
        while (scanner.hasNextLine()) {
            String data = scanner.nextLine();
            System.out.println(new String(data.getBytes("ISO-8859-1"), "UTF-8"));
            System.out.println(new String(data.getBytes("GBK"), "UTF-8"));
            System.out.println(new String(data.getBytes("GB2312"), "UTF-8"));
            JSONObject json = JSONObject.fromObject(data);
            if (!StringUtil.isNullOrEmpty(json.optString("message"))) {
                try {
                    searchKeyList.add(new Gson().fromJson(json.optString("message"), SearchKey.class));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        scanner.close();
        return searchKeyList;
    }
 
    public static List<SearchKey> downLoadData(Date dateTime) throws IOException {
        String day = TimeUtil.getGernalTime(dateTime.getTime(), "yyyy/MM/dd");
        List<COSObjectSummary> resultList = COSManager.getInstance().listObject(BUCKET_NAME, "ap-guangzhou-buwan-search-key-1652433911/" + day);
        List<String> urlList = new ArrayList<>();
        for (COSObjectSummary object : resultList) {
            String key = object.getKey();
            String url = String.format("https://%s.cos.ap-guangzhou.myqcloud.com/%s", BUCKET_NAME, key);
            urlList.add(url);
        }
        //下载文件
        List<SearchKey> finalList = new ArrayList<>();
        for (String url : urlList) {
            List<SearchKey> list = download(url);
            finalList.addAll(list);
        }
        return finalList;
    }
 
    public static List<RankInfo> rank(List<SearchKey> keyList, int num) {
        Map<String, Integer> countMap = new HashMap<>();
        Set<String> identitySet = new HashSet<>();
        for (SearchKey key : keyList) {
            String id = key.getKey() + "-" + key.getDevice();
            if (!identitySet.contains(id)) {
                identitySet.add(id);
                if (countMap.get(key.getKey()) == null) {
                    countMap.put(key.getKey(), 0);
                }
                countMap.put(key.getKey(), countMap.get(key.getKey()) + 1);
            }
        }
        List<RankInfo> rankList = new ArrayList<>();
        for (Iterator<String> its = countMap.keySet().iterator(); its.hasNext(); ) {
            String key = its.next();
            rankList.add(new RankInfo(key, countMap.get(key)));
        }
        Comparator<RankInfo> cm = ((o1, o2) -> o2.getCount() - o1.getCount());
        Collections.sort(rankList, cm);
        if (rankList.size() > num) {
            return rankList.subList(0, num);
        }
        return rankList;
    }
 
    /**
     * @return java.util.List<com.yeshi.buwan.util.log.SearchKeyLogUtil.RankInfo>
     * @author hxh
     * @description 获取今日搜索排行
     * @date 18:54 2022/11/10
     * @param: num
     **/
    public static List<RankInfo> getTodayRankList(int num) throws IOException {
        List<SearchKey> dataList = downLoadData(new Date());
        List<RankInfo> rankList = rank(dataList, 100);
        return rankList;
    }
 
    /**
     * @return java.util.List<com.yeshi.buwan.util.log.SearchKeyLogUtil.RankInfo>
     * @author hxh
     * @description 获取日期搜索排行
     * @date 18:54 2022/11/10
     * @param: num
     **/
    public static List<RankInfo> getDayRankList(Date date, int num) throws IOException {
        List<SearchKey> dataList = downLoadData(date);
        List<RankInfo> rankList = rank(dataList, 100);
        return rankList;
    }
 
 
    public static void main(String[] args) throws IOException {
        List<RankInfo> rankList = getTodayRankList(100);
        for (RankInfo rank : rankList) {
            System.out.println(rank.getKey() + " : " + rank.getCount());
        }
    }
 
 
    static class SearchKey {
        /**
         * key : 排球少年
         * type : 153
         * detailSystemId : 44
         * device : 75f425d9-95e7-383a-914b-a33bad909280
         * createTime : 1668010401229
         */
 
        private String key;
        private String type;
        private String detailSystemId;
        private String device;
        private long createTime;
 
        public String getKey() {
            return key;
        }
 
        public void setKey(String key) {
            this.key = key;
        }
 
        public String getType() {
            return type;
        }
 
        public void setType(String type) {
            this.type = type;
        }
 
        public String getDetailSystemId() {
            return detailSystemId;
        }
 
        public void setDetailSystemId(String detailSystemId) {
            this.detailSystemId = detailSystemId;
        }
 
        public String getDevice() {
            return device;
        }
 
        public void setDevice(String device) {
            this.device = device;
        }
 
        public long getCreateTime() {
            return createTime;
        }
 
        public void setCreateTime(long createTime) {
            this.createTime = createTime;
        }
    }
 
    public static class RankInfo {
        private String key;
        private int count;
 
        public RankInfo(String key, int count) {
            this.key = key;
            this.count = count;
        }
 
        public String getKey() {
            return key;
        }
 
        public void setKey(String key) {
            this.key = key;
        }
 
        public int getCount() {
            return count;
        }
 
        public void setCount(int count) {
            this.count = count;
        }
    }
}