public void SetData(ExecutorStorageView ex)
 {
     RefreshData(ex);
     InitSystemPlot();
     RefreshSystemPlot();
     tmRefreshSystem.Enabled = true;
 }
        public string AddExecutor(ExecutorStorageView executor)
        {
            if (executor == null)
            {
                return null;
            }

            string executorId;
            if (executor.ExecutorId == null)
            {
                executorId = Guid.NewGuid().ToString();
            }
            else
            {
                executorId = executor.ExecutorId;
            }

            executor.ExecutorId = executorId;

            IObjectContainer container = GetStorage();
            try
            {
                container.Set(executor);
            }
            finally
            {
                container.Close();
            }
            return executorId;
        }
        public void AvailableDedicatedExecutorsTestSimpleScenario()
        {
            // add one that is OK
            ExecutorStorageView executor1 = new ExecutorStorageView(true, true, DateTime.Now, "hostname", 10, "username", 1, 1, 1, 1);
            // add one that is timed out
            ExecutorStorageView executor2 = new ExecutorStorageView(true, true, DateTime.Now.AddDays((-1)), "hostname", 10, "username", 1, 1, 1, 1);
            // add one that is not connected
            ExecutorStorageView executor3 = new ExecutorStorageView(false, false, DateTime.Now, "hostname", 10, "username", 1, 1, 1, 1);

            String executorId1 = m_managerStorage.AddExecutor(executor1);
            String executorId2 = m_managerStorage.AddExecutor(executor2);
            String executorId3 = m_managerStorage.AddExecutor(executor3);

            String applicationId = Guid.NewGuid().ToString();

            // add a few threads
            m_managerStorage.AddThread(new ThreadStorageView(applicationId, executorId1, 1, ThreadState.Started, DateTime.Now, DateTime.Now, 1, false));
            m_managerStorage.AddThread(new ThreadStorageView(applicationId, executorId2, 1, ThreadState.Dead, DateTime.Now, DateTime.Now, 1, false));
            m_managerStorage.AddThread(new ThreadStorageView(applicationId, executorId3, 1, ThreadState.Started, DateTime.Now, DateTime.Now, 1, false));

            ExecutorStorageView[] executors = m_executorCollection.AvailableDedicatedExecutors;

            Assert.AreEqual(1, executors.Length);
            Assert.AreEqual(executorId2, executors[0].ExecutorId);
        }
        public void DisconnectTimedOutExecutorsTestSimpleScenario()
        {
            // add one that is OK
            ExecutorStorageView executor1 = new ExecutorStorageView(true, true, DateTime.Now, "hostname", 10, "username", 1, 1, 1, 1);
            // add one that is timed out
            ExecutorStorageView executor2 = new ExecutorStorageView(true, true, DateTime.Now.AddDays((-1)), "hostname", 10, "username", 1, 1, 1, 1);
            // add one that is not connected
            ExecutorStorageView executor3 = new ExecutorStorageView(false, false, DateTime.Now, "hostname", 10, "username", 1, 1, 1, 1);

            String executorId1 = m_managerStorage.AddExecutor(executor1);
            String executorId2 = m_managerStorage.AddExecutor(executor2);
            String executorId3 = m_managerStorage.AddExecutor(executor3);

            // whatever was not responsive in the last 60 seconds should get lost
            TimeSpan timeSpan = new TimeSpan(0, 0, 60);
            DisconnectTimedOutExecutors(timeSpan);

            // check what was disconnected

            Assert.IsTrue(m_managerStorage.GetExecutor(executorId1).Connected);
            Assert.IsFalse(m_managerStorage.GetExecutor(executorId2).Connected);
            Assert.IsFalse(m_managerStorage.GetExecutor(executorId3).Connected);
        }
        public string AddExecutor(ExecutorStorageView executor)
        {
            if (executor == null)
            {
                return null;
            }

            string executorId;
            if (executor.ExecutorId == null)
            {
                executorId = Guid.NewGuid().ToString();
            }
            else
            {
                executorId = executor.ExecutorId;
            }

            executor.ExecutorId = executorId;

            _executors.Add(executor);

            return executorId;
        }
        public void DeleteExecutor(ExecutorStorageView executor)
        {
            if (executor == null)
            {
                return;
            }

            IObjectContainer container = GetStorage();
            try
            {
                IList<ExecutorStorageView> executors =
                    container.Query<ExecutorStorageView>(delegate(ExecutorStorageView executorFinder)
                {
                    return executorFinder.ExecutorId == executor.ExecutorId;
                });

                if (executors.Count > 0)
                    container.Delete(executors[0]);
            }
            finally
            {
                container.Close();
            }
        }
        public void UpdateExecutor(ExecutorStorageView updatedExecutor)
        {
            ExecutorStorageView executor = null;
            IObjectContainer container = GetStorage();
            try
            {
                IList<ExecutorStorageView> executors =
                    container.Query<ExecutorStorageView>(delegate(ExecutorStorageView executorFinder)
                {
                    return executorFinder.ExecutorId == updatedExecutor.ExecutorId;
                });

                if (executors.Count > 0)
                {
                    executor = executors[0];
                    container.Delete(executor);
                    container.Set(updatedExecutor);
                }
            }
            finally
            {
                container.Close();
            }
        }
        public void DeleteExecutor(ExecutorStorageView executorToDelete)
        {
            if (executorToDelete == null)
            {
                return;
            }

            string sqlQuery;

            sqlQuery = string.Format("DELETE FROM executor WHERE executor_id='{0}'",
                executorToDelete.ExecutorId);

            logger.Debug(String.Format("Deleting executor {0}.", executorToDelete.ExecutorId));

            RunSql(sqlQuery);
        }
        private ExecutorStorageView[] DecodeExecutorFromDataReader(IDataReader dataReader)
        {
            ArrayList executors = new ArrayList();

            using(dataReader)
            {
                while(dataReader.Read())
                {
                    // in SQL the executor ID is stored as a GUID so we use GetValue instead of GetString in order to maximize compatibility with other databases
                    string executorId = dataReader.GetValue(dataReader.GetOrdinal("executor_id")).ToString();

                    bool dedicated = dataReader.GetBoolean(dataReader.GetOrdinal("is_dedicated"));
                    bool connected = dataReader.GetBoolean(dataReader.GetOrdinal("is_connected"));
                    DateTime pingTime = GetDateTime(dataReader, "ping_time");
                    string hostname = dataReader.GetString(dataReader.GetOrdinal("host"));
                    int port = dataReader.IsDBNull(dataReader.GetOrdinal("port")) ? 0 : dataReader.GetInt32(dataReader.GetOrdinal("port"));
                    string username = dataReader.GetString(dataReader.GetOrdinal("usr_name"));
                    int maxCpu = dataReader.IsDBNull(dataReader.GetOrdinal("cpu_max")) ? 0 : dataReader.GetInt32(dataReader.GetOrdinal("cpu_max"));
                    int cpuUsage = dataReader.IsDBNull(dataReader.GetOrdinal("cpu_usage")) ? 0 : dataReader.GetInt32(dataReader.GetOrdinal("cpu_usage"));
                    int availableCpu = dataReader.IsDBNull(dataReader.GetOrdinal("cpu_avail")) ? 0 : dataReader.GetInt32(dataReader.GetOrdinal("cpu_avail"));
                    float totalCpuUsage = dataReader.IsDBNull(dataReader.GetOrdinal("cpu_totalusage")) ? 0 : (float)dataReader.GetDouble(dataReader.GetOrdinal("cpu_totalusage"));

                    float maxMemory = dataReader.IsDBNull(dataReader.GetOrdinal("mem_max")) ? 0 : (float)dataReader.GetDouble(dataReader.GetOrdinal("mem_max"));;
                    float maxDisk = dataReader.IsDBNull(dataReader.GetOrdinal("disk_max")) ? 0 : (float)dataReader.GetDouble(dataReader.GetOrdinal("disk_max"));
                    int numberOfCpu = dataReader.IsDBNull(dataReader.GetOrdinal("num_cpus")) ? 0 : dataReader.GetInt32(dataReader.GetOrdinal("num_cpus"));
                    string os = dataReader.IsDBNull(dataReader.GetOrdinal("os")) ? "" : dataReader.GetString(dataReader.GetOrdinal("os"));
                    string architecture = dataReader.IsDBNull(dataReader.GetOrdinal("arch")) ? "" : dataReader.GetString(dataReader.GetOrdinal("arch"));

                    ExecutorStorageView executor = new ExecutorStorageView(
                        executorId,
                        dedicated,
                        connected,
                        pingTime,
                        hostname,
                        port,
                        username,
                        maxCpu,
                        cpuUsage,
                        availableCpu,
                        totalCpuUsage,
                        maxMemory,
                        maxDisk,
                        numberOfCpu,
                        os,
                        architecture
                        );

                    executors.Add(executor);
                }

                dataReader.Close();
            }

            return (ExecutorStorageView[])executors.ToArray(typeof(ExecutorStorageView));
        }
        public string AddExecutor(ExecutorStorageView executor)
        {
            if (executor == null)
            {
                return null;
            }

            string executorId;
            if (executor.ExecutorId == null)
            {
                executorId = Guid.NewGuid().ToString();
            }
            else
            {
                executorId = executor.ExecutorId;
            }

            RunSql(String.Format("insert into executor(executor_id, is_dedicated, is_connected, usr_name) values ('{0}', {1}, {2}, '{3}')",
                executorId,
                Convert.ToInt16(executor.Dedicated),
                Convert.ToInt16(executor.Connected),
                Utils.MakeSqlSafe(executor.Username)
                ));

            UpdateExecutorPingTime(executorId, executor.PingTime);

            UpdateExecutorHostAddress(executorId, executor.HostName, executor.Port);

            UpdateExecutorCpuUsage(executorId, executor.MaxCpu, executor.CpuUsage, executor.AvailableCpu, executor.TotalCpuUsage);

            UpdateExecutorAdditionalInformation(executorId, executor.MaxMemory, executor.MaxDisk, executor.NumberOfCpu, executor.OS, executor.Architecture);

            return executorId;
        }
        public void UpdateExecutor(ExecutorStorageView executor)
        {
            if (executor == null || executor.ExecutorId == null || executor.ExecutorId.Length == 0)
            {
                return;
            }

            UpdateExecutorDetails(executor.ExecutorId, executor.Dedicated, executor.Connected, executor.Username);

            UpdateExecutorPingTime(executor.ExecutorId, executor.PingTime);

            UpdateExecutorHostAddress(executor.ExecutorId, executor.HostName, executor.Port);

            UpdateExecutorCpuUsage(executor.ExecutorId, executor.MaxCpu, executor.CpuUsage, executor.AvailableCpu, executor.TotalCpuUsage);
        }
        /// <summary>
        /// Registers a new executor with the manager
        /// </summary>
        /// <param name="sc">security credentials used to perform this operation.</param>
        /// <param name="executorId">The preferred executor ID. Set it to null to generate a new ID.</param>
        /// <param name="info">information about the executor (see ExecutorInfo)</param>
        /// <returns></returns>
        public string RegisterNew(SecurityCredentials sc, string executorId, ExecutorInfo info)
        {
            //NOTE: when registering, all executors are initially set to non-dedicated.non-connected.
            //once they connect and can be pinged back, then these values are set accordingly.
            ExecutorStorageView executorStorage = new ExecutorStorageView(
                executorId,
                false,
                false,
                info.Hostname,
                sc.Username,
                info.MaxCpuPower,
                info.MaxMemory,
                info.MaxDiskSpace,
                info.NumberOfCpus,
                info.OS,
                info.Architecture
                );

            if (this[executorId].VerifyExists())
            {
                // executor already exists, just update the details
                ManagerStorageFactory.ManagerStorage().UpdateExecutor(executorStorage);

                logger.Debug("Updated executor details = " + executorId);

                return executorId;
            }
            else
            {
                string newExecutorId = ManagerStorageFactory.ManagerStorage().AddExecutor(executorStorage);

                logger.Debug("Registered new executor id=" + newExecutorId);

                return newExecutorId;
            }
        }
        public ExecutorStorageView[] GetExecutors()
        {
            ExecutorStorageView[] allExecutors;
            IObjectContainer container = GetStorage();
            try
            {
                IList<ExecutorStorageView> executors =
                    container.Query<ExecutorStorageView>(delegate(ExecutorStorageView executorFinder)
                {
                    return true;
                });

                if (executors.Count > 0)
                {
                    allExecutors = new ExecutorStorageView[executors.Count];
                    executors.CopyTo(allExecutors, 0);
                }
                else
                    allExecutors = new ExecutorStorageView[0];
            }
            finally
            {
                container.Close();
            }
            return allExecutors;
        }
        public void GetExecutorTestAnotherConstructor()
        {
            ExecutorStorageView executorStorage = new ExecutorStorageView(
                false,
                true,
                "hostname",
                "username",
                1,
                2,
                3,
                4,
                "Microsoft Windows NT 5.0.2195.0",
                "x86 Family 6 Model 13 Stepping 6"
                );

            string executorId = ManagerStorage.AddExecutor(executorStorage);

            ExecutorStorageView executor = ManagerStorage.GetExecutor(executorId);

            Assert.IsNotNull(executor);
            Assert.AreEqual(executorId, executor.ExecutorId);
            Assert.AreEqual(false, executor.Dedicated);
            Assert.AreEqual(true, executor.Connected);
            Assert.IsFalse(executor.PingTimeSet);
            Assert.AreEqual("hostname", executor.HostName);
            Assert.AreEqual(0, executor.Port);
            Assert.AreEqual("username", executor.Username);
            Assert.AreEqual(1, executor.MaxCpu);
            Assert.AreEqual(0, executor.CpuUsage);
            Assert.AreEqual(0, executor.AvailableCpu);
            Assert.AreEqual(0, executor.TotalCpuUsage);
            Assert.AreEqual(2, executor.MaxMemory);
            Assert.AreEqual(3, executor.MaxDisk);
            Assert.AreEqual(4, executor.NumberOfCpu);
            Assert.AreEqual("Microsoft Windows NT 5.0.2195.0", executor.OS);
            Assert.AreEqual("x86 Family 6 Model 13 Stepping 6", executor.Architecture);
        }
        public void AddExecutorTestLatestConstructor()
        {
            ExecutorStorageView executorStorage = new ExecutorStorageView(
                false,
                false,
                "hostname",
                "username",
                1,
                2,
                3,
                4,
                "Windows",
                "686"
                );

            string executorId = ManagerStorage.AddExecutor(executorStorage);

            Assert.IsNotNull(executorId);
            Assert.AreNotEqual("", executorId);
        }
        private void RefreshData(ExecutorStorageView ex)
        {
            if (ex == null)
                return;

            txId.Text = ex.ExecutorId;
            lbName.Text = ex.HostName;

            this.Text = ex.HostName + " Properties";

            txPort.Text = ex.Port.ToString();
            txUsername.Text = ex.Username;

            chkConnected.Checked = ex.Connected;
            chkDedicated.Checked = ex.Dedicated;

            txArch.Text = ex.Architecture;
            txOS.Text = ex.OS;

            txMaxCPU.Text = ex.MaxCpu.ToString();
            txMaxDisk.Text = ex.MaxDisk.ToString();
            txNumCPUs.Text = ex.NumberOfCpu.ToString();

            txPingTime.Text = GetDiff(ex.PingTime);
        }
        private string AddExecutor(
            bool dedicated,
            bool connected,
            DateTime pingTime,
            string hostname,
            int port,
            string username,
            int maxCpu,
            int cpuUsage,
            int availableCpu,
            float totalCpuUsage
            )
        {
            ExecutorStorageView executor = new ExecutorStorageView(
                        dedicated,
                        connected,
                        pingTime,
                        hostname,
                        port,
                        username,
                        maxCpu,
                        cpuUsage,
                        availableCpu,
                        totalCpuUsage
                    );

            return ManagerStorage.AddExecutor(executor);
        }
        public void UpdateExecutorTest2()
        {
            DateTime pingTime2 = DateTime.Now.AddDays(1);

            ExecutorStorageView updatedExecutor = new ExecutorStorageView(true, false, pingTime2, "test2", 8888, "username2", 222, 456, 56, (float)5.6);

            updatedExecutor.ExecutorId = "";

            ManagerStorage.UpdateExecutor(updatedExecutor);

            ExecutorStorageView[] executors = ManagerStorage.GetExecutors();

            Assert.AreEqual(0, executors.Length);
        }
        public void UpdateExecutorTest1()
        {
            // TB: due to rounding errors the milliseconds might be lost in the database storage.
            // TB: I think this is OK so we create a test DateTime without milliseconds
            DateTime now = DateTime.Now;
            DateTime pingTime1 = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, 0);
            now = now.AddDays(1);
            DateTime pingTime2 = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, 0);

            string executorId = AddExecutor(false, true, pingTime1, "test1", 9999, "username1", 111, 123, 34, (float)3.4);

            ExecutorStorageView updatedExecutor = new ExecutorStorageView(true, false, pingTime2, "test2", 8888, "username2", 222, 456, 56, (float)5.6);

            updatedExecutor.ExecutorId = executorId;

            ManagerStorage.UpdateExecutor(updatedExecutor);

            ExecutorStorageView[] executors = ManagerStorage.GetExecutors();

            Assert.AreEqual(1, executors.Length);
            Assert.AreEqual(true, executors[0].Dedicated);
            Assert.AreEqual(false, executors[0].Connected);
            Assert.AreEqual(pingTime2, executors[0].PingTime);
            Assert.AreEqual("test2", executors[0].HostName);
            Assert.AreEqual(8888, executors[0].Port);
            Assert.AreEqual("username2", executors[0].Username);
            Assert.AreEqual(222, executors[0].MaxCpu);
            Assert.AreEqual(456, executors[0].CpuUsage);
            Assert.AreEqual(56, executors[0].AvailableCpu);
            Assert.AreEqual(5.6, executors[0].TotalCpuUsage);
            Assert.AreEqual(executorId, executors[0].ExecutorId);
        }
        public ExecutorStorageView[] GetExecutors(TriStateBoolean dedicated, TriStateBoolean connected)
        {
            ExecutorStorageView[] allExecutors;
            IObjectContainer container = GetStorage();
            try
            {
                IList<ExecutorStorageView> executors =
                    container.Query<ExecutorStorageView>(delegate(ExecutorStorageView executorFinder)
                {
                    return ((dedicated == TriStateBoolean.True && executorFinder.Dedicated)
                        || dedicated == TriStateBoolean.Undefined) &&
                        ((connected == TriStateBoolean.True && executorFinder.Connected)
                        || connected == TriStateBoolean.Undefined);
                });

                if (executors.Count > 0)
                {
                    allExecutors = new ExecutorStorageView[executors.Count];
                    executors.CopyTo(allExecutors, 0);
                }
                else
                    allExecutors = new ExecutorStorageView[0];
            }
            finally
            {
                container.Close();
            }
            return allExecutors;
        }
        public void UpdateExecutor(ExecutorStorageView updatedExecutor)
        {
            if( updatedExecutor == null )
            {
                return;
            }

            ArrayList newExecutorList = new ArrayList();

            foreach(ExecutorStorageView executor in _executors)
            {
                if (executor.ExecutorId == updatedExecutor.ExecutorId)
                {
                    newExecutorList.Add(updatedExecutor);
                }
                else
                {
                    newExecutorList.Add(executor);
                }
            }

            _executors = newExecutorList;
        }
        public void DeleteExecutor(ExecutorStorageView executorToDelete)
        {
            if (executorToDelete == null)
            {
                return;
            }

            ArrayList newExecutorList = new ArrayList();

            foreach (ExecutorStorageView executor in _executors)
            {
                if (executor.ExecutorId != executorToDelete.ExecutorId)
                {
                    newExecutorList.Add(executor);
                }
            }

            _executors = newExecutorList;
        }
        public String AddExecutor(ExecutorStorageView executor)
        {
            if (executor == null)
            {
                return null;
            }

            if (m_executors == null)
            {
                m_executors = new ArrayList();
            }

            String executorId;
            if (executor.ExecutorId == null)
            {
                executorId = Guid.NewGuid().ToString();
            }
            else
            {
                executorId = executor.ExecutorId;
            }

            executor.ExecutorId = executorId;

            m_executors.Add(executor);

            return executorId;
        }