public static void SendHealthReport() { HealthData healthReport = new HealthData(); //Check to see if health Report has any Kfi Errors with Failure, if so send these to Zudello var f = healthReport.data.queueHealthCheck.Where(i => i.status == "Failure").Count(); if (f > 0) { ErrorFileSender.KfiErrorSender(); } string Token = ZudelloLogin.Login(); Data QData = new Data(); Output output = new Output(); QData.object_type = "HealthCheck"; QData.data = healthReport; output.data = QData; string data = JsonConvert.SerializeObject(output); Console.WriteLine(data); Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, data)); }
public static void RunResponse(ConcurrentDictionary <int, ProccessResponse> successDictionary) { string Token = ZudelloLogin.Login(); List <Zconnections> teams = new List <Zconnections>(); using (var db = new ZudelloContext()) { //Maybe hash the records teams = db.Zconnections.ToList(); } foreach (var update in successDictionary) { int counter = 0; //think there is a bug with duplicate invoices trying to be processed. ProccessResponse pr = successDictionary.Values.FirstOrDefault(); if (pr.Team == null) { continue; } string success = "Succuess"; string msg = pr.Information; Response sendHome = new Response(); sendHome.body = pr.Information; sendHome.status = 200; sendHome.uuid = pr.SyncHistryUuid; if (pr.Successful == false) { sendHome.status = 500; } string body = JsonConvert.SerializeObject(sendHome); try { Console.WriteLine(ZudelloLogin.SendProcessResponse(Token, body, pr.Team)); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Message); Console.WriteLine(ex.InnerException); } } }
public static void KfiErrorSender() { ErrorToZudello KfiError = new ErrorToZudello(); string Token = ZudelloLogin.Login(); Data QData = new Data(); Output output = new Output(); QData.object_type = "KfiError"; QData.data = KfiError; output.data = QData; string data = JsonConvert.SerializeObject(output); Console.WriteLine(data); Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, data)); }
public static async Task AttacheResponseSender() { string token = ZudelloLogin.Login(); ResponseZudello ResponseStatus = new ResponseZudello(); using (var db = new ZudelloContext()) { var queueList = db.Zqueue.Where(i => i.Status == "Success" && (i.ResponseSent == 0)).ToList(); try { foreach (var l in queueList) { ResponseObject successList = new ResponseObject(); dynamic Body = JObject.Parse(l.Body); successList.body = "Success"; successList.uuid = Body.uuid; successList.queueid = l.Id; successList.status = 200; ResponseStatus.data.Add(successList); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Master Data"); } foreach (var data in ResponseStatus.data) { string responseBody = JsonConvert.SerializeObject(data); HttpStatusCode Response = ZudelloLogin.SendProcessResponse(token, responseBody); if (Response == HttpStatusCode.OK) { var updateSent = db.Zqueue.Where(i => i.Id == data.queueid).FirstOrDefault(); updateSent.ResponseSent = 1; db.SaveChanges(); } } db.Dispose(); } }
public static void PushGpDataToZudello() { string Token = ZudelloLogin.Login(); SQLCredentials ConnectionString = new SQLCredentials(); Dictionary <int, string> Connection = ConnectionString.ConnectionStringBuilder(); /**** * * Add threading into here for the foreach loop * will need to chunk based on amount of connections */ //get the query from mappings table foreach (var Mappings in getBody()) { //bool isXml = false; //Get the SQL statement string mySql = Mappings.Value.Body; //Get the Mappings dynamic zudelloObject = Mappings.Value; string SQLQuery = GpTools.RenderToSql(mySql, zudelloObject); //if (mySql.Contains("XML PATH")) isXml = true; #warning make this in config also for process method string cmd = SQLQuery; //Open SQL connection and run the SQL query //Get connection details by Key ID SqlConnection con = new SqlConnection(Connection[Mappings.Value.connection_id]); SqlCommand SelectCommand = new SqlCommand(cmd, con); // SqlDataReader myreader; con.Open(); var jsonResult = new StringBuilder(); var myreader = SelectCommand.ExecuteReader(); if (!myreader.HasRows) { //if there is no data then close connection and next loop con.Close(); continue; } else { while (myreader.Read()) { jsonResult.Append(myreader.GetValue(0).ToString()); } dynamic obj; //Console.WriteLine(jsonResult); obj = JsonConvert.DeserializeObject <ExpandoObject>(jsonResult.ToString()); foreach (var ObjectType in obj) { foreach (var dataValues in ObjectType.Value) { MyQueueObject dataWrapper = new MyQueueObject(); dataWrapper.data = dataValues; string data = JsonConvert.SerializeObject(dataWrapper); Console.WriteLine(data); Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, data)); } } //Update last sync time for this mapping ID using (var db = new ZudelloContext()) { var lastSync = db.Zlastsync.Where(s => s.MappingId == Mappings.Value.Id).FirstOrDefault(); lastSync.LastSync = DateTime.Now.ToString(); db.SaveChanges(); } } con.Close(); } }
public static async Task PushExoDataToZudello() { string Token = ZudelloLogin.Login(); SQLCredentials ConnectionString = new SQLCredentials(); Dictionary <int, string> Connection = ConnectionString.ConnectionStringBuilder(); /**** * * Add threading into here for the foreach loop * will need to chunk based on amount of connections */ //get the query from mappings table Dictionary <int, Zmapping> MappingsBody = getBody(); int numberOfLogicalCores = Environment.ProcessorCount; List <Thread> threads = new List <Thread>(numberOfLogicalCores); int sizeOfOneChunk = (MappingsBody.Count / numberOfLogicalCores) + 1; ConcurrentBag <string> cb = new ConcurrentBag <string>(); ConcurrentBag <int> cbMaps = new ConcurrentBag <int>(); for (int i = 0; i < numberOfLogicalCores; i++) { int ab = i; var thr = new Thread( () => { try { int count = 0; Dictionary <int, Zmapping> MappingsChunked = MappingsBody.Skip(ab * sizeOfOneChunk) .Take(sizeOfOneChunk).ToDictionary(p => p.Key, p => p.Value); foreach (var Mappings in MappingsChunked) { bool isXMl = true; //Get the SQL statement string mySql = Mappings.Value.Body; //Get the Mappings dynamic zudelloObject = Mappings.Value; string SQLQuery = ExoTools.RenderToSql(mySql, zudelloObject); #warning make this in config also for process method string cmd = SQLQuery; //Open SQL connection and run the SQL query //Get connection details by Key ID SqlConnection con = new SqlConnection(Connection[Mappings.Value.connection_id]); SqlCommand SelectCommand = new SqlCommand(cmd, con); // SqlDataReader myreader; con.Open(); if (cmd.ToLower().Contains("for xml")) { isXMl = true; } if (String.IsNullOrEmpty(cmd)) { continue; } var jsonResult = new StringBuilder(); var xmlConvert = new StringBuilder(); //SelectCommand.CommandText = cmd; var myreader = SelectCommand.ExecuteReader(); if (!myreader.HasRows) { //if there is no data then close connection and next loop con.Close(); con.Dispose(); continue; } else { while (myreader.Read()) { jsonResult.Append(myreader.GetValue(0).ToString()); } //Console.WriteLine(jsonResult); dynamic obj = ""; string ConvertedXml = ""; if (isXMl == true) { ConvertedXml = ExoTools.ZudelloXMLConverter(jsonResult.ToString()); // string ConvertedXml = ExoTools.ZudelloXMLConverter(jsonResult.ToString()); obj = JsonConvert.DeserializeObject <ExpandoObject>(ConvertedXml); } else { obj = JsonConvert.DeserializeObject <ExpandoObject>(jsonResult.ToString()); } foreach (var ObjectType in obj) { //not sure why .Value is causing error but not affecting build foreach (var dataValues in ObjectType.Value) { string data = ""; if (isXMl == true) { data = JsonConvert.SerializeObject(dataValues); } else { MyQueueObject dataWrapper = new MyQueueObject(); dataWrapper.data = dataValues; data = JsonConvert.SerializeObject(dataWrapper); } // add to concurrent bag cb.Add(data); count++; // Console.WriteLine(data); } } cbMaps.Add(Mappings.Value.Id); } con.Close(); con.Dispose(); } } catch (Exception ex) { } } ); threads.Add(thr); } foreach (var thread in threads) { thread.Start(); } foreach (var thread in threads) { thread.Join(); } threads.Clear(); int sizeOfOneChunkQueue = (cb.Count / numberOfLogicalCores) + 1; for (int x = 0; x < numberOfLogicalCores; x++) { int abx = x; var SendToQueuethr = new Thread( () => { try { int count = 0; List <string> queuedBag = cb.Skip(abx * sizeOfOneChunkQueue) .Take(sizeOfOneChunkQueue).ToList(); foreach (var Mappings in queuedBag) { Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, Mappings)); } } catch (Exception ex) { } }); threads.Add(SendToQueuethr); } foreach (var qthread in threads) { qthread.Start(); } foreach (var qthread in threads) { qthread.Join(); } /* * List<Task> bagConsumeTasks = new List<Task>(); * int itemsInBag = 0; * while (!cb.IsEmpty) * { * bagConsumeTasks.Add(Task.Run(() => * { * string item; * if (cb.TryTake(out item)) * { * * Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, item)); * itemsInBag++; * } * })); * } */ // Task.WaitAll(bagConsumeTasks.ToArray()); foreach (int id in cbMaps) { using (var db = new ZudelloContext()) { var lastSync = db.Zlastsync.Where(s => s.MappingId == id).FirstOrDefault(); lastSync.LastSync = DateTime.Now.ToString(); db.SaveChanges(); db.DisposeAsync(); } } }
public static async Task AddtoQueue() { //Check Mappings ZudelloLogin.GetZudelloMappings().Wait(); string Token = ZudelloLogin.Login(); List <Zconnections> teams = new List <Zconnections>(); using (var db = new ZudelloContext()) { //Maybe hash the records teams = db.Zconnections.ToList(); await db.DisposeAsync(); } int numberOfLogicalCores = Environment.ProcessorCount; List <Thread> threads = new List <Thread>(numberOfLogicalCores); int sizeOfOneChunk = (teams.Count / numberOfLogicalCores) + 1; ConcurrentDictionary <int, dynamic> cb = new ConcurrentDictionary <int, dynamic>(); ConcurrentDictionary <dynamic, LastSync> lastSyncBag = new ConcurrentDictionary <dynamic, LastSync>(); for (int i = 0; i < numberOfLogicalCores; i++) { int ab = i; var thr = new Thread( () => { int count = 0; List <Zconnections> teamChunked = teams.Skip(ab * sizeOfOneChunk) .Take(sizeOfOneChunk).ToList(); foreach (var team in teamChunked) { try { dynamic ZudelloTeam = JsonConvert.DeserializeObject <ExpandoObject>(team.ZudelloCredentials); string InvoiceList = ZudelloLogin.CallZudelloDocs(Token, ZudelloTeam.team, team.Id); dynamic ToSync = JObject.Parse(InvoiceList); foreach (var data in ToSync.data) { try { string myDataString = data.ToString(); // Console.WriteLine(Id.uuid + "," + Id.items); Console.WriteLine(data); ExoMappingAndDB mapId = GetMappingAndDatabase(data.document.docType.ToString(), data.document.connectionUuid.ToString()); int?rowId = null; if (mapId != null) { rowId = SaveToDBQueue(myDataString, mapId.mappingID, "Waiting", mapId); } var validate = Validations.ExoSupplierInvoice(myDataString, mapId); if (validate.CreateSupplier == true) { //HAVE to hard code type for the supplier and inventory? mapId = GetMappingAndDatabase(data.document.supplier.docType.ToString(), data.document.connectionUuid.ToString()); // Check if already in the queue if (InQueue(mapId.mappingID, data.document.supplier.code.ToString()) == false) { SaveToDBQueue(data.document.supplier.ToString(), mapId.mappingID, "Waiting", mapId, rowId); } } if (validate.CreateInventory == true) { foreach (var itemData in data.document.lines) { mapId = GetMappingAndDatabase(itemData.docType.ToString(), data.document.connectionUuid.ToString()); if (validate.InventoryToCreate.Contains(itemData.item.sku.ToString())) { // Check if already in the queue if (InQueue(mapId.mappingID, itemData.item.sku.ToString()) == false) { SaveToDBQueue(itemData.ToString(), mapId.mappingID, "Waiting", mapId, rowId); } } } } LastSync lastSync = new LastSync(); lastSync.teamId = team.Id; lastSync.lastUpdated = data.created_at.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"); lastSyncBag.TryAdd(data.uuid, lastSync); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } catch (Exception ex) { //Write to loggings tables Console.WriteLine(ex.Message); } } } ); threads.Add(thr); } foreach (var thread in threads) { thread.Start(); } foreach (var thread in threads) { thread.Join(); } var dataList = lastSyncBag.Values.ToList(); foreach (var lastSync in dataList) { using (var db = new ZudelloContext()) { var updateLastSync = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.DocType == "CallZudelloDocs" && c.connection_id == lastSync.teamId select a).FirstOrDefault(); if (DateTime.Parse(updateLastSync.LastSync) > DateTime.Parse(lastSync.lastUpdated)) { } else { updateLastSync.LastSync = lastSync.lastUpdated; db.SaveChanges(); } db.Dispose(); } } }
public static void AddtoQueue() { //Check Mappings ZudelloLogin.GetZudelloMappings(); string Token = ZudelloLogin.Login(); string InvoiceList = ZudelloLogin.CallZudelloDocs(Token); dynamic ToSync = JObject.Parse(InvoiceList); foreach (var data in ToSync.data) { try { string myDataString = data.ToString(); // Console.WriteLine(Id.uuid + "," + Id.items); Console.WriteLine(data); GpMappingAndDatabase mapId = GetMappingAndDatabase(data.document.docType.ToString(), data.document.connectionUuid.ToString()); if (mapId != null) { SaveToDBQueue(myDataString, mapId.mappingID, "Waiting", mapId); } var validate = GpValidations.SupplierInvoiceValidate(myDataString, mapId); if (validate.CreateSupplier == true) { //HAVE to hard code type for the supplier and inventory? mapId = GetMappingAndDatabase(data.document.supplier.docType.ToString(), data.document.connectionUuid.ToString()); // Check if already in the queue if (InQueue(mapId.mappingID, data.document.supplier.code.ToString()) == false) { SaveToDBQueue(data.document.supplier.ToString(), mapId.mappingID, "Waiting", mapId); } } if (validate.CreateInventory == true) { foreach (var itemData in data.document.lines) { mapId = GetMappingAndDatabase(itemData.docType.ToString(), data.document.connectionUuid.ToString()); if (validate.InventoryToCreate.Contains(itemData.item.sku.ToString())) { // Check if already in the queue if (InQueue(mapId.mappingID, itemData.item.sku.ToString()) == false) { SaveToDBQueue(itemData.ToString(), mapId.mappingID, "Waiting", mapId); } } } } using (var db = new ZudelloContext()) { var updateLastSync = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.DocType == "CallZudelloDocs" select a).FirstOrDefault(); //var dateParse = DateTime.ParseExact(data.created_at.ToString(), "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'", CultureInfo.InvariantCulture); //string zudelloDate = data.created_at; // dateParse = dateParse string pr = data.created_at.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"); updateLastSync.LastSync = pr; db.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
public static async Task PushDataToZudello() { string Token = ZudelloLogin.Login(); foreach (var Mappings in getBody()) { string myReplacement = ""; string myObjType = Mappings.Value.DocType; dynamic myObj = JsonConvert.DeserializeObject <ExpandoObject>(Mappings.Value.Body); string myLinkValue = myObj.LINK.ToString(); string myQuery = myObj.HDR_QUERY.ToString(); if (myQuery.Contains("{LAST_ID}")) { try { using (var db = new ZudelloContext()) { var lastSyncValue = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.Id == Mappings.Value.Id select a).FirstOrDefault(); myQuery = myQuery.Replace("{LAST_ID}", lastSyncValue.lastID.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (myQuery.Contains("{LAST_SYNC}")) { try { using (var db = new ZudelloContext()) { var lastSyncValue = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.Id == Mappings.Value.Id select a).FirstOrDefault(); DateTime MyQueryDate = DateTime.Parse(lastSyncValue.LastSync); myQuery = myQuery.Replace("{LAST_SYNC}", MyQueryDate.ToString("yyyy-MM-dd HHmmss")); } } catch { } } var headerDs = AttacheFetchData.GetDataSet(myQuery); try { myReplacement = myObj.REPLACE_ME.ToString(); } catch { } bool LineQuery = false; if (myLinkValue != "") { LineQuery = true; } foreach (DataTable dt in headerDs.Tables) { foreach (DataRow dr in dt.Rows) { Data QData = new Data(); Output output = new Output(); QData.object_type = myObjType; //dynamic obj = new DynamicClass(fields); Dictionary <string, object> temp = new Dictionary <string, object>(); foreach (DataColumn dc in dt.Columns) { dynamic Colvalue; if (dr[dc.ColumnName] is String) { Colvalue = dr[dc.ColumnName].ToString(); Colvalue = Colvalue.Trim(); } else { Colvalue = dr[dc.ColumnName]; } temp.Add(dc.ColumnName, Colvalue); } QData.data = temp; output.data = QData; if (LineQuery == true) { var query = temp[myLinkValue]; // output.data.data = temp; string myQuery2 = myObj.LINE_QUERY.ToString().Replace("{0}", query.ToString()); var lineDs = GetDataSet(myQuery2); List <object> mArray = new List <object>(); foreach (DataTable lt in lineDs.Tables) { foreach (DataRow lr in lt.Rows) { Dictionary <string, object> lstemp = new Dictionary <string, object>(); foreach (DataColumn lc in lt.Columns) { dynamic lineValue; if (lr[lc.ColumnName] is String) { lineValue = lr[lc.ColumnName].ToString(); lineValue = lineValue.Trim(); } else { lineValue = lr[lc.ColumnName]; } lstemp.Add(lc.ColumnName, lineValue); } mArray.Add(lstemp); } } Dictionary <int, object> myDic = new Dictionary <int, object>(); object REPLACEME = new object(); string data = JsonConvert.SerializeObject(output); dynamic obj = JsonConvert.DeserializeObject <ExpandoObject>(data); obj.data.data.REPLACEME = mArray; string lineData = JsonConvert.SerializeObject(obj); lineData = lineData.Replace("REPLACEME", myReplacement); Console.WriteLine(lineData); //If this record already exisits in the hashing table then continue with the loop. if (HashingRecords.Hash(lineData, Mappings.Value.Id) == true) { continue; } Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, lineData)); using (var db = new ZudelloContext()) { var updateLastSync = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.Id == Mappings.Value.Id select a).FirstOrDefault(); updateLastSync.LastSync = DateTime.UtcNow.ToString(); try { int myInternalId = Convert.ToInt32(temp[myLinkValue]); updateLastSync.lastID = myInternalId; } catch { } db.SaveChanges(); } } else { string data = JsonConvert.SerializeObject(output); Console.WriteLine(data); //If this record already exisits in the hashing table then continue with the loop. if (HashingRecords.Hash(data, Mappings.Value.Id) == true) { continue; } Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, data)); using (var db = new ZudelloContext()) { var updateLastSync = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.Id == Mappings.Value.Id select a).FirstOrDefault(); updateLastSync.LastSync = DateTime.UtcNow.ToString(); db.SaveChanges(); } } } } } }