誰でもこれで私を助けることができます。3 つのサーバーがあります。Notstarted ジョブが最も少ないサーバーにジョブを割り当てたいと考えています。開始されたジョブと開始されていないジョブを検討したいと思います。私はこれを達成しました。しかし、私のコードは非常に長いです。コンセプトを変えずにコードを最小化する方法がわかりません。これが私のコードです:
public List<int> GetPaServer()
{
List<int> PaServers = new List<int>();
using (PaEntities pa = new PaEntities())
{
var PaServer = from server in pa.AppPM_Pa_Server
where server.IsActive == true
select server.ServerId;
foreach (var paServer in PaServer)
{
PaServers.Add(paServer);
}
}
return PaServers;
}
//Method to get the serverid for each request.
public int GetPaQueue()
{
using (PaEntities server = new PaEntities())
{
List<int> Paserver = new List<int>();
Paserver = GetPaServer();
string server1 = string.Empty;
string server2 = string.Empty;
string server3 = string.Empty;
foreach (int paserver in Paserver)
{
if (paserver == 1)
{
server1 = "active";
}
else if (paserver == 2)
{
server2 = "active";
}
else if (paserver == 3)
{
server3 = "active";
}
}
int retVal = 0;
// Get the Server NotStarted details here
var NotStarted1 = (from serverID in server.AppPM_Pat
where serverID.Status == "NotStarted" && serverID.ServerId == 1
select serverID.ServerId).Count();
var NotStarted2 = (from serverID in server.AppPM_Pat
where serverID.Status == "NotStarted" && serverID.ServerId == 2
select serverID.ServerId).Count();
var NotStarted3 = (from serverID in server.AppPM_Pat
where serverID.Status == "NotStarted" && serverID.ServerId == 3
select serverID.ServerId).Count();
// Get the Server Started details here
var server_Started1 = (from serverID in server.AppPM_Pat
where serverID.Status == "Started" && serverID.ServerId == 1
select serverID.ServerId).Count();
var server_Started2 = (from serverID in server.AppPM_Pat
where serverID.Status == "Started" && serverID.ServerId == 2
select serverID.ServerId).Count();
var server_Started3 = (from serverID in server.AppPM_Pat
where serverID.Status == "Started" && serverID.ServerId == 3
select serverID.ServerId).Count();
//Get the server number for each request
//control comes here only when the server is active
if (server1 == "active" && server2 == "active" && server3 == "active")
{
if (NotStarted1 == 0 && NotStarted2 == 0 && NotStarted3 == 0)
{
if ((server_Started1 > server_Started2) && (server_Started1 > server_Started3))
{
retVal = 2;
}
else if (server_Started1 == 0 && server_Started2 == 0 && server_Started3 == 0)
{
retVal = 1;
}
else if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
retVal = 1;
}
else if ((server_Started2 > server_Started1) && (server_Started2 > server_Started3))
{
retVal = 1;
}
else if ((server_Started3 > server_Started1) && (server_Started3 > server_Started2))
{
retVal = 1;
}
else if (server_Started1 == server_Started2 && server_Started3 == 0)
{
retVal = 3;
}
else if (server_Started1 == server_Started3 && server_Started2 == 0)
{
retVal = 2;
}
else if (server_Started2 == server_Started3 && server_Started1 == 0)
{
retVal = 1;
}
}
//control comes here only when the third server is active after some time
else if (NotStarted1 == NotStarted2 && NotStarted3 == 0)
{
if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 0)
{
retVal = 3;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 3;
}
}
else if ((NotStarted1 > NotStarted2) && NotStarted3 == 0)
{
if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 0)
{
retVal = 3;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 3;
}
}
else if ((NotStarted2 > NotStarted1) && NotStarted3 == 0)
{
if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 0)
{
retVal = 3;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 3;
}
}
//control comes here only when the first server is active after some time
else if (NotStarted2 == NotStarted3 && NotStarted1 == 0)
{
if (server_Started3 == 1 & server_Started2 == 1 & server_Started1 == 0)
{
retVal = 1;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 1;
}
}
else if ((NotStarted2 > NotStarted3) && NotStarted1 == 0)
{
if (server_Started3 == 1 & server_Started2 == 1 & server_Started1 == 0)
{
retVal = 1;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 1;
}
}
else if ((NotStarted3 > NotStarted2) && NotStarted1 == 0)
{
if (server_Started3 == 1 & server_Started2 == 1 & server_Started1 == 0)
{
retVal = 1;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 1;
}
}
//control comes here only when the second server is active after some time
else if (NotStarted3 == NotStarted1 && NotStarted2 == 0)
{
if (server_Started3 == 1 & server_Started1 == 1 & server_Started2 == 0)
{
retVal = 2;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 2;
}
}
else if ((NotStarted3 > NotStarted1) && NotStarted2 == 0)
{
if (server_Started3 == 1 & server_Started1 == 1 & server_Started2 == 0)
{
retVal = 2;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 2;
}
}
else if ((NotStarted1 > NotStarted3) && NotStarted2 == 0)
{
if (server_Started3 == 1 & server_Started1 == 1 & server_Started2 == 0)
{
retVal = 2;
}
else if (server_Started1 == 1 & server_Started2 == 1 & server_Started3 == 1)
{
retVal = 2;
}
}
else if (NotStarted1 == 1 && NotStarted2 == 0 && NotStarted3 == 0)
{
if ((server_Started1 > server_Started2) && (server_Started1 > server_Started3))
{
retVal = 2;
}
else if (server_Started1 == 0 && server_Started2 == 0 && server_Started3 == 0)
{
retVal = 1;
}
else if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
retVal = 1;
}
else if ((server_Started2 > server_Started1) && (server_Started2 > server_Started3))
{
retVal = 1;
}
else if ((server_Started3 > server_Started1) && (server_Started3 > server_Started2))
{
retVal = 1;
}
else if (server_Started1 == server_Started2 && server_Started3 == 0)
{
retVal = 3;
}
else if (server_Started1 == server_Started3 && server_Started2 == 0)
{
retVal = 2;
}
else if (server_Started2 == server_Started3 && server_Started1 == 0)
{
retVal = 1;
}
}
else if (NotStarted1 == 1 && NotStarted2 == 1 && NotStarted3 == 0)
{
if ((server_Started1 > server_Started2) && (server_Started1 > server_Started3))
{
retVal = 2;
}
else if (server_Started1 == 0 && server_Started2 == 0 && server_Started3 == 0)
{
retVal = 1;
}
else if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
retVal = 1;
}
else if ((server_Started2 > server_Started1) && (server_Started2 > server_Started3))
{
retVal = 1;
}
else if ((server_Started3 > server_Started1) && (server_Started3 > server_Started2))
{
retVal = 1;
}
else if (server_Started1 == server_Started2 && server_Started3 == 0)
{
retVal = 3;
}
else if (server_Started1 == server_Started3 && server_Started2 == 0)
{
retVal = 2;
}
else if (server_Started2 == server_Started3 && server_Started1 == 0)
{
retVal = 1;
}
}
else if (NotStarted1 > NotStarted2 && NotStarted1 > NotStarted3)
{
if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
if (NotStarted2 > NotStarted3)
{
retVal = 3;
}
else
{
retVal = 2;
}
}
}
else if (NotStarted2 > NotStarted3 && NotStarted2 > NotStarted1)
{
if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
if (NotStarted1 > NotStarted3)
{
retVal = 3;
}
else
{
retVal = 1;
}
}
}
else if (NotStarted3 > NotStarted1 && NotStarted3 > NotStarted2)
{
if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
if (NotStarted1 > NotStarted2)
{
retVal = 2;
}
else
{
retVal = 1;
}
}
}
else if (NotStarted1 == NotStarted2 && NotStarted2 == NotStarted3 && NotStarted1 == NotStarted3)
{
if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
retVal = 1;
}
}
else if (NotStarted1 == NotStarted2 && NotStarted1 > NotStarted3 && NotStarted2 > NotStarted3)
{
if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
retVal = 3;
}
}
else if (NotStarted2 == NotStarted3 && NotStarted2 > NotStarted1 && NotStarted3 > NotStarted1)
{
if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
retVal = 1;
}
}
else if (NotStarted1 == NotStarted3 && NotStarted1 > NotStarted2 && NotStarted3 > NotStarted2)
{
if (server_Started1 == 1 && server_Started2 == 1 && server_Started3 == 1)
{
retVal = 2;
}
}
}
//control comes here only when server1 and server2 is active
else if (server1 == "active" && server2 == "active")
{
if (NotStarted1 > NotStarted2)
{
if (server_Started1 == 1 && server_Started2 == 1)
{
retVal = 2;
}
}
else if (NotStarted1 == NotStarted2)
{
if (server_Started1 == 0 && server_Started2 == 0)
{
retVal = 1;
}
else if (server_Started1 == 1 && server_Started2 == 0)
{
retVal = 2;
}
else if (server_Started1 == 0 && server_Started2 == 1)
{
retVal = 1;
}
else if (server_Started1 == 1 && server_Started2 == 1)
{
retVal = 1;
}
}
else
{
if (server_Started1 == 1 && server_Started2 == 1)
{
retVal = 1;
}
}
}
//control comes here only when server3 and server2 is active
else if (server2 == "active" && server3 == "active")
{
if (NotStarted2 > NotStarted3)
{
if (server_Started2 == 1 && server_Started3 == 1)
{
retVal = 3;
}
}
else if (NotStarted2 == NotStarted3)
{
if (server_Started2 == 0 && server_Started3 == 0)
{
retVal = 2;
}
else if (server_Started2 == 1 && server_Started3 == 0)
{
retVal = 3;
}
else if (server_Started2 == 0 && server_Started3 == 1)
{
retVal = 2;
}
else if (server_Started2 == 1 && server_Started3 == 1)
{
retVal = 2;
}
}
else
{
if (server_Started2 == 1 && server_Started3 == 1)
{
retVal = 2;
}
}
}
//control comes here only when server1 and server3 is active
else if (server1 == "active" && server3 == "active")
{
if (NotStarted1 > NotStarted3)
{
if (server_Started1 == 1 && server_Started3 == 1)
{
retVal = 3;
}
}
else if (NotStarted1 == NotStarted3)
{
if (server_Started1 == 0 && server_Started3 == 0)
{
retVal = 1;
}
else if (server_Started1 == 1 && server_Started3 == 0)
{
retVal = 3;
}
else if (server_Started1 == 0 && server_Started3 == 1)
{
retVal = 1;
}
else if (server_Started1 == 1 && server_Started3 == 1)
{
retVal = 1;
}
}
else
{
if (server_Started1 == 1 && server_Started3 == 1)
{
retVal = 1;
}
}
}
return retVal;
}
}