Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Sqlite confiuration and aggregation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Nov 10, 2020
1 parent ce91f36 commit a094ff7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,6 +9,7 @@ dump.rdb
newrelic_agent.log
*.retry
coverage
*.sqlite

# API
log
Expand Down
8 changes: 7 additions & 1 deletion apps/staging-public/.env.example
Expand Up @@ -28,7 +28,13 @@ REDIS_URL="redis://localhost:6379/0"
## DATABASE ##
##############

DATABASE_URL="postgresql://localhost:5432/grouparoo_development"
# To use sqlite
DATABASE_URL="sqlite://grouparoo_development.sqlite"

# To use Postgres
# DATABASE_URL="postgresql://localhost:5432/grouparoo_development"

# With custom SSL options:
# DATABASE_SSL=true
# DATABASE_SSL_SELF_SIGNED=true

Expand Down
55 changes: 45 additions & 10 deletions core/api/src/actions/events.ts
@@ -1,5 +1,5 @@
import { AuthenticatedAction } from "../classes/authenticatedAction";
import { api } from "actionhero";
import { api, config } from "actionhero";
import { Event } from "../models/Event";
import { EventData } from "../models/EventData";
import { Op } from "sequelize";
Expand Down Expand Up @@ -119,17 +119,52 @@ export class EventsCounts extends AuthenticatedAction {

if (params.type) where["type"] = params.type;

let timeFunc = api.sequelize.fn(
"date_trunc",
params.dateTrunc,
api.sequelize.col("occurredAt")
);

if (config.sequelize.dialect === "sqlite") {
switch (params.dateTrunc) {
case "year": {
timeFunc = api.sequelize.literal(`strftime('%Y', \`occurredAt\`)`);
break;
}
case "month": {
timeFunc = api.sequelize.literal(`strftime('%Y %m', \`occurredAt\`)`);
break;
}
case "day": {
timeFunc = api.sequelize.literal(
`strftime('%Y %m %d', \`occurredAt\`)`
);
break;
}
case "hour": {
timeFunc = api.sequelize.literal(
`strftime('%Y %m %d %H', \`occurredAt\`)`
);
break;
}
case "minute": {
timeFunc = api.sequelize.literal(
`strftime('%Y %m %d %H %M', \`occurredAt\`)`
);
break;
}
default: {
throw new Error(
`cannot build time function for ${params.dateTrunc} on sqlite`
);
}
}
}

const counts = await Event.findAll({
attributes: [
"type",
[
api.sequelize.fn(
"date_trunc",
params.dateTrunc,
api.sequelize.col("occurredAt")
),
"time",
],
[timeFunc, "time"],
[api.sequelize.fn("COUNT", "guid"), "count"],
],
where,
Expand Down Expand Up @@ -184,7 +219,7 @@ export class EventsTypes extends AuthenticatedAction {
limit: params.limit,
offset: params.offset,
group: ["type"],
order: [["count", "desc"]],
order: [[api.sequelize.literal("count"), "desc"]],
});

const total = await Event.count({
Expand Down
20 changes: 13 additions & 7 deletions core/api/src/actions/totals.ts
@@ -1,4 +1,4 @@
import { api } from "actionhero";
import { api, config } from "actionhero";
import { AuthenticatedAction } from "../classes/authenticatedAction";
import { Op } from "sequelize";
import { Group } from "../models/Group";
Expand Down Expand Up @@ -27,7 +27,11 @@ const modelClasses = {
TeamMember,
};

function dateString(d) {
function dateString(d: Date | string) {
if (typeof d === "string") {
d = new Date(d);
}

return d.toJSON().slice(0, 10);
}

Expand Down Expand Up @@ -65,17 +69,19 @@ export class TotalsAction extends AuthenticatedAction {

const total: number = await model.count();

const groupStatement =
config.sequelize.dialect === "postgres"
? api.sequelize.fn("date_trunc", "day", api.sequelize.col("createdAt"))
: api.sequelize.literal(`strftime('%Y %m %d', \`createdAt\`)`);
const rolling: Array<{ date: string; count: number }> = await model
.count({
where: { createdAt: { [Op.gte]: new Date(dates[0]) } },
group: [
api.sequelize.fn("date_trunc", "day", api.sequelize.col("createdAt")),
],
group: [groupStatement],
})
.map((row) => {
// @ts-ignore
const dateKey = Object.keys(row).find((r) => r !== "count");
return {
date: dateString(row.date_trunc),
date: dateString(row[dateKey]),
count: row.count,
};
});
Expand Down
10 changes: 9 additions & 1 deletion core/api/src/config/sequelize.ts
Expand Up @@ -53,9 +53,16 @@ export const DEFAULT = {
ssl = { rejectUnauthorized: false };
}

// sqlite overrides
if (dialect === "sqlite") {
if (!host) host = ":memory:";
}

// console.log(`grouparoo using ${dialect} database @ ${host}`);

return {
autoMigrate: true,
logging: false,
logging: (...msg) => console.log(msg),
dialect: dialect,
port: parseInt(port),
database: database,
Expand All @@ -64,6 +71,7 @@ export const DEFAULT = {
password: password,
models: [join(__dirname, "..", "models")],
migrations: [join(__dirname, "..", "migrations")],
storage: host, // only used for sqlite
pool: {
max: process.env.SEQUELIZE_POOL_SIZE
? parseInt(process.env.SEQUELIZE_POOL_SIZE)
Expand Down
1 change: 1 addition & 0 deletions core/package.json
Expand Up @@ -93,6 +93,7 @@
"reflect-metadata": "0.1.13",
"sequelize": "5.22.3",
"sequelize-typescript": "1.1.0",
"sqlite3": "^5.0.0",
"stacktrace-js": "2.0.2",
"swagger-ui": "3.36.2",
"ts-node": "9.0.0",
Expand Down

0 comments on commit a094ff7

Please sign in to comment.