Skip to content

Instantly share code, notes, and snippets.

@Anasboulbali
Last active January 3, 2021 20:38
Show Gist options
  • Save Anasboulbali/121b2f826d6b8ad5f02f049d92561cc2 to your computer and use it in GitHub Desktop.
Save Anasboulbali/121b2f826d6b8ad5f02f049d92561cc2 to your computer and use it in GitHub Desktop.
# Transaction generator
# needed for any cluster connection
from couchbase.cluster import Cluster, ClusterOptions
from couchbase_core.cluster import PasswordAuthenticator
# needed to support SQL++ (N1QL) query
from couchbase.cluster import QueryOptions
import json
import time
import random
import datetime
# get a reference to our cluster
cluster = Cluster('couchbase://172.17.0.2:8091', ClusterOptions(PasswordAuthenticator('Administrator', 'password')))
# get a reference to our bucket
cb = cluster.bucket('PFA')
# bind our bucket 'default' on the right port
#bucket = Bucket('couchbase://192.168.3.174/default')
transaction_id = 0
card_id = 0
card_type_list = ["Amex", "CB", "Visa", "Mastercard"]
amount = 0
timestamp_event = datetime.datetime.now()
i=1
while(i<20):
if(random.randint(0, 10)>3):
delay = random.uniform(0, 0.05)
else:
delay = random.uniform(0, 3)
timestamp_event += datetime.timedelta(seconds=delay)
transaction_id += 1
card_id = random.randint(0,10000)
card_type = card_type_list[random.randint(0,3)]
amount = random.uniform(0, 1000)
# create json
transaction = {}
transaction['transaction_id'] = transaction_id
transaction['card_id'] = card_id
transaction['card_type'] = card_type
transaction['amount'] = amount
transaction['@timestamp'] = str(timestamp_event)
print(transaction)
# upsert json in couchbase bucket
cb.upsert("transaction_"+str(transaction_id), transaction)
time.sleep(delay)
i=i+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment