diff --git a/Cluster/Chart/BarChart/BarCharTopN.swift b/Cluster/Chart/BarChart/BarCharTopN.swift index 45d56f6..ae34d8b 100644 --- a/Cluster/Chart/BarChart/BarCharTopN.swift +++ b/Cluster/Chart/BarChart/BarCharTopN.swift @@ -10,16 +10,169 @@ import Charts import DataTransferObjects struct BarChartTopN: View { - let result: TopNQueryResult + let topNQueryResult: TopNQueryResult + let query: CustomQuery var body: some View { + Chart { - //ForEach(result.rows) { row in - // BarMark( - // x: .value("Date", 4), - // y: .value("Total Count", 5) - // ) - //} + ForEach(topNQueryResult.rows, id: \.self) { (row: TopNQueryResultRow) in + + ForEach(row.result, id: \.self) { (rowResult: AdaptableQueryResultItem) in + + ForEach(query.aggregations ?? [], id: \.self) { (aggregator: Aggregator) in + if let metricValue = getMetricValue(rowResult: rowResult){ + getBarMark( + timeStamp: row.timestamp, + name: getAggregatorName(aggregator: aggregator), + metricValue: metricValue, + metricName: getMetricName(rowResult: rowResult) + ) + } + } + + } + + } + } + .chartForegroundStyleScale(range: Color.chartColors) + + } + + // swiftlint:disable cyclomatic_complexity + func granularity() -> Calendar.Component{ + switch query.granularity { + case .all: + .month + case .none: + .month + case .second: + .hour + case .minute: + .hour + case .fifteen_minute: + .hour + case .thirty_minute: + .hour + case .hour: + .hour + case .day: + .day + case .week: + .weekOfYear + case .month: + .month + case .quarter: + .quarter + case .year: + .year + } + } + // swiftlint:enable cyclomatic_complexity + + func getBarMark(timeStamp: Date, name: String, metricValue: Double, metricName: String) -> some ChartContent { + return BarMark( + x: .value("Date", timeStamp, unit: granularity()), + y: .value(name, metricValue) + ) + .foregroundStyle(by: .value(getDimensionName(from: query.dimension) ?? "Not found", metricName)) + } + + func getDimensionName(from: DimensionSpec?) -> String? { + switch from { + case .default(let defaultDimensionSpec): + defaultDimensionSpec.outputName + case .extraction(let extractionDimensionSpec): + extractionDimensionSpec.outputName + case .none: + nil } } + + func getMetricName(rowResult: AdaptableQueryResultItem) -> String{ + let dimensionName = getDimensionName(from: query.dimension) ?? "Not found" + let metricName = rowResult.dimensions[dimensionName] ?? "Not found" + return metricName + } + + // swiftlint:disable cyclomatic_complexity + // swiftlint:disable function_body_length + func getAggregatorName(aggregator: Aggregator) -> String { + switch aggregator { + case .count(let a): + a.name + case .cardinality(let a): + a.name + case .longSum(let a): + a.name + case .doubleSum(let a): + a.name + case .floatSum(let a): + a.name + case .doubleMin(let a): + a.name + case .doubleMax(let a): + a.name + case .floatMin(let a): + a.name + case .floatMax(let a): + a.name + case .longMin(let a): + a.name + case .longMax(let a): + a.name + case .doubleMean(let a): + a.name + case .doubleFirst(let a): + a.name + case .doubleLast(let a): + a.name + case .floatFirst(let a): + a.name + case .floatLast(let a): + a.name + case .longFirst(let a): + a.name + case .longLast(let a): + a.name + case .stringFirst(let a): + a.name + case .stringLast(let a): + a.name + case .doubleAny(let a): + a.name + case .floatAny(let a): + a.name + case .longAny(let a): + a.name + case .stringAny(let a): + a.name + case .thetaSketch(let a): + a.name + case .filtered(let a): + getAggregatorName(aggregator: a.aggregator) + } + } + // swiftlint:enable cyclomatic_complexity + // swiftlint:enable function_body_length + + func getMetricValue(rowResult: AdaptableQueryResultItem) -> Double? { + guard let metric = query.metric, let metricName = getMetricName(metric: metric) else { + return nil + } + let value = rowResult.metrics[metricName] + return value + } + + func getMetricName(metric: TopNMetricSpec) -> String? { + switch metric { + case .numeric(let numericTopNMetricSpec): + return numericTopNMetricSpec.metric + case .inverted(let invertedTopNMetricSpec): + return getMetricName(metric: invertedTopNMetricSpec.metric) + default: + return nil + } + } + } diff --git a/Cluster/Chart/BarChart/ClusterBarChart.swift b/Cluster/Chart/BarChart/ClusterBarChart.swift index 9cf3d9f..446b782 100644 --- a/Cluster/Chart/BarChart/ClusterBarChart.swift +++ b/Cluster/Chart/BarChart/ClusterBarChart.swift @@ -19,15 +19,9 @@ struct ClusterBarChart: View { } else { Text("Mismatch in query type and result type 1") } - /*case .groupBy: - if case let .groupBy(result) = result { - BarChartGroupBy(result: result) - } else { - Text("Mismatch in query type and result type 2") - }*/ case .topN: if case let .topN(result) = result { - BarChartTopN(result: result) + BarChartTopN(topNQueryResult: result, query: query) } else { Text("Mismatch in query type and result type 2") } diff --git a/Shared/Supporting Files/Assets.xcassets/Tamber.colorset/Contents.json b/Shared/Supporting Files/Assets.xcassets/Tamber.colorset/Contents.json new file mode 100644 index 0000000..d39d211 --- /dev/null +++ b/Shared/Supporting Files/Assets.xcassets/Tamber.colorset/Contents.json @@ -0,0 +1,20 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "1.000", + "blue" : "0.141", + "green" : "0.749", + "red" : "0.984" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Shared/Supporting Files/Assets.xcassets/Tamerald.colorset/Contents.json b/Shared/Supporting Files/Assets.xcassets/Tamerald.colorset/Contents.json new file mode 100644 index 0000000..7aa4cbe --- /dev/null +++ b/Shared/Supporting Files/Assets.xcassets/Tamerald.colorset/Contents.json @@ -0,0 +1,20 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "1.000", + "blue" : "0.600", + "green" : "0.827", + "red" : "0.204" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Shared/Supporting Files/Assets.xcassets/Tcyan.colorset/Contents.json b/Shared/Supporting Files/Assets.xcassets/Tcyan.colorset/Contents.json new file mode 100644 index 0000000..87e7abc --- /dev/null +++ b/Shared/Supporting Files/Assets.xcassets/Tcyan.colorset/Contents.json @@ -0,0 +1,20 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "1.000", + "blue" : "0.933", + "green" : "0.827", + "red" : "0.133" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Shared/Supporting Files/Assets.xcassets/Tfuchsia.colorset/Contents.json b/Shared/Supporting Files/Assets.xcassets/Tfuchsia.colorset/Contents.json new file mode 100644 index 0000000..7c184a5 --- /dev/null +++ b/Shared/Supporting Files/Assets.xcassets/Tfuchsia.colorset/Contents.json @@ -0,0 +1,20 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "1.000", + "blue" : "0.976", + "green" : "0.475", + "red" : "0.910" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Shared/Supporting Files/Assets.xcassets/Tindigo.colorset/Contents.json b/Shared/Supporting Files/Assets.xcassets/Tindigo.colorset/Contents.json new file mode 100644 index 0000000..4a4d591 --- /dev/null +++ b/Shared/Supporting Files/Assets.xcassets/Tindigo.colorset/Contents.json @@ -0,0 +1,20 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "1.000", + "blue" : "0.980", + "green" : "0.647", + "red" : "0.376" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Shared/Supporting Files/Assets.xcassets/Tlime.colorset/Contents.json b/Shared/Supporting Files/Assets.xcassets/Tlime.colorset/Contents.json new file mode 100644 index 0000000..c6025e8 --- /dev/null +++ b/Shared/Supporting Files/Assets.xcassets/Tlime.colorset/Contents.json @@ -0,0 +1,20 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "1.000", + "blue" : "0.208", + "green" : "0.902", + "red" : "0.639" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Shared/Supporting Files/Assets.xcassets/Trose.colorset/Contents.json b/Shared/Supporting Files/Assets.xcassets/Trose.colorset/Contents.json new file mode 100644 index 0000000..15579d5 --- /dev/null +++ b/Shared/Supporting Files/Assets.xcassets/Trose.colorset/Contents.json @@ -0,0 +1,20 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "1.000", + "blue" : "0.522", + "green" : "0.443", + "red" : "0.984" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Shared/Supporting Files/Assets.xcassets/Tsky.colorset/Contents.json b/Shared/Supporting Files/Assets.xcassets/Tsky.colorset/Contents.json new file mode 100644 index 0000000..579cbf7 --- /dev/null +++ b/Shared/Supporting Files/Assets.xcassets/Tsky.colorset/Contents.json @@ -0,0 +1,20 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "1.000", + "blue" : "0.973", + "green" : "0.741", + "red" : "0.220" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/UIComponents/Color.swift b/UIComponents/Color.swift index 0488444..3e0c30d 100644 --- a/UIComponents/Color.swift +++ b/UIComponents/Color.swift @@ -17,7 +17,6 @@ extension Color { static let separatorColor = Color(UIColor.separator) #endif - static let telemetryOrange = Color("Torange") static let cardBackground = Color("CardBackgroundColor") static let customTextColor = Color("CustomTextColor") @@ -27,4 +26,31 @@ extension Color { static let Zinc400 = Color("Zinc400") static let Zinc600 = Color("Zinc600") + static let telemetryOrange = Color("Torange") + static let telemetryAmber = Color("Tamber") + static let telemetryLime = Color("Tlime") + static let telemetryEmerald = Color("Temerald") + static let telemetryCyan = Color("Tcyan") + static let telemetrySky = Color("Tsky") + static let telemetryIndigo = Color("Tindigo") + static let telemetryFuchsia = Color("Tfuchsia") + static let telemetryRose = Color("Trose") + + static var chartColors: [Color] { + [ + telemetryOrange, + telemetryAmber, + telemetryLime, + telemetryEmerald, + telemetryCyan, + telemetrySky, + telemetryIndigo, + telemetryFuchsia, + telemetryRose + ] + } + + + + }