Skip to content

Commit

Permalink
Merge pull request #1 from Prabuddhraj88/main
Browse files Browse the repository at this point in the history
LGTM
  • Loading branch information
0x0is1 authored Nov 26, 2023
2 parents 9de37d6 + 2023fcd commit 7837646
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
10 changes: 9 additions & 1 deletion Server/Trainer/collect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
from rich.progress import track
import json

current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
Expand All @@ -25,4 +26,11 @@ def collect_data(longitude, latitude, initial_ts, days_count):
periodic_aqi_data[period]["timestamp"] = int(initial_ts/1000)
initial_ts -= CHECK_INTERVAL

return periodic_aqi_data
with open("db/mapped_data.json", "r+") as f:
data = json.load(f)
if sid not in list(data["data"].keys()):
data["data"][sid] = {}
data["data"][sid][str(days_count)] = periodic_aqi_data
f.seek(0)
json.dump(data, f, indent=2)
f.truncate()
17 changes: 15 additions & 2 deletions Server/Trainer/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@
warnings.simplefilter(action='ignore', category=FutureWarning)

class Trainer:
def __init__(self, longitude, latitude, train_duration, from_date_timestamp, prediction_interval):
self.data = collect_data(longitude, latitude, from_date_timestamp, train_duration)
def __init__(self, longitude, latitude, train_duration, from_date_timestamp, prediction_interval, sid):
with open("db/mapped_data.json", "r") as f:
mdata = json.load(f)
if (sid not in list(mdata["data"].keys())):
collect_data(longitude, latitude, from_date_timestamp, train_duration)
self.__init__(longitude, latitude, train_duration, from_date_timestamp, prediction_interval, sid)
return
else:
print(str(train_duration), list(mdata["data"][sid].keys()), str(train_duration) not in list(mdata["data"][sid].keys()))
if (str(train_duration) not in list(mdata["data"][sid].keys())):
collect_data(longitude, latitude, from_date_timestamp, train_duration)
self.__init__(longitude, latitude, train_duration, from_date_timestamp, prediction_interval, sid)
return
else:
self.data = mdata["data"][sid][str(train_duration)]
self.frame = pd.DataFrame.from_dict(self.data, orient="index")
self.frame.replace("-", 0, inplace=True)
self.prediction_interval = prediction_interval
Expand Down
5 changes: 0 additions & 5 deletions Server/db/mapped.json

This file was deleted.

3 changes: 3 additions & 0 deletions Server/db/mapped_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": {}
}
3 changes: 3 additions & 0 deletions Server/db/models.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stations": {}
}
34 changes: 26 additions & 8 deletions Server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
yet_mapping_nsid = []

def start_trainer(longitude, latitude, train_duration, from_date_timestamp, prediction_interval, sid):
trainer = Trainer(longitude, latitude, train_duration, from_date_timestamp, prediction_interval)
trainer = Trainer(longitude, latitude, train_duration, from_date_timestamp, prediction_interval, sid)
forecasts = trainer.get_all_forecasts()
with open("db/mapped.json", "r+") as f:
with open("db/models.json", "r+") as f:
data = json.load(f)
data["stations"][sid] = forecasts
if sid not in list(data["stations"].keys()):
data["stations"][sid] = {}

if str(prediction_interval) not in list(data["stations"][sid].keys()):
data["stations"][sid][str(prediction_interval)] = {}

data["stations"][sid][str(prediction_interval)]["train_duration"] = train_duration
data["stations"][sid][str(prediction_interval)]["forecasts"] = forecasts
f.seek(0)
json.dump(data, f, indent=2)
f.truncate()
Expand Down Expand Up @@ -46,13 +53,24 @@ def get_station_forecast():

response = None
nsid = client.get_nearest_station([longitude, latitude])
with open("db/mapped.json", "r") as f:
with open("db/models.json", "r") as f:
data = json.load(f)
if nsid in list(data["stations"].keys()):
response = {
"status": 101,
"data": data["stations"][nsid]
}
if (str(prediction_interval) in list(data["stations"][nsid].keys())) and (str(train_duration) == str(data["stations"][nsid][str(prediction_interval)]["train_duration"])):
response = {
"status": 101,
"data": data["stations"][nsid][str(prediction_interval)]["forecasts"]
}
else:
response = {
"status": 102,
"data": "Your area is not mapped for given interval. Mapping your area..."
}

if nsid not in yet_mapping_nsid:
yet_mapping_nsid.append(nsid)
t1 = threading.Thread(target=start_trainer, args=(longitude, latitude, train_duration, from_date_timestamp, prediction_interval, nsid))
t1.start()
else:
response = {
"status": 102,
Expand Down

0 comments on commit 7837646

Please sign in to comment.