|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Command-line tool to simulate user actions and write to SQL database. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import division |
| 21 | + |
| 22 | +import argparse |
| 23 | +import datetime |
| 24 | +import random |
| 25 | +import uuid |
| 26 | + |
| 27 | +from six.moves.urllib import parse |
| 28 | +import sqlalchemy |
| 29 | +from sqlalchemy.ext import declarative |
| 30 | +import sqlalchemy.orm |
| 31 | + |
| 32 | + |
| 33 | +SECONDS_IN_DAY = 24 * 60 * 60 |
| 34 | +SECONDS_IN_2016 = 366 * SECONDS_IN_DAY |
| 35 | + |
| 36 | +# Unix timestamp for the beginning of 2016. |
| 37 | +# http://stackoverflow.com/a/19801806/101923 |
| 38 | +TIMESTAMP_2016 = ( |
| 39 | + datetime.datetime(2016, 1, 1, 0, 0, 0) - |
| 40 | + datetime.datetime.fromtimestamp(0)).total_seconds() |
| 41 | + |
| 42 | + |
| 43 | +Base = declarative.declarative_base() |
| 44 | + |
| 45 | + |
| 46 | +class User(Base): |
| 47 | + __tablename__ = 'Users' |
| 48 | + |
| 49 | + id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True) |
| 50 | + date_joined = sqlalchemy.Column(sqlalchemy.DateTime) |
| 51 | + |
| 52 | + |
| 53 | +class UserSession(Base): |
| 54 | + __tablename__ = 'UserSessions' |
| 55 | + |
| 56 | + id = sqlalchemy.Column(sqlalchemy.String(length=36), primary_key=True) |
| 57 | + user_id = sqlalchemy.Column( |
| 58 | + sqlalchemy.Integer, sqlalchemy.ForeignKey('Users.id')) |
| 59 | + login_time = sqlalchemy.Column(sqlalchemy.DateTime) |
| 60 | + logout_time = sqlalchemy.Column(sqlalchemy.DateTime) |
| 61 | + ip_address = sqlalchemy.Column(sqlalchemy.String(length=40)) |
| 62 | + |
| 63 | + |
| 64 | +def generate_users(session, num_users): |
| 65 | + users = [] |
| 66 | + |
| 67 | + for userid in range(1, num_users + 1): |
| 68 | + year_portion = random.random() |
| 69 | + date_joined = datetime.datetime.fromtimestamp( |
| 70 | + TIMESTAMP_2016 + SECONDS_IN_2016 * year_portion) |
| 71 | + user = User(id=userid, date_joined=date_joined) |
| 72 | + users.append(user) |
| 73 | + session.add(user) |
| 74 | + |
| 75 | + session.commit() |
| 76 | + return users |
| 77 | + |
| 78 | + |
| 79 | +def random_ip(): |
| 80 | + """Choose a random example IP address. |
| 81 | +
|
| 82 | + Examples are chosen from the test networks described in |
| 83 | + https://tools.ietf.org/html/rfc5737 |
| 84 | + """ |
| 85 | + network = random.choice([ |
| 86 | + '192.0.2', # RFC-5737 TEST-NET-1 |
| 87 | + '198.51.100', # RFC-5737 TEST-NET-2 |
| 88 | + '203.0.113', # RFC-5737 TEST-NET-3 |
| 89 | + ]) |
| 90 | + ip_address = '{}.{}'.format(network, random.randrange(256)) |
| 91 | + return ip_address |
| 92 | + |
| 93 | + |
| 94 | +def simulate_user_session(session, user, previous_user_session=None): |
| 95 | + """Simulates a single session (login to logout) of a user's history.""" |
| 96 | + login_time = user.date_joined |
| 97 | + |
| 98 | + if previous_user_session is not None: |
| 99 | + login_time = ( |
| 100 | + previous_user_session.logout_time + |
| 101 | + datetime.timedelta( |
| 102 | + days=1, seconds=random.randrange(SECONDS_IN_DAY))) |
| 103 | + |
| 104 | + session_id = str(uuid.uuid4()) |
| 105 | + user_session = UserSession( |
| 106 | + id=session_id, |
| 107 | + user_id=user.id, |
| 108 | + login_time=login_time, |
| 109 | + ip_address=random_ip()) |
| 110 | + user_session.logout_time = ( |
| 111 | + login_time + |
| 112 | + datetime.timedelta(seconds=(1 + random.randrange(59)))) |
| 113 | + session.commit() |
| 114 | + session.add(user_session) |
| 115 | + return user_session |
| 116 | + |
| 117 | + |
| 118 | +def simulate_user_history(session, user): |
| 119 | + """Simulates the entire history of activity for a single user.""" |
| 120 | + total_sessions = random.randrange(10) |
| 121 | + previous_user_session = None |
| 122 | + |
| 123 | + for _ in range(total_sessions): |
| 124 | + user_session = simulate_user_session( |
| 125 | + session, user, previous_user_session) |
| 126 | + previous_user_session = user_session |
| 127 | + |
| 128 | + |
| 129 | +def run_simulation(session, users): |
| 130 | + """Simulates app activity for all users.""" |
| 131 | + |
| 132 | + for n, user in enumerate(users): |
| 133 | + if n % 100 == 0 and n != 0: |
| 134 | + print('Simulated data for {} users'.format(n)) |
| 135 | + |
| 136 | + simulate_user_history(session, user) |
| 137 | + |
| 138 | + print('COMPLETE: Simulated data for {} users'.format(len(users))) |
| 139 | + |
| 140 | + |
| 141 | +def populate_db(session, total_users=3): |
| 142 | + """Populate database with total_users simulated users and their actions.""" |
| 143 | + users = generate_users(session, total_users) |
| 144 | + run_simulation(session, users) |
| 145 | + |
| 146 | + |
| 147 | +def create_session(engine): |
| 148 | + Base.metadata.drop_all(engine) |
| 149 | + Base.metadata.create_all(engine) |
| 150 | + Session = sqlalchemy.orm.sessionmaker(bind=engine) |
| 151 | + return Session() |
| 152 | + |
| 153 | + |
| 154 | +def main(total_users, host, user, password, db_name): |
| 155 | + engine = sqlalchemy.create_engine( |
| 156 | + 'mysql+pymysql://{user}:{password}@{host}/{db_name}'.format( |
| 157 | + user=user, |
| 158 | + password=parse.quote_plus(password), |
| 159 | + host=host, |
| 160 | + db_name=db_name)) |
| 161 | + session = create_session(engine) |
| 162 | + |
| 163 | + try: |
| 164 | + populate_db(session, total_users) |
| 165 | + finally: |
| 166 | + session.close() |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + parser = argparse.ArgumentParser( |
| 171 | + description=__doc__, |
| 172 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 173 | + parser.add_argument( |
| 174 | + 'total_users', help='How many simulated users to create.', type=int) |
| 175 | + parser.add_argument('host', help='Host of the database to write to.') |
| 176 | + parser.add_argument('user', help='User to connect to the database.') |
| 177 | + parser.add_argument('password', help='Password for the database user.') |
| 178 | + parser.add_argument('db', help='Name of the database to write to.') |
| 179 | + |
| 180 | + args = parser.parse_args() |
| 181 | + |
| 182 | + main(args.total_users, args.host, args.user, args.password, args.db) |
0 commit comments