Skip to content

Commit

Permalink
Merge pull request #48 from arjunkomath/main
Browse files Browse the repository at this point in the history
Use Swift charts
  • Loading branch information
arjunkomath authored Dec 27, 2023
2 parents 46d05f7 + 01a14ed commit 7194011
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 180 deletions.
6 changes: 6 additions & 0 deletions AlarmWidget/AlarmWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ struct AlarmWidgetEntryView : View {
Text("Unsupported")
case .systemExtraLarge:
Text("Unsupported")
case .accessoryCircular:
Text("unknown")
case .accessoryRectangular:
Text("unknown")
case .accessoryInline:
Text("unknown")
@unknown default: Text("unknown")
}
}
Expand Down
4 changes: 0 additions & 4 deletions netdata.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
552862E524CD018B008E2B0E /* ErrorMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 552862E424CD018B008E2B0E /* ErrorMessage.swift */; };
552C2C2624D6B9BB00D3DFBE /* CustomChartDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 552C2C2524D6B9BB00D3DFBE /* CustomChartDetailView.swift */; };
552E19D324CDB71500CDB97F /* EditServerForm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 552E19D224CDB71500CDB97F /* EditServerForm.swift */; };
553DF8B12733DEA00067047A /* InfoMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553DF8B02733DEA00067047A /* InfoMessage.swift */; };
5548A9BC273147FD003ACBBD /* ChartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5548A9BB273147FD003ACBBD /* ChartView.swift */; };
555401A324D850D100B1B727 /* CustomTextStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 555401A224D850D100B1B727 /* CustomTextStyle.swift */; };
556064BE25C7E8CA0018FA14 /* MediumWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 556064BD25C7E8CA0018FA14 /* MediumWidget.swift */; };
Expand Down Expand Up @@ -131,7 +130,6 @@
552B4E7924CD16C0008B9DA6 /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = System/Library/Frameworks/SwiftUI.framework; sourceTree = SDKROOT; };
552C2C2524D6B9BB00D3DFBE /* CustomChartDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomChartDetailView.swift; sourceTree = "<group>"; };
552E19D224CDB71500CDB97F /* EditServerForm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditServerForm.swift; sourceTree = "<group>"; };
553DF8B02733DEA00067047A /* InfoMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoMessage.swift; sourceTree = "<group>"; };
5548A9BB273147FD003ACBBD /* ChartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartView.swift; sourceTree = "<group>"; };
555401A224D850D100B1B727 /* CustomTextStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextStyle.swift; sourceTree = "<group>"; };
556064BD25C7E8CA0018FA14 /* MediumWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediumWidget.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -230,7 +228,6 @@
5508041224D04A79002FFAA5 /* UI */ = {
isa = PBXGroup;
children = (
553DF8B02733DEA00067047A /* InfoMessage.swift */,
5548A9BB273147FD003ACBBD /* ChartView.swift */,
55C638FE24CC1A4D00395B23 /* BorderedBarButtonStyle.swift */,
555401A224D850D100B1B727 /* CustomTextStyle.swift */,
Expand Down Expand Up @@ -589,7 +586,6 @@
55FC8F6B24BAD8F100CA8751 /* ServerDetailView.swift in Sources */,
55BFBA2924CC5875004461D3 /* ServerListViewModel.swift in Sources */,
55D3077424D19D1E00CF076B /* AlarmListRow.swift in Sources */,
553DF8B12733DEA00067047A /* InfoMessage.swift in Sources */,
5548A9BC273147FD003ACBBD /* ChartView.swift in Sources */,
55A3A0EF2B368DC200E4DB07 /* ServerDetailItem.swift in Sources */,
558CABAC24B99931002D115B /* CloudModel.swift in Sources */,
Expand Down
48 changes: 16 additions & 32 deletions netdata/Modules/ServerDetail/ServerDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ struct ServerDetailView: View {
showArrows: false)

case .fifteenMins:
ChartView(datas: [viewModel.cpuUsageData])
.frame(height: 240)
.padding()
.listRowInsets(.init())
ChartView(data: viewModel.cpuUsage)
.frame(height: 280)
}
}
}
Expand All @@ -63,31 +61,21 @@ struct ServerDetailView: View {
showArrows: false)

case .fifteenMins:
ChartView(datas: [viewModel.ramChartData], max: viewModel.ramMax)
.frame(height: 240)
.padding()
.listRowInsets(.init())
ChartView(data: viewModel.ramUsage)
.frame(height: 105)
}
}
}

