Skip to content

Commit

Permalink
feat(back): optimize queries and reduce size to last 2-3hours
Browse files Browse the repository at this point in the history
  • Loading branch information
macojaune committed Oct 26, 2024
1 parent 3b1c46a commit b6b9610
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
44 changes: 29 additions & 15 deletions server/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import { sqliteTable, real, text, integer } from "drizzle-orm/sqlite-core";
import {
sqliteTable,
real,
text,
integer,
index,
} from "drizzle-orm/sqlite-core";

export const powerStatuses = sqliteTable("power_statuses", {
id: text("id").primaryKey(),
latitude: real("latitude").notNull(),
longitude: real("longitude").notNull(),
isOn: integer("has_power", { mode: "boolean" }).notNull(),
createdAt: integer("created_at", { mode: "timestamp" }).notNull(),
});
export const powerStatuses = sqliteTable(
"power_statuses",
{
id: text("id").primaryKey(),
latitude: real("latitude").notNull(),
longitude: real("longitude").notNull(),
isOn: integer("has_power", { mode: "boolean" }).notNull(),
createdAt: integer("created_at", { mode: "timestamp" }).notNull(),
},
(table) => ({ dateIdx: index("power_date_idx").on(table.createdAt) }),
);

export type PowerStatus = typeof powerStatuses.$inferSelect;
export type InsertPowerStatus = typeof powerStatuses.$inferInsert;

export const waterStatuses = sqliteTable("water_statuses", {
id: text("id").primaryKey(),
latitude: real("latitude").notNull(),
longitude: real("longitude").notNull(),
isOn: integer("has_water", { mode: "boolean" }).notNull(),
createdAt: integer("created_at", { mode: "timestamp" }).notNull(),
});
export const waterStatuses = sqliteTable(
"water_statuses",
{
id: text("id").primaryKey(),
latitude: real("latitude").notNull(),
longitude: real("longitude").notNull(),
isOn: integer("has_water", { mode: "boolean" }).notNull(),
createdAt: integer("created_at", { mode: "timestamp" }).notNull(),
},
(table) => ({ dateIdx: index("water_date_idx").on(table.createdAt) }),
);

export type WaterStatus = typeof waterStatuses.$inferSelect;
export type InsertWaterStatus = typeof waterStatuses.$inferInsert;
4 changes: 2 additions & 2 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ app.get("/api/power-statuses", async (c) => {
})
.from(powerStatuses)
.where(
gte(powerStatuses.createdAt, new Date(Date.now() - 6 * 60 * 60 * 1000)),
gte(powerStatuses.createdAt, new Date(Date.now() - 2 * 60 * 60 * 1000)),
) //limit to 6 hours ago
.orderBy(desc(powerStatuses.createdAt))
.groupBy(powerStatuses.latitude, powerStatuses.longitude);
Expand Down Expand Up @@ -88,7 +88,7 @@ app.get("/api/water-statuses", async (c) => {
})
.from(waterStatuses)
.where(
gte(waterStatuses.createdAt, new Date(Date.now() - 6 * 60 * 60 * 1000)),
gte(waterStatuses.createdAt, new Date(Date.now() - 3 * 60 * 60 * 1000)),
) //limit to 6 hours ago
.orderBy(desc(waterStatuses.createdAt))
.groupBy(waterStatuses.latitude, waterStatuses.longitude);
Expand Down

0 comments on commit b6b9610

Please sign in to comment.