landcover/web_tool/Session.py

136 строки
4.6 KiB
Python
Исходник Обычный вид История

2019-10-22 10:35:29 +03:00
import sys
import os
import time
import datetime
import collections
import subprocess
2020-01-26 01:39:06 +03:00
import shutil
2019-10-22 10:35:29 +03:00
import base64
import json
import uuid
2019-11-09 02:57:07 +03:00
import pickle
2019-10-22 10:35:29 +03:00
import numpy as np
import joblib
2020-07-05 08:33:49 +03:00
from .Utils import get_random_string, AtomicCounter
2019-10-22 10:35:29 +03:00
2020-07-05 08:33:49 +03:00
from .log import LOGGER
2019-10-22 10:35:29 +03:00
2020-01-26 01:39:06 +03:00
SESSION_BASE_PATH = './data/session'
SESSION_FOLDER = SESSION_BASE_PATH + "/" + datetime.datetime.now().strftime('%Y-%m-%d')
def manage_session_folders():
if not os.path.exists(SESSION_BASE_PATH):
os.makedirs(SESSION_BASE_PATH)
if not os.path.exists(SESSION_FOLDER):
shutil.rmtree(SESSION_BASE_PATH)
os.makedirs(SESSION_FOLDER)
2019-10-22 10:35:29 +03:00
class Session():
def __init__(self, session_id, model):
LOGGER.info("Instantiating a new session object with id: %s" % (session_id))
2020-01-21 02:49:28 +03:00
self.storage_type = "file" # this will be "table" or "file"
self.storage_path = "data/" # this will be a file path
2019-10-22 10:35:29 +03:00
self.table_service = None # this will be an instance of TableService
2020-01-14 03:54:19 +03:00
self.model = model
2019-10-22 10:35:29 +03:00
self.current_transform = ()
self.current_snapshot_string = get_random_string(8)
self.current_snapshot_idx = 0
self.current_request_counter = AtomicCounter()
self.request_list = []
self.session_id = session_id
self.creation_time = time.time()
self.last_interaction_time = self.creation_time
2019-10-22 10:35:29 +03:00
def reset(self, soft=False, from_cached=None):
if not soft:
self.model.reset() # can't fail, so don't worry about it
self.current_snapshot_string = get_random_string(8)
self.current_snapshot_idx = 0
self.current_request_counter = AtomicCounter()
self.request_list = []
if self.storage_type == "table":
self.table_service.insert_entity("webtoolsessions",
{
"PartitionKey": str(np.random.randint(0,8)),
"RowKey": str(uuid.uuid4()),
"session_id": self.current_snapshot_string,
"server_hostname": os.uname()[1],
"server_sys_argv": ' '.join(sys.argv),
"base_model": from_cached
})
def load(self, encoded_model_fn):
model_fn = base64.b64decode(encoded_model_fn).decode('utf-8')
print(model_fn)
del self.model
self.model = joblib.load(model_fn)
def save(self, model_name):
if self.storage_type is not None:
assert self.storage_path is not None # we check for this when starting the program
snapshot_id = "%s_%d" % (model_name, self.current_snapshot_idx)
print("Saving state for %s" % (snapshot_id))
base_dir = os.path.join(self.storage_path, self.current_snapshot_string)
if not os.path.exists(base_dir):
os.makedirs(base_dir, exist_ok=False)
model_fn = os.path.join(base_dir, "%s_model.p" % (snapshot_id))
2020-01-21 02:49:28 +03:00
#joblib.dump(self.model, model_fn, protocol=pickle.HIGHEST_PROTOCOL)
2019-10-22 10:35:29 +03:00
if self.storage_type == "file":
request_list_fn = os.path.join(base_dir, "%s_request_list.p" % (snapshot_id))
joblib.dump(self.request_list, request_list_fn, protocol=pickle.HIGHEST_PROTOCOL)
elif self.storage_type == "table":
# We don't serialize the request list when saving to table storage
pass
self.current_snapshot_idx += 1
return base64.b64encode(model_fn.encode('utf-8')).decode('utf-8') # this is super dumb
else:
return None
def add_entry(self, data):
2019-10-22 10:35:29 +03:00
data = data.copy()
data["time"] = datetime.datetime.now()
data["current_snapshot_index"] = self.current_snapshot_idx
current_request_counter = self.current_request_counter.increment()
data["current_request_index"] = current_request_counter
assert "experiment" in data
if self.storage_type == "file":
self.request_list.append(data)
elif self.storage_type == "table":
data["PartitionKey"] = self.current_snapshot_string
data["RowKey"] = "%s_%d" % (data["experiment"], current_request_counter)
for k in data.keys():
if isinstance(data[k], dict) or isinstance(data[k], list):
data[k] = json.dumps(data[k])
try:
self.table_service.insert_entity("webtoolinteractions", data)
except Exception as e:
print(e)
else:
# The storage_type / --storage_path command line args were not set
2019-10-23 09:37:38 +03:00
pass