RedactedView(loading: viewModel.diskSpaceUsage.labels.count < 1) {
ServerDetailItem(label: "Space (GiB)") {
switch (viewModel.dataMode) {
case .now:
Meter(progress: viewModel.diskSpaceUsageGauge)
.redacted(reason: viewModel.diskSpaceUsage.labels.count < 1 ? .placeholder : .init())

DataGrid(labels: viewModel.diskSpaceUsage.labels,
data: viewModel.diskSpaceUsage.data,
dataType: .absolute,
showArrows: false)
case .fifteenMins:
ChartView(datas: [viewModel.diskChartData], max: viewModel.diskMax)
.frame(height: 240)
.padding()
.listRowInsets(.init())
}
Meter(progress: viewModel.diskSpaceUsageGauge)
.redacted(reason: viewModel.diskSpaceUsage.labels.count < 1 ? .placeholder : .init())

DataGrid(labels: viewModel.diskSpaceUsage.labels,
data: viewModel.diskSpaceUsage.data,
dataType: .absolute,
showArrows: false)
}
}
}
Expand All @@ -103,10 +91,8 @@ struct ServerDetailView: View {
showArrows: false)

case .fifteenMins:
ChartView(datas: [viewModel.load1ChartData, viewModel.load5ChartData, viewModel.load15ChartData], max: 1)
.frame(height: 250)
.padding()
.listRowInsets(.init())
ChartView(data: viewModel.load)
.frame(height: 105)
}
}
}
Expand Down Expand Up @@ -150,7 +136,7 @@ struct ServerDetailView: View {
}
.padding(16)

