提交 a41a8adf 编写于 作者: M mehaotian

feat(stat): reportInterval

上级 6846cb50
......@@ -14,6 +14,8 @@ const APP_PVER_TIME = 300; // 应用在后台结束访问时间 单位s
const OPERATING_TIME = 10; // 数据上报时间 单位s
const DIFF_TIME = 60 * 1000 * 60 * 24;
// 获取 manifest.json 中统计配置
const uniStatisticsConfig = process.env.UNI_STATISTICS_CONFIG;
let statConfig = {
appid: process.env.UNI_APP_ID,
};
......@@ -471,7 +473,8 @@ const is_debug = debug;
* 日志输出
* @param {*} data
*/
const log = (data) => {
const log = (data, type) => {
let msg_type = '';
switch (data.lt) {
case '1':
......@@ -494,11 +497,41 @@ const log = (data) => {
msg_type = 'PUSH';
break
}
// #ifdef APP
// 在 app 中,日志转为 字符串
if(typeof data === 'object') {
data = JSON.stringify(data);
}
// #endif
if (type) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
return
}
if (msg_type) {
console.log(`=== 统计数据采集:${msg_type} ===`);
console.log(data);
console.log(`=== 采集结束 ===`);
}
};
/**
* 获取上报时间间隔
* @param {*} defaultTime 默认上报间隔时间 单位s
*/
const get_report_Interval = (defaultTime) => {
let time = uniStatisticsConfig.reportInterval;
// 如果上报时间配置为0 相当于立即上报
if (Number(time) === 0) return 0
time = time || defaultTime;
let reg = /(^[1-9]\d*$)/;
// 如果不是整数,则默认为上报间隔时间
if (!reg.test(time)) return defaultTime
return Number(time)
};
const appid = process.env.UNI_APP_ID; // 做应用隔离
......@@ -672,6 +705,7 @@ const get_residence_time = (type) => {
}
};
const eport_Interval = get_report_Interval(OPERATING_TIME);
// 统计数据默认值
let statData = {
uuid: get_uuid(), // 设备标识
......@@ -1131,7 +1165,7 @@ class Report {
log(data);
}
// 判断时候到达上报时间 ,默认 10 秒上报
if (page_residence_time < OPERATING_TIME && !type) return
if (page_residence_time < eport_Interval && !type) return
// 时间超过,重新获取时间戳
set_page_residence_time();
......@@ -1167,7 +1201,9 @@ class Report {
sendRequest(optionsData) {
{
if (!uni.__stat_uniCloud_space) {
console.error('应用未关联服务空间,统计上报失败,请在uniCloud目录右键关联服务空间.');
console.error(
'应用未关联服务空间,统计上报失败,请在uniCloud目录右键关联服务空间.'
);
return
}
......@@ -1181,9 +1217,7 @@ class Report {
.report(optionsData)
.then(() => {
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(optionsData);
console.log(`=== 上报结束 ===`);
log(optionsData, true);
}
})
.catch((err) => {
......@@ -1204,9 +1238,7 @@ class Report {
let options = get_sgin(get_encodeURIComponent_options(data)).options;
image.src = STAT_H5_URL + '?' + options;
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
log(data, true);
}
});
}
......@@ -1254,6 +1286,10 @@ class Stat extends Report {
// '=== 当前绑定的统计服务空间spaceId:' +
// uni.__stat_uniCloud_space.config.spaceId
// )
} else {
console.error(
'应用未关联服务空间,请在uniCloud目录右键关联服务空间'
);
}
}
}
......
......@@ -12,6 +12,8 @@ const APP_PVER_TIME = 300; // 应用在后台结束访问时间 单位s
const OPERATING_TIME = 10; // 数据上报时间 单位s
const DIFF_TIME = 60 * 1000 * 60 * 24;
// 获取 manifest.json 中统计配置
const uniStatisticsConfig = process.env.UNI_STATISTICS_CONFIG;
let statConfig = {
appid: process.env.UNI_APP_ID,
};
......@@ -469,7 +471,8 @@ const is_debug = debug;
* 日志输出
* @param {*} data
*/
const log = (data) => {
const log = (data, type) => {
let msg_type = '';
switch (data.lt) {
case '1':
......@@ -492,11 +495,41 @@ const log = (data) => {
msg_type = 'PUSH';
break
}
// #ifdef APP
// 在 app 中,日志转为 字符串
if(typeof data === 'object') {
data = JSON.stringify(data);
}
// #endif
if (type) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
return
}
if (msg_type) {
console.log(`=== 统计数据采集:${msg_type} ===`);
console.log(data);
console.log(`=== 采集结束 ===`);
}
};
/**
* 获取上报时间间隔
* @param {*} defaultTime 默认上报间隔时间 单位s
*/
const get_report_Interval = (defaultTime) => {
let time = uniStatisticsConfig.reportInterval;
// 如果上报时间配置为0 相当于立即上报
if (Number(time) === 0) return 0
time = time || defaultTime;
let reg = /(^[1-9]\d*$)/;
// 如果不是整数,则默认为上报间隔时间
if (!reg.test(time)) return defaultTime
return Number(time)
};
const appid = process.env.UNI_APP_ID; // 做应用隔离
......@@ -670,6 +703,7 @@ const get_residence_time = (type) => {
}
};
const eport_Interval = get_report_Interval(OPERATING_TIME);
// 统计数据默认值
let statData = {
uuid: get_uuid(), // 设备标识
......@@ -1129,7 +1163,7 @@ class Report {
log(data);
}
// 判断时候到达上报时间 ,默认 10 秒上报
if (page_residence_time < OPERATING_TIME && !type) return
if (page_residence_time < eport_Interval && !type) return
// 时间超过,重新获取时间戳
set_page_residence_time();
......@@ -1165,7 +1199,9 @@ class Report {
sendRequest(optionsData) {
{
if (!uni.__stat_uniCloud_space) {
console.error('应用未关联服务空间,统计上报失败,请在uniCloud目录右键关联服务空间.');
console.error(
'应用未关联服务空间,统计上报失败,请在uniCloud目录右键关联服务空间.'
);
return
}
......@@ -1179,9 +1215,7 @@ class Report {
.report(optionsData)
.then(() => {
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(optionsData);
console.log(`=== 上报结束 ===`);
log(optionsData, true);
}
})
.catch((err) => {
......@@ -1202,9 +1236,7 @@ class Report {
let options = get_sgin(get_encodeURIComponent_options(data)).options;
image.src = STAT_H5_URL + '?' + options;
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
log(data, true);
}
});
}
......@@ -1252,6 +1284,10 @@ class Stat extends Report {
// '=== 当前绑定的统计服务空间spaceId:' +
// uni.__stat_uniCloud_space.config.spaceId
// )
} else {
console.error(
'应用未关联服务空间,请在uniCloud目录右键关联服务空间'
);
}
}
}
......
......@@ -14,6 +14,8 @@ const APP_PVER_TIME = 300; // 应用在后台结束访问时间 单位s
const OPERATING_TIME = 10; // 数据上报时间 单位s
const DIFF_TIME = 60 * 1000 * 60 * 24;
// 获取 manifest.json 中统计配置
const uniStatisticsConfig = process.env.UNI_STATISTICS_CONFIG;
let statConfig = {
appid: process.env.UNI_APP_ID,
};
......@@ -450,7 +452,8 @@ const is_debug = debug;
* 日志输出
* @param {*} data
*/
const log = (data) => {
const log = (data, type) => {
let msg_type = '';
switch (data.lt) {
case '1':
......@@ -473,11 +476,41 @@ const log = (data) => {
msg_type = 'PUSH';
break
}
// #ifdef APP
// 在 app 中,日志转为 字符串
if(typeof data === 'object') {
data = JSON.stringify(data);
}
// #endif
if (type) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
return
}
if (msg_type) {
console.log(`=== 统计数据采集:${msg_type} ===`);
console.log(data);
console.log(`=== 采集结束 ===`);
}
};
/**
* 获取上报时间间隔
* @param {*} defaultTime 默认上报间隔时间 单位s
*/
const get_report_Interval = (defaultTime) => {
let time = uniStatisticsConfig.reportInterval;
// 如果上报时间配置为0 相当于立即上报
if (Number(time) === 0) return 0
time = time || defaultTime;
let reg = /(^[1-9]\d*$)/;
// 如果不是整数,则默认为上报间隔时间
if (!reg.test(time)) return defaultTime
return Number(time)
};
const appid = process.env.UNI_APP_ID; // 做应用隔离
......@@ -651,6 +684,7 @@ const get_residence_time = (type) => {
}
};
const eport_Interval = get_report_Interval(OPERATING_TIME);
// 统计数据默认值
let statData = {
uuid: get_uuid(), // 设备标识
......@@ -1117,7 +1151,7 @@ class Report {
log(data);
}
// 判断时候到达上报时间 ,默认 10 秒上报
if (page_residence_time < OPERATING_TIME && !type) return
if (page_residence_time < eport_Interval && !type) return
// 时间超过,重新获取时间戳
set_page_residence_time();
......@@ -1167,9 +1201,7 @@ class Report {
data: optionsData,
success: () => {
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(optionsData);
console.log(`=== 上报结束 ===`);
log(optionsData, true);
}
},
fail: (e) => {
......@@ -1197,9 +1229,7 @@ class Report {
let options = get_sgin(get_encodeURIComponent_options(data)).options;
image.src = STAT_H5_URL + '?' + options;
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
log(data, true);
}
});
}
......
......@@ -12,6 +12,8 @@ const APP_PVER_TIME = 300; // 应用在后台结束访问时间 单位s
const OPERATING_TIME = 10; // 数据上报时间 单位s
const DIFF_TIME = 60 * 1000 * 60 * 24;
// 获取 manifest.json 中统计配置
const uniStatisticsConfig = process.env.UNI_STATISTICS_CONFIG;
let statConfig = {
appid: process.env.UNI_APP_ID,
};
......@@ -448,7 +450,8 @@ const is_debug = debug;
* 日志输出
* @param {*} data
*/
const log = (data) => {
const log = (data, type) => {
let msg_type = '';
switch (data.lt) {
case '1':
......@@ -471,11 +474,41 @@ const log = (data) => {
msg_type = 'PUSH';
break
}
// #ifdef APP
// 在 app 中,日志转为 字符串
if(typeof data === 'object') {
data = JSON.stringify(data);
}
// #endif
if (type) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
return
}
if (msg_type) {
console.log(`=== 统计数据采集:${msg_type} ===`);
console.log(data);
console.log(`=== 采集结束 ===`);
}
};
/**
* 获取上报时间间隔
* @param {*} defaultTime 默认上报间隔时间 单位s
*/
const get_report_Interval = (defaultTime) => {
let time = uniStatisticsConfig.reportInterval;
// 如果上报时间配置为0 相当于立即上报
if (Number(time) === 0) return 0
time = time || defaultTime;
let reg = /(^[1-9]\d*$)/;
// 如果不是整数,则默认为上报间隔时间
if (!reg.test(time)) return defaultTime
return Number(time)
};
const appid = process.env.UNI_APP_ID; // 做应用隔离
......@@ -649,6 +682,7 @@ const get_residence_time = (type) => {
}
};
const eport_Interval = get_report_Interval(OPERATING_TIME);
// 统计数据默认值
let statData = {
uuid: get_uuid(), // 设备标识
......@@ -1115,7 +1149,7 @@ class Report {
log(data);
}
// 判断时候到达上报时间 ,默认 10 秒上报
if (page_residence_time < OPERATING_TIME && !type) return
if (page_residence_time < eport_Interval && !type) return
// 时间超过,重新获取时间戳
set_page_residence_time();
......@@ -1165,9 +1199,7 @@ class Report {
data: optionsData,
success: () => {
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(optionsData);
console.log(`=== 上报结束 ===`);
log(optionsData, true);
}
},
fail: (e) => {
......@@ -1195,9 +1227,7 @@ class Report {
let options = get_sgin(get_encodeURIComponent_options(data)).options;
image.src = STAT_H5_URL + '?' + options;
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`);
console.log(data);
console.log(`=== 上报结束 ===`);
log(data, true);
}
});
}
......
......@@ -58,7 +58,9 @@ var index = () => [
isEnable = statConfig.enable === true;
if (isEnable) {
const uniCloudConfig = statConfig.uniCloud || {};
statVersion = statConfig.version === '2' ? '2' : '1';
// 获取manifest.json 统计配置,插入环境变量中
process.env.UNI_STATISTICS_CONFIG = JSON.stringify(statConfig);
statVersion = Number(statConfig.version) === 2 ? '2' : '1';
process.env.UNI_STAT_UNI_CLOUD = JSON.stringify(uniCloudConfig);
process.env.UNI_STAT_DEBUG = statConfig.debug ? 'true' : 'false';
if (process.env.NODE_ENV === 'production') {
......@@ -91,8 +93,9 @@ var index = () => [
return {
define: {
'process.env.UNI_STAT_TITLE_JSON': process.env.UNI_STAT_TITLE_JSON,
'process.env.UNI_STAT_UNI_CLOUD': process.env.UNI_STAT_UNI_CLOUD || JSON.stringify({}),
'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG || 'false',
'process.env.UNI_STAT_UNI_CLOUD': process.env.UNI_STAT_UNI_CLOUD,
'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG,
'process.env.UNI_STATISTICS_CONFIG': process.env.UNI_STATISTICS_CONFIG,
},
};
},
......
......@@ -29,6 +29,7 @@ import {
get_page_vm,
is_debug,
log,
get_report_Interval,
} from '../utils/pageInfo.js'
import { sys } from '../utils/util.js'
......@@ -41,7 +42,7 @@ import {
} from '../config.ts'
import { dbSet, dbGet, dbRemove } from '../utils/db.js'
const eport_Interval = get_report_Interval(OPERATING_TIME)
// 统计数据默认值
let statData = {
uuid: get_uuid(), // 设备标识
......@@ -508,7 +509,7 @@ export default class Report {
log(data)
}
// 判断时候到达上报时间 ,默认 10 秒上报
if (page_residence_time < OPERATING_TIME && !type) return
if (page_residence_time < eport_Interval && !type) return
// 时间超过,重新获取时间戳
set_page_residence_time()
......@@ -551,7 +552,9 @@ export default class Report {
sendRequest(optionsData) {
if (__STAT_VERSION__ === '2') {
if (!uni.__stat_uniCloud_space) {
console.error('应用未关联服务空间,统计上报失败,请在uniCloud目录右键关联服务空间.')
console.error(
'应用未关联服务空间,统计上报失败,请在uniCloud目录右键关联服务空间.'
)
return
}
......@@ -565,9 +568,7 @@ export default class Report {
.report(optionsData)
.then(() => {
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`)
console.log(optionsData)
console.log(`=== 上报结束 ===`)
log(optionsData, true)
}
})
.catch((err) => {
......@@ -586,9 +587,7 @@ export default class Report {
data: optionsData,
success: () => {
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`)
console.log(optionsData)
console.log(`=== 上报结束 ===`)
log(optionsData, true)
}
},
fail: (e) => {
......@@ -616,9 +615,7 @@ export default class Report {
let options = get_sgin(get_encodeURIComponent_options(data)).options
image.src = STAT_H5_URL + '?' + options
if (is_debug) {
console.log(`=== 统计队列数据上报 ===`)
console.log(data)
console.log(`=== 上报结束 ===`)
log(data, true)
}
})
}
......
......@@ -34,9 +34,9 @@ class Stat extends Report {
// uni.__stat_uniCloud_space.config.spaceId
// )
} else {
// console.error(
// '当前尚未关联统计服务空间,请先在manifest.json中配置服务空间!'
// )
console.error(
'应用未关联服务空间,请在uniCloud目录右键关联服务空间'
)
}
}
}
......
......@@ -55,8 +55,9 @@ export default () => [
if (isEnable) {
const uniCloudConfig = statConfig.uniCloud || {}
statVersion = statConfig.version === '2' ? '2' : '1'
// 获取manifest.json 统计配置,插入环境变量中
process.env.UNI_STATISTICS_CONFIG = JSON.stringify(statConfig)
statVersion = Number(statConfig.version) === 2 ? '2' : '1'
process.env.UNI_STAT_UNI_CLOUD = JSON.stringify(uniCloudConfig)
process.env.UNI_STAT_DEBUG = statConfig.debug ? 'true' : 'false'
if (process.env.NODE_ENV === 'production') {
......@@ -89,9 +90,10 @@ export default () => [
return {
define: {
'process.env.UNI_STAT_TITLE_JSON': process.env.UNI_STAT_TITLE_JSON,
'process.env.UNI_STAT_UNI_CLOUD':
process.env.UNI_STAT_UNI_CLOUD || JSON.stringify({}),
'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG || 'false',
'process.env.UNI_STAT_UNI_CLOUD': process.env.UNI_STAT_UNI_CLOUD,
'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG,
'process.env.UNI_STATISTICS_CONFIG':
process.env.UNI_STATISTICS_CONFIG,
},
}
},
......
......@@ -2,6 +2,8 @@ import { sys } from './util.js'
import { STAT_URL, STAT_VERSION, DIFF_TIME } from '../config.ts'
// 获取 manifest.json 中统计配置
const uniStatisticsConfig = process.env.UNI_STATISTICS_CONFIG
let statConfig = {
appid: process.env.UNI_APP_ID,
}
......@@ -478,7 +480,8 @@ export const is_debug = debug
* 日志输出
* @param {*} data
*/
export const log = (data) => {
export const log = (data, type) => {
let msg_type = ''
switch (data.lt) {
case '1':
......@@ -501,9 +504,39 @@ export const log = (data) => {
msg_type = 'PUSH'
break
}
// #ifdef APP
// 在 app 中,日志转为 字符串
if(typeof data === 'object') {
data = JSON.stringify(data)
}
// #endif
if (type) {
console.log(`=== 统计队列数据上报 ===`)
console.log(data)
console.log(`=== 上报结束 ===`)
return
}
if (msg_type) {
console.log(`=== 统计数据采集:${msg_type} ===`)
console.log(data)
console.log(`=== 采集结束 ===`)
}
}
/**
* 获取上报时间间隔
* @param {*} defaultTime 默认上报间隔时间 单位s
*/
export const get_report_Interval = (defaultTime) => {
let time = uniStatisticsConfig.reportInterval
// 如果上报时间配置为0 相当于立即上报
if (Number(time) === 0) return 0
time = time || defaultTime
let reg = /(^[1-9]\d*$)/
// 如果不是整数,则默认为上报间隔时间
if (!reg.test(time)) return defaultTime
return Number(time)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册