Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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 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();
                }
            }
        }
Ejemplo n.º 5
0
        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();
                            }
                        }
                    }
                }
            }
        }