public void AddReportTimeSeriesInputsTable(System.Data.DataSet dataSet, string tableName, ReportRequestInputs inputs) { Log.DebugFormat("AddReportTimeSeriesInputsTable {0}", tableName); if (dataSet.Tables.Contains(tableName)) { return; } DataTable table = new DataTable(tableName); dataSet.Tables.Add(table); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Guid", typeof(Guid)); table.Columns.Add("Label", typeof(string)); table.Columns.Add("IsMaster", typeof(bool)); if (inputs == null) { return; } foreach (TimeSeriesReportRequestInput timeseriesInput in inputs.TimeSeriesInputs) { DataRow row = table.NewRow(); row["Name"] = timeseriesInput.Name; row["Guid"] = timeseriesInput.UniqueId; row["Label"] = timeseriesInput.Label; row["IsMaster"] = timeseriesInput.IsMaster; table.Rows.Add(row); } }
public void AddPointToTable(DataTable groupBy, DataTable pointsTable, DateTimeOffset timestamp, double value, string displayValue) { int weekNumber = -1; foreach (DataRow groupRow in groupBy.Rows) { DateTimeOffset startTime = (DateTimeOffset)groupRow["StartTime"]; DateTimeOffset endTime = (DateTimeOffset)groupRow["EndTime"]; if ((startTime <= timestamp) && (timestamp < endTime)) { weekNumber = (int)groupRow["Week"]; break; } } if (weekNumber == -1) { Log.DebugFormat("AddPointToTable - weekNumber is -1 which is invalid so SKIP, {0}, {1}", timestamp, value); return; } DataRow row = pointsTable.NewRow(); row["NoGroupBy"] = 0; row["Year"] = timestamp.Year; row["WaterYear"] = (timestamp.Month >= _Common.GetWaterYearMonth()) ? timestamp.Year : timestamp.Year - 1; row["Month"] = timestamp.Month; row["Day"] = timestamp.Day; row["Week"] = weekNumber; row["TimeStamp"] = timestamp; row["TimeStampDouble"] = timestamp.DateTime.ToOADate(); row["Value"] = value; row["DisplayValue"] = displayValue; pointsTable.Rows.Add(row); }
public static int GetNumberOfHistoricalPointsAvailable(string urlPrefix, string locationIdentifier, int httpRepeatCallLimit, int httpCallTimeoutInSeconds) { string path = urlPrefix; path += System.Net.WebUtility.UrlEncode(locationIdentifier); path += "&startindex=0&limit=0"; string answer = CallHttpClient(path, httpRepeatCallLimit, httpCallTimeoutInSeconds); if (!string.IsNullOrEmpty(answer)) { dynamic dynData = JsonConvert.DeserializeObject(answer); int numberOfPoints = dynData.numberMatched; Log.DebugFormat("There are {0} historical points in location {1}", numberOfPoints, locationIdentifier); return(numberOfPoints); } return(-1); }
public DateTimeOffsetInterval FindTimeSeriesTimeRange(Guid timeseriesUniqueId) { List <TimeSeriesPoint> points = FindAnnualCountPoints(timeseriesUniqueId); if (points.Count == 0) { return(new DateTimeOffsetInterval()); } TimeSpan offset = points[0].Timestamp.Offset; int binAdjust = _Common.GetBinAdjustment(timeseriesUniqueId); DateTimeOffset FirstYear = new DateTimeOffset(points[0].Timestamp.Year + binAdjust, 1, 1, 0, 0, 0, offset); DateTimeOffset LastYear = new DateTimeOffset(points[points.Count - 1].Timestamp.Year + binAdjust, 1, 1, 0, 0, 0, offset); Log.DebugFormat("Found first point is in Year = {0}, last point is in Year = {1}", FirstYear, LastYear); points = FindDailyCountPoints(timeseriesUniqueId, FirstYear, FirstYear.AddYears(1).AddDays(1)); if (points.Count == 0) { return(new DateTimeOffsetInterval()); } DateTimeOffset firstTime = FirstNonZeroPoint(points).Timestamp; DateTimeOffset queryFromTime = new DateTimeOffset(firstTime.Year, firstTime.Month, firstTime.Day, 0, 0, 0, offset); points = _Common.GetTimeSeriesPoints(timeseriesUniqueId, queryFromTime.AddDays(binAdjust), queryFromTime.AddDays(binAdjust + 1)); if (points.Count == 0) { return(new DateTimeOffsetInterval()); } firstTime = points[0].Timestamp; Log.DebugFormat("Found first point at time = {0}", firstTime.ToString(Common._DateFormat)); points = FindDailyCountPoints(timeseriesUniqueId, LastYear, LastYear.AddYears(1).AddDays(1)); if (points.Count == 0) { return(new DateTimeOffsetInterval(firstTime, null)); } DateTimeOffset lastTime = LastNonZeroPoint(points).Timestamp; queryFromTime = new DateTimeOffset(lastTime.Year, lastTime.Month, lastTime.Day, 0, 0, 0, offset); points = _Common.GetTimeSeriesPoints(timeseriesUniqueId, queryFromTime.AddDays(binAdjust), queryFromTime.AddDays(binAdjust + 1)); if (points.Count == 0) { return(new DateTimeOffsetInterval(firstTime, null)); } lastTime = points[points.Count - 1].Timestamp; Log.DebugFormat("Found last point at time = {0}", lastTime.ToString(Common._DateFormat)); DateTimeOffsetInterval interval = new DateTimeOffsetInterval(firstTime, lastTime); return(interval); }
public static string CallHttpClient(string path, double timeoutInSeconds) { using (HttpClient client = new HttpClient()) { Log.Debug("GetHttpData - begin by setting client timeout"); client.Timeout = TimeSpan.FromSeconds(timeoutInSeconds); Log.Debug("GetHttpData - after set client timeout to " + timeoutInSeconds.ToString() + " seconds"); HttpResponseMessage response = client.GetAsync(path).Result; Log.DebugFormat("GetHttpData after call 'client.GetAsync(path).Result', response status is {0}", response.StatusCode); if (!response.IsSuccessStatusCode) { return(""); } Log.Debug("GetHttpData response is success"); string data = response.Content.ReadAsStringAsync().Result; Log.Debug("GetHttpData after call 'response.Content.ReadAsStringAsync().Result'"); return(data); } }
public DateTimeOffsetInterval AdjustIntervalToGroupBy(DateTimeOffsetInterval SelectedPeriod, DateTimeOffsetInterval TimeSeriesRange, string GroupBy, TimeSpan timeseriesOffset) { Log.DebugFormat("AdjustIntervalToGroupBy selected interval = {0}, timeseries range = {1}, groupBy = {2}, tsOffset = {3}", _Common.TimeRangeString(SelectedPeriod), _Common.TimeSeriesRangeString(TimeSeriesRange), GroupBy, timeseriesOffset); DateTimeOffsetInterval reportPeriod = SelectedPeriod; try { DateTimeOffsetInterval selectedInterval = _Common.GetIntervalInUtcOffset(SelectedPeriod, timeseriesOffset); DateTimeOffsetInterval trimmedPeriodSelected = GetTrimmedPeriodSelected(selectedInterval); DateTimeOffsetInterval overlappedInterval = GetIntervalOfOverlap(trimmedPeriodSelected, TimeSeriesRange, timeseriesOffset); reportPeriod = AdjustIntervalToGroupBy(overlappedInterval, GroupBy); } catch (Exception exp) { Log.Error("Error in AdjustIntervalToGroupBy", exp); } Log.DebugFormat("AdjustIntervalToGroupBy returns report period = {0}", _Common.TimeRangeString(reportPeriod)); return(reportPeriod); }
public static void AddReportSpecificTables(DataSet dataSet) { try { if (dataSet.Tables.Contains("DischargeTimeSeriesFromRatingCurve")) { dataSet.Tables["DischargeTimeSeriesFromRatingCurve"].TableName = "SourceData"; dataSet.Tables["DischargeTimeSeriesFromRatingCurveLocation"].TableName = "SourceDataLocation"; dataSet.Tables["DischargeTimeSeriesFromRatingCurveLocationExtendedAttributes"].TableName = "SourceDataLocationExtendedAttributes"; } Common common = (Common)dataSet.Tables["RunReportRequest"].Rows[0]["CommonLibrary"]; DataTable settingsTable = dataSet.Tables["ReportSettings"]; settingsTable.Columns.Add("ReportTitle", typeof(string)); settingsTable.Rows[0]["ReportTitle"] = Resources.ContinuousDataProduction; DataTable table = new DataTable("ContinuousDataProductionDataTable"); dataSet.Tables.Add(table); RunFileReportRequest runReportRequest = (RunFileReportRequest)dataSet.Tables["RunReportRequest"].Rows[0]["RunReportRequest"]; IPublishGateway publish = runReportRequest.Publish; Guid timeSeriesUniqueId = (Guid)dataSet.Tables["SourceData"].Rows[0]["UniqueId"]; string dllName = (string)dataSet.Tables["RunReportRequest"].Rows[0]["DllName"]; Log.DebugFormat("{0} - Document GenerateScript input = {1}", dllName, timeSeriesUniqueId); /////////////////////////////////////////////////////////////// string timeseriesType = (string)dataSet.Tables["SourceData"].Rows[0]["TimeSeriesType"]; if (timeseriesType != "ProcessorDerived") { throw (new Exception("Input is not a derived time-series. Input signal must be a rating curve derived time-series.")); } Server.Services.PublishService.ServiceModel.RequestDtos.UpchainProcessorListByTimeSeriesServiceRequest upchain = new Server.Services.PublishService.ServiceModel.RequestDtos.UpchainProcessorListByTimeSeriesServiceRequest(); upchain.TimeSeriesUniqueId = timeSeriesUniqueId; List <Server.Services.PublishService.ServiceModel.Dtos.Processor> processors = publish.Get(upchain).Processors; string ratingModelIdentifier = ""; foreach (Server.Services.PublishService.ServiceModel.Dtos.Processor processor in processors) { if (string.IsNullOrEmpty(processor.InputRatingModelIdentifier)) { continue; } if (processor.InputTimeSeriesUniqueIds.Count == 0) { continue; } ratingModelIdentifier = processor.InputRatingModelIdentifier; break; } if (string.IsNullOrEmpty(ratingModelIdentifier)) { throw (new Exception("Input is not a rating model derived time-series. Input time-series must be a derived from a rating model.")); } double tableIncrement = common.GetParameterDouble("TableIncrement", 0.1); int httpRepeatCallLimit = common.GetParameterInt("HttpRepeatCallLimit", 4); int httpCallLimitSize = common.GetParameterInt("HttpCallLimitSize", 2000); int httpCallTimeoutInSeconds = common.GetParameterInt("HttpCallTimeoutInSeconds", 30); string httpUrlPrefix = common.GetParameterString("HttpUrlPrefix", @"http://geo.weather.gc.ca/geomet/features/collections/hydrometric-daily-mean/items?f=json&STATION_NUMBER="); TimeSpan timezone = TimeSpan.FromHours(common.GetTimeSeriesDescription(timeSeriesUniqueId).UtcOffset); DateTimeOffsetInterval selectedInterval = (DateTimeOffsetInterval)dataSet.Tables["ReportData"].Rows[0]["SelectedInterval"]; DateTimeOffset ReportTime = DateTimeOffset.Now; if (selectedInterval.End.HasValue) { ReportTime = selectedInterval.End.Value.ToOffset(timezone); } else { ReportTime = DateTimeOffset.Now.ToOffset(timezone); } string locationIdentifier = common.GetTimeSeriesDescription(timeSeriesUniqueId).LocationIdentifier; int numberOfHistoricalPoints = ReportSpecificFunctions.GetNumberOfHistoricalPointsAvailable( httpUrlPrefix, locationIdentifier, httpRepeatCallLimit, httpCallTimeoutInSeconds); settingsTable.Columns.Add("NumberOfHistoricalPoints", typeof(int)); settingsTable.Rows[0]["NumberOfHistoricalPoints"] = numberOfHistoricalPoints; } catch (Exception exp) { Log.Error("Error creating report specific data tables ", exp); throw exp; } }
public static TimeAlignedPoint[] GetInstMinMaxPoints(List <TimeAlignedPoint> inputPoints, bool extrema, DateTimeOffset theStartTime, DateTimeOffset[] binEnds) { bool MIN = true; bool MAX = false; TimeAlignedPoint[] pointsOfExtrema = new TimeAlignedPoint[binEnds.Length]; for (int i = 0; i < binEnds.Length; i++) { pointsOfExtrema[i] = new TimeAlignedPoint(); } if (inputPoints.Count == 0) { return(pointsOfExtrema); } int j = 0; double leftBinValue = double.NaN; while ((j < inputPoints.Count) && inputPoints[j].Timestamp < theStartTime) { j++; } if (j >= inputPoints.Count) { return(pointsOfExtrema); } if ((inputPoints[j].Timestamp >= theStartTime) && (j > 0)) { leftBinValue = calcInterpolatedPointValue(inputPoints[j - 1], inputPoints[j], theStartTime); } for (int i = 0; i < binEnds.Length; i++) { DateTimeOffset currentTime = (i > 0) ? binEnds[i - 1] : theStartTime; double currentVal = leftBinValue; Log.DebugFormat("i = {0}, j = {1}, leftBinValue = {2}, time = {3}", i, j, leftBinValue, currentTime); for (; j < inputPoints.Count; j++) { TimeAlignedPoint inputPoint = inputPoints[j]; if (inputPoint.Timestamp > binEnds[i]) { break; } if ((inputPoint.NumericValue1.HasValue) && (double.IsNaN(currentVal) || ((extrema == MIN) && (inputPoint.NumericValue1.Value < currentVal)) || ((extrema == MAX) && (inputPoint.NumericValue1.Value > currentVal)))) { currentTime = inputPoint.Timestamp; currentVal = inputPoint.NumericValue1.Value; } } double rightBinValue = double.NaN; if ((j > 0) && (j < inputPoints.Count) && (inputPoints[j].Timestamp > binEnds[i])) { DateTimeOffset binEnd = binEnds[i]; rightBinValue = calcInterpolatedPointValue(inputPoints[j - 1], inputPoints[j], binEnd); Log.DebugFormat("checking boundary extrema i = {0}, j = {1}, binEnd = {2}, rightBinValue = {3}", i, j, binEnd, rightBinValue); if (!double.IsNaN(rightBinValue) && (double.IsNaN(currentVal) || ((extrema == MIN) && (rightBinValue < currentVal)) || ((extrema == MAX) && (rightBinValue > currentVal)))) { currentTime = binEnd; currentVal = rightBinValue; Log.DebugFormat("checking boundary extrema i = {0}, j = {1}, currentTime = {2}, currentVal = {3}", i, j, currentTime, currentVal); } } if (!double.IsNaN(currentVal)) { pointsOfExtrema[i].Timestamp = currentTime; pointsOfExtrema[i].NumericValue1 = currentVal; Log.DebugFormat("setting extrema i = {0}, j = {1}, currentTime = {2}, currentVal = {3}", i, j, currentTime, currentVal); } leftBinValue = rightBinValue; } return(pointsOfExtrema); }
public static void AddReportSpecificTables(DataSet dataSet) { try { Common common = (Common)dataSet.Tables["RunReportRequest"].Rows[0]["CommonLibrary"]; DataTable settingsTable = dataSet.Tables["ReportSettings"]; settingsTable.Columns.Add("ReportTitle", typeof(string)); settingsTable.Rows[0]["ReportTitle"] = Resources.BenchmarkHistory; DataTable table = new DataTable("BenchmarkHistoryDataTable"); dataSet.Tables.Add(table); RunFileReportRequest runReportRequest = (RunFileReportRequest)dataSet.Tables["RunReportRequest"].Rows[0]["RunReportRequest"]; IPublishGateway publish = runReportRequest.Publish; string locationIdentifier = (string)dataSet.Tables["LocationInput"].Rows[0]["Identifier"]; string dllName = (string)dataSet.Tables["RunReportRequest"].Rows[0]["DllName"]; Log.DebugFormat("{0} - Document GenerateScript input location = {1}", dllName, locationIdentifier); /////////////////////////////////////////////////////////////// DateTimeOffsetInterval reportPeriod = (DateTimeOffsetInterval)dataSet.Tables["ReportPeriods"].Rows[0]["NoGroupBy"]; int formatPrecision = common.GetParameterInt("FormatPrecision", 3); string formatType = common.GetParameterString("FormatType", "Fixed"); bool formatFixed = (formatType == "Fixed"); int groupSizeLimit = 6; DataTable benchmarks = dataSet.Tables.Add("Benchmarks"); benchmarks.Columns.Add("Name", typeof(string)); benchmarks.Columns.Add("Elevation", typeof(string)); benchmarks.Columns.Add("DateConstructed", typeof(string)); benchmarks.Columns.Add("DateRemoved", typeof(string)); benchmarks.Columns.Add("Status", typeof(string)); benchmarks.Columns.Add("Description", typeof(string)); DataTable groupBy = dataSet.Tables.Add("GroupBy"); groupBy.Columns.Add("GroupNumber", typeof(int)); for (int i = 0; i < groupSizeLimit; i++) { groupBy.Columns.Add("Name" + (i + 1).ToString(), typeof(string)); } DataTable benchmarkHistory = dataSet.Tables.Add("BenchmarkHistory"); benchmarkHistory.Columns.Add("GroupNumber", typeof(int)); benchmarkHistory.Columns.Add("DateTime", typeof(string)); benchmarkHistory.Columns.Add("Party", typeof(string)); benchmarkHistory.Columns.Add("Comments", typeof(string)); for (int i = 0; i < groupSizeLimit; i++) { benchmarkHistory.Columns.Add("Elevation" + (i + 1).ToString(), typeof(string)); benchmarkHistory.Columns.Add("Correction" + (i + 1).ToString(), typeof(string)); } dataSet.Relations.Add("GroupByBenchmarkHistory", groupBy.Columns["GroupNumber"], benchmarkHistory.Columns["GroupNumber"]); LocationDataServiceResponse locData = common.GetLocationData(locationIdentifier); List <ReferencePoint> refPoints = new List <ReferencePoint>(); foreach (ReferencePoint refPoint in locData.ReferencePoints) { List <ReferencePointPeriod> refPointPeriods = refPoint.ReferencePointPeriods; if (refPointPeriods.Count == 0) { continue; } refPointPeriods.Sort((x, y) => x.ValidFrom.CompareTo(y.ValidFrom)); DateTimeOffset validFrom = refPointPeriods[0].ValidFrom; if (ReportSpecificFunctions.TimeRangeOverlaps(validFrom, refPoint.DecommissionedDate, reportPeriod)) { refPoints.Add(refPoint); } } if (refPoints.Count == 0) { return; } Dictionary <Guid, List <ReferencePointPeriod> > refPointHistories = new Dictionary <Guid, List <ReferencePointPeriod> >(); Dictionary <Guid, int> refPointNumbers = new Dictionary <Guid, int>(); for (int i = 0; i < refPoints.Count; i++) { ReferencePoint refPoint = refPoints[i]; refPointNumbers.Add(refPoint.UniqueId, i); DataRow row = benchmarks.NewRow(); row["Name"] = refPoint.Name; row["Description"] = refPoint.Description; if (refPoint.DecommissionedDate.HasValue) { row["DateRemoved"] = refPoint.DecommissionedDate.Value.ToString("yyyy-MM-dd"); } string status = (refPoint.DecommissionedDate.HasValue) ? Resources.InactiveLowerCase : Resources.ActiveLowerCase; status += (refPoint.PrimarySinceDate.HasValue) ? " " + Resources.PrimaryLowerCase : ""; row["Status"] = status; DateTimeOffset?primarySince = refPoint.PrimarySinceDate; List <ReferencePointPeriod> refPointPeriods = refPoint.ReferencePointPeriods; refPointPeriods.Sort((x, y) => x.ValidFrom.CompareTo(y.ValidFrom)); DateTimeOffset validFrom = refPointPeriods[0].ValidFrom; row["DateConstructed"] = validFrom.ToString("yyyy-MM-dd"); double elevation = refPointPeriods[refPointPeriods.Count - 1].Elevation; row["Elevation"] = Common.FormatDoubleValue(elevation, formatFixed, formatPrecision, ""); refPointPeriods.Reverse(); refPointHistories[refPoint.UniqueId] = refPointPeriods; benchmarks.Rows.Add(row); } int numberOfGroups = (int)Math.Ceiling((double)benchmarks.Rows.Count / groupSizeLimit); FieldVisitDataByLocationServiceRequest fdRequest = new FieldVisitDataByLocationServiceRequest(); fdRequest.LocationIdentifier = locationIdentifier; fdRequest.Activities = new List <ActivityType> { ActivityType.LevelSurvey }; FieldVisitDataByLocationServiceResponse fdResponse = publish.Get(fdRequest); List <FieldVisit> fieldVisitData = fdResponse.FieldVisitData; try { fieldVisitData.Sort((x, y) => y.StartTime.Value.CompareTo(x.StartTime.Value)); } catch { } for (int i = 0; i < numberOfGroups; i++) { DataRow groupByRow = dataSet.Tables["GroupBy"].NewRow(); groupByRow["GroupNumber"] = i; for (int j = 0; j < groupSizeLimit; j++) { int index = (i * groupSizeLimit) + j; if (index < refPoints.Count) { ReferencePoint refPoint = refPoints[index]; groupByRow["Name" + (j + 1).ToString()] = refPoint.Name; } } dataSet.Tables["GroupBy"].Rows.Add(groupByRow); } foreach (FieldVisit fieldVisit in fieldVisitData) { if (!ReportSpecificFunctions.TimeRangeOverlaps(fieldVisit.StartTime.Value, fieldVisit.StartTime.Value, reportPeriod)) { continue; } string dateTime = fieldVisit.StartTime.Value.ToString("yyyy-MM-dd HH:mm"); string comments = fieldVisit.LevelSurveyActivity.Comments; string party = fieldVisit.LevelSurveyActivity.Party; int fieldVisitFirstRow = dataSet.Tables["BenchmarkHistory"].Rows.Count; for (int i = 0; i < numberOfGroups; i++) { DataRow row = dataSet.Tables["BenchmarkHistory"].NewRow(); row["GroupNumber"] = i; row["DateTime"] = dateTime; row["Party"] = party; row["Comments"] = comments; dataSet.Tables["BenchmarkHistory"].Rows.Add(row); } List <LevelSurveyMeasurement> levelMeasurements = fieldVisit.LevelSurveyActivity.LevelMeasurements; foreach (LevelSurveyMeasurement lsm in levelMeasurements) { if (!lsm.MeasuredElevation.Numeric.HasValue) { continue; } Guid id = lsm.ReferencePointUniqueId; if (!refPointNumbers.ContainsKey(id)) { continue; } int number = refPointNumbers[id]; int theGroupNumber = (int)Math.Floor((double)number / groupSizeLimit); int thePosition = (int)(number % groupSizeLimit); double level = lsm.MeasuredElevation.Numeric.Value; double correction = ReportSpecificFunctions.calculateDifference(lsm, refPointHistories[id]); DataRow row = dataSet.Tables["BenchmarkHistory"].Rows[fieldVisitFirstRow + theGroupNumber]; row["Elevation" + (1 + thePosition).ToString()] = Common.FormatDoubleValue(level, formatFixed, formatPrecision, ""); row["Correction" + (1 + thePosition).ToString()] = Common.FormatDoubleValue(correction, formatFixed, formatPrecision, ""); } } List <DataRow> emptyRows = new List <DataRow>(); for (int i = 0; i < dataSet.Tables["BenchmarkHistory"].Rows.Count; i++) { DataRow row = dataSet.Tables["BenchmarkHistory"].Rows[i]; int emptyCount = 0; for (int j = 0; j < groupSizeLimit; j++) { bool empty = string.IsNullOrEmpty(row["Elevation" + (j + 1).ToString()].ToString()); if (empty) { emptyCount++; } } if (emptyCount == groupSizeLimit) { emptyRows.Add(row); } } for (int i = 0; i < emptyRows.Count; i++) { dataSet.Tables["BenchmarkHistory"].Rows.Remove(emptyRows[i]); } } catch (Exception exp) { Log.Error("Error creating report specific data tables ", exp); throw exp; } }
public static void AddReportSpecificTables(DataSet dataSet) { try { Common common = (Common)dataSet.Tables["RunReportRequest"].Rows[0]["CommonLibrary"]; DataTable settingsTable = dataSet.Tables["ReportSettings"]; settingsTable.Columns.Add("ReportTitle", typeof(string)); settingsTable.Rows[0]["ReportTitle"] = Resources.Instrumentation; DataTable table = new DataTable("InstrumentationDataTable"); dataSet.Tables.Add(table); RunFileReportRequest runReportRequest = (RunFileReportRequest)dataSet.Tables["RunReportRequest"].Rows[0]["RunReportRequest"]; IPublishGateway publish = runReportRequest.Publish; string locationIdentifier = (string)dataSet.Tables["LocationInput"].Rows[0]["Identifier"]; string dllName = (string)dataSet.Tables["RunReportRequest"].Rows[0]["DllName"]; Log.DebugFormat("{0} - Document GenerateScript input location = {1}", dllName, locationIdentifier); /////////////////////////////////////////////////////////////// DateTimeOffsetInterval reportPeriod = (DateTimeOffsetInterval)dataSet.Tables["ReportPeriods"].Rows[0]["NoGroupBy"]; string sortOrder = common.GetParameterString("SortOrder", "ReverseChronologicalOnMeasurementTime"); DataTable table1 = dataSet.Tables.Add("Instrumentation"); table1.Columns.Add("Date", typeof(string)); table1.Columns.Add("Time", typeof(string)); table1.Columns.Add("Timezone", typeof(string)); table1.Columns.Add("ActivityType", typeof(string)); table1.Columns.Add("Stage", typeof(string)); table1.Columns.Add("StageUnit", typeof(string)); table1.Columns.Add("Discharge", typeof(string)); table1.Columns.Add("DischargeUnit", typeof(string)); table1.Columns.Add("ControlCondition", typeof(string)); table1.Columns.Add("ActivityRemarks", typeof(string)); table1.Columns.Add("FieldVisitComments", typeof(string)); table1.Columns.Add("Method", typeof(string)); table1.Columns.Add("InstrumentType", typeof(string)); table1.Columns.Add("Software", typeof(string)); table1.Columns.Add("Firmware", typeof(string)); table1.Columns.Add("SerialNumber", typeof(string)); table1.Columns.Add("Manufacturer", typeof(string)); table1.Columns.Add("Model", typeof(string)); ActiveMetersAndCalibrationsServiceRequest amcRequest = new ActiveMetersAndCalibrationsServiceRequest(); ActiveMetersAndCalibrationsServiceResponse amcResponse = publish.Get(amcRequest); List <ActiveMeterDetails> activeMeterDetailsList = amcResponse.ActiveMeterDetails; FieldVisitDataByLocationServiceRequest fdRequest = new FieldVisitDataByLocationServiceRequest(); fdRequest.LocationIdentifier = locationIdentifier; fdRequest.Activities = new List <ActivityType> { ActivityType.Reading, ActivityType.DischargeSummary, ActivityType.DischargePointVelocity, ActivityType.DischargeAdcp }; fdRequest.ApplyRounding = true; fdRequest.IncludeVerticals = true; FieldVisitDataByLocationServiceResponse fdResponse = publish.Get(fdRequest); List <FieldVisit> fieldVisitData = fdResponse.FieldVisitData; try { if (sortOrder != "ReverseChronologicalOnMeasurementTime") { fieldVisitData.Sort((x, y) => x.StartTime.Value.CompareTo(y.StartTime.Value)); } else { fieldVisitData.Sort((x, y) => y.StartTime.Value.CompareTo(x.StartTime.Value)); } } catch { } foreach (FieldVisit fieldVisit in fieldVisitData) { if (!ReportSpecificFunctions.TimeRangeOverlaps(fieldVisit.StartTime.Value, fieldVisit.StartTime.Value, reportPeriod)) { continue; } string controlCondition = (fieldVisit.ControlConditionActivity != null) ? fieldVisit.ControlConditionActivity.ControlCondition.ToString() : ""; var activities = fieldVisit.DischargeActivities; if (activities != null) { if (sortOrder != "ReverseChronologicalOnMeasurementTime") { activities.Sort((x, y) => x.DischargeSummary.MeasurementTime.CompareTo(y.DischargeSummary.MeasurementTime)); } else { activities.Sort((x, y) => y.DischargeSummary.MeasurementTime.CompareTo(x.DischargeSummary.MeasurementTime)); } foreach (DischargeActivity dischargeActivity in activities) { DataRow row = table1.NewRow(); row["Date"] = (fieldVisit.StartTime.HasValue) ? fieldVisit.StartTime.Value.ToString("yyyy-MM-dd") : ""; row["ControlCondition"] = controlCondition; row["FieldVisitComments"] = fieldVisit.Remarks; row["ActivityType"] = Resources.DischargeSummary; row["Method"] = dischargeActivity.DischargeSummary.DischargeMethod; DischargeSummary dischargeSummary = dischargeActivity.DischargeSummary; row["Time"] = dischargeSummary.MeasurementTime.ToString("HH:mm"); row["Timezone"] = dischargeSummary.MeasurementTime.ToString("zzz"); string discharge = (dischargeSummary.Discharge != null) ? dischargeSummary.Discharge.Numeric.ToString() : ""; string dischargeUnit = (dischargeSummary.Discharge != null) ? dischargeSummary.Discharge.Unit : ""; if (dischargeUnit != "m^3/s") { discharge += " " + dischargeUnit; } row["Discharge"] = discharge; string stage = (dischargeSummary.MeanGageHeight != null) ? dischargeSummary.MeanGageHeight.Numeric.ToString() : ""; string stageUnit = (dischargeSummary.MeanGageHeight != null) ? dischargeSummary.MeanGageHeight.Unit : ""; if (!string.IsNullOrEmpty(stage) && (stageUnit != "m")) { stage += " " + stageUnit; } row["Stage"] = stage; row["ActivityRemarks"] = dischargeSummary.Comments; string serialNumber = ""; string model = ""; string manufacturer = ""; string instrumentType = ""; string software = ""; string firmware = ""; if ((dischargeActivity.PointVelocityDischargeActivities != null) && (dischargeActivity.PointVelocityDischargeActivities.Count > 0)) { PointVelocityDischargeActivity pointVelocityDischargeActivity = dischargeActivity.PointVelocityDischargeActivities[0]; if ((pointVelocityDischargeActivity.Verticals != null) && (pointVelocityDischargeActivity.Verticals.Count > 0)) { Vertical vertical = pointVelocityDischargeActivity.Verticals[0]; serialNumber = vertical.CurrentMeter.SerialNumber; model = vertical.CurrentMeter.Model; manufacturer = vertical.CurrentMeter.Manufacturer; foreach (ActiveMeterDetails activeMeterDetails in activeMeterDetailsList) { if ((activeMeterDetails.Manufacturer == manufacturer) && (activeMeterDetails.SerialNumber == serialNumber) && (activeMeterDetails.Model == model)) { instrumentType = (activeMeterDetails.MeterType.HasValue) ? activeMeterDetails.MeterType.Value.ToString() : ""; software = activeMeterDetails.SoftwareVersion; firmware = activeMeterDetails.FirmwareVersion; break; } } } } if ((dischargeActivity.AdcpDischargeActivities != null) && (dischargeActivity.AdcpDischargeActivities.Count > 0)) { AdcpDischargeActivity adcpDischargeActivity = dischargeActivity.AdcpDischargeActivities[0]; instrumentType = adcpDischargeActivity.AdcpDeviceType; software = adcpDischargeActivity.SoftwareVersion; firmware = adcpDischargeActivity.FirmwareVersion; manufacturer = adcpDischargeActivity.Manufacturer; serialNumber = adcpDischargeActivity.SerialNumber; model = adcpDischargeActivity.Model; Log.DebugFormat("AdcpDischargeActivity: {0}, {1}, {2}", instrumentType, software, firmware); } row["Software"] = software; row["InstrumentType"] = instrumentType; row["Firmware"] = firmware; row["Manufacturer"] = manufacturer; row["Model"] = model; row["SerialNumber"] = serialNumber; table1.Rows.Add(row); } } } } catch (Exception exp) { Log.Error("Error creating report specific data tables ", exp); throw exp; } }
public string GenerateReportIntoFile(RunFileReportRequest request) { Thread.CurrentThread.CurrentCulture.NumberFormat = CultureInfo.InvariantCulture.NumberFormat; DataSet set = GenerateDataSet(request); Log.Debug("GenerateReport - after generate dataset"); string dllFolder = (string)set.Tables["RunReportRequest"].Rows[0]["DllFolder"]; string dllName = (string)set.Tables["RunReportRequest"].Rows[0]["DllName"]; string templateFileName = Path.Combine(dllFolder, dllName + ".rst"); Log.DebugFormat("GenerateReport rst template file name is {0}", templateFileName); string xmlTemplate = File.ReadAllText(templateFileName); Log.Debug("GenerateReport after read rst template file"); ReportManager reportManager = new ReportManager(); reportManager.DataSources.Add(set.DataSetName, set); reportManager.OwnerForm = null; Log.Debug("GenerateReport after init ReportManager"); InlineReportSlot inlineReportSlot = new InlineReportSlot(); inlineReportSlot.DocumentStream = xmlTemplate; inlineReportSlot.RenderingError += InlineReportSlot_RenderingError; Log.Debug("GenerateReport after init InlineReportSlot"); reportManager.Reports.Add(inlineReportSlot); Document document = inlineReportSlot.RenderDocument(); Log.Debug("GenerateReport after RenderDocument"); string outputFormat = request.OutputFormat; Log.DebugFormat("GenerateReport after RenderDocument, document name is {0}, page count is {1}, outputFormat is {2}", document.Name, document.Pages.Count, outputFormat); string tempFileName = Path.GetTempFileName(); string outputFileName = Path.ChangeExtension(tempFileName, outputFormat); try { File.Delete(tempFileName); } catch { } try { File.Delete(outputFileName); } catch { } Log.DebugFormat("GenerateReport - the name of file for export report to write to is set to {0}", outputFileName); try { switch (outputFormat.ToUpper()) { case "PDF": { PdfExportFilter exportFilter = new PdfExportFilter(); exportFilter.Compress = true; exportFilter.Export(document, outputFileName, false); Log.DebugFormat("GenerateReport after export document to PDF into tempFileName = {0}", outputFileName); break; } case "CSV": { CsvExportFilter exportFilter = new CsvExportFilter(); exportFilter.Separator = ","; exportFilter.Export(document, outputFileName, false); Log.DebugFormat("GenerateReport after export document to CSV into tempFileName = {0}", outputFileName); break; } case "XLSX": { ExcelExportFilter exportFilter = new ExcelExportFilter(); exportFilter.ExportAsData = true; exportFilter.Export(document, outputFileName, false); Log.DebugFormat("GenerateReport after export document to XLSX into tempFileName = {0}", outputFileName); break; } case "PNG": { PngExportFilter exportFilter = new PngExportFilter(); exportFilter.PagesRange = "1"; exportFilter.Export(document, outputFileName, false); Log.DebugFormat("GenerateReport after export document to PNG into tempFileName = {0}", outputFileName); break; } default: { Log.DebugFormat("Unable to export report - unknown export format '{0}'", outputFormat); break; } } } catch (System.Exception exp) { Log.Debug("Exception during export report", exp); throw exp; } return(outputFileName); }
public static void AddReportSpecificTables(DataSet dataSet) { try { Common common = (Common)dataSet.Tables["RunReportRequest"].Rows[0]["CommonLibrary"]; DataTable settingsTable = dataSet.Tables["ReportSettings"]; settingsTable.Columns.Add("ReportTitle", typeof(string)); settingsTable.Rows[0]["ReportTitle"] = Resources.FieldActivity; DataTable table = new DataTable("FieldActivityDataTable"); dataSet.Tables.Add(table); RunFileReportRequest runReportRequest = (RunFileReportRequest)dataSet.Tables["RunReportRequest"].Rows[0]["RunReportRequest"]; IPublishGateway publish = runReportRequest.Publish; string locationIdentifier = (string)dataSet.Tables["LocationInput"].Rows[0]["Identifier"]; string dllName = (string)dataSet.Tables["RunReportRequest"].Rows[0]["DllName"]; Log.DebugFormat("{0} - Document GenerateScript input location = {1}", dllName, locationIdentifier); /////////////////////////////////////////////////////////////// DateTimeOffsetInterval reportPeriod = (DateTimeOffsetInterval)dataSet.Tables["ReportPeriods"].Rows[0]["NoGroupBy"]; string sortOrder = common.GetParameterString("SortOrder", "ReverseChronologicalOnMeasurementTime"); DataTable table1 = dataSet.Tables.Add("FieldActivity"); table1.Columns.Add("Date", typeof(string)); table1.Columns.Add("Time", typeof(string)); table1.Columns.Add("Timezone", typeof(string)); table1.Columns.Add("ActivityType", typeof(string)); table1.Columns.Add("Stage", typeof(string)); table1.Columns.Add("StageUnit", typeof(string)); table1.Columns.Add("Discharge", typeof(string)); table1.Columns.Add("DischargeUnit", typeof(string)); table1.Columns.Add("ControlCondition", typeof(string)); table1.Columns.Add("ActivityRemarks", typeof(string)); table1.Columns.Add("FieldVisitComments", typeof(string)); FieldVisitDataByLocationServiceRequest fdRequest = new FieldVisitDataByLocationServiceRequest(); fdRequest.LocationIdentifier = locationIdentifier; fdRequest.ApplyRounding = true; fdRequest.Activities = new List <ActivityType> { ActivityType.Reading, ActivityType.DischargeSummary, ActivityType.ControlCondition }; FieldVisitDataByLocationServiceResponse fdResponse = publish.Get(fdRequest); List <FieldVisit> fieldVisitData = fdResponse.FieldVisitData; try { if (sortOrder != "ReverseChronologicalOnMeasurementTime") { fieldVisitData.Sort((x, y) => x.StartTime.Value.CompareTo(y.StartTime.Value)); } else { fieldVisitData.Sort((x, y) => y.StartTime.Value.CompareTo(x.StartTime.Value)); } } catch { } foreach (FieldVisit fieldVisit in fieldVisitData) { if (!ReportSpecificFunctions.TimeRangeOverlaps(fieldVisit.StartTime.Value, fieldVisit.StartTime.Value, reportPeriod)) { continue; } if (fieldVisit.DischargeActivities != null) { var activities = fieldVisit.DischargeActivities; if (sortOrder != "ReverseChronologicalOnMeasurementTime") { activities.Sort((x, y) => x.DischargeSummary.MeasurementTime.CompareTo(y.DischargeSummary.MeasurementTime)); } else { activities.Sort((x, y) => y.DischargeSummary.MeasurementTime.CompareTo(x.DischargeSummary.MeasurementTime)); } foreach (DischargeActivity dischargeActivity in activities) { if (dischargeActivity.DischargeSummary == null) { continue; } DataRow row = table1.NewRow(); row["Date"] = (fieldVisit.StartTime.HasValue) ? fieldVisit.StartTime.Value.ToString("yyyy-MM-dd") : ""; row["ControlCondition"] = (fieldVisit.ControlConditionActivity != null) ? fieldVisit.ControlConditionActivity.ControlCondition.ToString() : ""; row["FieldVisitComments"] = fieldVisit.Remarks; row["ActivityType"] = Resources.DischargeSummary; DischargeSummary dischargeSummary = dischargeActivity.DischargeSummary; row["Time"] = dischargeSummary.MeasurementTime.ToString("HH:mm"); row["Timezone"] = dischargeSummary.MeasurementTime.ToString("zzz"); string discharge = (dischargeSummary.Discharge != null) ? dischargeSummary.Discharge.Display : ""; string dischargeUnit = (dischargeSummary.Discharge != null) ? dischargeSummary.Discharge.Unit : ""; if (dischargeUnit != "m^3/s") { discharge += " " + dischargeUnit; } row["Discharge"] = discharge; string stage = (dischargeSummary.MeanGageHeight != null) ? dischargeSummary.MeanGageHeight.Display : ""; string stageUnit = (dischargeSummary.MeanGageHeight != null) ? dischargeSummary.MeanGageHeight.Unit : ""; if (!string.IsNullOrEmpty(stage) && (stageUnit != "m")) { stage += " " + stageUnit; } row["Stage"] = stage; row["ActivityRemarks"] = dischargeSummary.Comments; table1.Rows.Add(row); } } if ((fieldVisit.InspectionActivity != null) && (fieldVisit.InspectionActivity.Readings != null)) { foreach (Reading reading in fieldVisit.InspectionActivity.Readings) { if (!reading.Publish || !reading.IsValid) { continue; } if ("Stage" != reading.Parameter) { continue; } DataRow row = table1.NewRow(); row["Date"] = (fieldVisit.StartTime.HasValue) ? fieldVisit.StartTime.Value.ToString("yyyy-MM-dd") : ""; row["ControlCondition"] = (fieldVisit.ControlConditionActivity != null) ? fieldVisit.ControlConditionActivity.ControlCondition.ToString() : ""; row["FieldVisitComments"] = fieldVisit.Remarks; row["ActivityType"] = Resources.Reading; row["Time"] = (reading.Time.HasValue) ? reading.Time.Value.ToString("HH:mm") : ""; row["Timezone"] = (reading.Time.HasValue) ? reading.Time.Value.ToString("zzz") : ""; string stage = reading.Value.Display; string stageUnit = reading.Unit; if (stageUnit != "m") { stage += " " + stageUnit; } row["Stage"] = stage; row["ActivityRemarks"] = reading.Comments; table1.Rows.Add(row); } } } } catch (Exception exp) { Log.Error("Error creating report specific data tables ", exp); throw exp; } }