Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package: Analytics Engine #399

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6319887
first up
CraigglesO Oct 1, 2022
e6351c5
remove TODOs; adjust readmes
CraigglesO Oct 1, 2022
6eac0a4
migrate to execute sql data inside engine code
CraigglesO Oct 1, 2022
3ae1bfd
parse INTERVAL; add test cases
CraigglesO Oct 2, 2022
da11a07
interval, complete test cases
CraigglesO Oct 2, 2022
b307341
add miniflare api access + tests; drop 'UNIQUE'
CraigglesO Oct 2, 2022
a5d2fee
fix options; all analytics exists inside singular db now
CraigglesO Oct 2, 2022
cc1547d
add jest testings
CraigglesO Oct 2, 2022
5fb6b54
add jest testings
CraigglesO Oct 2, 2022
b2810df
fix get->all; add http testing in minfilare; remove unique
CraigglesO Oct 2, 2022
11b2692
add vitest tests
CraigglesO Oct 2, 2022
13916f2
fix internal problem
CraigglesO Oct 2, 2022
d798479
TODATETIME fix
CraigglesO Oct 2, 2022
9b359ac
support QUANTILEWEIGHTED
CraigglesO Oct 3, 2022
39ed5cf
support QUANTILEWEIGHTED
CraigglesO Oct 3, 2022
7cb5d40
add formatting; minor fixes/adjustments
CraigglesO Oct 3, 2022
556d5eb
import sorting fix
CraigglesO Oct 3, 2022
16b28f0
more error cases
CraigglesO Oct 4, 2022
7abffeb
re-arrange
CraigglesO Oct 4, 2022
8e1e310
writeDataPoint is sync
CraigglesO Oct 4, 2022
44dc3d8
minor fixes
CraigglesO Oct 4, 2022
c25b639
ensure added functions & keywords are working
CraigglesO Oct 5, 2022
5865e9e
temporarily edit npx-import to get passing tests
CraigglesO Oct 6, 2022
62f63d5
pre-add better-sqlite
CraigglesO Oct 6, 2022
55b1d93
tmp move npx-import
CraigglesO Oct 6, 2022
ca5fe4f
ensure TODATETIME sticks to UTC
CraigglesO Oct 6, 2022
aabd9fb
update to latest master
CraigglesO Oct 24, 2022
c9b272d
first fix set
CraigglesO Oct 24, 2022
f93f54e
bug fixes
CraigglesO Oct 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix get->all; add http testing in minfilare; remove unique
  • Loading branch information
CraigglesO committed Oct 2, 2022
commit b2810df4abebf3e1ab4659a3ede5dcabe317bc0f
4 changes: 2 additions & 2 deletions packages/analytics-engine/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ CREATE TABLE IF NOT EXISTS {{BINDING}} (
blob20 TEXT
);

CREATE UNIQUE INDEX IF NOT EXISTS {{BINDING}}_index ON {{BINDING}} (dataset, timestamp);
CREATE UNIQUE INDEX IF NOT EXISTS {{BINDING}}_index ON {{BINDING}} (dataset, index1, timestamp);
CREATE INDEX IF NOT EXISTS {{BINDING}}_index ON {{BINDING}} (dataset, timestamp);
CREATE INDEX IF NOT EXISTS {{BINDING}}_index ON {{BINDING}} (dataset, index1, timestamp);

