From 7b062143af8df82df77558d74d7869001039f226 Mon Sep 17 00:00:00 2001 From: extreme4all <40169115+extreme4all@users.noreply.github.com> Date: Sun, 24 Sep 2023 01:01:39 +0200 Subject: [PATCH] trying to add db --- docker-compose.yaml | 16 +++ mysql/Dockerfile | 3 + mysql/docker-entrypoint-initdb.d/00_init.sql | 1 + .../docker-entrypoint-initdb.d/01_tables.sql | 79 ++++++++++++++ mysql/docker-entrypoint-initdb.d/02_data.sql | 102 ++++++++++++++++++ notes.md | 3 +- src/app/models/player.py | 2 + 7 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 mysql/Dockerfile create mode 100644 mysql/docker-entrypoint-initdb.d/00_init.sql create mode 100644 mysql/docker-entrypoint-initdb.d/01_tables.sql create mode 100644 mysql/docker-entrypoint-initdb.d/02_data.sql diff --git a/docker-compose.yaml b/docker-compose.yaml index ae69f32d..dc589724 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -50,6 +50,21 @@ services: kafka: condition: service_healthy + mysql: + container_name: database + build: + context: ./mysql + image: bot-detector/mysql:latest + environment: + - MYSQL_ROOT_PASSWORD=root_bot_buster + volumes: + - ./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d + # - ./mysql/mount:/var/lib/mysql # creates persistence + ports: + - 3306:3306 + networks: + - botdetector-network + public_api: container_name: public_api build: @@ -72,6 +87,7 @@ services: - .env depends_on: - kafka_setup + - database networks: botdetector-network: \ No newline at end of file diff --git a/mysql/Dockerfile b/mysql/Dockerfile new file mode 100644 index 00000000..178b556a --- /dev/null +++ b/mysql/Dockerfile @@ -0,0 +1,3 @@ +FROM mysql:latest + +EXPOSE 3306 \ No newline at end of file diff --git a/mysql/docker-entrypoint-initdb.d/00_init.sql b/mysql/docker-entrypoint-initdb.d/00_init.sql new file mode 100644 index 00000000..7d82066d --- /dev/null +++ b/mysql/docker-entrypoint-initdb.d/00_init.sql @@ -0,0 +1 @@ +CREATE DATABASE playerdata; \ No newline at end of file diff --git a/mysql/docker-entrypoint-initdb.d/01_tables.sql b/mysql/docker-entrypoint-initdb.d/01_tables.sql new file mode 100644 index 00000000..bbab7947 --- /dev/null +++ b/mysql/docker-entrypoint-initdb.d/01_tables.sql @@ -0,0 +1,79 @@ +USE playerdata; + +CREATE TABLE Players ( + id SERIAL PRIMARY KEY, + name TEXT, + created_at TIMESTAMP, + updated_at TIMESTAMP, + possible_ban BOOLEAN, + confirmed_ban BOOLEAN, + confirmed_player BOOLEAN, + label_id INTEGER, + label_jagex INTEGER, + ironman BOOLEAN, + hardcore_ironman BOOLEAN, + ultimate_ironman BOOLEAN, + normalized_name TEXT +); + +CREATE TABLE Reports ( + ID BIGINT PRIMARY KEY AUTO_INCREMENT, + created_at TIMESTAMP, + reportedID INT, + reportingID INT, + region_id INT, + x_coord INT, + y_coord INT, + z_coord INT, + timestamp TIMESTAMP, + manual_detect SMALLINT, + on_members_world INT, + on_pvp_world SMALLINT, + world_number INT, + equip_head_id INT, + equip_amulet_id INT, + equip_torso_id INT, + equip_legs_id INT, + equip_boots_id INT, + equip_cape_id INT, + equip_hands_id INT, + equip_weapon_id INT, + equip_shield_id INT, + equip_ge_value BIGINT, + CONSTRAINT `FK_Reported_Players_id` FOREIGN KEY (`reportedID`) REFERENCES `Players` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT, + CONSTRAINT `FK_Reporting_Players_id` FOREIGN KEY (`reportingID`) REFERENCES `Players` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT +); + + +CREATE TABLE Predictions ( + id SERIAL PRIMARY KEY, + name VARCHAR(12), + prediction VARCHAR(50), + created TIMESTAMP, + predicted_confidence DECIMAL(5, 2), + real_player DECIMAL(5, 2) DEFAULT 0, + pvm_melee_bot DECIMAL(5, 2) DEFAULT 0, + smithing_bot DECIMAL(5, 2) DEFAULT 0, + magic_bot DECIMAL(5, 2) DEFAULT 0, + fishing_bot DECIMAL(5, 2) DEFAULT 0, + mining_bot DECIMAL(5, 2) DEFAULT 0, + crafting_bot DECIMAL(5, 2) DEFAULT 0, + pvm_ranged_magic_bot DECIMAL(5, 2) DEFAULT 0, + pvm_ranged_bot DECIMAL(5, 2) DEFAULT 0, + hunter_bot DECIMAL(5, 2) DEFAULT 0, + fletching_bot DECIMAL(5, 2) DEFAULT 0, + clue_scroll_bot DECIMAL(5, 2) DEFAULT 0, + lms_bot DECIMAL(5, 2) DEFAULT 0, + agility_bot DECIMAL(5, 2) DEFAULT 0, + wintertodt_bot DECIMAL(5, 2) DEFAULT 0, + runecrafting_bot DECIMAL(5, 2) DEFAULT 0, + zalcano_bot DECIMAL(5, 2) DEFAULT 0, + woodcutting_bot DECIMAL(5, 2) DEFAULT 0, + thieving_bot DECIMAL(5, 2) DEFAULT 0, + soul_wars_bot DECIMAL(5, 2) DEFAULT 0, + cooking_bot DECIMAL(5, 2) DEFAULT 0, + vorkath_bot DECIMAL(5, 2) DEFAULT 0, + barrows_bot DECIMAL(5, 2) DEFAULT 0, + herblore_bot DECIMAL(5, 2) DEFAULT 0, + unknown_bot DECIMAL(5, 2) DEFAULT 0 +); diff --git a/mysql/docker-entrypoint-initdb.d/02_data.sql b/mysql/docker-entrypoint-initdb.d/02_data.sql new file mode 100644 index 00000000..86e4deca --- /dev/null +++ b/mysql/docker-entrypoint-initdb.d/02_data.sql @@ -0,0 +1,102 @@ +USE playerdata; + +-- Insert data into the Players table +INSERT INTO Players (name, created_at, updated_at, possible_ban, confirmed_ban, confirmed_player, label_id, label_jagex, ironman, hardcore_ironman, ultimate_ironman, normalized_name) +SELECT + CONCAT('Player', id) AS name, + NOW() - INTERVAL FLOOR(RAND() * 365) DAY AS created_at, + NOW() - INTERVAL FLOOR(RAND() * 365) DAY AS updated_at, + 1 AS possible_ban, + 0 AS confirmed_ban, + 0 AS confirmed_player, + 0 AS label_id, + ROUND(RAND() * 1) AS label_jagex, -- Random label_jagex between 0 and 2 (inclusive) + null ironman, + null AS hardcore_ironman, + null AS ultimate_ironman, + CONCAT('player', id) AS normalized_name +FROM ( + SELECT + (a.N + b.N * 10) AS id + FROM + (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS a, + (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS b +) AS numbers +union +SELECT + CONCAT('Player', id) AS name, + NOW() - INTERVAL FLOOR(RAND() * 365) DAY AS created_at, + NOW() - INTERVAL FLOOR(RAND() * 365) DAY AS updated_at, + 1 AS possible_ban, -- 50% chance of possible_ban being true + 1 AS confirmed_ban, -- 30% chance of confirmed_ban being true, with possible_ban and label_jagex=2 + 0 AS confirmed_player, -- 80% chance of confirmed_player being true + 0 AS label_id, -- Random label_id between 0 and 2 (inclusive) + 2 AS label_jagex, -- Random label_jagex between 0 and 2 (inclusive) + null ironman, + null AS hardcore_ironman, + null AS ultimate_ironman, + CONCAT('player', id) AS normalized_name +FROM ( + SELECT + (a.N + b.N * 10+100) AS id + FROM + (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS a, + (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS b +) AS numbers +union +SELECT + CONCAT('Player', id) AS name, + NOW() - INTERVAL FLOOR(RAND() * 365) DAY AS created_at, + NOW() - INTERVAL FLOOR(RAND() * 365) DAY AS updated_at, + 0 AS possible_ban, -- 50% chance of possible_ban being true + 0 AS confirmed_ban, -- 30% chance of confirmed_ban being true, with possible_ban and label_jagex=2 + 1 AS confirmed_player, -- 80% chance of confirmed_player being true + 0 AS label_id, -- Random label_id between 0 and 2 (inclusive) + 0 AS label_jagex, -- Random label_jagex between 0 and 2 (inclusive) + null ironman, + null AS hardcore_ironman, + null AS ultimate_ironman, + CONCAT('player', id) AS normalized_name +FROM ( + SELECT + (a.N + b.N * 10+200) AS id + FROM + (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS a, + (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS b +) AS numbers + +-- Insert data into the Reports table +INSERT INTO Reports (created_at, reportedID, reportingID, region_id, x_coord, y_coord, z_coord, timestamp, manual_detect, on_members_world, on_pvp_world, world_number, equip_head_id, equip_amulet_id, equip_torso_id, equip_legs_id, equip_boots_id, equip_cape_id, equip_hands_id, equip_weapon_id, equip_shield_id, equip_ge_value) +SELECT + NOW() - INTERVAL FLOOR(RAND() * 365) DAY AS created_at, + p1.id AS reportedID, + p2.id AS reportingID, + ROUND(RAND() * 1000) AS region_id, -- Random region_id + ROUND(RAND() * 1000) AS x_coord, -- Random x_coord + ROUND(RAND() * 1000) AS y_coord, -- Random y_coord + ROUND(RAND() * 1000) AS z_coord, -- Random z_coord + NOW() - INTERVAL FLOOR(RAND() * 365) DAY AS timestamp, + ROUND(RAND()) AS manual_detect, -- Random manual_detect (0 or 1) + ROUND(RAND() * 1000) AS on_members_world, -- Random on_members_world + ROUND(RAND()) AS on_pvp_world, -- Random on_pvp_world (0 or 1) + ROUND(RAND() * 100) AS world_number, -- Random world_number + ROUND(RAND() * 1000) AS equip_head_id, -- Random equip_head_id + ROUND(RAND() * 1000) AS equip_amulet_id, -- Random equip_amulet_id + ROUND(RAND() * 1000) AS equip_torso_id, -- Random equip_torso_id + ROUND(RAND() * 1000) AS equip_legs_id, -- Random equip_legs_id + ROUND(RAND() * 1000) AS equip_boots_id, -- Random equip_boots_id + ROUND(RAND() * 1000) AS equip_cape_id, -- Random equip_cape_id + ROUND(RAND() * 1000) AS equip_hands_id, -- Random equip_hands_id + ROUND(RAND() * 1000) AS equip_weapon_id, -- Random equip_weapon_id + ROUND(RAND() * 1000) AS equip_shield_id, -- Random equip_shield_id + ROUND(RAND() * 10000) AS equip_ge_value -- Random equip_ge_value +FROM + Players p1 + CROSS JOIN Players p2 +WHERE + p1.id <> p2.id -- Ensure reportedID and reportingID are different +ORDER BY + RAND() -- Randomize the order of the combinations +LIMIT + 10000 -- Limit the number of combinations to insert +; \ No newline at end of file diff --git a/notes.md b/notes.md index 597fe6e6..8adf8839 100644 --- a/notes.md +++ b/notes.md @@ -1,6 +1,7 @@ kubectl ```sh -Kubectl port-forward -n kafka svc/bd-prd-kafka-service 9094:9094 +kubectl port-forward -n kafka svc/bd-prd-kafka-service 9094:9094 +kubectl port-forward -n database svc/mysql 3306:3306 ``` ```sh diff --git a/src/app/models/player.py b/src/app/models/player.py index 9a1a83b4..e2f8beb3 100644 --- a/src/app/models/player.py +++ b/src/app/models/player.py @@ -26,6 +26,7 @@ async def get_kc(self, player_names: list[str]): - "possible_ban": Whether the player has a possible ban (True or False). - "confirmed_ban": Whether the player has a confirmed ban (True or False). - "confirmed_player": Whether the player is confirmed as a valid player (True or False). + - "manual_detect": Wheter the detection was manual (True or False) """ async with self.session: # Create aliases for the tables @@ -38,6 +39,7 @@ async def get_kc(self, player_names: list[str]): reported_player.possible_ban, reported_player.confirmed_ban, reported_player.confirmed_player, + dbReport.manual_detect, ] ) query = query.select_from(dbReport)