标签:file report temp max dir start roc abs ret
泛型静态类 & function作为参数
///
///
///
/// original datas type:get from proxy api
/// save datas type
public static class ReportOperation where OD : class where SD : class
{
static int _syncCallApiMaxNum= ConfigHelper.SyncCallApiMaxNum;
static int _MaxSaveDataNum = ConfigHelper.MaxSaveDataNum;
static string OriginalDatasType = typeof(OD).FullName;
static string SaveDatasType = typeof(SD).FullName;
private static bool TaskSplitForSave(List> taskList, ILogBase _logger)
{
bool saveResult = true;
try
{
_logger.Info($"TaskSplitForSave.SaveDatasType:{SaveDatasType};");
if (taskList == null || taskList.Count() == 0)
{
return false;
}
int index = 0;
int sumNum = taskList.Count();
int syncCallApiNum = sumNum > 0 ? _syncCallApiMaxNum : sumNum;
while (index > temp = new List>();
syncCallApiNum = syncCallApiNum > sumNum - index ? sumNum - index : syncCallApiNum;
temp = taskList.GetRange(index, syncCallApiNum);
index = index + syncCallApiNum;
System.Threading.Thread.Sleep(5000);
foreach (var oneT in temp)
{
_logger.Info($"start save Report datas to database. start:{index},num:{syncCallApiNum},request:{JsonConvert.SerializeObject(oneT.Data)}");
oneT.SaveResult.Start();
}
Task.WaitAll(temp.Select(each => each.SaveResult).ToArray());
foreach (var teskOne in temp)
{
var saveTempResult = teskOne.SaveResult.Result;
saveResult = saveResult && saveTempResult;
if (!saveTempResult)
{
_logger.Warn($"failed to async Report datas to database!,request:{JsonConvert.SerializeObject(teskOne.Data)}");
}
else
{
_logger.Info($"report datas to DB successed!,request:{JsonConvert.SerializeObject(teskOne.Data)}");
}
}
}
_logger.Info($"TaskSplitForSave result:{JsonConvert.SerializeObject(saveResult)};");
return saveResult;
}
catch (Exception ex)
{
_logger.Error($"ProxyReportCallSplit:{ex.ToString()}");
throw ex;
}
}
private static List> DataSplitForSave(List reportDB,Func,bool> saveOperate, ILogBase _logger)
{
List> taskList = new List>();
try
{
_logger.Info($"DataSplitForSave request.reportDB:{JsonConvert.SerializeObject(reportDB)};");
if (reportDB == null || reportDB.Count() == 0)
{
return null;
}
int index = 0;
int sumNum = reportDB.Count();
int eachInsert = sumNum > 0 ? _MaxSaveDataNum : sumNum;
while (index temp = new List();
eachInsert = eachInsert > sumNum - index ? sumNum - index : eachInsert;
temp = reportDB.GetRange(index, eachInsert);
index = index + eachInsert;
var taskTemp = new Task(()=>saveOperate(temp));//_FacebookReportClient.Save(temp)
taskList.Add(new ReportSaveTask() { SaveResult = taskTemp, Data = temp });
}
return taskList;
}
catch (Exception ex)
{
_logger.Error($"save datas to DB error:{ex.ToString()},request:{JsonConvert.SerializeObject(reportDB)}");
throw ex;
}
}
public static bool ProxyApiSplitToCall(List>> taskList,Func,List> DateConvertToDB, Func,bool> saveOperate, ILogBase _logger)
{
bool result = true;
try
{
_logger.Info($"ProxyApiSplitToCall");
if (taskList == null || taskList.Count() == 0)
{
return false;
}
int index = 0;
int sumNum = taskList.Count();
int syncCallApiNum = sumNum > 0 ? _syncCallApiMaxNum : sumNum;
while (index sumNum - index ? sumNum - index : syncCallApiNum;
List>> temp = taskList.GetRange(index, syncCallApiNum);
index = index + syncCallApiNum;
System.Threading.Thread.Sleep(5000);
foreach (var oneT in temp)
{
oneT.Start();
}
Task.WaitAll(temp.ToArray());
#region save data
List allReports = new List();
CommandHelper.UnionAllTaskResult(temp, ref allReports);
if (allReports == null || allReports.Count() == 0)
{
_logger.Warn($"no datas can get from proxy call![{JsonConvert.SerializeObject(allReports)}]");
continue;
}
_logger.Info($"datas get from proxy call:{JsonConvert.SerializeObject(allReports)}");
List reportDB = DateConvertToDB(allReports);
if (reportDB == null || reportDB.Count() == 0)
{
_logger.Warn($"no datas need to be created or updated:{JsonConvert.SerializeObject(reportDB)}");
continue;
}
_logger.Info($"datas need to be save :{JsonConvert.SerializeObject(reportDB)}");
List> DataSaveT= DataSplitForSave(reportDB, saveOperate, _logger);
bool saveResult = TaskSplitForSave(DataSaveT, _logger);
_logger.Info($"datas save result:{JsonConvert.SerializeObject(saveResult)}");
#endregion
result = result & saveResult;
}
return result;
}
catch (Exception ex)
{
_logger.Error($"ProxyReportCallSplit:{ex.ToString()}");
throw ex;
}
}
}
静态类的使用
bool saveResult = ReportOperation.ProxyApiSplitToCall(taskList, dataConvertFunc, dataSaveFunc, _logger);
function作为参数的使用
Func, List> dataConvertFunc = (originalData) => { return DateConvertToDB(originalData, accounts); };
Func, bool> dataSaveFunc = (saveDB) => { return SaveReportsToDB(saveDB); };
bool saveResult = ReportOperation.ProxyApiSplitToCall(taskList, dataConvertFunc, dataSaveFunc, _logger);
泛型抽象类:
///
/// get reoports from proxy client
///
/// request type
/// response list type
/// proxy client request type
/// proxy client response type
public abstract class ReportDownloadBase where RQ : class where RP : class where CRQ : class where CRP : class
{
protected string exportPath;
protected System.Collections.Specialized.NameValueCollection appSettings;
protected ILogBase _logger;
protected string _plantform;
public ReportDownloadBase(ILogBase logger,string plantform)
{
_plantform = plantform;
appSettings = WestWin.Common.Configuration.ConfigurationManager.Default.AppSettings;
exportPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), appSettings["ReportRawPath"], plantform);
_logger = logger;
}
///
/// Generate ReportRequest for proxy client
///
/// request
///
protected abstract CRQ GenerateReportRequest(RQ req);
///
/// Generate Results to return
///
/// ReportResponse
/// breakdownValue
/// result list
protected abstract List
被使用:
public class FacebookCountryCmd : ReportDownloadBase, IFacebookCountryCmd
{
IFacebookCountryReportService _facebookCountryReportService;
IFacebookProxyService _FacebookProxyClient;
IAccountCampaignOperation _campaignOperation;
IFacebookCampaignHelper _facebookCampaignHelper;
Platform.Service.Enterprise.AccountChannel _accountChannel = Platform.Service.Enterprise.AccountChannel.Facebook;
Dictionary _countryDic=new Dictionary();
public FacebookCountryCmd(ILogBase logger,
IFacebookCountryReportService facebookCountryReportService, IAccountCampaignOperation campaignOperation,
IFacebookProxyService FacebookProxyClient,IFacebookCampaignHelper facebookCampaignHelper) : base(logger, "facebook")
{
_facebookCountryReportService = facebookCountryReportService;
_FacebookProxyClient = FacebookProxyClient;
_campaignOperation = campaignOperation;
_facebookCampaignHelper = facebookCampaignHelper;
GetAllCountry();
}
public List DateConvertToDB(List reportDatas, List accounts)
{
//*****
}
public void Execute(FacebookCmdOptions opt)
{
//*****
}
public bool SaveReportsToDB(List reportDB)
{
//****
}
protected override FacebookGetAdsInsightRequest GenerateReportRequest(FacebookDailyReportEachRequest req)
{
//****
}
protected override List GenerateResultsByReportResponse(FacebookGetAdsInsightsResponse response, FacebookDailyReportEachRequest req)
{
//***
}
protected override FacebookGetAdsInsightsResponse GetResponseFromRequest(FacebookGetAdsInsightRequest req)
{
//****
}
private bool IfNextPage(FacebookGetAdsInsightsResponse response, ref FacebookGetAdsInsightRequest request)
{
//****
}
}
public interface IFacebookCountryCmd: IReportCmdBase
{
}
///
/// execute download report
///
/// report type define by DB
/// command type
/// report response including all data get from proxy client
public interface IReportCmdBase where RDB : class where CMD:class where RP:class
{
///
/// Save Reports To DB
///
///
///
bool SaveReportsToDB(List reportDB);
///
/// convert report to match datebase define
///
///
///
List DateConvertToDB(List reportDatas, List accounts);
///
/// Execute Download Report :get from proxy client api & save original data to local file & save to DB
///
/// command
void Execute(CMD opt);
}
c# 泛型的应用
标签:file report temp max dir start roc abs ret
原文地址:https://www.cnblogs.com/panpanwelcome/p/9107493.html