1
1
package org .code4everything .qiniu .api ;
2
2
3
3
import com .qiniu .cdn .CdnResult ;
4
- import com .qiniu .cdn .CdnResult .BandwidthData ;
5
- import com .qiniu .cdn .CdnResult .FluxData ;
6
4
import com .qiniu .cdn .CdnResult .LogData ;
7
5
import com .qiniu .common .QiniuException ;
8
6
import com .qiniu .http .Response ;
9
7
import com .qiniu .storage .BucketManager ;
10
8
import com .qiniu .storage .model .BatchStatus ;
11
9
import com .zhazhapan .util .Checker ;
12
10
import com .zhazhapan .util .Formatter ;
13
- import javafx .application .Platform ;
14
11
import javafx .collections .FXCollections ;
15
12
import javafx .collections .ObservableList ;
16
- import javafx .scene .chart .XYChart .Data ;
17
- import javafx .scene .chart .XYChart .Series ;
18
13
import org .apache .log4j .Logger ;
19
14
import org .code4everything .qiniu .QiniuApplication ;
20
- import org .code4everything .qiniu .api .config .SdkConfigurer ;
21
15
import org .code4everything .qiniu .constant .QiniuValueConsts ;
22
16
import org .code4everything .qiniu .controller .MainWindowController ;
23
17
import org .code4everything .qiniu .model .FileBean ;
24
- import org .code4everything .qiniu .util .QiniuUtils ;
25
18
import org .code4everything .qiniu .util .DialogUtils ;
19
+ import org .code4everything .qiniu .util .QiniuUtils ;
26
20
27
21
import java .io .UnsupportedEncodingException ;
28
22
import java .net .URLEncoder ;
32
26
/**
33
27
* @author pantao
34
28
*/
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 {
51
30
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 );
98
32
99
33
/**
100
- * 获取空间的流量统计,使用默认的KB单位
101
- *
102
- * @param domains domains
103
- * @param fromDate fromDate
104
- * @param toDate toDate
105
- *
106
- * @return {@link Series}
34
+ * 获取空间带宽统计
107
35
*/
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 " );
110
38
}
111
39
112
40
/**
113
- * 获取空间的流量统计,自定义统计单位
114
- *
115
- * @param domains domains
116
- * @param fromDate fromDate
117
- * @param toDate toDate
118
- * @param countUnit countUnit
119
- *
120
- * @return {@link Series}
41
+ * 获取空间的流量统计
121
42
*/
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" );
157
45
}
158
46
159
47
/**
@@ -173,7 +61,7 @@ public void downloadCdnLog(String logDate) {
173
61
}
174
62
}
175
63
} 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 ());
177
65
DialogUtils .showException (e );
178
66
}
179
67
}
@@ -200,9 +88,9 @@ private void refreshFile(String[] files) {
200
88
try {
201
89
// 单次方法调用刷新的链接不可以超过100个
202
90
SdkConfigurer .getCdnManager ().refreshUrls (files );
203
- logger .info ("refresh files success" );
91
+ LOGGER .info ("refresh files success" );
204
92
} catch (QiniuException e ) {
205
- logger .error ("refresh files error, message: " + e .getMessage ());
93
+ LOGGER .error ("refresh files error, message: " + e .getMessage ());
206
94
DialogUtils .showException (e );
207
95
}
208
96
}
@@ -245,9 +133,9 @@ public void updateFile(String bucket, String key) {
245
133
String log = "update file '" + key + "' on bucket '" + bucket ;
246
134
try {
247
135
SdkConfigurer .getBucketManager ().prefetch (bucket , key );
248
- logger .info (log + "' success" );
136
+ LOGGER .info (log + "' success" );
249
137
} catch (QiniuException e ) {
250
- logger .error (log + "' error, message: " + e .getMessage ());
138
+ LOGGER .error (log + "' error, message: " + e .getMessage ());
251
139
DialogUtils .showException (QiniuValueConsts .UPDATE_ERROR , e );
252
140
}
253
141
}
@@ -259,9 +147,9 @@ public void setFileLife(String bucket, String key, int days) {
259
147
String log = "set file of '" + key + "' life to " + days + " day(s) " ;
260
148
try {
261
149
SdkConfigurer .getBucketManager ().deleteAfterDays (bucket , key , days );
262
- logger .info (log + "success" );
150
+ LOGGER .info (log + "success" );
263
151
} catch (QiniuException e ) {
264
- logger .error (log + "error, message: " + e .getMessage ());
152
+ LOGGER .error (log + "error, message: " + e .getMessage ());
265
153
DialogUtils .showException (QiniuValueConsts .MOVE_OR_RENAME_ERROR , e );
266
154
}
267
155
}
@@ -294,10 +182,10 @@ public boolean moveOrCopyFile(String fromBucket, String fromKey, String toBucket
294
182
} else {
295
183
SdkConfigurer .getBucketManager ().move (fromBucket , fromKey , toBucket , toKey , true );
296
184
}
297
- logger .info (log + " success" );
185
+ LOGGER .info (log + " success" );
298
186
return true ;
299
187
} catch (QiniuException e ) {
300
- logger .error (log + " failed, message: " + e .getMessage ());
188
+ LOGGER .error (log + " failed, message: " + e .getMessage ());
301
189
DialogUtils .showException (QiniuValueConsts .MOVE_OR_RENAME_ERROR , e );
302
190
return false ;
303
191
}
@@ -313,10 +201,10 @@ public boolean changeType(String fileName, String newType, String bucket) {
313
201
String log = "change file '" + fileName + "' type '" + newType + "' on bucket '" + bucket ;
314
202
try {
315
203
SdkConfigurer .getBucketManager ().changeMime (bucket , fileName , newType );
316
- logger .info (log + "' success" );
204
+ LOGGER .info (log + "' success" );
317
205
return true ;
318
206
} catch (QiniuException e ) {
319
- logger .error (log + "' failed, message: " + e .getMessage ());
207
+ LOGGER .error (log + "' failed, message: " + e .getMessage ());
320
208
DialogUtils .showException (QiniuValueConsts .CHANGE_FILE_TYPE_ERROR , e );
321
209
return false ;
322
210
}
@@ -350,15 +238,15 @@ public void deleteFiles(ObservableList<FileBean> fileInfos, String bucket) {
350
238
BatchStatus status = batchStatusList [i ];
351
239
String file = files [i ];
352
240
if (status .code == 200 ) {
353
- logger .info ("delete file '" + file + "' success" );
241
+ LOGGER .info ("delete file '" + file + "' success" );
354
242
QiniuApplication .data .remove (seletecFileInfos .get (i ));
355
243
QiniuApplication .totalLength --;
356
244
QiniuApplication .totalSize -= Formatter .sizeToLong (seletecFileInfos .get (i ).getSize ());
357
245
if (sear ) {
358
246
currentRes .remove (seletecFileInfos .get (i ));
359
247
}
360
248
} else {
361
- logger .error ("delete file '" + file + "' failed, message: " + status .data .error );
249
+ LOGGER .error ("delete file '" + file + "' failed, message: " + status .data .error );
362
250
DialogUtils .showError ("删除文件:" + file + " 失败" );
363
251
}
364
252
}
@@ -379,7 +267,7 @@ public void listFileOfBucket() {
379
267
BucketManager .FileListIterator iterator = SdkConfigurer .getBucketManager ().createFileListIterator (bucket , "" ,
380
268
QiniuValueConsts .BUCKET_LIST_LIMIT_SIZE , "" );
381
269
ArrayList <FileBean > files = new ArrayList <>();
382
- logger .info ("get file list of bucket: " + bucket );
270
+ LOGGER .info ("get file list of bucket: " + bucket );
383
271
QiniuApplication .totalLength = 0 ;
384
272
QiniuApplication .totalSize = 0 ;
385
273
// 处理获取的file list结果
@@ -393,15 +281,15 @@ public void listFileOfBucket() {
393
281
String size = Formatter .formatSize (item .fsize );
394
282
FileBean file = new FileBean (item .key , item .mimeType , size , time );
395
283
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 + ", "
397
285
+ "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "file time: " + time );
398
286
}
399
287
}
400
288
QiniuApplication .data = FXCollections .observableArrayList (files );
401
289
}
402
290
403
291
private void urlError (Exception e ) {
404
- logger .error ("generate url error: " + e .getMessage ());
292
+ LOGGER .error ("generate url error: " + e .getMessage ());
405
293
DialogUtils .showException (QiniuValueConsts .GENERATE_URL_ERROR , e );
406
294
}
407
295
0 commit comments