2016-07-03 12:21:36 +08:00
|
|
|
import logging
|
|
|
|
import asyncio
|
|
|
|
|
2016-04-05 18:50:19 +08:00
|
|
|
from PyQt5 import QtCore, QtWidgets
|
|
|
|
|
2019-11-10 15:55:17 +08:00
|
|
|
from sipyco.pc_rpc import AsyncioClient as RPCClient
|
|
|
|
|
2016-04-05 18:50:19 +08:00
|
|
|
from artiq.tools import short_format
|
2016-08-14 18:33:24 +08:00
|
|
|
from artiq.gui.tools import LayoutWidget, QRecursiveFilterProxyModel
|
2016-04-05 18:50:19 +08:00
|
|
|
from artiq.gui.models import DictSyncTreeSepModel
|
2016-07-03 12:21:36 +08:00
|
|
|
|
|
|
|
# reduced read-only version of artiq.dashboard.datasets
|
2016-04-05 18:50:19 +08:00
|
|
|
|
2016-07-03 12:21:36 +08:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2016-04-05 18:50:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Model(DictSyncTreeSepModel):
|
|
|
|
def __init__(self, init):
|
|
|
|
DictSyncTreeSepModel.__init__(self, ".", ["Dataset", "Value"], init)
|
|
|
|
|
|
|
|
def convert(self, k, v, column):
|
|
|
|
return short_format(v[1])
|
|
|
|
|
|
|
|
|
|
|
|
class DatasetsDock(QtWidgets.QDockWidget):
|
2016-07-03 12:21:36 +08:00
|
|
|
def __init__(self, datasets_sub, master_host, master_port):
|
2016-04-05 18:50:19 +08:00
|
|
|
QtWidgets.QDockWidget.__init__(self, "Datasets")
|
|
|
|
self.setObjectName("Datasets")
|
|
|
|
self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable |
|
|
|
|
QtWidgets.QDockWidget.DockWidgetFloatable)
|
|
|
|
|
|
|
|
grid = LayoutWidget()
|
|
|
|
self.setWidget(grid)
|
|
|
|
|
|
|
|
self.search = QtWidgets.QLineEdit()
|
|
|
|
self.search.setPlaceholderText("search...")
|
|
|
|
self.search.editingFinished.connect(self._search_datasets)
|
|
|
|
grid.addWidget(self.search, 0, 0)
|
|
|
|
|
|
|
|
self.table = QtWidgets.QTreeView()
|
|
|
|
self.table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
|
|
|
|
self.table.setSelectionMode(
|
|
|
|
QtWidgets.QAbstractItemView.SingleSelection)
|
|
|
|
grid.addWidget(self.table, 1, 0)
|
|
|
|
|
2016-07-10 00:29:01 +08:00
|
|
|
metadata_grid = LayoutWidget()
|
|
|
|
self.metadata = {}
|
|
|
|
for i, label in enumerate("artiq_version repo_rev file class_name "
|
|
|
|
"rid start_time".split()):
|
|
|
|
metadata_grid.addWidget(QtWidgets.QLabel(label), i, 0)
|
|
|
|
v = QtWidgets.QLabel()
|
|
|
|
v.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
|
|
|
|
metadata_grid.addWidget(v, i, 1)
|
|
|
|
self.metadata[label] = v
|
|
|
|
grid.addWidget(metadata_grid, 2, 0)
|
|
|
|
|
2016-07-03 12:21:36 +08:00
|
|
|
self.table.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
|
2016-07-10 00:29:01 +08:00
|
|
|
upload_action = QtWidgets.QAction("Upload dataset to master",
|
|
|
|
self.table)
|
2016-07-03 12:21:36 +08:00
|
|
|
upload_action.triggered.connect(self.upload_clicked)
|
|
|
|
self.table.addAction(upload_action)
|
|
|
|
|
2016-05-13 21:27:23 +08:00
|
|
|
self.set_model(Model(dict()))
|
2016-04-05 18:50:19 +08:00
|
|
|
datasets_sub.add_setmodel_callback(self.set_model)
|
|
|
|
|
2016-07-03 12:21:36 +08:00
|
|
|
self.master_host = master_host
|
|
|
|
self.master_port = master_port
|
|
|
|
|
2016-04-05 18:50:19 +08:00
|
|
|
def _search_datasets(self):
|
|
|
|
if hasattr(self, "table_model_filter"):
|
|
|
|
self.table_model_filter.setFilterFixedString(
|
|
|
|
self.search.displayText())
|
|
|
|
|
2016-07-10 00:29:01 +08:00
|
|
|
def metadata_changed(self, new):
|
|
|
|
for k, v in new.items():
|
|
|
|
self.metadata[k].setText("{}".format(v))
|
|
|
|
|
2016-04-05 18:50:19 +08:00
|
|
|
def set_model(self, model):
|
|
|
|
self.table_model = model
|
2016-08-14 18:33:24 +08:00
|
|
|
self.table_model_filter = QRecursiveFilterProxyModel()
|
2016-04-05 18:50:19 +08:00
|
|
|
self.table_model_filter.setSourceModel(self.table_model)
|
|
|
|
self.table.setModel(self.table_model_filter)
|
|
|
|
|
2016-07-03 12:21:36 +08:00
|
|
|
async def _upload_dataset(self, name, value,):
|
|
|
|
logger.info("Uploading dataset '%s' to master...", name)
|
|
|
|
try:
|
|
|
|
remote = RPCClient()
|
|
|
|
await remote.connect_rpc(self.master_host, self.master_port,
|
|
|
|
"master_dataset_db")
|
|
|
|
try:
|
|
|
|
await remote.set(name, value)
|
|
|
|
finally:
|
|
|
|
remote.close_rpc()
|
|
|
|
except:
|
|
|
|
logger.error("Failed uploading dataset '%s'",
|
|
|
|
name, exc_info=True)
|
|
|
|
else:
|
|
|
|
logger.info("Finished uploading dataset '%s'", name)
|
|
|
|
|
|
|
|
def upload_clicked(self):
|
|
|
|
idx = self.table.selectedIndexes()
|
|
|
|
if idx:
|
|
|
|
idx = self.table_model_filter.mapToSource(idx[0])
|
|
|
|
key = self.table_model.index_to_key(idx)
|
|
|
|
if key is not None:
|
|
|
|
persist, value = self.table_model.backing_store[key]
|
|
|
|
asyncio.ensure_future(self._upload_dataset(key, value))
|
|
|
|
|
2016-04-05 18:50:19 +08:00
|
|
|
def save_state(self):
|
|
|
|
return bytes(self.table.header().saveState())
|
|
|
|
|
|
|
|
def restore_state(self, state):
|
|
|
|
self.table.header().restoreState(QtCore.QByteArray(state))
|