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
interval, complete test cases
  • Loading branch information
CraigglesO committed Oct 2, 2022
commit da11a07442f4609ac30dbdc683a0b7c38763fe7b
16 changes: 9 additions & 7 deletions packages/analytics-engine/src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { SqliteDB } from "@miniflare/shared";

// https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/IntervalKind.cpp#L13
const TIME = {
SECOND: 1,
MINUTE: 60, // 60sec
HOUR: 60 * 60, // 60sec * 60min
DAY: 60 * 60 * 24, // 60sec * 60min * 24hours
MONTH: 60 * 60 * 24 * 30, // 60sec * 60min * 24hours
YEAR: 60 * 60 * 24 * 365,
HOUR: 3600, // 60sec * 60min
DAY: 86400, // 60sec * 60min * 24hours
MONTH: 2_629_746, // Exactly 1/12 of a year
YEAR: 31_556_952, // The average length of a Gregorian year is equal to 365.2425 days
};

export default function buildSQLFunctions(sqliteDB: SqliteDB) {
Expand Down Expand Up @@ -58,12 +59,13 @@ export default function buildSQLFunctions(sqliteDB: SqliteDB) {
"INTERVAL",
(
intervalValue: string | number,
IntervalType: keyof typeof TIME
): number => {
intervalType: keyof typeof TIME
): number | null => {
if (typeof intervalValue === "string") {
intervalValue = parseInt(intervalValue);
}
const multiplier = TIME[IntervalType] ?? 0;
const multiplier = TIME[intervalType];
if (multiplier === undefined) return null;
return intervalValue * multiplier;
}
);
Expand Down
49 changes: 47 additions & 2 deletions packages/analytics-engine/test/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,51 @@ test("Analytics Engine: Test each function to ensure they work.", async (t) => {
const res12 = stmt12.get("a3cd45");
t.is(res12.answer, 42 * 60 * 60 * 24);

// TODO: test SECOND, MINUTE, ...
// TODO: test if not the right word
// test INTERVAL SECOND
const stmt13Input = prepare(
"SELECT INTERVAL 42 SECOND AS answer FROM TEST_BINDING WHERE index1 = ?"
);
const stmt13 = sqliteDB.prepare(stmt13Input);
const res13 = stmt13.get("a3cd45");
t.is(res13.answer, 42);

// test INTERVAL MINUTE
const stmt14Input = prepare(
"SELECT INTERVAL 42 MINUTE AS answer FROM TEST_BINDING WHERE index1 = ?"
);
const stmt14 = sqliteDB.prepare(stmt14Input);
const res14 = stmt14.get("a3cd45");
t.is(res14.answer, 42 * 60);

// test INTERVAL HOUR
const stmt15Input = prepare(
"SELECT INTERVAL 42 HOUR AS answer FROM TEST_BINDING WHERE index1 = ?"
);
const stmt15 = sqliteDB.prepare(stmt15Input);
const res15 = stmt15.get("a3cd45");
t.is(res15.answer, 42 * 60 * 60);

// test INTERVAL MONTH
const stmt16Input = prepare(
"SELECT INTERVAL 2 MONTH AS answer FROM TEST_BINDING WHERE index1 = ?"
);
const stmt16 = sqliteDB.prepare(stmt16Input);
const res16 = stmt16.get("a3cd45");
t.is(res16.answer, 2 * 2_629_746);

// test INTERVAL YEAR
const stmt17Input = prepare(
"SELECT INTERVAL 2 YEAR AS answer FROM TEST_BINDING WHERE index1 = ?"
);
const stmt17 = sqliteDB.prepare(stmt17Input);
const res17 = stmt17.get("a3cd45");
t.is(res17.answer, 2 * 31_556_952);

// test INTERVAL BAD INPUT
const stmt18Input = prepare(
"SELECT INTERVAL 1 UNKNOWN AS answer FROM TEST_BINDING WHERE index1 = ?"
);
const stmt18 = sqliteDB.prepare(stmt18Input);
const res18 = stmt18.get("a3cd45");
t.is(res18.answer, null);
});