Skip to content

Commit

Permalink
Feat/newjitsu/rotor http (#1075)
Browse files Browse the repository at this point in the history
rotor: replace redis with in-mem store 
rotor: http-only mode
rotor: various optimizations
rotor: ga4-destination: adjust event name to requriement
---------

Co-authored-by: Vladimir Klimontovich <[email protected]>
  • Loading branch information
absorbb and vklimontovich authored Dec 27, 2023
1 parent d7a602a commit 397f898
Show file tree
Hide file tree
Showing 18 changed files with 650 additions and 325 deletions.
7 changes: 5 additions & 2 deletions libs/core-functions/__tests__/lib/mem-store.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { AnonymousEventsStore, SetOpts, Store } from "@jitsu/protocols/functions";
import { AnonymousEventsStore, SetOpts, Store, TTLStore } from "@jitsu/protocols/functions";
import { AnalyticsServerEvent } from "@jitsu/protocols/analytics";

export function createStore(): Store {
export function createStore(): TTLStore {
return {
del(key: string): Promise<void> {
throw new Error("Method not implemented.");
},
get(key: string): Promise<any> {
throw new Error("Method not implemented.");
},
getWithTTL(key: string): Promise<{ value: any; ttl: number } | undefined> {
throw new Error("Method not implemented.");
},
set(key: string, value: any, opts?: SetOpts): Promise<void> {
throw new Error("Method not implemented.");
},
Expand Down
24 changes: 14 additions & 10 deletions libs/core-functions/src/functions/amplitude-destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ const AmplitudeDestination: JitsuFunction<AnalyticsServerEvent, AmplitudeDestina
) => {
try {
const deviceId = event.anonymousId;
let sessionId: string | undefined = undefined;
let sessionId: number | undefined = undefined;
if (deviceId) {
const systemContext = requireDefined((ctx as any as SystemContext).$system, `$system context is not available`);
const ttlStore = systemContext.store;
const ttlSec = 60 * (props.sessionWindow ?? 30);
const sessionKey = `${ctx.source.id}_${deviceId}_sess`;
const newSession = new Date().getTime();
const savedSession = await ttlStore.get(sessionKey);
if (savedSession) {
log.debug(
`Amplitude session found: ${savedSession} for deviceId: ${deviceId} ttl: ${await ttlStore.ttl(sessionKey)}`
);
const savedSessionValue = await ttlStore.getWithTTL(sessionKey);
if (savedSessionValue) {
sessionId = savedSessionValue.value;
const ttl = savedSessionValue.ttl;
log.debug(`Amplitude session found: ${sessionId} for deviceId: ${deviceId} ttl: ${ttl}`);
if (ttl < ttlSec - 60) {
// refresh ttl not often than once per minute
await ttlStore.set(sessionKey, sessionId, { ttl: ttlSec });
}
} else {
log.debug(`Amplitude session not found for deviceId: ${deviceId} new session: ${newSession}`);
sessionId = new Date().getTime();
log.debug(`Amplitude session not found for deviceId: ${deviceId} new session: ${sessionId}`);
await ttlStore.set(sessionKey, sessionId, { ttl: ttlSec });
}
sessionId = savedSession || newSession;
await ttlStore.set(sessionKey, sessionId, { ttl: 60 * (props.sessionWindow ?? 30) });
}
const groupType = props.groupType || "group";
const endpoint =
Expand Down
7 changes: 6 additions & 1 deletion libs/core-functions/src/functions/ga4-destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ function pageViewEvent(event: AnalyticsServerEvent): Ga4Event {
};
}

function adjustName(name: string): string {
name.replace(/[^a-zA-Z0-9_]/g, "_");
return name.substring(0, 40);
}

function trackEvent(event: AnalyticsServerEvent): Ga4Event {
const evp = event.properties || {};
let params: Record<string, any> = {};
Expand Down Expand Up @@ -275,7 +280,7 @@ function trackEvent(event: AnalyticsServerEvent): Ga4Event {
params.items = getItems(event);
break;
default:
name = eventName;
name = adjustName(eventName);
params = { ...evp };
params = removeProperties(params, StandardProperties);
params.currency = evp.currency;
Expand Down
5 changes: 3 additions & 2 deletions libs/core-functions/src/functions/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ProcessingContext,
ServerContextReservedProps,
} from "@jitsu/protocols/analytics";
import { AnonymousEventsStore, Store } from "@jitsu/protocols/functions";
import { AnonymousEventsStore, EventsStore, TTLStore } from "@jitsu/protocols/functions";

export type MetricsMeta = {
workspaceId: string;
Expand All @@ -24,7 +24,8 @@ export type SystemContext = {
$system: {
anonymousEventsStore: AnonymousEventsStore;
metricsMeta: MetricsMeta;
store: Store;
store: TTLStore;
eventsStore: EventsStore;
};
};

Expand Down
2 changes: 1 addition & 1 deletion libs/core-functions/src/functions/lib/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function createClient() {
log.atInfo().log(`Connecting to MongoDB server...`);

// Create a new MongoClient
const client = new MongoClient(mongodbURL, { compressors: ["zstd"] });
const client = new MongoClient(mongodbURL);
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Establish and verify connection
Expand Down
15 changes: 13 additions & 2 deletions libs/core-functions/src/functions/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SetOpts, Store } from "@jitsu/protocols/functions";
import { SetOpts, Store, TTLStore } from "@jitsu/protocols/functions";
import type { Redis } from "ioredis";
import parse from "parse-duration";
import type { MongoClient } from "mongodb";
Expand Down Expand Up @@ -62,7 +62,7 @@ export const createTtlStore = (namespace: string, redisClient: Redis, defaultTtl
},
});

export const createMongoStore = (namespace: string, mongo: MongoClient, defaultTtlSec: number): Store => {
export const createMongoStore = (namespace: string, mongo: MongoClient, defaultTtlSec: number): TTLStore => {
interface StoreValue {
_id: string;
value: any;
Expand Down Expand Up @@ -101,6 +101,17 @@ export const createMongoStore = (namespace: string, mongo: MongoClient, defaultT
.findOne({ _id: key }, { readPreference: "nearest" });
return res ? res.value : undefined;
},
getWithTTL: async (key: string) => {
const res = await mongo
.db(dbName)
.collection<StoreValue>(namespace)
.findOne({ _id: key }, { readPreference: "nearest" });
if (!res) {
return undefined;
}
const ttl = res.expireAt ? Math.max(Math.floor((res.expireAt.getTime() - new Date().getTime()) / 1000), 0) : -1;
return { value: res.value, ttl };
},
set: async (key: string, obj: any, opts?: SetOpts) => {
await ensureCollection();
const colObj: any = { value: obj };
Expand Down
1 change: 1 addition & 0 deletions libs/core-functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ export * as posthogDestination from "./functions/posthog-destination";
export * as mongodbDestination from "./functions/mongodb-destination";
export { mongodb, mongoAnonymousEventsStore } from "./functions/lib/mongodb";
export type { SystemContext, MetricsMeta } from "./functions/lib/index";
export { httpAgent, httpsAgent } from "./functions/lib/http-agent";
export * from "./functions/lib/store";
export * from "./functions/lib/ua";
Loading

1 comment on commit 397f898

@vercel
Copy link

@vercel vercel bot commented on 397f898 Dec 27, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

new-jitsu – ./webapps/console

ag.ru
logu.au
ozon.ru
sse.ere
erxes.io
baidu.dom
ilmiya.io
sambla.se
bobsec.com
sambla.com
agro4u.life
bluetick.ai
myilmiya.io
protontv.eu
t.quenti.io
alicesec.com
d.askloan.tw
dev.aclis.io
docs.dh19.de
docs.dh19.eu
joseviso.com
mydomain.dom
t.democo.dev
t.shoppub.io
thinkr.com.br
use.jitsu.com
usepolygon.io
www.sambla.se
ajewellers.com
data.uselog.io
gpt.whatfa.com
sidetrekai.com
t.papermark.io
t.saasmonk.app
use2.jitsu.com
w.d2.jitsu.com
www.kellen.top
*.dataspecc.com
app.bluetick.ai
caddy.jitsu.com
data.askloan.tw
enterticket.com
events.mitzu.io
ildar.jitsu.com
jitsu.efeer.com
jitsu.ivve.tech
krestomatio.com
sevenbillion.co
w2.d2.jitsu.com
xrt.webxr.tools
app.jotverse.com
caddy2.jitsu.com
cname2.jitsu.com
data.mysitee.com
data.toptere.com
dev-t.democo.dev
events.quenti.io
utils.doogma.com
worthsystems.com
data.music2me.com
data.timeplus.com
event-gateway.com
https.bluetick.ai
teste.fazcomex.com.br
analytics.dev.knekt.io
loraboutiquedental.com
notion.twelftree.co.uk
dev-portal.zoopsign.com
event.tradejobsnz.co.nz
investing-poc.jitsu.dev
savvy-replay.jitsu.tech
data.analytics-smart.com
data.handelsregister.app
event.clickncruise.co.uk
jt.fairhopeweb.github.io
savvy-replay2.jitsu.tech
savvy-replay3.jitsu.tech
savvy-replay4.jitsu.tech
track.alquimiaweb.com.br
track.pressance-group.jp
track.uniquecafes.com.br
colectha.agenciavoolu.com
kolectha.agenciavoolu.com
lp.loraboutiquedental.com
stage-portal.zoopsign.com
new-jitsu-jitsu.vercel.app
lodercom-colectha.voolu.shop
warehouse1.trendstyle.com.au
d0.livingdesignsfurniture.com
ingest-load-testing.jitsu.dev
jitsu.precisaosistemas.com.br
analytics.inspiresolutions.app
betteruptime-monitoring.jitsu.dev
canvas.livingdesignsfurniture.com
analytics.dev.inspiresolutions.app
cl9vt45z50001znkunc6v8fmm.d.jitsu.com
clm2jikrm00002v6r5l6niws3.d.jitsu.com
new-jitsu-git-newjitsu-jitsu.vercel.app
3000-rajaraodv-customerdemo-nmpsqwflswt.ws-us102.gitpod.io
new.jitsu.dev

Please sign in to comment.