Пример #1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Generates the key with the temporal and computer specific attributes
        /// </summary>
        protected StringBuilder GenerateKey()
        {
            //-----a----- Start building the encrypted string
            StringBuilder tokenToEncrypt = new StringBuilder();

            tokenToEncrypt.Append(MGLEncryption.GetSalt(1));

            //-----b----- Double check that the anonID cookie has been set and set it again if not... (this should never not have happened as it is called first OnInit in the MGLBasePage)
            SetAnonIDCookie();

            //-----c----- The AnonID is a GUID - always 36 characters
            string tempValue = DefaultAnonID;

            if (Request.Cookies["AnonID"] != null)
            {
                tempValue = Request.Cookies["AnonID"].Value;
            }
            tokenToEncrypt.Append(tempValue);

            //-----d----- Add some padding
            tokenToEncrypt.Append(MGLEncryption.GetSalt(1));

            //-----e----- The date time will be of the form yyyy-mm-dd hh:mm:ss - and will always be dd and mm (not d and m for e.g. 01)
            tokenToEncrypt.Append(DateTimeInformation.FormatDatabaseDate(DateTime.Now, true, true));
            tokenToEncrypt.Append(MGLEncryption.GetSalt(1));

            //-----f----- Do the encryption itself
            StringBuilder authKey = MGLEncryption.Encrypt(tokenToEncrypt);

            //-----g----- And lastly, lets make this key HTML secure ...
            authKey = MGLEncryption.HTMLifyString(authKey);

            return(authKey);
        }
