Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit b1f5862

Browse files
committed
重构带宽和流量统计
1 parent 0ecd302 commit b1f5862

File tree

7 files changed

+158
-165
lines changed

7 files changed

+158
-165
lines changed

src/main/java/org/code4everything/qiniu/api/config/SdkConfigurer.java renamed to src/main/java/org/code4everything/qiniu/api/SdkConfigurer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.code4everything.qiniu.api.config;
1+
package org.code4everything.qiniu.api;
22

33
import com.qiniu.cdn.CdnManager;
44
import com.qiniu.common.Zone;
@@ -11,8 +11,8 @@
1111
import javafx.application.Platform;
1212
import org.apache.log4j.Logger;
1313
import org.code4everything.qiniu.constant.QiniuValueConsts;
14-
import org.code4everything.qiniu.util.QiniuUtils;
1514
import org.code4everything.qiniu.util.DialogUtils;
15+
import org.code4everything.qiniu.util.QiniuUtils;
1616

1717
import java.io.IOException;
1818
import java.nio.file.Paths;
@@ -22,7 +22,6 @@
2222
/**
2323
* @author pantao
2424
*/
25-
2625
public class SdkConfigurer {
2726

2827
private static final Map<String, Zone> ZONE = new HashMap<>();

src/main/java/org/code4everything/qiniu/api/QiManager.java renamed to src/main/java/org/code4everything/qiniu/api/SdkManager.java

Lines changed: 25 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
package org.code4everything.qiniu.api;
22

33
import com.qiniu.cdn.CdnResult;
4-
import com.qiniu.cdn.CdnResult.BandwidthData;
5-
import com.qiniu.cdn.CdnResult.FluxData;
64
import com.qiniu.cdn.CdnResult.LogData;
75
import com.qiniu.common.QiniuException;
86
import com.qiniu.http.Response;
97
import com.qiniu.storage.BucketManager;
108
import com.qiniu.storage.model.BatchStatus;
119
import com.zhazhapan.util.Checker;
1210
import com.zhazhapan.util.Formatter;
13-
import javafx.application.Platform;
1411
import javafx.collections.FXCollections;
1512
import javafx.collections.ObservableList;
16-
import javafx.scene.chart.XYChart.Data;
17-
import javafx.scene.chart.XYChart.Series;
1813
import org.apache.log4j.Logger;
1914
import org.code4everything.qiniu.QiniuApplication;
20-
import org.code4everything.qiniu.api.config.SdkConfigurer;
2115
import org.code4everything.qiniu.constant.QiniuValueConsts;
2216
import org.code4everything.qiniu.controller.MainWindowController;
2317
import org.code4everything.qiniu.model.FileBean;
24-
import org.code4everything.qiniu.util.QiniuUtils;
2518
import org.code4everything.qiniu.util.DialogUtils;
19+
import org.code4everything.qiniu.util.QiniuUtils;
2620

2721
import java.io.UnsupportedEncodingException;
2822
import java.net.URLEncoder;
@@ -32,128 +26,22 @@
3226
/**
3327
* @author pantao
3428
*/
35-
public class QiManager {
36-
37-
private Logger logger = Logger.getLogger(QiManager.class);
38-
39-
/**
40-
* 获取空间带宽统计,使用默认的KB单位
41-
*
42-
* @param domains domains
43-
* @param fromDate fromDate
44-
* @param toDate toDate
45-
*
46-
* @return {@link Series}
47-
*/
48-
public Series<String, Long> getBucketBandwidth(String[] domains, String fromDate, String toDate) {
49-
return getBucketBandwidth(domains, fromDate, toDate, "KB");
50-
}
29+
public class SdkManager {
5130

52-
/**
53-
* 获取空间带宽统计,使用自定义单位
54-
*
55-
* @param domains domains
56-
* @param fromDate from Date
57-
* @param toDate toDate
58-
* @param countUnit countUnit
59-
*
60-
* @return {@link Series}
61-
*/
62-
public Series<String, Long> getBucketBandwidth(String[] domains, String fromDate, String toDate, String countUnit) {
63-
CdnResult.BandwidthResult bandwidthResult = null;
64-
try {
65-
bandwidthResult = SdkConfigurer.getCdnManager().getBandwidthData(domains, fromDate, toDate, "day");
66-
} catch (QiniuException e) {
67-
logger.error("get bucket bandwidth error, message: " + e.getMessage());
68-
Platform.runLater(() -> DialogUtils.showException(QiniuValueConsts.BUCKET_BAND_ERROR, e));
69-
}
70-
Series<String, Long> bandSer = new Series<>();
71-
bandSer.setName(QiniuValueConsts.BUCKET_BANDWIDTH_COUNT.replaceAll("[A-Z]+", countUnit));
72-
// 获取带宽统计
73-
if (Checker.isNotNull(bandwidthResult) && Checker.isNotEmpty(bandwidthResult.data)) {
74-
for (Map.Entry<String, BandwidthData> bandwidth : bandwidthResult.data.entrySet()) {
75-
String[] times = bandwidthResult.time;
76-
BandwidthData bandwidthData = bandwidth.getValue();
77-
int i = 0;
78-
for (String time : times) {
79-
long size = 0;
80-
if (Checker.isNotNull(bandwidthData)) {
81-
if (Checker.isNotNull(bandwidthData.china)) {
82-
size += bandwidthData.china[i];
83-
}
84-
if (Checker.isNotNull(bandwidthData.oversea)) {
85-
size += bandwidthData.oversea[i];
86-
}
87-
}
88-
long unit = Formatter.sizeToLong("1 " + countUnit);
89-
bandSer.getData().add(new Data<>(time.substring(5, 10), size / unit));
90-
i++;
91-
}
92-
}
93-
} else {
94-
logger.info("bandwidth is empty of this domain");
95-
}
96-
return bandSer;
97-
}
31+
private static final Logger LOGGER = Logger.getLogger(SdkManager.class);
9832

9933
/**
100-
* 获取空间的流量统计,使用默认的KB单位
101-
*
102-
* @param domains domains
103-
* @param fromDate fromDate
104-
* @param toDate toDate
105-
*
106-
* @return {@link Series}
34+
* 获取空间带宽统计
10735
*/
108-
public Series<String, Long> getBucketFlux(String[] domains, String fromDate, String toDate) {
109-
return getBucketFlux(domains, fromDate, toDate, "KB");
36+
public CdnResult.BandwidthResult getBucketBandwidth(String[] domains, String startDate, String endDate) throws QiniuException {
37+
return SdkConfigurer.getCdnManager().getBandwidthData(domains, startDate, endDate, "day");
11038
}
11139

11240
/**
113-
* 获取空间的流量统计,自定义统计单位
114-
*
115-
* @param domains domains
116-
* @param fromDate fromDate
117-
* @param toDate toDate
118-
* @param countUnit countUnit
119-
*
120-
* @return {@link Series}
41+
* 获取空间的流量统计
12142
*/
122-
public Series<String, Long> getBucketFlux(String[] domains, String fromDate, String toDate, String countUnit) {
123-
CdnResult.FluxResult fluxResult = null;
124-
try {
125-
fluxResult = SdkConfigurer.getCdnManager().getFluxData(domains, fromDate, toDate, "day");
126-
} catch (QiniuException e) {
127-
logger.error("get bucket flux error, message: " + e.getMessage());
128-
Platform.runLater(() -> DialogUtils.showException(QiniuValueConsts.BUCKET_FLUX_ERROR, e));
129-
}
130-
Series<String, Long> fluxSer = new Series<>();
131-
fluxSer.setName(QiniuValueConsts.BUCKET_FLUX_COUNT.replaceAll("[A-Z]+", countUnit));
132-
// 获取流量统计
133-
if (Checker.isNotNull(fluxResult) && Checker.isNotEmpty(fluxResult.data)) {
134-
for (Map.Entry<String, FluxData> flux : fluxResult.data.entrySet()) {
135-
String[] times = fluxResult.time;
136-
FluxData fluxData = flux.getValue();
137-
int i = 0;
138-
for (String time : times) {
139-
long size = 0;
140-
if (Checker.isNotNull(fluxData)) {
141-
if (Checker.isNotNull(fluxData.china)) {
142-
size += fluxData.china[i];
143-
}
144-
if (Checker.isNotNull(fluxData.oversea)) {
145-
size += fluxData.oversea[i];
146-
}
147-
}
148-
long unit = Formatter.sizeToLong("1 " + countUnit);
149-
fluxSer.getData().add(new Data<>(time.substring(5, 10), size / unit));
150-
i++;
151-
}
152-
}
153-
} else {
154-
logger.info("flux is empty of this domain");
155-
}
156-
return fluxSer;
43+
public CdnResult.FluxResult getBucketFlux(String[] domains, String startDate, String endDate) throws QiniuException {
44+
return SdkConfigurer.getCdnManager().getFluxData(domains, startDate, endDate, "day");
15745
}
15846

15947
/**
@@ -173,7 +61,7 @@ public void downloadCdnLog(String logDate) {
17361
}
17462
}
17563
} catch (QiniuException e) {
176-
logger.error("get cdn log url error, message: " + e.getMessage());
64+
LOGGER.error("get cdn log url error, message: " + e.getMessage());
17765
DialogUtils.showException(e);
17866
}
17967
}
@@ -200,9 +88,9 @@ private void refreshFile(String[] files) {
20088
try {
20189
// 单次方法调用刷新的链接不可以超过100个
20290
SdkConfigurer.getCdnManager().refreshUrls(files);
203-
logger.info("refresh files success");
91+
LOGGER.info("refresh files success");
20492
} catch (QiniuException e) {
205-
logger.error("refresh files error, message: " + e.getMessage());
93+
LOGGER.error("refresh files error, message: " + e.getMessage());
20694
DialogUtils.showException(e);
20795
}
20896
}
@@ -245,9 +133,9 @@ public void updateFile(String bucket, String key) {
245133
String log = "update file '" + key + "' on bucket '" + bucket;
246134
try {
247135
SdkConfigurer.getBucketManager().prefetch(bucket, key);
248-
logger.info(log + "' success");
136+
LOGGER.info(log + "' success");
249137
} catch (QiniuException e) {
250-
logger.error(log + "' error, message: " + e.getMessage());
138+
LOGGER.error(log + "' error, message: " + e.getMessage());
251139
DialogUtils.showException(QiniuValueConsts.UPDATE_ERROR, e);
252140
}
253141
}
@@ -259,9 +147,9 @@ public void setFileLife(String bucket, String key, int days) {
259147
String log = "set file of '" + key + "' life to " + days + " day(s) ";
260148
try {
261149
SdkConfigurer.getBucketManager().deleteAfterDays(bucket, key, days);
262-
logger.info(log + "success");
150+
LOGGER.info(log + "success");
263151
} catch (QiniuException e) {
264-
logger.error(log + "error, message: " + e.getMessage());
152+
LOGGER.error(log + "error, message: " + e.getMessage());
265153
DialogUtils.showException(QiniuValueConsts.MOVE_OR_RENAME_ERROR, e);
266154
}
267155
}
@@ -294,10 +182,10 @@ public boolean moveOrCopyFile(String fromBucket, String fromKey, String toBucket
294182
} else {
295183
SdkConfigurer.getBucketManager().move(fromBucket, fromKey, toBucket, toKey, true);
296184
}
297-
logger.info(log + " success");
185+
LOGGER.info(log + " success");
298186
return true;
299187
} catch (QiniuException e) {
300-
logger.error(log + " failed, message: " + e.getMessage());
188+
LOGGER.error(log + " failed, message: " + e.getMessage());
301189
DialogUtils.showException(QiniuValueConsts.MOVE_OR_RENAME_ERROR, e);
302190
return false;
303191
}
@@ -313,10 +201,10 @@ public boolean changeType(String fileName, String newType, String bucket) {
313201
String log = "change file '" + fileName + "' type '" + newType + "' on bucket '" + bucket;
314202
try {
315203
SdkConfigurer.getBucketManager().changeMime(bucket, fileName, newType);
316-
logger.info(log + "' success");
204+
LOGGER.info(log + "' success");
317205
return true;
318206
} catch (QiniuException e) {
319-
logger.error(log + "' failed, message: " + e.getMessage());
207+
LOGGER.error(log + "' failed, message: " + e.getMessage());
320208
DialogUtils.showException(QiniuValueConsts.CHANGE_FILE_TYPE_ERROR, e);
321209
return false;
322210
}
@@ -350,15 +238,15 @@ public void deleteFiles(ObservableList<FileBean> fileInfos, String bucket) {
350238
BatchStatus status = batchStatusList[i];
351239
String file = files[i];
352240
if (status.code == 200) {
353-
logger.info("delete file '" + file + "' success");
241+
LOGGER.info("delete file '" + file + "' success");
354242
QiniuApplication.data.remove(seletecFileInfos.get(i));
355243
QiniuApplication.totalLength--;
356244
QiniuApplication.totalSize -= Formatter.sizeToLong(seletecFileInfos.get(i).getSize());
357245
if (sear) {
358246
currentRes.remove(seletecFileInfos.get(i));
359247
}
360248
} else {
361-
logger.error("delete file '" + file + "' failed, message: " + status.data.error);
249+
LOGGER.error("delete file '" + file + "' failed, message: " + status.data.error);
362250
DialogUtils.showError("删除文件:" + file + " 失败");
363251
}
364252
}
@@ -379,7 +267,7 @@ public void listFileOfBucket() {
379267
BucketManager.FileListIterator iterator = SdkConfigurer.getBucketManager().createFileListIterator(bucket, "",
380268
QiniuValueConsts.BUCKET_LIST_LIMIT_SIZE, "");
381269
ArrayList<FileBean> files = new ArrayList<>();
382-
logger.info("get file list of bucket: " + bucket);
270+
LOGGER.info("get file list of bucket: " + bucket);
383271
QiniuApplication.totalLength = 0;
384272
QiniuApplication.totalSize = 0;
385273
// 处理获取的file list结果
@@ -393,15 +281,15 @@ public void listFileOfBucket() {
393281
String size = Formatter.formatSize(item.fsize);
394282
FileBean file = new FileBean(item.key, item.mimeType, size, time);
395283
files.add(file);
396-
logger.info("file name: " + item.key + ", file type: " + item.mimeType + ", file size: " + size + ", "
284+
LOGGER.info("file name: " + item.key + ", file type: " + item.mimeType + ", file size: " + size + ", "
397285
+ "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "file time: " + time);
398286
}
399287
}
400288
QiniuApplication.data = FXCollections.observableArrayList(files);
401289
}
402290

403291
private void urlError(Exception e) {
404-
logger.error("generate url error: " + e.getMessage());
292+
LOGGER.error("generate url error: " + e.getMessage());
405293
DialogUtils.showException(QiniuValueConsts.GENERATE_URL_ERROR, e);
406294
}
407295

0 commit comments

Comments
 (0)