import types from '../types' import moment from 'moment/min/moment.min' let FileSaver = require('file-saver') const systemInitialState = { loading: false, error: null, site: { name: 'loading', }, app: { tool: localStorage.getItem('system.last_tool') || 'pix2pix', }, server: { connected: false, status: "disconnected", error: null, }, relay: { connected: false, status: "unknown", error: null, }, rpc: { connected: false, status: "disconnected", error: null, }, cmd: { loading: false, loaded: false, name: null, error: null, stdout: "", stderr: "", }, runner: { cpu: { status: 'IDLE', task: {} }, gpu: { status: 'IDLE', task: {} }, }, stdout: "", stderr: "", } const modules = ['pix2pix','samplernn','pix2wav'].reduce((a,b) => (a[b]=b,a),{}) const systemReducer = (state = systemInitialState, action) => { // console.log(action.type) let processor = null switch(action.type) { case types.socket.connect: case types.socket.reconnecting: return { ...state, server: { status: 'connected', connected: true, error: null, }, } case types.socket.reconnect: return { ...state, server: { status: 'reconnecting (attempt ' + action.attempt + ')', connected: false, error: null, }, } case types.socket.connect_error: case types.socket.reconnect_error: case types.socket.disconnect: case types.socket.reconnect_failed: return { ...state, server: { status: 'disconnected', connected: false, error: action.error || null, }, } case types.socket.error: return { ...state, server: { ...state.socket, error: action.error, }, } case types.system.relay_connected: return { ...state, relay: { status: 'connected', connected: true, error: null, } } case types.system.relay_disconnected: return { ...state, relay: { status: 'disconnected', connected: false, error: null, }, rpc: { status: 'disconnected', connected: false, error: null, }, } case types.system.rpc_connected: return { ...state, rpc: { status: 'connected', connected: true, error: null, }, runner: action.runner, } case types.system.rpc_disconnected: return { ...state, rpc: { status: 'disconnected', connected: false, error: null, }, runner: action.runner || state.runner, } case types.system.load_site: document.querySelector('title').innerHTML = action.site.name + '.cortex' let tool = state.app.tool const path = window.location.pathname.split('/')[1] if (path in modules) { tool = path } return { ...state, site: action.site, app: { ...state.app, tool: tool, } } case types.system.running_command: return { ...state, cmd: { loading: true, loaded: false, name: action.cmd, error: null, stdout: null, stderr: null, } } case types.system.command_output: return { ...state, cmd: { loading: false, loaded: true, name: action.data.cmd, error: action.data.error, stdout: action.data.stdout, stderr: action.data.stderr, } } case types.task.task_begin: console.log('task begin', action.task, action.task.processor) return { ...state, runner: { ...state.runner, [action.task.processor]: { status: 'RUNNING', task: action.task }, }, cmd: { ...state.cmd, loaded: false, stdout: "", stderr: "", }, stdout: "", stderr: "", } case types.task.task_finish: if (action.task.processor === 'cpu' || (state.runner.cpu.task && state.runner.cpu.task.uuid === action.task.uuid)) { processor = 'cpu' } else if (action.task.processor === 'gpu' || (state.runner.gpu.task && state.runner.gpu.task.uuid === action.task.uuid)) { processor = 'gpu' } else { processor = null } console.log('task finish', action) return { ...state, rpc: { connected: false, status: "disconnected", error: null, }, runner: processor ? { ...state.runner, [processor]: { status: 'IDLE', task: {} }, } : state.runner, } case types.app.change_tool: return { ...state, app: { ...state.app, tool: action.tool, } } case types.system.stdout: if (action.data.processor && state.runner[action.data.processor]) { return { ...state, runner: { ...state.runner, [action.data.processor]: { ...state.runner[action.data.processor], last_message: action.data.data, } }, stdout: state.stdout + action.data.data, } } return { ...state, stdout: state.stdout + action.data.data, } case types.system.stderr: return { ...state, last_message: action.data.data, stderr: state.stderr + action.data.data, } default: return state } } export default systemReducer