How to generate fake real time data continuously for MySQL?
06:41 17 Sep 2020

I need to visualise real-time data in grafana in MySQL. I initially used a seed_python file to generate random numbers, but now I want to have continuous values in MySQL

Here is the python code I used initially

POSSIBLE_STATES = ['ACTIVE', 'INACTIVE']

class MySqlSeeder:

    def __init__(self):
        config = {
            'user': 'root',
            'password': 'something',
            'host': '192.168.99.100' if script_runs_within_container() else 'localhost',
            'port': '3306',
            'database': 'team'
        }
        while not hasattr(self, 'connection'):
            try:
                self.connection = mysql.connector.connect(**config)
                self.cursor = self.connection.cursor()
            except InterfaceError:
                print("MySQL Container has not started yet. Sleep and retry...")
                time.sleep(1)

    def seed(self):
        print("Clearing old data...")
        self.drop_user_table()
        print("Start seeding...")
        self.create_user_table()
        self.insert_users()

        self.connection.commit()
        self.cursor.close()
        self.connection.close()
        print("Done")

    def create_user_table(self):
        sql = '''
        CREATE TABLE users(
          id INT PRIMARY KEY AUTO_INCREMENT,
          number INT
        );
        '''
        self.cursor.execute(sql)

    def insert_users(self):
        for _ in range(300):
            sql = '''
            INSERT INTO users (number)
            VALUES (%(number)s);
            '''
            user_data = {
                'number': random.randint(1,100)
            }
            self.cursor.execute(sql, user_data)

    def drop_user_table(self):
        self.cursor.execute('DROP TABLE IF EXISTS users;')


    def script_runs_within_container():
        with open('/proc/1/cgroup', 'r') as cgroup_file:
            return 'docker' in cgroup_file.read()
    
    
    MySqlSeeder().seed()

Below is the docker-compose file

version: '3.3'
services:
  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: something
  grafana:
    image: grafana/grafana
    restart: always
    ports:
      - "3000:3000"
    depends_on:
      - mysql
  mysql_seeding:
    build: ./DockerFiles
    depends_on:
      - mysql

volumes:
  grafana-storage:
  db_data:

What could be the best way to perform continuous real-time data generation?

python mysql grafana test-data