Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Update Map component to adjust zoom level and data rendering logic

Bill Yang67af002ec749
diff --git a/client/src/app/[site]/main/components/sections/Map.tsx b/client/src/app/[site]/main/components/sections/Map.tsx
index e9d9ca9df..f41a9d361 100644
--- a/client/src/app/[site]/main/components/sections/Map.tsx
+++ b/client/src/app/[site]/main/components/sections/Map.tsx
@@ -165,7 +165,7 @@ export function Map() {
const MapEventHandler = () => {
const map = useMapEvent("zoomend", () => {
- const newMapView = map.getZoom() >= 4 ? "subdivisions" : "countries";
+ const newMapView = map.getZoom() >= 5 ? "subdivisions" : "countries";
if (newMapView !== mapView) {
setMapView(newMapView);
setTooltipContent(null);
@@ -208,18 +208,18 @@ export function Map() {
}}
>
<MapEventHandler />
- {mapView === "countries" && countriesGeoData && (
+ {mapView === "subdivisions" && subdivisionsGeoData && (
<GeoJSON
- key={`countries-${dataVersion}`}
- data={countriesGeoData as GeoJsonObject}
+ key={`subdivisions-${dataVersion}`}
+ data={subdivisionsGeoData as GeoJsonObject}
style={handleStyle}
onEachFeature={handleEachFeature}
/>
)}
- {mapView === "subdivisions" && subdivisionsGeoData && (
+ {mapView === "countries" && countriesGeoData && (
<GeoJSON
- key={`subdivisions-${dataVersion}`}
- data={subdivisionsGeoData as GeoJsonObject}
+ key={`countries-${dataVersion}`}
+ data={countriesGeoData as GeoJsonObject}
style={handleStyle}
onEachFeature={handleEachFeature}
/>
@@ -256,7 +256,7 @@ export function Map() {
{tooltipContent.count.toLocaleString()}
</span>{" "}
<span className="text-neutral-300">
- ({tooltipContent.percentage.toFixed(1)}%) pageviews
+ ({tooltipContent.percentage.toFixed(1)}%) sessions
</span>
</div>
</div>
diff --git a/server/src/api/getLiveUsercount.ts b/server/src/api/getLiveUsercount.ts
index 22d2a7fad..5a6c79bd2 100644
--- a/server/src/api/getLiveUsercount.ts
+++ b/server/src/api/getLiveUsercount.ts
@@ -1,8 +1,7 @@
import { FastifyReply, FastifyRequest } from "fastify";
-import { db } from "../db/postgres/postgres.js";
-import { activeSessions } from "../db/postgres/schema.js";
-import { eq, count } from "drizzle-orm";
+import clickhouse from "../db/clickhouse/clickhouse.js";
import { getUserHasAccessToSite } from "../lib/auth-utils.js";
+import { processResults } from "./utils.js";
export const getLiveUsercount = async (
req: FastifyRequest<{ Params: { site: string } }>,
@@ -14,10 +13,15 @@ export const getLiveUsercount = async (
return res.status(403).send({ error: "Forbidden" });
}
- const result = await db
- .select({ count: count() })
- .from(activeSessions)
- .where(eq(activeSessions.siteId, Number(site)));
+ const query = await clickhouse.query({
+ query: `SELECT COUNT(DISTINCT(session_id)) AS count FROM pageviews WHERE timestamp > now() - interval '5 minute' AND site_id = {siteId:Int32}`,
+ format: "JSONEachRow",
+ query_params: {
+ siteId: Number(site),
+ },
+ });
+
+ const result = await processResults<{ count: number }>(query);
return res.send({ count: result[0].count });
};