if viewModel.bookmarkedChartData.count > 0 && viewModel.dataMode == .now {
if viewModel.bookmarkedChartData.count > 0 {
VStack(alignment: .leading, spacing: 12) {
Label("Pinned Charts", systemImage: "pin.circle.fill")

Expand Down Expand Up @@ -209,11 +195,9 @@ struct ServerDetailView: View {
}
.navigationBarTitle(server.name)
.toolbar {
ToolbarItem(placement: .navigation) {
ToolbarItemGroup(placement: .navigation) {
PulsatingView(live: viewModel.isLive)
}

ToolbarItem(placement: .principal) {

if userSettings.enableCharts {
Picker("Data mode", selection: $viewModel.dataMode) {
Text("Now").tag(DataMode.now)
Expand Down
21 changes: 0 additions & 21 deletions netdata/Modules/ServerDetail/ServerDetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,6 @@ enum DataMode {
@Published var isLive: Bool = false
@Published var dataMode: DataMode = .now

// MARK:- Charts
@Published var ramChartData: [Double] = []
@Published var ramMax: Double = 0

@Published var load1ChartData: [Double] = []
@Published var load5ChartData: [Double] = []
@Published var load15ChartData: [Double] = []

@Published var diskChartData: [Double] = []
@Published var diskMax: Double = 0

var baseUrl = ""
var basicAuthBase64 = ""

Expand All @@ -76,10 +65,6 @@ enum DataMode {
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("[fetchLoad] Failed to fetch chart data: \(error)")
}
Expand All @@ -95,9 +80,6 @@ enum DataMode {
if let dataPoint = self.ramUsage.data.first {
if let free = dataPoint[1], let used = dataPoint[2], let cached = dataPoint[3] {
self.ramUsageGauge = CGFloat(used / (free + used + cached))

self.ramChartData = Array(self.ramUsage.data).reversed().map({ $0[2] ?? 0 })
self.ramMax = free + used + cached
}
}
} catch {
Expand Down Expand Up @@ -133,9 +115,6 @@ enum DataMode {
if let dataPoint = self.diskSpaceUsage.data.first {
if let avail = dataPoint[1], let used = dataPoint[2] {
self.diskSpaceUsageGauge = CGFloat(used / (avail + used))

self.diskChartData = Array(self.diskSpaceUsage.data).reversed().map({ $0[2] ?? 0 })
self.diskMax = avail + used
}
}
} catch {
Expand Down
28 changes: 16 additions & 12 deletions netdata/Modules/ServerList/Components/AddServerForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import SwiftUI
import Combine
import AlertToast

struct AddServerForm: View {
@Environment(\.presentationMode) private var presentationMode
@StateObject var viewModel = ServerListViewModel()

var body: some View {
NavigationView {
Form {
Expand All @@ -25,10 +26,6 @@ struct AddServerForm: View {

Section(header: makeSectionHeader(text: "Server details"),
footer: Text("HTTPS is required for connections over the internet\nHTTP is allowed for LAN connections with IP or mDNS domains")) {
if viewModel.validationError {
ErrorMessage(message: viewModel.validationErrorMessage)
}

TextField("Name", text: $viewModel.name)
TextField("Description", text: $viewModel.description)
TextField("NetData Server Full URL", text: $viewModel.url)
Expand All @@ -45,10 +42,6 @@ struct AddServerForm: View {
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}

if viewModel.basicAuthvalidationError {
ErrorMessage(message: viewModel.basicAuthvalidationErrorMessage)
}

if viewModel.enableBasicAuth {
TextField("Username", text: $viewModel.basicAuthUsername)
.autocapitalization(UITextAutocapitalizationType.none)
Expand All @@ -61,6 +54,20 @@ struct AddServerForm: View {
await self.addServer()
}
}
.toast(isPresenting: $viewModel.validationError, duration: 5) {
AlertToast(
displayMode: .banner(.pop),
type: .error(.red),
title: viewModel.validationErrorMessage
)
}
.toast(isPresenting: $viewModel.basicAuthvalidationError, duration: 5) {
AlertToast(
displayMode: .banner(.pop),
type: .error(.red),
title: viewModel.basicAuthvalidationErrorMessage
)
}
.submitLabel(.done)
.navigationBarTitle("Add Server", displayMode: .inline)
.toolbar {
Expand Down Expand Up @@ -122,9 +129,6 @@ struct AddServerForm: View {
}
.buttonStyle(BorderedBarButtonStyle())
.disabled(viewModel.validatingUrl)
.alert(isPresented: $viewModel.invalidUrlAlert) {
Alert(title: Text("Oops!"), message: Text("You've entered an invalid URL"), dismissButton: .default(Text("OK")))
}
}

func addServer() async {
Expand Down
26 changes: 15 additions & 11 deletions netdata/Modules/ServerList/Components/EditServerForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftUI
import AlertToast
import Combine

struct EditServerForm: View {
Expand All @@ -20,10 +21,6 @@ struct EditServerForm: View {
Form {
Section(header: makeSectionHeader(text: "Server details"),
footer: Text("HTTPS is required for connections over the internet\nHTTP is allowed for LAN connections with IP or mDNS domains")) {
if viewModel.validationError {
ErrorMessage(message: viewModel.validationErrorMessage)
}

TextField("Name", text: $viewModel.name)
TextField("Description", text: $viewModel.description)
TextField("NetData Server Full URL", text: $viewModel.url)
Expand All @@ -40,10 +37,6 @@ struct EditServerForm: View {
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}

if viewModel.basicAuthvalidationError {
ErrorMessage(message: viewModel.basicAuthvalidationErrorMessage)
}

if viewModel.enableBasicAuth {
TextField("Username", text: $viewModel.basicAuthUsername)
.autocapitalization(UITextAutocapitalizationType.none)
Expand All @@ -56,6 +49,20 @@ struct EditServerForm: View {
await self.updateServer()
}
}
.toast(isPresenting: $viewModel.validationError, duration: 5) {
AlertToast(
displayMode: .banner(.pop),
type: .error(.red),
title: viewModel.validationErrorMessage
)
}
.toast(isPresenting: $viewModel.basicAuthvalidationError, duration: 5) {
AlertToast(
displayMode: .banner(.pop),
type: .error(.red),
title: viewModel.basicAuthvalidationErrorMessage
)
}
.submitLabel(.done)
.navigationBarTitle("Edit Server", displayMode: .inline)
.toolbar {
Expand Down Expand Up @@ -103,9 +110,6 @@ struct EditServerForm: View {
}
.buttonStyle(BorderedBarButtonStyle())
.disabled(viewModel.validatingUrl)
.alert(isPresented: $viewModel.invalidUrlAlert) {
Alert(title: Text("Oops!"), message: Text("You've entered an invalid URL"), dismissButton: .default(Text("OK")))
}
}

func updateServer() async {
Expand Down
4 changes: 2 additions & 2 deletions netdata/Modules/ServerList/ServerListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import SwiftUI
@Published var basicAuthvalidationErrorMessage = ""

@Published var validatingUrl = false
@Published var invalidUrlAlert = false
@Published var validationError = false
@Published var validationErrorMessage = ""

Expand Down Expand Up @@ -126,7 +125,8 @@ import SwiftUI
self.basicAuthvalidationError = false

if (!validateUrl(urlString: self.url)) {
invalidUrlAlert = true
validationError = true
validationErrorMessage = "Please enter a valid URL"
return false
}

Expand Down
4 changes: 2 additions & 2 deletions netdata/Modules/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ struct SettingsView: View {
}

Toggle(isOn: $userSettings.hapticFeedback) {
Label("Haptic feedback", systemImage: "waveform.path")
Label("Haptic Feedback", systemImage: "waveform.path")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))

Toggle(isOn: $userSettings.enableCharts) {
Label("Show charts", systemImage: "chart.xyaxis.line")
Label("Charts", systemImage: "chart.xyaxis.line")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}
Expand Down
Loading

0 comments on commit 7194011

Please sign in to comment.