COMMIT;
`;
Expand Down
15 changes: 13 additions & 2 deletions packages/http-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Transform, Writable } from "stream";
import { ReadableStream } from "stream/web";
import { URL } from "url";
import zlib from "zlib";
import { AnalyticsEnginePlugin } from "@miniflare/analytics-engine";
import {
CorePluginSignatures,
IncomingRequestCfProperties,
Expand Down Expand Up @@ -276,6 +277,8 @@ export function createRequestListener<Plugins extends HTTPPluginSignatures>(
// can't be handled by workers and are used for utility interfaces.
const pathname = url.pathname.replace(/\/$/, "");
if (pathname.startsWith("/cdn-cgi/")) {
const headStatus = { "Content-Type": "text/plain; charset=UTF-8" };
let body: undefined | string = undefined;
// TODO (someday): consider adding other utility interfaces for KV, DO, etc
// (maybe add another Plugin field/method/decorator for contributes)
if (pathname === "/cdn-cgi/mf/scheduled") {
Expand All @@ -288,11 +291,19 @@ export function createRequestListener<Plugins extends HTTPPluginSignatures>(
url
);
status = 200;
} else if (pathname === "/cdn-cgi/mf/analytics_engine/sql") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure here, maybe analytics-engine instead of _?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we match the actual api?
https://developers.cloudflare.com/analytics/analytics-engine/sql-api/
https://api.cloudflare.com/client/v4/accounts/<account_id>/analytics_engine/sql

const query = await request.text();
const plugin = (await mf.getPlugins())
.AnalyticsEnginePlugin as AnalyticsEnginePlugin;
const storage = mf.getPluginStorage("AnalyticsEnginePlugin");
const aeDB = await plugin.getStorage(storage);
body = JSON.stringify(aeDB.prepare(query).all() ?? []);
headStatus["Content-Type"] = "application/javascript";
} else {
status = 404;
}
res?.writeHead(status, { "Content-Type": "text/plain; charset=UTF-8" });
res?.end();
res?.writeHead(status, headStatus);
res?.end(body);
} else {
try {
response = await mf.dispatchFetch(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
async function get() {
// return fetch("http:https://localhost/cdn-cgi/mf/analytics_engine/sql", {
// method: "POST",
// body: "SELECT value FROM entries LIMIT 1;",
// }).then(async (b) => await b.json());
return queryMiniflareAnalyticsEngine(
"SELECT dataset, blob1, blob2, blob3, double1, double2, double3 FROM AE_TEST_DB LIMIT 1;"
);
Expand All @@ -13,13 +9,15 @@ test("AE test 1", async () => {
blobs: ["a", "b", "c"],
doubles: [0, 1, 2],
});
expect(await get()).toMatchObject({
dataset: "AE_TEST_DB",
blob1: "a",
blob2: "b",
blob3: "c",
double1: 0,
double2: 1,
double3: 2,
});
expect(await get()).toMatchObject([
{
dataset: "AE_TEST_DB",
blob1: "a",
blob2: "b",
blob3: "c",
double1: 0,
double2: 1,
double3: 2,
},
]);
});
16 changes: 14 additions & 2 deletions packages/miniflare/test/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,22 @@ test("Miniflare: getAnalyticsEngine: gets Analytics Engine from bindings", async
TEST_DB: "TEST_DB",
},
});
// await mf.dispatchFetch("http:https://localhost/");
const server = await mf.startServer();
const port = (server.address() as AddressInfo).port;
await mf.dispatchFetch("http:https://localhost/");
const ae = await mf.getAnalyticsEngine("TEST_DB");
await ae.writeDataPoint({ blobs: ["c", "d"], doubles: [3, 4] });
t.pass();
const res = await fetch(
`http:https://localhost:${port}/cdn-cgi/mf/analytics_engine/sql`,
{
method: "POST",
body: "SELECT blob1, blob2, double1, double2 FROM TEST_DB LIMIT 2;",
}
).then(async (b) => await b.json());
t.deepEqual(res, [
{ blob1: "a", blob2: "b", double1: 1, double2: 2 },
{ blob1: "c", blob2: "d", double1: 3, double2: 4 },
]);
});
test("Miniflare: getKVNamespace: gets KV namespace", async (t) => {
const mf = new Miniflare({
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-test-environment/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function createMiniflareEnvironmentUtilities(
const plugin = (await mf.getPlugins()).AnalyticsEnginePlugin;
const storage = mf.getPluginStorage("AnalyticsEnginePlugin");
const aeDB = await plugin.getStorage(storage);
return aeDB.prepare(query).get();
return aeDB.prepare(query).all();
},
};
}