mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-22 18:00:17 +01:00
Updated handling of date values for event data.
This commit is contained in:
parent
0c910f2abd
commit
7723576212
@ -216,6 +216,8 @@ export const UUID_REGEX =
|
|||||||
export const HOSTNAME_REGEX =
|
export const HOSTNAME_REGEX =
|
||||||
/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/;
|
/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/;
|
||||||
export const IP_REGEX = /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}$/;
|
export const IP_REGEX = /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}$/;
|
||||||
|
export const DATETIME_REGEX =
|
||||||
|
/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]{3}(Z|\+[0-9]{2}:[0-9]{2})?)?$/;
|
||||||
|
|
||||||
export const DESKTOP_SCREEN_WIDTH = 1920;
|
export const DESKTOP_SCREEN_WIDTH = 1920;
|
||||||
export const LAPTOP_SCREEN_WIDTH = 1024;
|
export const LAPTOP_SCREEN_WIDTH = 1024;
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
import { isValid } from 'date-fns';
|
import { DATA_TYPE, DATETIME_REGEX } from './constants';
|
||||||
import { DATA_TYPE } from './constants';
|
|
||||||
import { DynamicDataType } from './types';
|
import { DynamicDataType } from './types';
|
||||||
|
|
||||||
export function flattenJSON(
|
export function flattenJSON(
|
||||||
eventData: { [key: string]: any },
|
eventData: { [key: string]: any },
|
||||||
keyValues: { key: string; value: any; dynamicDataType: DynamicDataType }[] = [],
|
keyValues: { key: string; value: any; dataType: DynamicDataType }[] = [],
|
||||||
parentKey = '',
|
parentKey = '',
|
||||||
): { key: string; value: any; dynamicDataType: DynamicDataType }[] {
|
): { key: string; value: any; dataType: DynamicDataType }[] {
|
||||||
return Object.keys(eventData).reduce(
|
return Object.keys(eventData).reduce(
|
||||||
(acc, key) => {
|
(acc, key) => {
|
||||||
const value = eventData[key];
|
const value = eventData[key];
|
||||||
const type = typeof eventData[key];
|
const type = typeof eventData[key];
|
||||||
|
|
||||||
// nested object
|
// nested object
|
||||||
if (value && type === 'object' && !Array.isArray(value) && !isValid(value)) {
|
if (value && type === 'object' && !Array.isArray(value) && !isValidDateValue(value)) {
|
||||||
flattenJSON(value, acc.keyValues, getKeyName(key, parentKey));
|
flattenJSON(value, acc.keyValues, getKeyName(key, parentKey));
|
||||||
} else {
|
} else {
|
||||||
createKey(getKeyName(key, parentKey), value, acc);
|
createKey(getKeyName(key, parentKey), value, acc);
|
||||||
@ -25,48 +24,64 @@ export function flattenJSON(
|
|||||||
).keyValues;
|
).keyValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isValidDateValue(value: string) {
|
||||||
|
return typeof value === 'string' && DATETIME_REGEX.test(value);
|
||||||
|
}
|
||||||
|
|
||||||
export function getDataType(value: any): string {
|
export function getDataType(value: any): string {
|
||||||
let type: string = typeof value;
|
let type: string = typeof value;
|
||||||
|
|
||||||
if ((type === 'string' && isValid(value)) || isValid(new Date(value))) {
|
if (isValidDateValue(value)) {
|
||||||
type = 'date';
|
type = 'date';
|
||||||
}
|
}
|
||||||
|
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createKey(key, value, acc: { keyValues: any[]; parentKey: string }) {
|
export function getStringValue(value: string, dataType: number) {
|
||||||
|
if (dataType === DATA_TYPE.number) {
|
||||||
|
return parseFloat(value).toFixed(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dataType === DATA_TYPE.date) {
|
||||||
|
return new Date(value).toISOString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createKey(key: string, value: string, acc: { keyValues: any[]; parentKey: string }) {
|
||||||
const type = getDataType(value);
|
const type = getDataType(value);
|
||||||
|
|
||||||
let dynamicDataType = null;
|
let dataType = null;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'number':
|
case 'number':
|
||||||
dynamicDataType = DATA_TYPE.number;
|
dataType = DATA_TYPE.number;
|
||||||
break;
|
break;
|
||||||
case 'string':
|
case 'string':
|
||||||
dynamicDataType = DATA_TYPE.string;
|
dataType = DATA_TYPE.string;
|
||||||
break;
|
break;
|
||||||
case 'boolean':
|
case 'boolean':
|
||||||
dynamicDataType = DATA_TYPE.boolean;
|
dataType = DATA_TYPE.boolean;
|
||||||
value = value ? 'true' : 'false';
|
value = value ? 'true' : 'false';
|
||||||
break;
|
break;
|
||||||
case 'date':
|
case 'date':
|
||||||
dynamicDataType = DATA_TYPE.date;
|
dataType = DATA_TYPE.date;
|
||||||
break;
|
break;
|
||||||
case 'object':
|
case 'object':
|
||||||
dynamicDataType = DATA_TYPE.array;
|
dataType = DATA_TYPE.array;
|
||||||
value = JSON.stringify(value);
|
value = JSON.stringify(value);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
dynamicDataType = DATA_TYPE.string;
|
dataType = DATA_TYPE.string;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
acc.keyValues.push({ key, value, dynamicDataType });
|
acc.keyValues.push({ key, value, dataType });
|
||||||
}
|
}
|
||||||
|
|
||||||
function getKeyName(key, parentKey) {
|
function getKeyName(key: string, parentKey: string) {
|
||||||
if (!parentKey) {
|
if (!parentKey) {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import { Prisma } from '@prisma/client';
|
|||||||
import { DATA_TYPE } from 'lib/constants';
|
import { DATA_TYPE } from 'lib/constants';
|
||||||
import { uuid } from 'lib/crypto';
|
import { uuid } from 'lib/crypto';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||||
import { flattenJSON } from 'lib/data';
|
import { flattenJSON, getStringValue } from 'lib/data';
|
||||||
import kafka from 'lib/kafka';
|
import kafka from 'lib/kafka';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import { DynamicData } from 'lib/types';
|
import { DynamicData } from 'lib/types';
|
||||||
@ -37,15 +37,10 @@ async function relationalQuery(data: {
|
|||||||
websiteEventId: eventId,
|
websiteEventId: eventId,
|
||||||
websiteId,
|
websiteId,
|
||||||
eventKey: a.key,
|
eventKey: a.key,
|
||||||
stringValue:
|
stringValue: getStringValue(a.value, a.dataType),
|
||||||
a.dynamicDataType === DATA_TYPE.number
|
numberValue: a.dataType === DATA_TYPE.number ? a.value : null,
|
||||||
? parseFloat(a.value).toFixed(4)
|
dateValue: a.dataType === DATA_TYPE.date ? new Date(a.value) : null,
|
||||||
: a.dynamicDataType === DATA_TYPE.date
|
dataType: a.dataType,
|
||||||
? a.value.split('.')[0] + 'Z'
|
|
||||||
: a.value.toString(),
|
|
||||||
numberValue: a.dynamicDataType === DATA_TYPE.number ? a.value : null,
|
|
||||||
dateValue: a.dynamicDataType === DATA_TYPE.date ? new Date(a.value) : null,
|
|
||||||
dataType: a.dynamicDataType,
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return prisma.client.eventData.createMany({
|
return prisma.client.eventData.createMany({
|
||||||
@ -75,13 +70,10 @@ async function clickhouseQuery(data: {
|
|||||||
url_path: urlPath,
|
url_path: urlPath,
|
||||||
event_name: eventName,
|
event_name: eventName,
|
||||||
event_key: a.key,
|
event_key: a.key,
|
||||||
string_value:
|
string_value: getStringValue(a.value, a.dataType),
|
||||||
a.dynamicDataType === DATA_TYPE.date
|
number_value: a.dataType === DATA_TYPE.number ? a.value : null,
|
||||||
? getDateFormat(a.value, 'isoUtcDateTime')
|
date_value: a.dataType === DATA_TYPE.date ? getDateFormat(a.value) : null,
|
||||||
: a.value.toString(),
|
data_type: a.dataType,
|
||||||
number_value: a.dynamicDataType === DATA_TYPE.number ? a.value : null,
|
|
||||||
date_value: a.dynamicDataType === DATA_TYPE.date ? getDateFormat(a.value) : null,
|
|
||||||
data_type: a.dynamicDataType,
|
|
||||||
created_at: createdAt,
|
created_at: createdAt,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { DATA_TYPE } from 'lib/constants';
|
import { DATA_TYPE } from 'lib/constants';
|
||||||
import { uuid } from 'lib/crypto';
|
import { uuid } from 'lib/crypto';
|
||||||
import { flattenJSON } from 'lib/data';
|
import { flattenJSON, getStringValue } from 'lib/data';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import { DynamicData } from 'lib/types';
|
import { DynamicData } from 'lib/types';
|
||||||
|
|
||||||
@ -19,15 +19,10 @@ export async function saveSessionData(data: {
|
|||||||
websiteId,
|
websiteId,
|
||||||
sessionId,
|
sessionId,
|
||||||
key: a.key,
|
key: a.key,
|
||||||
stringValue:
|
stringValue: getStringValue(a.value, a.dataType),
|
||||||
a.dynamicDataType === DATA_TYPE.number
|
numberValue: a.dataType === DATA_TYPE.number ? a.value : null,
|
||||||
? parseFloat(a.value).toFixed(4)
|
dateValue: a.dataType === DATA_TYPE.date ? new Date(a.value) : null,
|
||||||
: a.dynamicDataType === DATA_TYPE.date
|
dataType: a.dataType,
|
||||||
? a.value.split('.')[0] + 'Z'
|
|
||||||
: a.value.toString(),
|
|
||||||
numberValue: a.dynamicDataType === DATA_TYPE.number ? a.value : null,
|
|
||||||
dateValue: a.dynamicDataType === DATA_TYPE.date ? new Date(a.value) : null,
|
|
||||||
dataType: a.dynamicDataType,
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return transaction([
|
return transaction([
|
||||||
|
Loading…
Reference in New Issue
Block a user