Skip to content

Commit da81266

Browse files
committed
add traffic statistics
1 parent 4001166 commit da81266

File tree

21 files changed

+499
-31
lines changed

21 files changed

+499
-31
lines changed

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Applets/Landscape/VPLuaAppletLandscapeContainer.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,12 @@ - (void)setGetUserInfoBlock:(NSDictionary *(^)(void))getUserInfoBlock {
253253
- (void)loadLuaFiles {
254254
__weak typeof(self) weakSelf = self;
255255

256-
[[VPLuaLoader sharedLoader] checkAndDownloadFilesList:_applet.luaList resumePath:self.appletPath complete:^(NSError * error) {
256+
[[VPLuaLoader sharedLoader] checkAndDownloadFilesList:_applet.luaList resumePath:self.appletPath complete:^(NSError * error, VPUPTrafficStatisticsList *trafficList) {
257257
//已回到主线程
258+
if (trafficList) {
259+
[VPUPTrafficStatistics sendTrafficeStatistics:trafficList type:VPUPTrafficTypeRealTime];
260+
}
261+
258262
if (error) {
259263
weakSelf.retryLuaFiles = YES;
260264
[weakSelf showRetryView];

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/LuaView/VPLuaImageView.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#import <VPLuaViewSDK/LVBaseView.h>
2222
#import <VPLuaViewSDK/LVNinePatchImage.h>
2323

24+
#import "VPUPPathUtil.h"
25+
#import "VPUPTrafficStatistics.h"
26+
#import "VPUPMD5Util.h"
27+
2428

2529
static NSString *const VPDefaultImageBundle = @"VideoPlsDefaultImages";
2630

@@ -81,7 +85,27 @@ -(void) setWebImageUrl:(NSURL *)url finished:(LVLoadFinished)finished {
8185
}
8286

8387
// config.placeholder = [VPMGoodsListLoadingImage loadingImage];
88+
89+
__block BOOL needStatistics = YES;
90+
NSString *destinationPath = [VPUPPathUtil imagePath];
91+
NSString *fileName = [NSString stringWithFormat:@"%@.%@",[VPUPMD5Util md5HashString:url.absoluteString],[url pathExtension]];
92+
NSString *filePath = [destinationPath stringByAppendingPathComponent:fileName];
93+
94+
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
95+
needStatistics = NO;
96+
}
97+
8498
config.completedBlock = ^(UIImage *image, NSError *error, VPUPImageCacheType cacheType, NSURL *imageURL) {
99+
if (!error && needStatistics) {
100+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
101+
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
102+
VPUPTrafficStatisticsList *list = [[VPUPTrafficStatisticsList alloc] init];
103+
[list addFileTrafficByName:fileName fileUrl:[url absoluteString] filePath:filePath];
104+
[VPUPTrafficStatistics sendTrafficeStatistics:list type:VPUPTrafficTypeRealTime];
105+
}
106+
});
107+
}
108+
85109
if (finished) {
86110
finished(error);
87111
}

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaHttpRequest.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ static int httpRequest(lua_State *L, VPUPRequestMethodType methodType) {
201201
api.apiRequestMethodType = methodType;
202202
[api setRequestParameters:data];
203203
__weak VPLuaHttpRequest *weakRequest = request;
204+
// NSLog(@"apirequest=====%@%@", api.baseUrl, api.requestMethod);
204205
NSString *apiId = [NSString stringWithFormat:@"%ld",api.apiId];
205206
api.apiCompletionHandler = ^(id _Nonnull responseObject, NSError * _Nullable error, NSURLResponse * _Nullable response) {
206207

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaLoader.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import <Foundation/Foundation.h>
10+
#import "VPUPTrafficStatistics.h"
1011

1112
NS_ASSUME_NONNULL_BEGIN
1213

@@ -18,12 +19,13 @@ NS_ASSUME_NONNULL_BEGIN
1819
@property (nonatomic) NSMutableArray *filesName;
1920
@property (nonatomic) NSString *destinationPath;
2021
@property (nonatomic) NSString *tempFilePath;
22+
@property (nonatomic) VPUPTrafficStatisticsList *statisticsList;
2123

2224
+ (instancetype)objectWithFilesList:(NSArray *)filesList destinationPath:(NSString *)destinationPath;
2325

2426
@end
2527

26-
typedef void(^VPLuaLoaderCompletionBlock)(NSError *error);
28+
typedef void(^VPLuaLoaderCompletionBlock)(NSError *error, VPUPTrafficStatisticsList *trafficList);
2729

2830
@interface VPLuaLoader : NSObject
2931

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaLoader.m

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
@implementation VPLuaLoaderObject
1919

20+
- (instancetype)init {
21+
self = [super init];
22+
if (self) {
23+
self.statisticsList = [[VPUPTrafficStatisticsList alloc] init];
24+
}
25+
return self;
26+
}
27+
2028
+ (instancetype)objectWithFilesList:(NSArray *)filesList destinationPath:(NSString *)destinationPath {
2129
VPLuaLoaderObject *loadObject = [[VPLuaLoaderObject alloc] init];
2230
loadObject.filesList = filesList;
@@ -49,6 +57,8 @@ @interface VPLuaLoader()
4957

5058
- (void)callbackComplete:(VPLuaLoaderCompletionBlock)complete withError:(NSError *)error;
5159

60+
- (void)callbackComplete:(VPLuaLoaderCompletionBlock)complete withError:(NSError *)error withStatistics:(VPUPTrafficStatisticsList *)trafficList;
61+
5262
@end
5363

5464

@@ -117,51 +127,63 @@ - (void)checkFilesListWithLocal:(NSArray *)fileList resumePath:(NSString *)resum
117127
}
118128
}
119129
if (downloadFileList.count > 0) {
120-
[weakSelf downloadLuaFilesList:downloadFileList destinationPath:resumePath complete:complete];
130+
VPLuaLoaderObject *loaderObject = [VPLuaLoaderObject objectWithFilesList:downloadFileList destinationPath:resumePath];
131+
[weakSelf downloadLuaFilesWithLoaderObject:loaderObject complete:complete];
121132
}
122133
else {
123134
[weakSelf callbackComplete:complete withError:nil];
124135
}
125136
});
126137
}
127138

128-
- (void)downloadLuaFilesList:(NSArray *)filesList destinationPath:(NSString *)destinationPath complete:(VPLuaLoaderCompletionBlock)complete {
139+
- (void)downloadLuaFilesWithLoaderObject:(VPLuaLoaderObject *)loaderObject complete:(VPLuaLoaderCompletionBlock)complete {
140+
[self downloadLuaFilesWithLoaderObject:loaderObject complete:complete repeatCount:0];
141+
}
142+
143+
- (void)downloadLuaFilesWithLoaderObject:(VPLuaLoaderObject *)loaderObject complete:(VPLuaLoaderCompletionBlock)complete repeatCount:(NSInteger)repeatCount {
129144

130-
if (!filesList || filesList.count == 0) {
145+
if (!loaderObject || !loaderObject.filesList || loaderObject.filesList.count == 0) {
131146
[self callbackComplete:complete withError:nil];
132147
return;
133148
}
134149

135-
__block VPLuaLoaderObject *loaderObject = [VPLuaLoaderObject objectWithFilesList:filesList destinationPath:destinationPath];
150+
__block VPLuaLoaderObject *blockObject = loaderObject;
136151

137-
// static NSInteger count = 0;
138152
__weak typeof(self) weakSelf = self;
139-
// dispatch_async(dispatch_get_global_queue(0, 0), ^{
140153

141154
[self.prefetchManager prefetchURLs:loaderObject.filesUrl
142155
fileNames:loaderObject.filesName
143156
destinationPath:loaderObject.tempFilePath
144157
completionBlock:^(NSUInteger numberOfFinishedUrls, NSUInteger numberOfSkippedUrls) {
145158
if (numberOfSkippedUrls > 0) {
159+
if (repeatCount == 0) {
160+
[self downloadLuaFilesWithLoaderObject:blockObject complete:complete repeatCount:1];
161+
} else {
146162
NSError *error = [NSError errorWithDomain:VPLuaErrorDomain code:-3002 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Download file error, success count %ld, faild count %ld", numberOfFinishedUrls, numberOfSkippedUrls]}];
147-
[weakSelf callbackComplete:complete withError:error];
163+
// [weakSelf callbackComplete:complete withError:error];
164+
[weakSelf checkDownloadObject:blockObject complete:complete skippedCount:numberOfSkippedUrls];
165+
}
148166
}
149167
else {
150168
// [weakSelf checkDownloadFilesList:filesList complete:complete];
151-
[weakSelf checkDownloadObject:loaderObject complete:complete];
169+
[weakSelf checkDownloadObject:blockObject complete:complete];
152170
}
153171
}];
154-
// });
155172
}
156173

157174
- (void)checkDownloadObject:(VPLuaLoaderObject *)loaderObject complete:(VPLuaLoaderCompletionBlock)complete {
175+
[self checkDownloadObject:loaderObject complete:complete skippedCount:0];
176+
}
177+
178+
- (void)checkDownloadObject:(VPLuaLoaderObject *)loaderObject complete:(VPLuaLoaderCompletionBlock)complete skippedCount:(NSInteger)skippedCount {
158179
__weak typeof(self) weakSelf = self;
159180

160181
dispatch_async(self.luaLoaderQueue, ^{
161-
NSInteger failedCount = 0;
182+
NSInteger failedCount = skippedCount;
162183
NSMutableArray *downloadFiles = [NSMutableArray arrayWithCapacity:0];
163184
for (NSInteger i = 0; i < loaderObject.filesList.count; i++) {
164185
NSString *fileName = [loaderObject.filesName objectAtIndex:i];
186+
NSString *fileUrl = [loaderObject.filesUrl objectAtIndex:i];
165187
NSString *localPath = [loaderObject.tempFilePath stringByAppendingPathComponent:fileName];
166188
[downloadFiles addObject:localPath];
167189
NSDictionary *dict = [loaderObject.filesList objectAtIndex:i];
@@ -170,6 +192,8 @@ - (void)checkDownloadObject:(VPLuaLoaderObject *)loaderObject complete:(VPLuaLoa
170192
NSString *fileMD5 = [VPUPMD5Util md5File:localPath size:0];
171193
if (![[dict objectForKey:@"md5"] isEqualToString:fileMD5]) {
172194
failedCount += 1;
195+
} else {
196+
[loaderObject.statisticsList addFileTrafficByName:fileName fileUrl:fileUrl filePath:localPath];
173197
}
174198
}
175199
}
@@ -194,16 +218,17 @@ - (void)checkDownloadObject:(VPLuaLoaderObject *)loaderObject complete:(VPLuaLoa
194218
}
195219
if (moveErrors.count > 0) {
196220
NSError *error = [NSError errorWithDomain:VPLuaErrorDomain code:-3003 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Download file copy to target path error"]}];
197-
[self callbackComplete:complete withError:error];
221+
[self callbackComplete:complete withError:error withStatistics:loaderObject.statisticsList];
198222
}
199223
else {
200-
[self callbackComplete:complete withError:nil];
224+
[self callbackComplete:complete withError:nil withStatistics:loaderObject.statisticsList];
201225
}
202226
}
203227
else {
204228
NSError *error = [NSError errorWithDomain:VPLuaErrorDomain code:-3004 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Download file data error"]}];
205-
[weakSelf callbackComplete:complete withError:error];
229+
[weakSelf callbackComplete:complete withError:error withStatistics:loaderObject.statisticsList];
206230
}
231+
207232
[[NSFileManager defaultManager] removeItemAtPath:loaderObject.tempFilePath error:nil];
208233
});
209234
}
@@ -268,7 +293,15 @@ - (void)checkDownloadFilesList:(NSArray *)filesList complete:(VPLuaLoaderComplet
268293
- (void)callbackComplete:(VPLuaLoaderCompletionBlock)complete withError:(NSError *)error {
269294
dispatch_async(dispatch_get_main_queue(), ^{
270295
if (complete) {
271-
complete(error);
296+
complete(error, nil);
297+
}
298+
});
299+
}
300+
301+
- (void)callbackComplete:(VPLuaLoaderCompletionBlock)complete withError:(NSError *)error withStatistics:(VPUPTrafficStatisticsList *)trafficList {
302+
dispatch_async(dispatch_get_main_queue(), ^{
303+
if (complete) {
304+
complete(error, trafficList);
272305
}
273306
});
274307
}

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaNativeBridge.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#import "VPLuaBaseNode.h"
1515
#import "VPLuaNetworkManager.h"
1616

17-
@class VPLuaBaseNode;
18-
1917
extern NSString *const VPLuaScreenChangeNotification;
2018
extern NSString *const VPLuaNotifyUserLoginedNotification;
2119
extern NSString *const VPLuaRequireLoginNotification;

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaNativeBridge.m

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#import "VPUPDeviceUtil.h"
3535
#import "VPLuaCommonInfo.h"
3636
#import "VPUPSHAUtil.h"
37+
#import "VPLuaLoader.h"
3738

3839
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
3940
#import <CoreTelephony/CTCarrier.h>
@@ -57,6 +58,8 @@
5758
#import "VPLuaMacroDefine.h"
5859
#import "VPLuaSDK.h"
5960
#import "VPUPUrlUtil.h"
61+
#import "VPUPTrafficStatistics.h"
62+
#import "VPUPPrefetchImageManager.h"
6063

6164
#define IS_IOS11 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)
6265

@@ -294,11 +297,13 @@ +(int) lvClassDefine:(lua_State *)L globalName:(NSString*) globalName{
294297
{"px2Dpi",px2Dpi},
295298
{"preloadImage", preloadImage},
296299
{"preloadVideo", preloadVideo},
300+
{"preloadLuaList", preloadLuaList},
297301
{"copyStringToPasteBoard", copyStringToPasteBoard},
298302
{"videoOShost", videoOShost},
299303
{"isCacheVideo", isCacheVideo},
300304
{"currentVideoTime", currentVideoTime},
301305
{"videoDuration", videoDuration},
306+
// {"appletSize", getAppletSize},
302307
{NULL, NULL}
303308
};
304309
lv_createClassMetaTable(L,META_TABLE_NativeObject);
@@ -702,6 +707,7 @@ static int httpRequest(lua_State *L, VPUPRequestMethodType methodType) {
702707
api.apiRequestMethodType = methodType;
703708
[api setRequestParameters:data];
704709
// __weak typeof(cell) weakCell = cell;
710+
// NSLog(@"apirequestnative=====%@%@===%@", api.baseUrl, api.requestMethod, VPUP_DictionaryToJson(data));
705711
NSString *apiId = [NSString stringWithFormat:@"%ld",api.apiId];
706712
api.apiCompletionHandler = ^(id _Nonnull responseObject, NSError * _Nullable error, NSURLResponse * _Nullable response) {
707713
//没有回调,网络请求不处理回调
@@ -1159,7 +1165,6 @@ static int widgetNotify(lua_State *L) {
11591165
return 0;
11601166
}
11611167

1162-
11631168
static int titleBarHeight(lua_State *L) {
11641169
lua_pushnumber(L, [VPUPDeviceUtil statusBarHeight]);
11651170
return 1;
@@ -1328,7 +1333,15 @@ static int preloadImage(lua_State *L) {
13281333
if( lua_type(L, 2) == LUA_TTABLE ) {
13291334
NSArray *array = lv_luaValueToNativeObject(L, 2);
13301335
if ([array isKindOfClass:[NSArray class]] && array.count > 0) {
1331-
[[VPLuaNetworkManager Manager].imageManager prefetchURLs:array];
1336+
1337+
VPUPPrefetchImageManager *prefetchImageManager = [[VPUPPrefetchImageManager alloc] initWithImageManager:[VPLuaNetworkManager Manager].imageManager];
1338+
1339+
[prefetchImageManager prefetchURLs:array complete:^(VPUPTrafficStatisticsList *trafficList) {
1340+
1341+
if (trafficList) {
1342+
[VPUPTrafficStatistics sendTrafficeStatistics:trafficList type:VPUPTrafficTypeOpenVideo];
1343+
}
1344+
}];
13321345
}
13331346
}
13341347
}
@@ -1340,7 +1353,56 @@ static int preloadVideo(lua_State *L) {
13401353
if( lua_type(L, 2) == LUA_TTABLE ) {
13411354
NSArray *array = lv_luaValueToNativeObject(L, 2);
13421355
if ([array isKindOfClass:[NSArray class]] && array.count > 0) {
1343-
[[VPLuaNetworkManager Manager].videoManager prefetchURLs:array];
1356+
[[VPLuaNetworkManager Manager].videoManager prefetchURLs:array complete:^(VPUPTrafficStatisticsList *trafficList) {
1357+
1358+
if (trafficList) {
1359+
[VPUPTrafficStatistics sendTrafficeStatistics:trafficList type:VPUPTrafficTypeOpenVideo];
1360+
}
1361+
1362+
}];
1363+
}
1364+
}
1365+
}
1366+
return 0;
1367+
}
1368+
1369+
static int preloadLuaList(lua_State *L) {
1370+
if (lua_gettop(L) >= 2) {
1371+
if( lua_type(L, 2) == LUA_TTABLE ) {
1372+
NSArray *array = lv_luaValueToNativeObject(L, 2);
1373+
if ([array isKindOfClass:[NSArray class]] && array.count > 0) {
1374+
1375+
//根据md5,先去一波重
1376+
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:0];
1377+
for (NSDictionary *dict in array) {
1378+
1379+
if (![dict objectForKey:@"md5"] || [[dict objectForKey:@"md5"] isEqualToString:@""]) {
1380+
continue;
1381+
}
1382+
1383+
BOOL needAdd = YES;
1384+
for (NSDictionary *noRepDict in newArray) {
1385+
NSString *md5 = [dict objectForKey:@"md5"];
1386+
NSString *noRepMd5 = [noRepDict objectForKey:@"md5"];
1387+
if ([md5 isEqualToString:noRepMd5]) {
1388+
needAdd = NO;
1389+
break;
1390+
}
1391+
}
1392+
1393+
if (needAdd) {
1394+
[newArray addObject:dict];
1395+
}
1396+
}
1397+
1398+
[[VPLuaLoader sharedLoader] checkAndDownloadFilesList:newArray complete:^(NSError * _Nonnull error, VPUPTrafficStatisticsList *trafficList) {
1399+
1400+
if (trafficList) {
1401+
[VPUPTrafficStatistics sendTrafficeStatistics:trafficList type:VPUPTrafficTypeOpenVideo];
1402+
}
1403+
1404+
NSLog(@"preloadLuaList error %@", error);
1405+
}];
13441406
}
13451407
}
13461408
}
@@ -1399,4 +1461,11 @@ static int videoDuration(lua_State *L) {
13991461
return 1;
14001462
}
14011463

1464+
//static int getAppletSize(lua_State *L) {
1465+
// VPLuaBaseNode *luaNade = [VPLuaNativeBridge luaNodeFromLuaState:L];
1466+
// lua_pushnumber(L, luaNade.luaController.rootView.bounds.size.width);
1467+
// lua_pushnumber(L, luaNade.luaController.rootView.bounds.size.height);
1468+
// return 2;
1469+
//}
1470+
14021471
@end

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaServiceAd.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ - (void)requestServiceAd {
106106
- (void)downloadFileFromData:(NSDictionary *)data {
107107
NSArray *filesList = [data objectForKey:@"templates"];
108108
__weak typeof(self) weakSelf = self;
109-
[[VPLuaLoader sharedLoader] checkAndDownloadFilesList:filesList complete:^(NSError * _Nonnull error) {
109+
[[VPLuaLoader sharedLoader] checkAndDownloadFilesList:filesList complete:^(NSError * _Nonnull error, VPUPTrafficStatisticsList *trafficList) {
110+
111+
if (trafficList) {
112+
[VPUPTrafficStatistics sendTrafficeStatistics:trafficList type:VPUPTrafficTypeRealTime];
113+
}
110114
if (error) {
111115
[weakSelf callbackComplete:weakSelf.complete withError:error];
112116
}

0 commit comments

Comments
 (0)