Skip to content

Commit

Permalink
store: support for disabling TTL
Browse files Browse the repository at this point in the history
mixpanel-destination: set-once initial_utm_* profile props
  • Loading branch information
absorbb committed Dec 22, 2023
1 parent 628977a commit 7d4568c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
45 changes: 27 additions & 18 deletions libs/core-functions/src/functions/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import parse from "parse-duration";
import type { MongoClient } from "mongodb";

export const defaultTTL = 60 * 60 * 24 * 31; // 31 days
export const maxAllowedTTL = 60 * 60 * 24 * 93; // 93 days
export const maxAllowedTTL = 2147483647; // max allowed value for ttl in redis (68years)

function getTtlSec(opts?: SetOpts): number {
let seconds = defaultTTL;
if (typeof opts === "number") {
seconds = Math.ceil(opts);
} else if (typeof opts === "string") {
try {
seconds = Math.ceil(parse(opts, "s") || defaultTTL);
} catch (e) {}
} else if (typeof opts === "object" && typeof opts.ttl === "number") {
seconds = Math.ceil(opts.ttl);
if (opts.toLowerCase() === "inf") {
seconds = -1;
} else {
try {
seconds = Math.ceil(parse(opts, "s") || defaultTTL);
} catch (e) {}
}
} else if (typeof opts === "object") {
return getTtlSec(opts.ttl);
}
return Math.min(seconds, maxAllowedTTL);
}
Expand Down Expand Up @@ -44,7 +48,11 @@ export const createTtlStore = (namespace: string, redisClient: Redis, defaultTtl
},
set: async (key: string, obj: any, opts?: SetOpts) => {
const ttl = getTtlSec(opts);
await redisClient.set(`store:${namespace}:${key}`, JSON.stringify(obj), "EX", ttl);
if (ttl >= 0) {
await redisClient.set(`store:${namespace}:${key}`, JSON.stringify(obj), "EX", ttl);
} else {
await redisClient.set(`store:${namespace}:${key}`, JSON.stringify(obj));
}
},
del: async (key: string) => {
await redisClient.del(`store:${namespace}:${key}`);
Expand Down Expand Up @@ -95,20 +103,17 @@ export const createMongoStore = (namespace: string, mongo: MongoClient, defaultT
},
set: async (key: string, obj: any, opts?: SetOpts) => {
await ensureCollection();
const colObj: any = { value: obj };
const ttl = getTtlSec(opts);
const expireAt = new Date();
expireAt.setSeconds(expireAt.getSeconds() + ttl);
if (ttl >= 0) {
const expireAt = new Date();
expireAt.setSeconds(expireAt.getSeconds() + ttl);
colObj.expireAt = expireAt;
}
await mongo
.db(dbName)
.collection<StoreValue>(namespace)
.replaceOne(
{ _id: key },
{
value: obj,
expireAt,
},
{ upsert: true, writeConcern: { w: 1, journal: false } }
);
.replaceOne({ _id: key }, colObj, { upsert: true, writeConcern: { w: 1, journal: false } });
},
del: async (key: string) => {
await ensureCollection();
Expand All @@ -122,7 +127,11 @@ export const createMongoStore = (namespace: string, mongo: MongoClient, defaultT
.db(dbName)
.collection<StoreValue>(namespace)
.findOne({ _id: key }, { readPreference: "nearest" });
return res ? Math.max(Math.floor((res.expireAt.getTime() - new Date().getTime()) / 1000), 0) : -2;
return res
? res.expireAt
? Math.max(Math.floor((res.expireAt.getTime() - new Date().getTime()) / 1000), 0)
: -1
: -2;
},
};
};
Expand Down
5 changes: 3 additions & 2 deletions libs/core-functions/src/functions/mixpanel-destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export type HttpRequest = {
headers?: Record<string, string>;
};

function utm(param: Record<any, string>): Record<string, any> {
function utm(param: Record<any, string>, prefix: string = "utm_"): Record<string, any> {
return Object.entries(param).reduce(
(acc, [key, value]) => ({
...acc,
[`utm_${key === "name" ? "campaign" : key}`]: value,
[`${prefix}${key === "name" ? "campaign" : key}`]: value,
}),
{}
);
Expand Down Expand Up @@ -195,6 +195,7 @@ function setProfileMessage(ctx: FullContext, distinctId: string, event: Analytic
$latitude: event.context?.geo?.location?.latitude,
$longitude: event.context?.geo?.location?.longitude,
$set_once: {
...utm(event.context?.campaign || {}, "initial_utm_"),
$initial_referrer: event.context?.page?.referrer,
$initial_referring_domain: event.context?.page?.referring_domain,
},
Expand Down

1 comment on commit 7d4568c

@vercel
Copy link

@vercel vercel bot commented on 7d4568c Dec 22, 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
hunterbi.com
joseviso.com
mydomain.dom
t.democo.dev
t.shoppub.io
t2.jitsu.com
timeplus.com
zoopsign.com
*.d.jitsu.com
beta.mitzu.io
d.versatus.io
data.light.so
data.loudy.co
data.schej.it
dog.jitsu.com
imusician.app
imusician.pro
jitsu.logu.au
jitsu.www1.ru
https.bluetick.ai
ji.degulesider.dk
jitsu.ivve.health
metabase.erxes.io
t.clickncruise.hu
test.d2.jitsu.com
cloud.yupaopao.com
data.investing.com
data.mycompany.com
data.usepolygon.io
demosite.jitsu.com
dev.driverdeck.app
n8n.paziresh24.com
new.enterticket.es
t-dev.papermark.io
test2.d2.jitsu.com
uniquecafes.com.br
www.sidetrekai.com
colectha.voolu.shop
crm.myguestcare.com
data.sidetrekai.com
data.timeplus.cloud
localhost.jitsu.com
report.improvado.io
trk.myguestcare.com
www.sevenbillion.co
analytics.mtrsvc.com
data.embeddables.com
dataqa.investing.com
dev.blazingboost.com
j.israeladvocate.org
mercury.stagehub.com
store.sidetrekai.com
teslahenry.github.io
data.hogarlylabs.tech
data.your-company.com
event.clickncruise.hu
event.clickncruise.ro
test-domain.jitsu.com
test.bigfootproof.com
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
stage-portal.zoopsign.com

Please sign in to comment.