using AIPractice.LabellerServer.Managers;
using AIPractice.LabellerServer.Managers.Entities;
using AIPractice.Shared.ImageRois;
using System;
using System.Linq;
using System.ServiceModel;
namespace AIPractice.LabellerServer
{
public enum LoginResult
{
//Login success.
Success = 0,
//The name is wrong.
WrongName,
//The password is wrong.
WrongPassword,
}
[ServiceContract]
public interface IAccountService
{
///
/// Gets account by name and password
///
/// The name of the account.
/// The account
[OperationContract]
Account GetAccout(string name);
///
/// Get the virtual case by given id.
///
/// The account of the virtual case.
/// The case id
/// The found virtual case.
[OperationContract]
VirtualCase GetVirtualCaseById(string accountId, string caseId);
///
/// Gets the virtual case by given index.
///
/// The account of the virtual case.
/// The index
/// The found virtual case.
[OperationContract]
VirtualCase GetVirtualCaseByIndex(string accountId, int caseIndex);
///
/// Get the case image content by the given id.
///
/// The account id
/// The case id
/// The image id of the image
/// The content of the found image.
[OperationContract]
string GetCaseImageById(string accountId, string caseId, string imageId);
///
/// Get the case image content by the given index.
///
/// The account id
/// The case id
/// The case index
/// The content of the found image.
[OperationContract]
string GetCaseImageByIndex(string accountId, string caseId, int index);
///
/// Get the committed image by image id.
///
/// The content of the image.
[OperationContract]
CommittedImage GetCommittedImage(string accountId, string imageId);
///
/// Get the latest committed image by image id.
///
/// The content of the image.
[OperationContract]
CommittedImage GetLatestCommittedImage(string accountId);
///
/// Get the virtual case image.
///
/// The image info contains the index.
[OperationContract]
VirtualCaseImage GetVirtualCaseImageById(string accountId, string imageId);
///
/// Get the virtual case image.
///
/// The image info contains the index.
[OperationContract]
VirtualCaseImage GetVirtualCaseImageByIndex(string caseId, int index);
///
/// Gets the total case count.
///
/// The case count of this account
[OperationContract]
int GetCaseCount(string accountId);
///
/// Gets the total image count of current case.
///
/// The total image count of current case
[OperationContract]
int GetCaseImageCount(string caseId);
///
/// Gets the total image count.
///
/// The total image count
[OperationContract]
int GetTotalImageCount(string accountId);
///
/// Gets the total commited image count of this account.
///
/// The total committed image count of this account
[OperationContract]
int GetCommittedImageCount(string accountId);
///
/// Commit a labeled image into database.
///
/// The image data to be committed.
[OperationContract]
void CommitImage(string accountId, string content);
///
/// Set current image to committedImage.
///
///
///
[OperationContract]
void SetCurrentCommitedImage(string accountId, string imageId);
}
public partial class LabellerService : IAccountService
{
private const string ReviewrCaseId = "6DEA2E08DC5A45EAAF1311935D6FE02E";
///
/// Get account by account name.
///
///
///
public Account GetAccout(string name)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountByName(name);
if(account != null)
{
LogManager.WriteLog($"客户端获取用户信息:{account.Name}。");
}
else
{
LogManager.WriteLog($"客户端获取用户信息:{name} 失败。");
}
return account;
}
///
/// Get the virtual case.
///
///
public VirtualCase GetVirtualCaseById(string accountId, string caseId)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var virtualCase = dataManager.GetVirtualCase(accountId, caseId);
if (virtualCase != null)
{
LogManager.WriteLog($"客户端 {account} 通过ID获取虚拟病例信息:{virtualCase.Id}。");
}
else
{
LogManager.WriteLog($"客户端 {account} 通过ID获取虚拟病例信息:{caseId} 失败。");
}
return virtualCase;
}
///
/// Get the virtual case.
///
///
public VirtualCase GetVirtualCaseByIndex(string accountId, int caseIndex)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var virtualCase = dataManager.GetVirtualCase(accountId, caseIndex);
if (virtualCase != null)
{
LogManager.WriteLog($"客户端 {account} 通过索引获取虚拟病例信息:{virtualCase.Id}。");
}
else
{
LogManager.WriteLog($"客户端 {account} 通过索引获取虚拟病例信息:{caseIndex} 失败。");
}
return virtualCase;
}
///
/// Get the image by image id, it could be the orginal image or committed image.
///
/// The content of the image.
public string GetCaseImageById(string accountId, string caseId, string imageId)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var committedImage = dataManager.GetCommittedImage(accountId, imageId);
if (committedImage != null && committedImage.CaseId == caseId)
{
LogManager.WriteLog($"客户端 {account} 通过Id获取图像,图像来自提交的图像:{caseId} - {imageId}。");
return committedImage.Content;
}
var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId,imageId);
if (virtualCaseImage != null && virtualCaseImage.CaseId == caseId)
{
var labelImage = dataManager.GetLabelImage(imageId);
if (labelImage != null)
{
LogManager.WriteLog($"客户端 {account} 通过Id获取图像,图像来自原始图像:{caseId} - {imageId}。");
return labelImage.Content;
}
}
LogManager.WriteLog($"客户端 {account} 通过Id获取图像失败:{caseId} - {imageId}。");
return null;
}
///
/// Get the image by image index, it could be the orginal image or committed image.
///
/// The content of the image.
public string GetCaseImageByIndex(string accountId, string caseId, int index)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var virtualCaseImage = dataManager.GetVirtualCaseImage(caseId, index);
if (virtualCaseImage != null)
{
var committedImage = dataManager.GetCommittedImage(accountId, virtualCaseImage.ImageId);
if (committedImage != null && committedImage.CaseId == caseId)
{
LogManager.WriteLog($"客户端 {account} 通过索引获取图像,图像来自提交的图像:{caseId} - {index}。");
return committedImage.Content;
}
if (virtualCaseImage != null && virtualCaseImage.CaseId == caseId)
{
var labelImage = dataManager.GetLabelImage(virtualCaseImage.ImageId);
if (labelImage != null)
{
LogManager.WriteLog($"客户端 {account} 通过索引获取图像,图像来自原始图像:{caseId} - {index}。");
return labelImage.Content;
}
}
}
LogManager.WriteLog($"客户端 {account} 通过索引获取图像失败:{caseId} - {index}。");
return null;
}
///
/// Get committed image by image id.
///
/// The content of the image.
public CommittedImage GetCommittedImage(string accountId, string imageId)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var committedImage = dataManager.GetCommittedImage(accountId, imageId);
if(committedImage != null)
{
LogManager.WriteLog($"客户端 {account} 通过Id获取提交的图像:{imageId}。");
}
else
{
LogManager.WriteLog($"客户端 {account} 通过Id获取提交的图像失败:{imageId}。");
}
return committedImage;
}
///
/// Get the latest committed image.
///
/// The content of the image.
public CommittedImage GetLatestCommittedImage(string accountId)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var committedImage = dataManager.GetLastCommittedImage(accountId);
if (committedImage != null)
{
LogManager.WriteLog($"客户端 {account} 获取最后提交的图像:{committedImage.ImageId}。");
}
else
{
LogManager.WriteLog($"客户端 {account} 获取最后提交的图像失败。");
}
return committedImage;
}
///
/// Get the virtual case image.
///
/// The image info contains the index.
public VirtualCaseImage GetVirtualCaseImageById(string accountId, string imageId)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId, imageId);
if (virtualCaseImage != null)
{
LogManager.WriteLog($"客户端 {account} 通过Id获取虚拟病例图像:{imageId}。");
}
else
{
LogManager.WriteLog($"客户端 {account} 通过Id获取虚拟病例图像失败:{imageId}。");
}
return virtualCaseImage;
}
///
/// Get the virtual case image.
///
/// The image info contains the index.
public VirtualCaseImage GetVirtualCaseImageByIndex(string caseId, int index)
{
var dataManager = ManagerContainer.GetManager();
var virtualCaseImage = dataManager.GetVirtualCaseImage(caseId, index);
if (virtualCaseImage != null)
{
LogManager.WriteLog($"客户端通过索引获取虚拟病例图像:{caseId} - {index}。");
}
else
{
LogManager.WriteLog($"客户端通过索引获取虚拟病例图像失败:{caseId} - {index}。");
}
return virtualCaseImage;
}
///
/// Gets the total case count.
///
/// The case count of this account
public int GetCaseCount(string accountId)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var caseCount = dataManager.GetVirtualCaseCount(accountId);
LogManager.WriteLog($"客户端 {account} 获取虚拟病例数量:{caseCount}。");
return caseCount;
}
///
/// Gets the total image count of current case.
///
/// The total image count of current case
public int GetCaseImageCount(string caseId)
{
var dataManager = ManagerContainer.GetManager();
var imageCount = dataManager.GetVirtualCaseImageCount(caseId);
LogManager.WriteLog($"客户端获取虚拟病例图像数量:{caseId} - {imageCount}。");
return imageCount;
}
///
/// Gets the total commited image count of this account.
///
/// The total committed image count of this account
public int GetCommittedImageCount(string accountId)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var imageCount = dataManager.GetCommittedImageCount(accountId);
LogManager.WriteLog($"客户端 {account} 获取提交图像数量:{imageCount}。");
return imageCount;
}
///
/// Commit one image.
///
///
///
public void CommitImage(string accountId, string content)
{
var start = Environment.TickCount;
var imageData = ImageData.FromXml(content);
var partCount = 0;
if (imageData.Rois.Count == 0)
{
partCount = 1;
}
else
{
foreach (var roi in imageData.Rois)
{
if (roi.Conclusion != null && roi.Conclusion.IsParts)
{
partCount++;
}
}
}
var imageId = imageData.Id;
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var existCommittedImage = dataManager.GetCommittedImage(accountId, imageId);
if (existCommittedImage != null)
{
existCommittedImage.Content = content;
existCommittedImage.PartCount = partCount;
existCommittedImage.CreatedTime = DateTime.Now;
existCommittedImage.Result = account.IsReviewer ? ReviewResult.Best : existCommittedImage.Result;
dataManager.UpdateCommittedImage(existCommittedImage);
LogManager.WriteLog($"客户端 {account} 提交图像,因为已经存在,实行更新操作:{imageId}。");
}
else
{
if (account.IsReviewer)
{
var labelImage = dataManager.GetLabelImage(imageId);
if (labelImage != null)
{
var committedImage = new CommittedImage
{
Id = Guid.NewGuid().ToString("N").ToUpper(),
AccountId = accountId,
CaseId = ReviewrCaseId,
ImageId = imageId,
CreatedTime = DateTime.Now,
IsShared = labelImage.IsShared,
IsDeleted = false,
Result = ReviewResult.Best,
Content = content,
PartCount = partCount,
Description = labelImage.Description,
};
dataManager.AddCommittedImage(committedImage);
LogManager.WriteLog($"客户端 {account} 提交图像:{imageId}。");
}
}
else
{
var virtualCaseImage = dataManager.GetVirtualCaseImage(accountId, imageId);
if (virtualCaseImage != null)
{
var labelImage = dataManager.GetLabelImage(virtualCaseImage.ImageId);
if (labelImage != null)
{
var virtualCase = dataManager.GetVirtualCase(accountId, virtualCaseImage.CaseId);
if (virtualCase != null)
{
var committedImage = new CommittedImage
{
Id = Guid.NewGuid().ToString("N").ToUpper(),
AccountId = accountId,
CaseId = virtualCase.Id,
ImageId = virtualCaseImage.ImageId,
CreatedTime = DateTime.Now,
IsShared = labelImage.IsShared,
IsDeleted = false,
Result = ReviewResult.No,
Content = content,
PartCount = partCount,
Description = labelImage.Description,
};
dataManager.AddCommittedImage(committedImage);
LogManager.WriteLog($"客户端 {account} 提交图像:{imageId}。");
}
}
}
}
}
var used = Environment.TickCount - start;
LogManager.WriteLog($"提交图像耗时 {used} ms");
}
///
/// Get the total image count
///
///
public int GetTotalImageCount(string accountId)
{
var dataManager = ManagerContainer.GetManager();
var caseCount = dataManager.GetVirtualCaseCount(accountId);
var imageCount = 0;
for(var i= 0; i< caseCount; i++)
{
var virtualCase = dataManager.GetVirtualCase(accountId, i);
if(virtualCase != null)
{
imageCount += dataManager.GetVirtualCaseImageCount(virtualCase.Id);
}
}
return imageCount;
}
public void SetCurrentCommitedImage(string accountId, string imageId)
{
var dataManager = ManagerContainer.GetManager();
var account = dataManager.GetAccountById(accountId);
var existCommittedImage = dataManager.GetCommittedImage(accountId, imageId);
if (existCommittedImage != null)
{
existCommittedImage.CreatedTime = DateTime.Now;
dataManager.UpdateCommittedImage(existCommittedImage);
LogManager.WriteLog($"客户端 {account} 将提交图像设置为当前已经处理图像:{imageId}。");
}
}
}
}