-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecc2330
commit 31881c4
Showing
9 changed files
with
253 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,93 @@ | ||
interface AnalyticsQueryResult { | ||
meta: string, | ||
data: [ | ||
{ | ||
[key: string]: any | ||
} | ||
], | ||
rows: number, | ||
rows_before_limit_at_least: number | ||
meta: string, | ||
data: [ | ||
{ | ||
[key: string]: any | ||
} | ||
], | ||
rows: number, | ||
rows_before_limit_at_least: number | ||
} | ||
|
||
export class AnalyticsEngineAPI { | ||
cfApiToken: string; | ||
cfAccountId: string; | ||
defaultHeaders: { | ||
"content-type": string; | ||
"X-Source": string; | ||
"Authorization": string; | ||
}; | ||
defaultUrl: string; | ||
cfApiToken: string; | ||
cfAccountId: string; | ||
defaultHeaders: { | ||
"content-type": string; | ||
"X-Source": string; | ||
"Authorization": string; | ||
}; | ||
defaultUrl: string; | ||
|
||
constructor(cfAccountId: string, cfApiToken: string) { | ||
this.cfAccountId = cfAccountId; | ||
this.cfApiToken = cfApiToken; | ||
constructor(cfAccountId: string, cfApiToken: string) { | ||
this.cfAccountId = cfAccountId; | ||
this.cfApiToken = cfApiToken; | ||
|
||
this.defaultUrl = `https://api.cloudflare.com/client/v4/accounts/${this.cfAccountId}/analytics_engine/sql`; | ||
this.defaultHeaders = { | ||
"content-type": "application/json;charset=UTF-8", | ||
"X-Source": "Cloudflare-Workers", | ||
"Authorization": `Bearer ${this.cfApiToken}` | ||
this.defaultUrl = `https://api.cloudflare.com/client/v4/accounts/${this.cfAccountId}/analytics_engine/sql`; | ||
this.defaultHeaders = { | ||
"content-type": "application/json;charset=UTF-8", | ||
"X-Source": "Cloudflare-Workers", | ||
"Authorization": `Bearer ${this.cfApiToken}` | ||
} | ||
} | ||
} | ||
|
||
async getCount(sinceDays: number): Promise<number> { | ||
// defaults to 1 day if not specified | ||
const interval = sinceDays || 1; | ||
async getCount(sinceDays: number): Promise<number> { | ||
// defaults to 1 day if not specified | ||
const interval = sinceDays || 1; | ||
|
||
const query = ` | ||
const query = ` | ||
SELECT SUM(_sample_interval) as count | ||
FROM metricsDataset | ||
WHERE timestamp > NOW() - INTERVAL '${interval}' DAY`; | ||
|
||
const returnPromise = new Promise<number>((resolve, reject) => (async () => { | ||
const response = await fetch(this.defaultUrl, { | ||
method: 'POST', | ||
body: query, | ||
headers: this.defaultHeaders, | ||
}); | ||
const returnPromise = new Promise<number>((resolve, reject) => (async () => { | ||
const response = await fetch(this.defaultUrl, { | ||
method: 'POST', | ||
body: query, | ||
headers: this.defaultHeaders, | ||
}); | ||
|
||
if (!response.ok) { | ||
reject(response.statusText); | ||
} | ||
if (!response.ok) { | ||
reject(response.statusText); | ||
} | ||
|
||
const responseData = await response.json() as AnalyticsQueryResult; | ||
resolve(responseData.data[0]['count']); | ||
})()); | ||
return returnPromise; | ||
} | ||
const responseData = await response.json() as AnalyticsQueryResult; | ||
resolve(responseData.data[0]['count']); | ||
})()); | ||
return returnPromise; | ||
} | ||
|
||
async getCountByReferer(sinceDays: number): Promise<any> { | ||
// defaults to 1 day if not specified | ||
const interval = sinceDays || 1; | ||
async getCountByReferer(sinceDays: number): Promise<any> { | ||
// defaults to 1 day if not specified | ||
const interval = sinceDays || 1; | ||
|
||
const query = ` | ||
const query = ` | ||
SELECT SUM(_sample_interval) as count, blob2 as referer | ||
FROM metricsDataset | ||
WHERE timestamp > NOW() - INTERVAL '${interval}' DAY | ||
GROUP BY referer | ||
ORDER BY count DESC | ||
`; | ||
|
||
const returnPromise = new Promise<any>((resolve, reject) => (async () => { | ||
const response = await fetch(this.defaultUrl, { | ||
method: 'POST', | ||
body: query, | ||
headers: this.defaultHeaders, | ||
}); | ||
const returnPromise = new Promise<any>((resolve, reject) => (async () => { | ||
const response = await fetch(this.defaultUrl, { | ||
method: 'POST', | ||
body: query, | ||
headers: this.defaultHeaders, | ||
}); | ||
|
||
if (!response.ok) { | ||
reject(response.statusText); | ||
} | ||
if (!response.ok) { | ||
reject(response.statusText); | ||
} | ||
|
||
const responseData = await response.json() as AnalyticsQueryResult; | ||
var result = responseData.data.reduce((acc, cur) => { | ||
acc.push([cur['referer'], cur['count']]); | ||
return acc; | ||
}, []); | ||
console.log(result);; | ||
resolve(result); | ||
})()); | ||
return returnPromise; | ||
} | ||
const responseData = await response.json() as AnalyticsQueryResult; | ||
var result = responseData.data.reduce((acc, cur) => { | ||
acc.push([cur['referer'], cur['count']]); | ||
return acc; | ||
}, []); | ||
console.log(result);; | ||
resolve(result); | ||
})()); | ||
return returnPromise; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import type { LinksFunction } from "@remix-run/cloudflare"; | ||
import { cssBundleHref } from "@remix-run/css-bundle"; | ||
import { | ||
Links, | ||
LiveReload, | ||
Meta, | ||
Outlet, | ||
Scripts, | ||
ScrollRestoration, | ||
Links, | ||
LiveReload, | ||
Meta, | ||
Outlet, | ||
Scripts, | ||
ScrollRestoration, | ||
} from "@remix-run/react"; | ||
|
||
export const links: LinksFunction = () => [ | ||
...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []), | ||
...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []), | ||
]; | ||
|
||
export default function App() { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<meta charSet="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<Meta /> | ||
<Links /> | ||
</head> | ||
<body> | ||
<Outlet /> | ||
<ScrollRestoration /> | ||
<Scripts /> | ||
<LiveReload /> | ||
</body> | ||
</html> | ||
); | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<meta charSet="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<Meta /> | ||
<Links /> | ||
</head> | ||
<body> | ||
<Outlet /> | ||
<ScrollRestoration /> | ||
<Scripts /> | ||
<LiveReload /> | ||
</body> | ||
</html> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.