1

I'm tring to do a for loop inside a infinite while loop. This infinite loop get data of 3 different index from ElasticCloud to operate it after. I'm trying to put conditions over the second loop but it turns infinite.

Here is what I'm trying to do:

while(True):
  datetime_BO = datetime.now(tz_BO) #Date of today

  ed_data1 = ed.DataFrame(es, "_index_1").tail(2) #Get last data of index 1 
  ed_data2 = ed.DataFrame(es, "_index_2").tail(2) #Get last data of index 2

  panda_data1 = ed.eland_to_pandas(ed_data1) #Converting to pandas
  panda_data2 = ed.eland_to_pandas(ed_data2)
  panda_data1 = panda_data1.sort_values(by="@timestamp") #Sort by date
  panda_data2 = panda_data2.sort_values(by="@timestamp")
  panda_data1.reset_index(drop=True, inplace=True) #Index date
  panda_data2.reset_index(drop=True, inplace=True)
  panda_data1 = panda_data1.groupby(panda_data1.index // 2).mean() #Mean this measure
  panda_data2 = panda_data2.groupby(panda_data2.index // 2).mean()

  i = 5
  list_n = [] #Empty list
  n = 0
  while n < i:
    ed_data3 = ed.DataFrame(es, "heartbeat-*").tail(4) #Getting last data of this index
    panda_data3 = ed.eland_to_pandas(ed_data3) #Converting to pandas
    panda_data3 = panda_data3.sort_values(by="@timestamp") #Sorting by date
    panda_data3 = panda_data3[panda_data3['field_1'].isin(['Yes','No'])] #Getting in the column "field_1" all values with "Yes" or "No"
    panda_data3.reset_index(drop=True, inplace=True) #New index
    status = panda_data3.iloc[:,1] #Lock the "field_1" column
    if status[0] == "down" and status[1] == "No": #If in field_1 column there is a "No", give a 0
      uptime = 0
    elif status[0] == "up" or status[1] == "Yes": #If in field_1 column there is a "No", give a 1
      uptime = 1
    else:
      pass
    list_n.append(int(uptime)) #Data for empty list
    time.sleep(5) #Wait 5 secs
    n+=1
    print(list_n)

What I'm getting with i = 10 is this

[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1]

I need this exit from loop after complete the condition n = i and continue getting other data, but it gets in an eternal loop.

Any idea what I'm doing worng?

Edit 1:

I tried to use break after

    n += 1
    print(list_n)
  break

But now, the infinite while(true) is not working.

1 Answers1

2

just add a second break after the end of the second while, in the indentation of the first

    n+=1
    print(list_n)
break
Paolo Vezzola
  • 105
  • 1
  • 5