Skip to content

Commit

Permalink
Merge pull request #34 from arjunkomath/fix/33-detail-screen-crash
Browse files Browse the repository at this point in the history
Fix #33 crash
  • Loading branch information
arjunkomath authored Oct 30, 2022
2 parents c9b9fe2 + e8abed8 commit 2b59b5c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 51 deletions.
33 changes: 25 additions & 8 deletions netdata.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
55D3076F24D18F9600CF076B /* UserDefaults+Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55D3076E24D18F9600CF076B /* UserDefaults+Color.swift */; };
55D3077224D197A600CF076B /* AlarmsListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55D3077124D197A600CF076B /* AlarmsListView.swift */; };
55D3077424D19D1E00CF076B /* AlarmListRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55D3077324D19D1E00CF076B /* AlarmListRow.swift */; };
55D82951290E2032009569BD /* AlertToast in Frameworks */ = {isa = PBXBuildFile; productRef = 55D82950290E2032009569BD /* AlertToast */; };
55E91EBD24D99D6900218F57 /* WelcomeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E91EBC24D99D6900218F57 /* WelcomeScreen.swift */; };
55EE682A24CD962100546B1A /* UserSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55EE682924CD962100546B1A /* UserSettings.swift */; };
55FC8F6B24BAD8F100CA8751 /* ServerDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FC8F6A24BAD8F100CA8751 /* ServerDetailView.swift */; };
Expand Down Expand Up @@ -201,6 +202,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
55D82951290E2032009569BD /* AlertToast in Frameworks */,
552715552622EB2A00A57510 /* Bugsnag in Frameworks */,
55D1C53824B969AC0087F8A2 /* CloudKit.framework in Frameworks */,
);
Expand Down Expand Up @@ -521,6 +523,7 @@
name = netdata;
packageProductDependencies = (
552715542622EB2A00A57510 /* Bugsnag */,
55D82950290E2032009569BD /* AlertToast */,
);
productName = netdata;
productReference = 55D1C4FE24B95E6D0087F8A2 /* netdata.app */;
Expand Down Expand Up @@ -598,6 +601,7 @@
mainGroup = 55D1C4F524B95E6D0087F8A2;
packageReferences = (
552715532622EB2A00A57510 /* XCRemoteSwiftPackageReference "bugsnag-cocoa" */,
55D8294F290E2032009569BD /* XCRemoteSwiftPackageReference "AlertToast" */,
);
productRefGroup = 55D1C4FF24B95E6D0087F8A2 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -757,7 +761,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = AlarmWidgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 48;
CURRENT_PROJECT_VERSION = 49;
DEVELOPMENT_TEAM = H78GYE72WQ;
INFOPLIST_FILE = AlarmWidget/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
Expand All @@ -766,7 +770,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.11.7;
MARKETING_VERSION = 1.11.8;
PRODUCT_BUNDLE_IDENTIFIER = com.techulus.netdata.AlarmWidget;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -784,7 +788,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = AlarmWidgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 48;
CURRENT_PROJECT_VERSION = 49;
DEVELOPMENT_TEAM = H78GYE72WQ;
INFOPLIST_FILE = AlarmWidget/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
Expand All @@ -793,7 +797,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.11.7;
MARKETING_VERSION = 1.11.8;
PRODUCT_BUNDLE_IDENTIFIER = com.techulus.netdata.AlarmWidget;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -928,7 +932,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = netdata/netdata.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 48;
CURRENT_PROJECT_VERSION = 49;
DEVELOPMENT_ASSET_PATHS = "\"netdata/Preview Content\"";
DEVELOPMENT_TEAM = H78GYE72WQ;
ENABLE_PREVIEWS = YES;
Expand All @@ -938,7 +942,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.11.7;
MARKETING_VERSION = 1.11.8;
PRODUCT_BUNDLE_IDENTIFIER = com.techulus.netdata;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTS_MACCATALYST = YES;
Expand All @@ -956,7 +960,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = netdata/netdata.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 48;
CURRENT_PROJECT_VERSION = 49;
DEVELOPMENT_ASSET_PATHS = "\"netdata/Preview Content\"";
DEVELOPMENT_TEAM = H78GYE72WQ;
ENABLE_PREVIEWS = YES;
Expand All @@ -966,7 +970,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.11.7;
MARKETING_VERSION = 1.11.8;
PRODUCT_BUNDLE_IDENTIFIER = com.techulus.netdata;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTS_MACCATALYST = YES;
Expand Down Expand Up @@ -1119,6 +1123,14 @@
minimumVersion = 6.8.3;
};
};
55D8294F290E2032009569BD /* XCRemoteSwiftPackageReference "AlertToast" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/elai950/AlertToast.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -1127,6 +1139,11 @@
package = 552715532622EB2A00A57510 /* XCRemoteSwiftPackageReference "bugsnag-cocoa" */;
productName = Bugsnag;
};
55D82950290E2032009569BD /* AlertToast */ = {
isa = XCSwiftPackageProductDependency;
package = 55D8294F290E2032009569BD /* XCRemoteSwiftPackageReference "AlertToast" */;
productName = AlertToast;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 55D1C4F624B95E6D0087F8A2 /* Project object */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "alerttoast",
"kind" : "remoteSourceControl",
"location" : "https://github.com/elai950/AlertToast.git",
"state" : {
"revision" : "a4f4267f6454259ba4ed8f9dadb87334a789c26c",
"version" : "1.3.7"
}
},
{
"identity" : "bugsnag-cocoa",
"kind" : "remoteSourceControl",
Expand Down
9 changes: 8 additions & 1 deletion netdata/Core/Network/NetdataClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Combine
enum NetDataEndpoint: String {
case info = "/api/v1/info"
case charts = "/api/v1/charts"
case data = "/api/v1/data?after=-900&points=900&chart="
case data = "/api/v1/data?after=-10&points=10&chart="
case dataFor15Mins = "/api/v1/data?after=-900&points=900&chart="
case alarms = "/api/v1/alarms"
}

Expand Down Expand Up @@ -53,6 +54,12 @@ public class NetdataClient {
return try await run(requestUrl: requestUrl, basicAuthBase64: basicAuthBase64)
}

func getChartDataWithHistory(baseUrl: String, basicAuthBase64: String = "", chart: String) async throws -> ServerData {
let requestUrl = URL(string: baseUrl)!.appendingPathComponent(NetDataEndpoint.dataFor15Mins.rawValue + chart)

return try await run(requestUrl: requestUrl, basicAuthBase64: basicAuthBase64)
}

private func run<T: Decodable>(requestUrl: URL, basicAuthBase64: String) async throws -> T {
var request = URLRequest(url: requestUrl)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftUI
import AlertToast

struct CustomChartDetailView: View {
var serverChart: ServerChart
Expand All @@ -15,6 +16,9 @@ struct CustomChartDetailView: View {
@State private var chartData = ServerData(labels: [], data: [])
@State private var isLive = false

@State private var showAddedToast = false
@State private var showRemovedToast = false

@ObservedObject var userSettings = UserSettings()

let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
Expand All @@ -32,19 +36,17 @@ struct CustomChartDetailView: View {

if userSettings.bookmarks.contains(serverChart.id) {
Button(action: {
withAnimation {
userSettings.bookmarks = userSettings.bookmarks.filter { $0 != serverChart.id }
}
userSettings.bookmarks = userSettings.bookmarks.filter { $0 != serverChart.id }
showRemovedToast.toggle()
}, label: {
Label("Remove pin", systemImage: "pin")
})
.readableGuidePadding()
} else {
Button(action: {
withAnimation {
// Fix warning https://developer.apple.com/forums/thread/711899
userSettings.bookmarks.insert(serverChart.id, at: 0)
}
// Fix warning https://developer.apple.com/forums/thread/711899
userSettings.bookmarks.insert(serverChart.id, at: 0)
showAddedToast.toggle()
}, label: {
Label("Pin chart", systemImage: "pin.fill")
})
Expand All @@ -71,6 +73,12 @@ struct CustomChartDetailView: View {
PulsatingView(live: isLive)
}
}
.toast(isPresenting: $showAddedToast) {
AlertToast(displayMode: .banner(.pop), type: .complete(.green), title: "Chart Added")
}
.toast(isPresenting: $showRemovedToast) {
AlertToast(displayMode: .banner(.pop), type: .complete(.green), title: "Chart Removed")
}
}

func units() -> String {
Expand Down
2 changes: 1 addition & 1 deletion netdata/Modules/ServerDetail/ServerDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ struct ServerDetailView: View {
do {
viewModel.bookmarkedChartData[index] = try await NetdataClient.shared.getChartData(baseUrl: server.url, basicAuthBase64: server.basicAuthBase64, chart: bookmark.id)
} catch {
debugPrint("Failed to fetch chart data")
debugPrint("[\(bookmark.id)] Failed to fetch chart data")
}
}
}
Expand Down
28 changes: 18 additions & 10 deletions netdata/Modules/ServerDetail/ServerDetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,39 @@ enum DataMode {

func fetchCpu() async {
do {
let data = try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.cpu")
let data = self.dataMode == .now ?
try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.cpu") :
try await NetdataClient.shared.getChartDataWithHistory(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.cpu")

self.cpuUsage = data
self.cpuUsageData = Array(self.cpuUsage.data).reversed().map({ d in Array(d[1..<d.count]).reduce(0, { acc, val in acc + (val ?? 0) }) })

self.isLive = true
} catch {
debugPrint("Failed to fetch chart data")
debugPrint("[fetchCpu] Failed to fetch chart data")
self.isLive = false
}
}

func fetchLoad() async {
do {
self.load = try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.load")
self.load = self.dataMode == .now ?
try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.load") :
try await NetdataClient.shared.getChartDataWithHistory(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.load")

self.load1ChartData = Array(self.load.data).reversed().map({ $0[1] ?? 0 })
self.load5ChartData = Array(self.load.data).reversed().map({ $0[2] ?? 0 })
self.load15ChartData = Array(self.load.data).reversed().map({ $0[3] ?? 0 })
} catch {
debugPrint("Failed to fetch chart data")
debugPrint("[fetchLoad] Failed to fetch chart data: \(error)")
}
}

func fetchRam() async {
do {
let data = try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.ram")
let data = self.dataMode == .now ?
try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.ram") :
try await NetdataClient.shared.getChartDataWithHistory(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.ram")
self.ramUsage = data

if let dataPoint = self.ramUsage.data.first {
Expand All @@ -95,15 +101,17 @@ enum DataMode {
}
}
} catch {
debugPrint("Failed to fetch chart data")
debugPrint("[fetchRam] Failed to fetch chart data")
}
}

func fetchDiskIo() async {
do {
self.diskIO = try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.io")
self.diskIO = self.dataMode == .now ?
try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.io") :
try await NetdataClient.shared.getChartDataWithHistory(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.io")
} catch {
debugPrint("Failed to fetch chart data")
debugPrint("[fetchDiskIo] Failed to fetch chart data")
}
}

Expand All @@ -113,7 +121,7 @@ enum DataMode {
self.networkIPv4 = try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.ip")
self.networkIPv6 = try await NetdataClient.shared.getChartData(baseUrl: baseUrl, basicAuthBase64: basicAuthBase64, chart: "system.ipv6")
} catch {
debugPrint("Failed to fetch chart data")
debugPrint("[fetchNetwork] Failed to fetch chart data")
}
}

Expand All @@ -131,7 +139,7 @@ enum DataMode {
}
}
} catch {
debugPrint("Failed to fetch chart data")
debugPrint("[fetchDiskSpace] Failed to fetch chart data")
}
}

Expand Down
50 changes: 26 additions & 24 deletions netdata/Modules/Shared/Charts/DataGrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ struct DataGrid: View {
var data: [[Double?]]
var dataType: GridDataType
var showArrows: Bool

var body: some View {
if labels.count > 1 && self.data.count > 0 {
LazyVGrid(columns: self.getGridColumns(), alignment: .leading, spacing: 8) {
ForEach(1..<self.labels.count) { i in
if self.dataType == .percentage {
PercentageUsageData(usage: CGFloat(self.data.first![i] ?? 0),
title: self.labels[i])
}
if self.dataType == .absolute {
AbsoluteUsageData(usage: CGFloat(self.data.first![i] ?? 0),
title: self.labels[i],
showArrows: self.showArrows)
}
if self.dataType == .secondsToHours {
AbsoluteUsageData(usage: CGFloat(self.data.first![i] ?? 0),
title: self.labels[i],
showArrows: self.showArrows,
convertSecondsToHours: true)
ForEach(Array(labels.enumerated()), id: \.offset) { i, label in
if (i > 0) { // First label is time, hence ignored
if dataType == .percentage {
PercentageUsageData(usage: CGFloat(self.data[0][i] ?? 0),
title: label)
}
if dataType == .absolute {
AbsoluteUsageData(usage: CGFloat(self.data[0][i] ?? 0),
title: label,
showArrows: showArrows)
}
if dataType == .secondsToHours {
AbsoluteUsageData(usage: CGFloat(self.data[0][i] ?? 0),
title: label,
showArrows: showArrows,
convertSecondsToHours: true)
}
}
}
}
Expand All @@ -46,28 +48,28 @@ struct DataGrid: View {
AbsoluteUsageData(usage: 0.1,
title: "loading",
showArrows: false)
.redacted(reason: .placeholder)
.redacted(reason: .placeholder)
}
}
}
}

func getGridColumns() -> [GridItem] {
#if targetEnvironment(macCatalyst)
#if targetEnvironment(macCatalyst)
return [GridItem(.adaptive(minimum: 120))]
#else
#else
var columnWidth: CGFloat = self.dataType == GridDataType.percentage ? 65 : 80

if UIDevice.current.userInterfaceIdiom == .pad {
columnWidth = 120
}

if self.showArrows {
columnWidth = 120
}

return [GridItem(.adaptive(minimum: columnWidth))]
#endif
#endif
}
}

Expand Down

0 comments on commit 2b59b5c

Please sign in to comment.