This commit is contained in:
1 parent
11e03d1bd7
commit
1a462c54da
2 files changed
+33
-14
No files matched your search
@@ -55,6 +55,10 @@
|
||||
if (r2Data.length > 0) {
|
||||
r2PayloadSize = r2Data[0]?.sum?.payloadSize || 0;
|
||||
}
|
||||
|
||||
if (data.errorMsg) {
|
||||
error = data.errorMsg;
|
||||
}
|
||||
}
|
||||
loading = false;
|
||||
})
|
||||
|
||||
@@ -115,7 +115,7 @@ export async function fetchAnalytics(platform: App.Platform | undefined) {
|
||||
}
|
||||
}
|
||||
|
||||
const query = `
|
||||
const trafficQuery = `
|
||||
query {
|
||||
viewer {
|
||||
zones(filter: { zoneTag: "${zoneId}" }) {
|
||||
@@ -134,39 +134,54 @@ export async function fetchAnalytics(platform: App.Platform | undefined) {
|
||||
requests
|
||||
pageViews
|
||||
}
|
||||
uniq {
|
||||
uniques
|
||||
}
|
||||
}
|
||||
}
|
||||
${accountQuery}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
try {
|
||||
const response = await fetch('https://api.cloudflare.com/client/v4/graphql', {
|
||||
const trafficRes = await fetch('https://api.cloudflare.com/client/v4/graphql', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({ query })
|
||||
body: JSON.stringify({ query: trafficQuery })
|
||||
});
|
||||
|
||||
const result = (await response.json()) as any;
|
||||
const trafficResult = (await trafficRes.json()) as any;
|
||||
const trafficData = trafficResult?.data?.viewer?.zones?.[0]?.httpRequests1dGroups || [];
|
||||
|
||||
const trafficData = result?.data?.viewer?.zones?.[0]?.httpRequests1dGroups || [];
|
||||
const accountData = result?.data?.viewer?.accounts?.[0];
|
||||
let d1Data: any[] = [];
|
||||
let r2Data: any[] = [];
|
||||
|
||||
if (accountQuery) {
|
||||
const accQueryStr = `query { viewer { ${accountQuery} } }`;
|
||||
const accRes = await fetch('https://api.cloudflare.com/client/v4/graphql', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({ query: accQueryStr })
|
||||
});
|
||||
const accResult = (await accRes.json()) as any;
|
||||
const accountData = accResult?.data?.viewer?.accounts?.[0];
|
||||
if (accountData) {
|
||||
d1Data = accountData.d1AnalyticsAdaptiveGroups || [];
|
||||
r2Data = accountData.r2StorageAdaptiveGroups || [];
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
traffic: trafficData,
|
||||
d1: accountData?.d1AnalyticsAdaptiveGroups || [],
|
||||
r2: accountData?.r2StorageAdaptiveGroups || [],
|
||||
d1: d1Data,
|
||||
r2: r2Data,
|
||||
missingEnvVars: !accountId || !d1Id || !r2Bucket
|
||||
};
|
||||
} catch (err) {
|
||||
} catch (err: any) {
|
||||
console.error('Failed to fetch analytics', err);
|
||||
return null;
|
||||
return { errorMsg: '获取统计数据失败,请检查 Token 权限或网络环境' };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user