Skip to content

Commit

Permalink
Merge pull request #192 from TelemetryDeck/PieCharts
Browse files Browse the repository at this point in the history
Pie Charts
  • Loading branch information
Black-Fox-2022 authored May 28, 2024
2 parents a70aac4 + 7f4e748 commit 8547355
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 156 deletions.
125 changes: 6 additions & 119 deletions Cluster/Chart/BarChart/BarCharTopN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct BarChartTopN: View {
if let metricValue = getMetricValue(rowResult: rowResult){
getBarMark(
timeStamp: row.timestamp,
name: getAggregatorName(aggregator: aggregator),
name: aggregator.name,
metricValue: metricValue,
metricName: getMetricName(rowResult: rowResult)
)
Expand All @@ -39,140 +39,27 @@ struct BarChartTopN: View {

}

// 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()),
x: .value("Date", timeStamp, unit: query.granularityAsCalendarComponent),
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
}
.foregroundStyle(by: .value(query.dimension?.name ?? "No value", metricName))
.cornerRadius(2)
}

func getMetricName(rowResult: AdaptableQueryResultItem) -> String{
let dimensionName = getDimensionName(from: query.dimension) ?? "Not found"
let dimensionName = query.dimension?.name ?? "No value"
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 {
guard let metricName = query.metric?.name 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
}
}

}
25 changes: 0 additions & 25 deletions Cluster/Chart/BarChart/BarChartGroupBy.swift

This file was deleted.

35 changes: 34 additions & 1 deletion Cluster/Chart/BarChart/BarChartTimeSeries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,48 @@ import DataTransferObjects

struct BarChartTimeSeries: View {
let result: TimeSeriesQueryResult
let query: CustomQuery

var body: some View {
Chart {
ForEach(result.rows, id: \.timestamp) { row in
BarMark(
x: .value("Date", row.timestamp),
x: .value("Date", row.timestamp, unit: granularity()),
y: .value("Total Count", row.result["count"]?.value ?? 0)
)
.cornerRadius(2)
}
}
}

// 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
}
6 changes: 1 addition & 5 deletions Cluster/Chart/BarChart/ClusterBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ struct ClusterBarChart: View {
switch query.queryType {
case .timeseries:
if case let .timeSeries(result) = result {
BarChartTimeSeries(result: result)
} else {
Text("Mismatch in query type and result type 1")
BarChartTimeSeries(result: result, query: query)
}
case .topN:
if case let .topN(result) = result {
BarChartTopN(topNQueryResult: result, query: query)
} else {
Text("Mismatch in query type and result type 2")
}
default:
Text("\(query.queryType.rawValue) bar charts are not supported.")
Expand Down
2 changes: 2 additions & 0 deletions Cluster/Chart/ClusterChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct ClusterChart: View {
ClusterBarChart(query: query, result: result)
case .lineChart:
ClusterLineChart(query: query, result: result)
case .pieChart:
ClusterPieChart(query: query, result: result)
default:
Text("Not supported")
}
Expand Down
123 changes: 123 additions & 0 deletions Cluster/Chart/Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// Extensions.swift
// Telemetry Viewer (iOS)
//
// Created by Lukas on 28.05.24.
//

import Foundation
import DataTransferObjects

extension CustomQuery {
var granularityAsCalendarComponent: Calendar.Component{
switch self.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
}
}
}

extension Aggregator {
var name: String {
switch self {
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):
a.aggregator.name
}
}
}

extension DimensionSpec {
var name: String? {
switch self {
case .default(let defaultDimensionSpec):
defaultDimensionSpec.outputName
case .extraction(let extractionDimensionSpec):
extractionDimensionSpec.outputName
}
}
}

extension TopNMetricSpec {
var name: String? {
switch self {
case .numeric(let numericTopNMetricSpec):
return numericTopNMetricSpec.metric
case .inverted(let invertedTopNMetricSpec):
return invertedTopNMetricSpec.metric.name
default:
return nil
}
}
}
Loading

0 comments on commit 8547355

Please sign in to comment.