Пример #2
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     14-Oct-13 - New method to log the page request information in the database ...
        /// </summary>
        public static void LogPageRequestInDatabase(ConfigurationInfo ci, string applicationName, string aspSessionID, string uniqueSessionID,
                                                    string pageURL, DateTime pageRequestTime, double serverProcessingMS, int currentUserID, string ipAddress)
        {
            bool success = false;

            DatabaseWrapper dbInfo = null;

            try {
                //_____ Check for a null configurationInfo or dbConInfo object
                if (ci == null || ci.DbConInfo.NAME == null)
                {
                    Logger.LogError(200, "Error inserting the PageRequest log into the database - null configurationInfo found, probably because of an application reset or session timeout.");
                }
                else
                {
                    // 3-Mar-2016 - ignore specific IP addresses (e.g. robots like the UptimeRobot), as these are artificially bloating the logs with limited utility!
                    if (ipAddress == null || AddressesToIgnore.Contains(ipAddress) == false)
                    {
                        //_____ Check for s***e data and make it blank if so, so it doesn't kill the database ...
                        applicationName = (applicationName == null) ? "" : applicationName;
                        aspSessionID    = (aspSessionID == null) ? "" : aspSessionID;
                        uniqueSessionID = (uniqueSessionID == null) ? "" : uniqueSessionID;
                        pageURL         = (pageURL == null) ? "" : pageURL;

                        //_____ Connect to the database ...
                        dbInfo = new DatabaseWrapper(ci);
                        dbInfo.Connect();

                        //_____ Create the table if it does not already exist
                        string createTableSQL = @"
                    CREATE TABLE " + logTN + @" (
                        ID int NOT NULL Auto_Increment, PRIMARY KEY(ID),
                        Application_Name VARCHAR(255), INDEX Application_Name(Application_Name),
                        Session_ID_ASP VARCHAR(50), INDEX Session_ID_ASP(Session_ID_ASP),
                        Session_ID_Unique VARCHAR(50), INDEX Session_ID_Unique(Session_ID_Unique),
                        Page_Name VARCHAR(255), INDEX Page_Name(Page_Name),
                        Page_URL LONGTEXT,
                        Page_Request_Date DATETIME, INDEX Page_Request_Date(Page_Request_Date),
                        Server_Render_Speed DOUBLE, INDEX Server_Render_Speed(Server_Render_Speed),
                        Current_User_ID INT, INDEX Current_User_ID(Current_User_ID),
                        IP_Address	VARCHAR(20),    INDEX IP_Address(IP_Address)
                    ) ENGINE=MyISAM;
                ";

                        if (dbInfo.TableExists(logTN) == false)
                        {
                            dbInfo.ExecuteSQL(createTableSQL, ref success);
                        }

                        //_____ Parse the page name
                        string pageName = "";
                        {
                            // remove the ?...... page params
                            string[] bits1 = pageURL.Split(new string[] { "?", "&" }, StringSplitOptions.None);
                            string[] bits2 = bits1[0].Split(new string[] { "/", "\\" }, StringSplitOptions.None);
                            pageName = bits2[bits2.Length - 1];
                        }

                        //_____ Build the SQL and run the insert ...
                        StringBuilder sql = new StringBuilder();
                        sql.Append("INSERT INTO " + logTN
                                   + " (Application_Name, Session_ID_ASP, Session_ID_Unique, Page_Name, Page_URL, Page_Request_Date, Server_Render_Speed, Current_User_ID, IP_Address ) VALUES (");
                        sql.Append(DataUtilities.Quote(applicationName) + ", ");
                        sql.Append(DataUtilities.Quote(aspSessionID) + ", ");
                        sql.Append(DataUtilities.Quote(uniqueSessionID) + ", ");
                        sql.Append(DataUtilities.Quote(pageName) + ", ");
                        sql.Append(DataUtilities.Quote(pageURL) + ", ");
                        sql.Append(DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(pageRequestTime, true, true)) + ", ");
                        sql.Append(serverProcessingMS + ", ");
                        sql.Append(currentUserID + ", ");
                        sql.Append(DataUtilities.DatabaseifyString(ipAddress, false));

                        sql.Append(");");

                        int numInserts = dbInfo.ExecuteSQL(sql.ToString(), ref success);

                        if (success == false)
                        {
                            Logger.LogError(201, "Error inserting the PageRequest log into the database - check the detailed log for details.");
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.LogError(202, "Error inserting the PageRequest log into the database: " + ex.ToString());
            } finally {
                if (dbInfo != null)
                {
                    dbInfo.Disconnect();
                }
            }
        }
Пример #3
0
        //--------------------------------------------------------------------------------------------------------------------------------------------------------------
        public static void PerformanceTestMySQLWrite()
        {
            Logger.LogSubHeading("Testing MySQL!");
            bool success = false;

            // Now lets connect to the MySQL db
            Logger.Log("Connecting to the database...");
            DatabaseWrapper dbInfo = new DatabaseWrapper(new DatabaseConnectionInfo("localhost", "mysql", "redis_comparison",
                                                                                    SecureStringWrapper.Encrypt("forum"), SecureStringWrapper.Encrypt(TestParameters.RedisAuth), 3306));

            dbInfo.Connect();

            if (TestParameters.DoWrite == true)
            {
                bool tnExists = dbInfo.TableExists(TestObj.ObjectName);

                // Drop the table if it exists already ...
                bool tnDeleted = dbInfo.DeleteTable(TestObj.ObjectName);

                // And then recreate it ...
                // 22-Aug-2016 - Remove the boolean index to mirror the Redis implementation - Index TestBool(TestBool),
                bool tnCreated = dbInfo.CreateTable(TestObj.ObjectName, @"
                ID bigint unsigned,	    Primary Key(ID),
                TestInt int,                    Index TestInt(TestInt),
                TestLong bigint,        Index TestLong(TestLong),
                TestDouble double,      Index TestDouble(TestDouble),
                TestBool bool,          
                TestStr Varchar(20),    Index TestStr(TestStr),
                TestDT DateTime,        Index TestDT(TestDT)
            ", false);
            }

            Logger.Log("Writing data");
            DateTime writeStart = DateTime.Now;

            if (TestParameters.DoWrite == true)
            {
                int writeCount = 0;
                foreach (object o in TestParameters.RandomObjects)
                {
                    TestObj rto = (TestObj)o;

                    StringBuilder sql = new StringBuilder();

                    sql.Append("INSERT INTO " + TestObj.ObjectName + " (ID, TestInt, TestLong, TestDouble, TestBool, TestStr, TestDT) VALUES (");
                    sql.Append(rto.ID + ",");
                    sql.Append(rto.TestInt + ",");
                    sql.Append(rto.TestLong + ",");
                    sql.Append(rto.TestDouble + ",");
                    sql.Append(rto.TestBool + ",");
                    sql.Append(DataUtilities.Quote(rto.TestStr) + ",");
                    sql.Append(DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(rto.TestDT, true, true)));
                    sql.Append(");");
                    dbInfo.ExecuteSQL(sql.ToString(), ref success);

                    Logger.Log(++writeCount, 100, TestParameters.RandomObjects.Count);
                }
            }
            Logger.Log("");
            Logger.Log("Writing completed.");
            TestParameters.WriteMySQL = DateTime.Now.Subtract(writeStart);

            Logger.Log("Disconnect from the database.");
            dbInfo.Disconnect();
        }
Пример #4
0
        //--------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     So this loads the full object for each search match found ... but does not persist those as that would likely kill the memory allocation
        /// </summary>
        /// <param name="chunkSize"></param>
        /// <param name="startIndex"></param>
        public static void PerformanceTestMySQLThreadBlock(int chunkSize, int startIndex)
        {
            Logger.LogSubHeading("Testing MySQL!");
            //bool success = false;

            int i = startIndex;

            if ((startIndex + chunkSize) >= TestParameters.PatternsToSearchFor.Count)
            {
                chunkSize = TestParameters.PatternsToSearchFor.Count - startIndex;
            }

            Logger.Log("Starting chunk with index " + startIndex + " and size " + chunkSize + " ...");


//            HashSet<ulong> ids = new HashSet<ulong>();

            // Now lets connect to the MySQL db
            Logger.Log("Connecting to the database...");
            DatabaseWrapper dbInfo = new DatabaseWrapper(new DatabaseConnectionInfo("localhost", "mysql", "redis_comparison",
                                                                                    SecureStringWrapper.Encrypt("forum"), SecureStringWrapper.Encrypt(TestParameters.RedisAuth), 3306));

            dbInfo.Connect();


            Logger.Log("Reading data");
            StringBuilder log      = new StringBuilder();
            StringBuilder queryTxt = new StringBuilder();

            //long searchIndex = startIndex;

            for (i = startIndex; i < (startIndex + chunkSize); i++)
            {
                List <DatabaseSearchPattern> rsps = TestParameters.PatternsToSearchFor[i];

                /////////////
                //bool storeQuery = false;

                StringBuilder sql          = new StringBuilder();
                StringBuilder searchParams = new StringBuilder();


                // And build the query here ...
                sql.Append("SELECT ID, TestInt, TestLong, TestDouble, TestBool, TestStr, TestDT FROM " + TestObj.ObjectName + " WHERE ");
                foreach (DatabaseSearchPattern rsp in rsps)
                {
                    if (searchParams.Length > 0)
                    {
                        searchParams.Append(" AND ");
                    }

                    if (rsp.SearchType == SearchType.PrimaryKey)
                    {
                        searchParams.Append(" " + rsp.Parameter + "=" + rsp.Score);
                    }
                    else if (rsp.SearchType == SearchType.Text)
                    {
                        searchParams.Append(" " + rsp.Parameter + " LIKE " + DataUtilities.Quote(rsp.PatternStartsWith(RedisWrapper.TextIndexLength) + "%"));
                        //searchParams.Append(" " + rsp.Parameter + " LIKE " + DataUtilities.Quote(rsp.Pattern.Substring(0, 1) + "%"));
                    }
                    else if (rsp.SearchType == SearchType.Bool)
                    {
                        searchParams.Append(" " + rsp.Parameter + "=" + rsp.Score);
                    }
                    else if (rsp.SearchType == SearchType.DateTime)
                    {
                        // could also add switches on ==, >= and <=
                        if (rsp.SearchComparisonExpression == SearchComparisonExpression.Equivalent)
                        {
                            //if (rsp.ScoreMin == rsp.ScoreMax) {                                    // an exact date and time
                            searchParams.Append(rsp.Parameter + " = "
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime(rsp.Score), true, true)));
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeLessThanOrEqualTo)
                        {
                            //} else if (rsp.ScoreMin == DateTime.MinValue.Ticks) {
                            // less than or equal to ...
                            //                            searchParams.Append(rsp.Parameter + " <= "
                            //                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)Math.Round(rsp.ScoreMax)), true, true)));
                            // Note that we want to search inclusively.  Which means we need to search less than the specified max day PLUS A DAY
                            // equivalent to <= 2016-04-20 23:59:59 to ensure that the day itself is included ....

                            //DateTime dt = new DateTime((long)Math.Round(rsp.ScoreMax));
                            DateTime dt = new DateTime((long)rsp.ScoreMax);

                            searchParams.Append(rsp.Parameter + " <= "
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(dt, true, true)));

                            /////////////////////////////////////
                            //string checkTHIS = "";
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeGreaterThanOrEqualTo)
                        {
                            //} else if (rsp.ScoreMax == DateTime.MaxValue.Ticks) {           // greater than or equal to ...

                            //DateTime dt = new DateTime((long)Math.Round(rsp.ScoreMin));
                            DateTime dt = new DateTime((long)rsp.ScoreMin);

                            searchParams.Append(rsp.Parameter + " >= "
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(dt, true, true)));

                            /////////////////////////////////////
                            //string checkTHIS = rsp.AsText();
                            //checkTHIS = rsp.AsText();
                            //storeQuery = true;
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeBetween)
                        {
                            //                        } else {                                                                            // Must be a real between

                            // Note that we want to search inclusively.  Which means we need to search less than the specified max day PLUS A DAY
                            // equivalent to <= 2016-04-20 23:59:59 to ensure that the day itself is included ....
                            searchParams.Append(rsp.Parameter + " BETWEEN "
                                                //+ DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)Math.Round(rsp.ScoreMin)), true, true))
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)rsp.ScoreMin), true, true))
                                                + " AND "
                                                //+ DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)Math.Round(rsp.ScoreMax)), true, true)));
                                                + DataUtilities.Quote(DateTimeInformation.FormatDatabaseDate(new DateTime((long)rsp.ScoreMax), true, true)));
                        }
                        else
                        {
                            string ohFuck = "";
                        }
                    }
                    else if (rsp.SearchType == SearchType.Score)
                    {
                        if (rsp.SearchComparisonExpression == SearchComparisonExpression.Equivalent)
                        {
                            //if (rsp.ScoreMin == rsp.ScoreMax) {                                    // an exact value
                            searchParams.Append(rsp.Parameter + " = " + rsp.ScoreMin.ToString("0." + new string('#', 339)));
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeLessThanOrEqualTo)
                        {
                            //} else if (rsp.ScoreMin == Double.MinValue) {           // less than or equal to ...
                            searchParams.Append(rsp.Parameter + " <= " + rsp.ScoreMax.ToString("0." + new string('#', 339)));
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeGreaterThanOrEqualTo)
                        {
                            //} else if (rsp.ScoreMax == Double.MaxValue) {           // greater than or equal to ...
                            searchParams.Append(rsp.Parameter + " >= " + rsp.ScoreMin.ToString("0." + new string('#', 339)));
                        }
                        else if (rsp.SearchComparisonExpression == SearchComparisonExpression.RangeBetween)
                        {
                            //} else {                                                                            // Must be a real between
                            searchParams.Append(rsp.Parameter + " BETWEEN "
                                                + rsp.ScoreMin.ToString("0." + new string('#', 339)) + " AND "
                                                + rsp.ScoreMax.ToString("0." + new string('#', 339)));
                        }
                        else
                        {
                            string ohFuck = "";
                        }
                    }
                }
                sql.Append(searchParams);
                sql.Append(";");

                List <string[]> sqlData = dbInfo.GetDataList(sql.ToString());

                int objCount = 0;
                if (sqlData != null)
                {
                    objCount = sqlData.Count;

                    foreach (string[] row in sqlData)
                    {
                        ulong id = 0;
                        ulong.TryParse(row[0], out id);

                        int testInt = 0;
                        int.TryParse(row[1], out testInt);

                        long testLong = 0;
                        long.TryParse(row[2], out testLong);

                        double testDouble = 0;
                        double.TryParse(row[3], out testDouble);

                        bool testBool = (row[4] != null && row[4].Equals("1") == true) ? true : false;

                        // string is obvs good to go!!

                        DateTime testDT = DateTimeInformation.FormatDateTime(row[6]);

                        TestObj rto = new TestObj(id, testInt, testLong, testDouble, testBool, row[5], testDT);

                        // Keep adding the objects, as long as they have not already been added and the MaxNumObjects is not exceeded - probably 1m
                        // Now append the ids to the global list ...
                        lock (IDs) {
                            if (IDs.Contains((uint)rto.ID) == false)
                            {
                                IDs.Add((uint)rto.ID);
                                TestObjects.Add(rto);
                            }
                        }
                    }
                }


                // Do the printing of all the specific queries only if DoDebug is set to true ....
                if (RedisWrapper.DoDebug == true)
                {
                    // 19-Aug-2016 - F**k a duck - DO NOT SORT THE QUERIES HERE
                    // We need to make sure not to sort the master list as it potentially confuses the calculations in the search objects method ...

                    queryTxt.Clear();
                    foreach (DatabaseSearchPattern rsp in rsps)
                    {
                        queryTxt.Append(" " + rsp.AsText());
                    }

                    log.Append("\n" + "Search " + i + " found " + objCount + " objects. Query Text: " + queryTxt);
                }



                //foreach (RedisSearchPattern rsp in rsps) {
                //    queryTxt.Append(" " + rsp.AsText());
                //}

                ////log.Append("\n" + "Search " + (++searchIndex) + " found " + objCount + " objects. Query Text: " + queryTxt);
                //log.Append("\n" + "Search " + (++searchIndex) + " found " + objCount + " objects. Query Text: " + queryTxt + "\t" + sql);

                ////////////////////////////
                //if (storeQuery == true && (sqlData == null || sqlData.Count == 0)) {
                //    log.Append("\t" + sql);
                //}


                if (TestParameters.UseThreading == true)
                {
                    DNThreadManager.IncrementNumIterationsCompleted();
                }
                else
                {
                    Logger.Log(i, 10, TestParameters.PatternsToSearchFor.Count);
                }
            }


            Logger.Log("");
            Logger.Log(log.ToString());

            // And lastly, lets disconnect from the database!
            dbInfo.Disconnect();
        }