1+ from influxdb_client_3 import InfluxDBClient3 ,InfluxDBError ,WriteOptions ,write_client_options
2+ import pandas as pd
3+ import random
4+
5+
6+ class BatchingCallback (object ):
7+
8+ def success (self , conf , data : str ):
9+ print (f"Written batch: { conf } , data: { data } " )
10+
11+ def error (self , conf , data : str , exception : InfluxDBError ):
12+ print (f"Cannot write batch: { conf } , data: { data } due: { exception } " )
13+
14+ def retry (self , conf , data : str , exception : InfluxDBError ):
15+ print (f"Retryable error occurs for batch: { conf } , data: { data } retry: { exception } " )
16+
17+ callback = BatchingCallback ()
18+
19+
20+ write_options = WriteOptions (batch_size = 100 ,
21+ flush_interval = 10_000 ,
22+ jitter_interval = 2_000 ,
23+ retry_interval = 5_000 ,
24+ max_retries = 5 ,
25+ max_retry_delay = 30_000 ,
26+ exponential_base = 2 )
27+
28+ wco = write_client_options (success_callback = callback .success ,
29+ error_callback = callback .error ,
30+ retry_callback = callback .retry ,
31+ WriteOptions = write_options
32+ )
33+
34+ client = InfluxDBClient3 (
35+ token = "" ,
36+ host = "https://eu-central-1-1.aws.cloud2.influxdata.com:442" ,
37+ org = "6a841c0c08328fb1" ,
38+ database = "pokemon-codex" , enable_gzip = True , write_client_options = wco , write_port_overwrite = 443 , query_port_overwrite = 443 )
39+
40+ now = pd .Timestamp .now (tz = 'UTC' ).floor ('ms' )
41+
42+ # Lists of possible trainers
43+ trainers = ["ash" , "brock" , "misty" , "gary" , "jessie" , "james" ]
44+
45+ # Read the CSV into a DataFrame
46+ pokemon_df = pd .read_csv ("https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv" )
47+
48+ # Creating an empty list to store the data
49+ data = []
50+
51+ # Dictionary to keep track of the number of times each trainer has caught each Pokémon
52+ trainer_pokemon_counts = {}
53+
54+ # Number of entries we want to create
55+ num_entries = 1000
56+
57+ # Generating random data
58+ for i in range (num_entries ):
59+ trainer = random .choice (trainers )
60+
61+ # Randomly select a row from pokemon_df
62+ random_pokemon = pokemon_df .sample ().iloc [0 ]
63+ caught = random_pokemon ['Name' ]
64+
65+ # Count the number of times this trainer has caught this Pokémon
66+ if (trainer , caught ) in trainer_pokemon_counts :
67+ trainer_pokemon_counts [(trainer , caught )] += 1
68+ else :
69+ trainer_pokemon_counts [(trainer , caught )] = 1
70+
71+ # Get the number for this combination of trainer and Pokémon
72+ num = trainer_pokemon_counts [(trainer , caught )]
73+
74+ entry = {
75+ "trainer" : trainer ,
76+ "id" : f"{ 0000 + random_pokemon ['#' ]:04d} " ,
77+ "num" : str (num ),
78+ "caught" : caught ,
79+ "level" : random .randint (5 , 20 ),
80+ "attack" : random_pokemon ['Attack' ],
81+ "defense" : random_pokemon ['Defense' ],
82+ "hp" : random_pokemon ['HP' ],
83+ "speed" : random_pokemon ['Speed' ],
84+ "type1" : random_pokemon ['Type 1' ],
85+ "type2" : random_pokemon ['Type 2' ],
86+ "timestamp" : now
87+ }
88+ data .append (entry )
89+
90+ # Convert the list of dictionaries to a DataFrame
91+ caught_pokemon_df = pd .DataFrame (data ).set_index ('timestamp' )
92+
93+ # Print the DataFrame
94+ print (caught_pokemon_df )
95+
96+
97+ try :
98+ client .write (caught_pokemon_df , data_frame_measurement_name = 'caught' ,
99+ data_frame_tag_columns = ['trainer' , 'id' , 'num' ])
100+ except Exception as e :
101+ print (f"Error writing point: { e } " )
0 commit comments