1- from influxdb_client_3 import InfluxDBClient3 ,InfluxDBError ,WriteOptions ,write_client_options
2- import pandas as pd
31import random
42
3+ import pandas as pd
4+
5+ from influxdb_client_3 import InfluxDBClient3 , InfluxDBError , WriteOptions , write_client_options
6+
57
68class BatchingCallback (object ):
79
@@ -14,36 +16,38 @@ def error(self, conf, data: str, exception: InfluxDBError):
1416 def retry (self , conf , data : str , exception : InfluxDBError ):
1517 print (f"Retryable error occurs for batch: { conf } , data: { data } retry: { exception } " )
1618
17- callback = BatchingCallback ()
1819
20+ callback = BatchingCallback ()
1921
2022write_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 )
23+ flush_interval = 10_000 ,
24+ jitter_interval = 2_000 ,
25+ retry_interval = 5_000 ,
26+ max_retries = 5 ,
27+ max_retry_delay = 30_000 ,
28+ exponential_base = 2 )
2729
2830wco = write_client_options (success_callback = callback .success ,
29- error_callback = callback .error ,
30- retry_callback = callback .retry ,
31- WriteOptions = write_options
32- )
31+ error_callback = callback .error ,
32+ retry_callback = callback .retry ,
33+ WriteOptions = write_options
34+ )
3335
3436client = InfluxDBClient3 (
3537 token = "" ,
3638 host = "https://eu-central-1-1.aws.cloud2.influxdata.com:442" ,
3739 org = "6a841c0c08328fb1" ,
38- database = "pokemon-codex" , enable_gzip = True , write_client_options = wco , write_port_overwrite = 443 , query_port_overwrite = 443 )
40+ database = "pokemon-codex" , enable_gzip = True , write_client_options = wco , write_port_overwrite = 443 ,
41+ query_port_overwrite = 443 )
3942
40- now = pd .Timestamp .now (tz = 'UTC' ).floor ('ms' )
43+ now = pd .Timestamp .now (tz = 'UTC' ).floor ('ms' )
4144
4245# Lists of possible trainers
4346trainers = ["ash" , "brock" , "misty" , "gary" , "jessie" , "james" ]
4447
4548# Read the CSV into a DataFrame
46- pokemon_df = pd .read_csv ("https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv" )
49+ pokemon_df = pd .read_csv (
50+ "https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv" ) # noqa: E501
4751
4852# Creating an empty list to store the data
4953data = []
@@ -57,17 +61,17 @@ def retry(self, conf, data: str, exception: InfluxDBError):
5761# Generating random data
5862for i in range (num_entries ):
5963 trainer = random .choice (trainers )
60-
64+
6165 # Randomly select a row from pokemon_df
6266 random_pokemon = pokemon_df .sample ().iloc [0 ]
6367 caught = random_pokemon ['Name' ]
64-
68+
6569 # Count the number of times this trainer has caught this Pokémon
6670 if (trainer , caught ) in trainer_pokemon_counts :
6771 trainer_pokemon_counts [(trainer , caught )] += 1
6872 else :
6973 trainer_pokemon_counts [(trainer , caught )] = 1
70-
74+
7175 # Get the number for this combination of trainer and Pokémon
7276 num = trainer_pokemon_counts [(trainer , caught )]
7377
@@ -93,9 +97,8 @@ def retry(self, conf, data: str, exception: InfluxDBError):
9397# Print the DataFrame
9498print (caught_pokemon_df )
9599
96-
97100try :
98101 client .write (caught_pokemon_df , data_frame_measurement_name = 'caught' ,
99- data_frame_tag_columns = ['trainer' , 'id' , 'num' ])
102+ data_frame_tag_columns = ['trainer' , 'id' , 'num' ])
100103except Exception as e :
101- print (f"Error writing point: { e } " )
104+ print (f"Error writing point: { e } " )
0 commit comments