I wanted to write a function that compresses a bunch of files in my firebase realtime database to one file. Therefore, I wrote a request to get the oldest x files from my database and iterate through them, to sum each parameter up (and later divide by there count to get the average). Along to this man page, I should be able to iterate through my created snapshot. But if I try to implement it myself, I get warnings that I can't iterate through an object type (although my debugger says its an OrderedDictionary).
Thats my function:
def freeSpace(size):
file_count = math.ceil(size/1024)
snapshot = db.reference('/Arduinos/data/').order_by_key().limit_to_first(file_count + 1).get()
json_list = [json.loads(json.dumps(item[1])) for item in snapshot]
sum_json = {}
time = snapshot[0]
for json_str in json_list: # iterate over jsons in list
json_dict = json.loads(json_str) # convert each json Python-dictionary
for key, value in json_dict.items(): # iterate over each pair of key and value
if key in sum_json: # if key (parameter) already in sum
sum_json[key] += value # add to current value of sum
else: # if key not already in sum
sum_json[key] = value # add new key to sum
print(sum_json)
# part where I write the new json into the db is future work
return True
And here is my structure of the database.
My snapshot looks like this:
OrderedDict([
('2023-06-09 12:37:10', {
'arduino_id': 1,
'data': {
'light': {
'ir': 20,
'uv': 0.01,
'visible': 10
},
'ph': 7.5,
'tds': 300,
'timestamp': 122257
},
'status': {
'light': True,
'ph': True,
'pump': True,
'tds': True
}
}),
('2023-06-09 12:37:15', {
'arduino_id': 1,
'data': {
'light': {
'ir': 20,
'uv': 0.01,
'visible': 10
},
'ph': 7.5,
'tds': 300,
'timestamp': 122257
},
'status': {
'light': True,
'ph': True,
'pump': True,
'tds': True
}
})
])
I guess my nested structure could be a reason for that. So my question is, can I still somehow iterate through this snapshot? (and if so, how?) If not, do I need to flatten the files to a "classic" json format without any sub parameters? I would like to keep this structure since I work in a bigger project and changes like that would affect a bunch of other peoples work.
I already tried casting the snapshot manually to a dictionary and calling .values() or .items() after doing some research here. But none of that would satisfy my IDE and led to old errors while running. I also tried to let ChatGPT light me up for fun, which ended in a circle of just switching from .values() to .items() and back. Also not very helpful.
*Ignore mistakes in the further part of my function. If I get the iterating done I will be able to debug those myself (hope so).