Commit 0a099572 by yexing

月度表优化

parent 958cd851
3.4.4 3.4.6
\ No newline at end of file \ No newline at end of file
No preview for this file type
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
{"name":"WalmartExports","description":"导出沃尔玛报表","version":"3.2.5","manifest_version":3,"background":{"service_worker":"./js/background.js"},"permissions":["tabs","activeTab","scripting","notifications","storage","alarms"],"action":{"default_title":"WalmartExports","default_popup":"popup.html","default_icon":{"16":"icon.png","32":"icon.png","48":"icon.png","128":"icon.png"}},"icons":{"16":"icon.png","32":"icon.png","48":"icon.png","128":"icon.png"},"web_accessible_resources":[{"resources":["src/main.js"],"matches":["<all_urls>"]}],"host_permissions":["https://seller.walmart.com/*","https://advertising.walmart.com/*","https://login.account.wal-mart.com/*","https://marketplace.walmartapis.com/*","https://walmart.meinuosha.com/*"],"content_scripts":[{"matches":["https://seller.walmart.com/*","https://advertising.walmart.com/*","https://login.account.wal-mart.com/*","https://marketplace.walmartapis.com/*","https://walmart.meinuosha.com/*"],"js":["./js/main.js"]}]} {"name":"WalmartExports","description":"导出沃尔玛报表","version":"3.4.6","manifest_version":3,"background":{"service_worker":"./js/background.js"},"permissions":["tabs","activeTab","scripting","notifications","storage","alarms"],"action":{"default_title":"WalmartExports","default_popup":"popup.html","default_icon":{"16":"icon.png","32":"icon.png","48":"icon.png","128":"icon.png"}},"icons":{"16":"icon.png","32":"icon.png","48":"icon.png","128":"icon.png"},"web_accessible_resources":[{"resources":["src/main.js"],"matches":["<all_urls>"]}],"host_permissions":["https://seller.walmart.com/*","https://advertising.walmart.com/*","https://login.account.wal-mart.com/*","https://marketplace.walmartapis.com/*","https://walmart.meinuosha.com/*"],"content_scripts":[{"matches":["https://seller.walmart.com/*","https://advertising.walmart.com/*","https://login.account.wal-mart.com/*","https://marketplace.walmartapis.com/*","https://walmart.meinuosha.com/*"],"js":["./js/main.js"]}]}
\ No newline at end of file \ No newline at end of file
import { xlsx2csv } from "./xlsx.js"; import { xlsx2csv } from "./xlsx.js";
import { createZip, checkResponse } from "./util.js"; import { createZip, checkResponse } from "./util.js";
import { Plan, makeLogEntry } from "./timezone.js"; import { makeLogEntry } from "./timezone.js";
import { uploadFile } from "./upload.js"; import { uploadFile } from "./upload.js";
import { TABLE } from "./conf.js";
const checkURL = (u1, u2) => { const checkURL = (u1, u2) => {
if (!u1 || !u2) return false; if (!u1 || !u2) return false;
...@@ -124,51 +123,3 @@ function startTask() { ...@@ -124,51 +123,3 @@ function startTask() {
} }
}, 1000); }, 1000);
} }
// eslint-disable-next-line no-unused-vars
const initTimedTask = () => {
for (const [key, { uri, params }] of Object.entries(TABLE)) {
const plan = new Plan(null, params);
const period = plan.get();
const when = plan.next().dt.valueOf();
chrome.alarms.create(key, { when });
console.log(`[timedTask] uri: ${uri} period: ${period} when: ${when}`);
}
}
chrome.alarms.onAlarm.addListener((alarm) => {
let [key, fromDate, toDate, when] = alarm.name.split('_');
if (!Object.keys(TABLE).includes(key)) return;
const { uri, params } = TABLE[key];
const plan = new Plan(null, params);
const period = !fromDate || !toDate ? plan.get() : [fromDate, toDate];
if (!when) {
when = plan.next().dt.valueOf();
chrome.alarms.create(alarm.name, { when });
console.log(`[timedTask] uri: ${uri} period: ${period} when: ${when}`);
}
startTask();
sendTabMsg({ type: "run", data: { uri, period } });
});
// 初始化
// initTimedTask();
// chrome.storage.local.get(null, console.log);
// chrome.storage.local.clear();
// chrome.alarms.getAll(console.log);
chrome.alarms.clearAll();
// eslint-disable-next-line no-unused-vars
const getTmpClock = () => {
const clock = new Date();
clock.setHours(22, 0, 0, 0);
return clock.getTime();
}
globalThis.initTmpTimedTask = (when) => {
const period = ['2025-02-01', '2025-02-28'];
for (const [key, { uri }] of Object.entries(TABLE)) {
const name = [key, ...period, when].join('_');
chrome.alarms.create(name, { when });
console.log(`[timedTask] uri: ${uri} period: ${period} when: ${when}`);
}
}
// globalThis.initTmpTimedTask(getTmpClock());
// initTmpTimedTask(new Date().getTime() + 10)
...@@ -132,7 +132,7 @@ const rrCallback = async function (response) { ...@@ -132,7 +132,7 @@ const rrCallback = async function (response) {
return null; return null;
} }
const gqlCallback = async function (response) { const gqlCallback = async function (response) {
const uri = this.uri, method = "POST", body = this.options.body, headers = this.options.headers, [fromDate, toDate] = this.options.period; const uri = this.uri, method = "POST", {body, headers, fromDate, toDate} = this.options;
async function getID(json) { async function getID(json) {
const { campaignReports = null, pagination = null } = json.data.get_coop_campaigns; const { campaignReports = null, pagination = null } = json.data.get_coop_campaigns;
if (!campaignReports?.length) { if (!campaignReports?.length) {
...@@ -378,12 +378,11 @@ const createOrderTask = function (sign, bodyArgs, options = {}) { ...@@ -378,12 +378,11 @@ const createOrderTask = function (sign, bodyArgs, options = {}) {
} }
); );
}; };
export async function createTasks(uri, period) { export async function createTasks(uri, fromDate, toDate) {
if (!uri.startsWith(location.origin)) { if (!uri.startsWith(location.origin)) {
return []; return [];
} }
uri = uri || TABLE.WFS.uri; uri = uri || TABLE.WFS.uri;
let [fromDate, toDate] = period || [fmt0(new Date(), -10), fmt0(new Date(), -1)];
Task.partnerId = sessionStorage.getItem('partnerId'); Task.partnerId = sessionStorage.getItem('partnerId');
Task.uri = uri; // 不适用于异步 Task.uri = uri; // 不适用于异步
if (fromDate && toDate) { if (fromDate && toDate) {
...@@ -526,7 +525,8 @@ export async function createTasks(uri, period) { ...@@ -526,7 +525,8 @@ export async function createTasks(uri, period) {
method: "POST", method: "POST",
ext: "json", ext: "json",
body, body,
period, fromDate,
toDate,
headers: rrHeaders(), headers: rrHeaders(),
callback: gqlCallback, callback: gqlCallback,
next: true, next: true,
...@@ -557,7 +557,7 @@ export async function createTasks(uri, period) { ...@@ -557,7 +557,7 @@ export async function createTasks(uri, period) {
attributionWindow: "days3", attributionWindow: "days3",
extraFields: ["noDate"], extraFields: ["noDate"],
}, },
{ initialState } { initialState, isMmonthly: true }
), ),
createAdvertTask( createAdvertTask(
"Placement Performance", "Placement Performance",
...@@ -568,7 +568,7 @@ export async function createTasks(uri, period) { ...@@ -568,7 +568,7 @@ export async function createTasks(uri, period) {
attributionWindow: "days3", attributionWindow: "days3",
extraFields: ["noDate"], extraFields: ["noDate"],
}, },
{ initialState } { initialState, isMmonthly: true }
), ),
createAdvertTask( createAdvertTask(
"Item Keyword Performance", "Item Keyword Performance",
...@@ -579,7 +579,7 @@ export async function createTasks(uri, period) { ...@@ -579,7 +579,7 @@ export async function createTasks(uri, period) {
attributionWindow: "days3", attributionWindow: "days3",
extraFields: ["noDate"], extraFields: ["noDate"],
}, },
{ initialState } { initialState, isMmonthly: true }
), ),
createAdvertTask( createAdvertTask(
"Item Performance", "Item Performance",
...@@ -600,6 +600,7 @@ export async function createTasks(uri, period) { ...@@ -600,6 +600,7 @@ export async function createTasks(uri, period) {
{ {
request: "/v1/snapshot/recommendations", request: "/v1/snapshot/recommendations",
initialState, initialState,
isMmonthly: true
} }
), ),
]; ];
......
...@@ -3,7 +3,7 @@ const p1 = [ ...@@ -3,7 +3,7 @@ const p1 = [
[`11 ${hour}:`, ["01", "10"]], [`11 ${hour}:`, ["01", "10"]],
[`21 ${hour}:`, ['11', '20']], [`21 ${hour}:`, ['11', '20']],
[`01 ${hour}:`, ['21', 0]], [`01 ${hour}:`, ['21', 0]],
[`05 00:`, ['01', 0]] [`05 00:`, ['01', 0], true]
]; ];
// const p2 = [ // const p2 = [
// [`11 ${hour}:`, ["01", "10"]], // [`11 ${hour}:`, ["01", "10"]],
......
...@@ -17,10 +17,11 @@ async function webJump() { ...@@ -17,10 +17,11 @@ async function webJump() {
} }
}; };
async function run(options = {}) { async function run(options = {}) {
const { uri, period, sn } = options; const { uri, plan, sn } = options;
const [fromDate, toDate] = period || [fmt0(new Date(), -10), fmt0(new Date(), -1)]; plan.period ||= [fmt0(new Date(), -10), fmt0(new Date(), -1)];
const [fromDate, toDate] = plan.period;
const [key, { name }] = Object.entries(TABLE).find((item) => item[1].uri === uri); const [key, { name }] = Object.entries(TABLE).find((item) => item[1].uri === uri);
let tasks = await createTasks(uri, period); let tasks = await createTasks(uri, fromDate, toDate);
tasks = sn ? [tasks.at(sn)] : tasks; tasks = sn ? [tasks.at(sn)] : tasks;
// 在季度周期过滤掉月度任务 // 在季度周期过滤掉月度任务
// tasks = tasks.filter((x)=>!x.isMonthly); // tasks = tasks.filter((x)=>!x.isMonthly);
...@@ -29,9 +30,14 @@ async function run(options = {}) { ...@@ -29,9 +30,14 @@ async function run(options = {}) {
for (const task of tasks) { for (const task of tasks) {
const pf = `${++idx}/${len}`; const pf = `${++idx}/${len}`;
addLog(`${name} 运行中 ${pf}`); addLog(`${name} 运行中 ${pf}`);
if (task.options.isMmonthly && !plan.isMmonthly) {
console.log(`${pf} skip1`);
continue;
}
console.log(`${pf} start: ${JSON.stringify(task)}`); console.log(`${pf} start: ${JSON.stringify(task)}`);
let data = await task.send(); let data = await task.send();
if (data === true || data === false) { if (data === true || data === false) {
console.log(`${pf} skip2`);
continue; continue;
} }
......
...@@ -26,20 +26,20 @@ function onclick(e) { ...@@ -26,20 +26,20 @@ function onclick(e) {
// const period = [fromInput.value, toInput.value]; // const period = [fromInput.value, toInput.value];
// const period = ['2025-01-01', '2025-02-01']; // const period = ['2025-01-01', '2025-02-01'];
const period = new Plan(null, params).get(); const plan = new Plan(null, params).get();
if (period === undefined) { if (plan.period === undefined) {
addLog(`${name} 未到指定时间`, true); addLog(`${name} 未到指定时间`, true);
return; return;
} }
// Deprecated // Deprecated
const diff = getTimeDiff(period[1], 1); const diff = getTimeDiff(plan.period[1], 1);
if (-12 < diff && diff < 0) { if (-12 < diff && diff < 0) {
const select = confirm("时间未到, 是否确认继续"); const select = confirm("时间未到, 是否确认继续");
if (!select) return; if (!select) return;
} }
chrome.storage.local.get(['isRunning'], ({ isRunning }) => { chrome.storage.local.get(['isRunning'], ({ isRunning }) => {
if (!isRunning) { if (!isRunning) {
chrome.runtime.sendMessage({ type: "run", data: { period, uri } }); chrome.runtime.sendMessage({ type: "run", data: { plan, uri } });
} }
}); });
}); });
......
...@@ -48,8 +48,7 @@ export class Plan { ...@@ -48,8 +48,7 @@ export class Plan {
}; };
} }
get(day = null) { get(day = null) {
const it = day ? this.table[day] : this.table[this.dt.date()]; return (day ? this.table[day] : this.table[this.dt.date()]) ?? {};
return it?.period;
} }
next() { next() {
let dt = this.dt; let dt = this.dt;
...@@ -67,12 +66,12 @@ export class Plan { ...@@ -67,12 +66,12 @@ export class Plan {
.set("month", month); .set("month", month);
return new Plan(dt, this.params); return new Plan(dt, this.params);
} }
_addParam(execTime, period) { _addParam(execTime, period, isMmonthly = false) {
// MM-dd HH:mm:ss // MM-dd HH:mm:ss
const dt = dayjs.tz(new Date(), CST) const dt = dayjs.tz(new Date(), CST)
.set("date", +execTime.substring(0, 2)) .set("date", +execTime.substring(0, 2))
.set("hour", +execTime.substring(3, 5)); .set("hour", +execTime.substring(3, 5));
this.table[dt.date()] = { period, dt }; this.table[dt.date()] = { period, dt, isMmonthly };
this.params.push(arguments); this.params.push(arguments);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment