/// <summary> Populates the date range from the database for which statistical information exists </summary> /// <param name="StatsDateObject"> Statistical range object to hold the beginning and ending of the statistical information </param> /// <param name="Tracer"> Trace object keeps a list of each method executed and important milestones in rendering</param> /// <returns> TRUE if successful, otherwise FALSE </returns> /// <remarks> This calls the 'SobekCM_Statistics_By_Date_Range' stored procedure <br /><br /> /// This is used by the Statistics_HtmlSubwriter class</remarks> public static bool Populate_Statistics_Dates(Statistics_Dates StatsDateObject, Custom_Tracer Tracer) { if (Tracer != null) { Tracer.Add_Trace("Engine_Database.Populate_Statistics_Dates", "Pulling statistics date information from database"); } try { // Execute this query stored procedure DataSet tempSet = EalDbAccess.ExecuteDataset( DatabaseType, Connection_String, CommandType.StoredProcedure, "SobekCM_Get_Statistics_Dates"); // Reset the values in the object and then set from the database result StatsDateObject.Clear(); StatsDateObject.Set_Statistics_Dates(tempSet.Tables[0]); // No error encountered return true; } catch (Exception ee) { Last_Exception = ee; if (Tracer != null) { Tracer.Add_Trace("Engine_Database.Populate_Statistics_Dates", "Exception caught during database work", Custom_Trace_Type_Enum.Error); Tracer.Add_Trace("Engine_Database.Populate_Statistics_Dates", ee.Message, Custom_Trace_Type_Enum.Error); Tracer.Add_Trace("Engine_Database.Populate_Statistics_Dates", ee.StackTrace, Custom_Trace_Type_Enum.Error); } return false; } }
/// <summary> Refresh the statistics date range by pulling the data back from the database </summary> /// <returns> TRUE if successful, otherwise FALSE </returns> public static bool RefreshStatsDateRange() { try { lock (statsDatesLock) { if (statsDates == null) { statsDates = new Statistics_Dates(); } // Get the data from the database Engine_Database.Populate_Statistics_Dates(statsDates, null); } return true; } catch { return false; } }
/// <summary> Checks to see if there are web logs that need to be processed to the usage statistics </summary> /// <param name="Settings"> Instance-wide settings which may be required for this process </param> public override void DoWork(InstanceWide_Settings Settings) { // If there is no IIS web log set, then do nothing. Don't even need a log here. if (String.IsNullOrEmpty(Settings.Builder.IIS_Logs_Directory)) return; // Just don't run the first day of the month - ensures logs still not being written if (DateTime.Now.Day < 2) return; // Ensure directory exists and is accessible string log_directory = Settings.Builder.IIS_Logs_Directory; try { if (!Directory.Exists(log_directory)) { OnError("CalculateUsageStatisticsModule : IIS web log directory ( " + log_directory + " ) does not exists or is inaccessible", null, null, -1); return; } } catch (Exception ee) { OnError("CalculateUsageStatisticsModule : IIS web log directory ( " + log_directory + " ) does not exists or is inaccessible : " + ee.Message, null, null, -1); return; } // Get the temporary workspace directory, and ensure it exists string temporary_workspace = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Temporary", "CalculateUsageStatisticsModule"); try { if (!Directory.Exists(temporary_workspace)) Directory.CreateDirectory(temporary_workspace); } catch ( Exception ee ) { OnError("CalculateUsageStatisticsModule : Unable to create the temporary workspace ( " + temporary_workspace + " ) : " + ee.Message, null, null, -1); return; } // Clear the temporary workspace try { string[] files = Directory.GetFiles(temporary_workspace); foreach (string thisFile in files) { File.Delete(thisFile); } } catch (Exception ee) { OnError("CalculateUsageStatisticsModule : Error caught clearing existing files from the temporary workspace ( " + temporary_workspace + " ) : " + ee.Message, null, null, -1); return; } // Get (and double check) the web directory if (String.IsNullOrEmpty(Settings.Servers.Application_Server_Network)) { OnError("CalculateUsageStatisticsModule : No application server network setting! Correct ASAP!", null, null, -1); return; } // Ensure directory exists and is accessible string sobekcm_directory = Settings.Servers.Application_Server_Network; try { if (!Directory.Exists(sobekcm_directory)) { OnError("CalculateUsageStatisticsModule : Web application server network directory ( " + sobekcm_directory + " ) does not exists or is inaccessible", null, null, -1); return; } } catch (Exception ee) { OnError("CalculateUsageStatisticsModule : Web application server network ( " + sobekcm_directory + " ) does not exists or is inaccessible : " + ee.Message, null, null, -1); return; } // Determine which year/months already have been analyzed for this instance Statistics_Dates statsDates = new Statistics_Dates(); Engine_Database.Populate_Statistics_Dates(statsDates, null); // Get the list of all IIS web logs string[] log_files = Directory.GetFiles( log_directory, "u_ex*.log"); // If no log files, just return if (log_files.Length == 0) { OnError("CalculateUsageStatisticsModule : No IIS web logs found in directory ( " + log_directory + " )", null, null, -1); return; } // Get the earliest and latest log files from the IIS web logs string earliest = "ZZZZZZZZ"; string latest = "AAAAAAAA"; foreach (string thisFile in log_files) { string thisFileName = Path.GetFileNameWithoutExtension(thisFile); if ( String.Compare(thisFileName, earliest, StringComparison.OrdinalIgnoreCase) < 0 ) earliest = thisFileName; if ( String.Compare(thisFileName, latest, StringComparison.OrdinalIgnoreCase) > 0 ) latest = thisFileName; } // Parse them to determine the earliest and latest year/months int earliest_year; int earliest_month; int latest_year; int latest_month; try { earliest_year = 2000 + Int32.Parse(earliest.Substring(4, 2)); earliest_month = Int32.Parse(earliest.Substring(6, 2)); latest_year = 2000 + Int32.Parse(latest.Substring(4, 2)); latest_month = Int32.Parse(latest.Substring(6, 2)); } catch (Exception ) { OnError("CalculateUsageStatisticsModule : Error parsing the earliest or latest log for year/month (" + earliest + " or " + latest + " )", null, null, -1); return; } // Determine what years/months are missing List<string> year_month = new List<string>(); if (statsDates.Earliest_Year == 2000) { // No stats every collected, so collect them all int curr_year = earliest_year; int curr_month = earliest_month; while ((curr_year < latest_year) || ((curr_year == latest_year) && (curr_month <= latest_month))) { if ((curr_year == DateTime.Now.Year) && (curr_month == DateTime.Now.Month)) break; year_month.Add(curr_year + curr_month.ToString().PadLeft(2, '0')); curr_month++; if (curr_month > 12) { curr_year++; curr_month = 1; } } } else { // No stats every collected, so collect them all int curr_year = earliest_year; int curr_month = earliest_month; while ((curr_year < latest_year) || ((curr_year == latest_year) && (curr_month <= latest_month))) { if ((curr_year == DateTime.Now.Year) && (curr_month == DateTime.Now.Month)) break; if (( curr_year > statsDates.Latest_Year ) || (( curr_year == statsDates.Latest_Year ) && ( curr_month > statsDates.Latest_Month ))) year_month.Add(curr_year + curr_month.ToString().PadLeft(2, '0')); curr_month++; if (curr_month > 12) { curr_year++; curr_month = 1; } } } // If no year/months were added , then no work to do if (year_month.Count == 0) return; // Refresh the items Engine_ApplicationCache_Gateway.RefreshItems(); // Create the processor SobekCM_Stats_Reader_Processor processor = new SobekCM_Stats_Reader_Processor(log_directory, temporary_workspace, sobekcm_directory, year_month); processor.New_Status += processor_New_Status; // Create the thread processor.Process_IIS_Logs(); // Delete the cached file, if one exists try { string temp_directory = Path.Combine(Settings.Servers.Application_Server_Network, "temp"); if (Directory.Exists(temp_directory)) { string cache_xml_file = Path.Combine(temp_directory, "overall_usage.xml"); if (File.Exists(cache_xml_file)) File.Delete(cache_xml_file); } } catch { } // Shoudl emails be sent? if (Settings.Builder.Send_Usage_Emails) { // Load the text string possible_email_body_dir = Path.Combine(Settings.Servers.Application_Server_Network, "design", "extra", "stats"); Usage_Stats_Email_Helper.Set_Email_Body(Path.Combine(possible_email_body_dir, "stats_email_body.txt")); // Send emails for each year/month (in order) foreach (string thisYearMonth in year_month) { int year = Convert.ToInt32(thisYearMonth.Substring(0, 4)); int month = Convert.ToInt32(thisYearMonth.Substring(4, 2)); Send_Usage_Emails(year, month, Settings.Servers.System_Base_URL, Settings.System.System_Name, Settings.Email.Setup.DefaultFromAddress, Settings.Email.Setup.DefaultFromDisplay ); } } }