Skip to content

Commit

Permalink
trying to add db
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Sep 23, 2023
1 parent de5fb33 commit 7b06214
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -72,6 +87,7 @@ services:
- .env
depends_on:
- kafka_setup
- database

networks:
botdetector-network:
3 changes: 3 additions & 0 deletions mysql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM mysql:latest

EXPOSE 3306
1 change: 1 addition & 0 deletions mysql/docker-entrypoint-initdb.d/00_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE DATABASE playerdata;
79 changes: 79 additions & 0 deletions mysql/docker-entrypoint-initdb.d/01_tables.sql
Original file line number Diff line number Diff line change
@@ -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
);
102 changes: 102 additions & 0 deletions mysql/docker-entrypoint-initdb.d/02_data.sql
Original file line number Diff line number Diff line change
@@ -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
;
3 changes: 2 additions & 1 deletion notes.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/app/models/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 7b06214

Please sign in to comment.