From 074fa2c5fc7193f1c43ff2264cc7ab7561c4dc3f Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Thu, 16 Feb 2023 09:52:07 -0800 Subject: [PATCH] add subdivision2 to schema --- db/clickhouse/schema.sql | 9 ++++--- db/mysql/migrations/01_init/migration.sql | 3 ++- db/mysql/schema.prisma | 25 ++++++++++--------- .../migration.sql | 3 ++- db/postgresql/schema.prisma | 25 ++++++++++--------- scripts/test.js | 23 +++++++++++++++++ 6 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 scripts/test.js diff --git a/db/clickhouse/schema.sql b/db/clickhouse/schema.sql index dc342509..15a9c33e 100644 --- a/db/clickhouse/schema.sql +++ b/db/clickhouse/schema.sql @@ -16,6 +16,7 @@ CREATE TABLE event language LowCardinality(String), country LowCardinality(String), subdivision LowCardinality(String), + subdivision2 LowCardinality(String), city String, --pageview url String, @@ -44,7 +45,8 @@ CREATE TABLE event_queue ( screen LowCardinality(String), language LowCardinality(String), country LowCardinality(String), - subdivision LowCardinality(String), + subdivision1 LowCardinality(String), + subdivision2 LowCardinality(String), city String, --pageview url String, @@ -76,13 +78,14 @@ SELECT website_id, screen, language, country, - subdivision, + subdivision1, + subdivision2, city, url, referrer, page_title, event_type, event_name, - if((empty(event_data) = 0) AND startsWith(event_data, '"'), concat('{', event_data, ': true}'), event_data) AS event_data, + event_data, created_at FROM event_queue; \ No newline at end of file diff --git a/db/mysql/migrations/01_init/migration.sql b/db/mysql/migrations/01_init/migration.sql index 80350b3f..367cf6a6 100644 --- a/db/mysql/migrations/01_init/migration.sql +++ b/db/mysql/migrations/01_init/migration.sql @@ -24,7 +24,8 @@ CREATE TABLE `session` ( `screen` VARCHAR(11) NULL, `language` VARCHAR(35) NULL, `country` CHAR(2) NULL, - `subdivision` CHAR(3) NULL, + `subdivision1` CHAR(3) NULL, + `subdivision2` CHAR(3) NULL, `city` VARCHAR(50) NULL, `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0), diff --git a/db/mysql/schema.prisma b/db/mysql/schema.prisma index d07b7637..ef03725c 100644 --- a/db/mysql/schema.prisma +++ b/db/mysql/schema.prisma @@ -25,18 +25,19 @@ model User { } model Session { - id String @id @unique @map("session_id") @db.VarChar(36) - websiteId String @map("website_id") @db.VarChar(36) - hostname String? @db.VarChar(100) - browser String? @db.VarChar(20) - os String? @db.VarChar(20) - device String? @db.VarChar(20) - screen String? @db.VarChar(11) - language String? @db.VarChar(35) - country String? @db.Char(2) - subdivision String? @db.Char(3) - city String? @db.VarChar(50) - createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0) + id String @id @unique @map("session_id") @db.VarChar(36) + websiteId String @map("website_id") @db.VarChar(36) + hostname String? @db.VarChar(100) + browser String? @db.VarChar(20) + os String? @db.VarChar(20) + device String? @db.VarChar(20) + screen String? @db.VarChar(11) + language String? @db.VarChar(35) + country String? @db.Char(2) + subdivision1 String? @db.Char(3) + subdivision2 String? @db.Char(3) + city String? @db.VarChar(50) + createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0) @@index([createdAt]) @@index([websiteId]) diff --git a/db/postgresql/migrations/05_add_page_title_subdivision_city/migration.sql b/db/postgresql/migrations/05_add_page_title_subdivision_city/migration.sql index 7e5891be..6f2a190c 100644 --- a/db/postgresql/migrations/05_add_page_title_subdivision_city/migration.sql +++ b/db/postgresql/migrations/05_add_page_title_subdivision_city/migration.sql @@ -1,6 +1,7 @@ -- AlterTable ALTER TABLE "session" ADD COLUMN "city" VARCHAR(50), -ADD COLUMN "subdivision" CHAR(3); +ADD COLUMN "subdivision1" CHAR(3), +ADD COLUMN "subdivision2" CHAR(3); -- AlterTable ALTER TABLE "website_event" ADD COLUMN "page_title" VARCHAR(500); diff --git a/db/postgresql/schema.prisma b/db/postgresql/schema.prisma index 8a161cee..3a422a50 100644 --- a/db/postgresql/schema.prisma +++ b/db/postgresql/schema.prisma @@ -25,18 +25,19 @@ model User { } model Session { - id String @id @unique @map("session_id") @db.Uuid - websiteId String @map("website_id") @db.Uuid - hostname String? @db.VarChar(100) - browser String? @db.VarChar(20) - os String? @db.VarChar(20) - device String? @db.VarChar(20) - screen String? @db.VarChar(11) - language String? @db.VarChar(35) - country String? @db.Char(2) - subdivision String? @db.Char(3) - city String? @db.VarChar(50) - createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6) + id String @id @unique @map("session_id") @db.Uuid + websiteId String @map("website_id") @db.Uuid + hostname String? @db.VarChar(100) + browser String? @db.VarChar(20) + os String? @db.VarChar(20) + device String? @db.VarChar(20) + screen String? @db.VarChar(11) + language String? @db.VarChar(35) + country String? @db.Char(2) + subdivision1 String? @db.Char(3) + subdivision2 String? @db.Char(3) + city String? @db.VarChar(50) + createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6) @@index([createdAt]) @@index([websiteId]) diff --git a/scripts/test.js b/scripts/test.js new file mode 100644 index 00000000..a913f1b0 --- /dev/null +++ b/scripts/test.js @@ -0,0 +1,23 @@ +/* eslint-disable no-console */ +require('dotenv').config(); +const fs = require('fs'); +const path = require('path'); +const https = require('https'); +const zlib = require('zlib'); +const tar = require('tar'); +const maxmind = require('maxmind'); + +async function getLocation() { + const lookup = await maxmind.open(path.resolve('../node_modules/.geo/GeoLite2-City.mmdb')); + const result = lookup.get('104.93.28.0'); + + const country = result?.country?.iso_code ?? result?.registered_country?.iso_code; + const subdivision = result?.subdivisions[0].iso_code; + const subdivision2 = result?.subdivisions[0].names; + const subdivision3 = result?.subdivisions[1].names; + const city = result?.city?.names?.en; + console.log(result); + console.log(country, subdivision, city, subdivision2, subdivision3); +} + +getLocation();