Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/arjunkomath/netdata-ios int…
Browse files Browse the repository at this point in the history
…o main

# Conflicts:
#	netdata/Modules/ServerDetail/ServerDetailView.swift
#	netdata/Modules/ServerList/ServerListView.swift
  • Loading branch information
arjunkomath committed Dec 6, 2020
2 parents 49a0128 + 8706ca1 commit 4c3e781
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 166 deletions.
25 changes: 25 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"files": [
"README.md"
],
"imageSize": 100,
"commit": false,
"contributors": [
{
"login": "kobimx",
"name": "Mirko Milovanovic",
"avatar_url": "https://avatars3.githubusercontent.com/u/1266640?v=4",
"profile": "https://github.com/kobimx",
"contributions": [
"code",
"design"
]
}
],
"contributorsPerLine": 7,
"projectName": "netdata-ios",
"projectOwner": "arjunkomath",
"repoType": "github",
"repoHost": "https://github.com",
"skipCi": true
}
2 changes: 1 addition & 1 deletion NetdataClip/MainView/ServerDetailDemoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct ServerDetailDemoView: View {
.readableGuidePadding()
}
.onAppear {
self.viewModel.fetch(baseUrl: serverUrl)
self.viewModel.fetch(baseUrl: serverUrl, basicAuthBase64: "")

// hide scroll indicators
UITableView.appearance().showsVerticalScrollIndicator = false
Expand Down
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Netdata client for iOS, iPadOS & macOS
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
![Xcode build](https://github.com/arjunkomath/netdata-ios/workflows/Xcode%20build/badge.svg)

### A beautiful and minimal client for Netdata that allows you to monitor cloud infrastructure in real-time.

## [Download on the App Store](https://apps.apple.com/us/app/netdata-server-monitoring/id1525322870)

*Warning! I'm learning Swift and SwiftUI, so the code is probably terrible.*

![Xcode build](https://github.com/arjunkomath/netdata-ios/workflows/Xcode%20build/badge.svg)

![screenshot-1](https://github.com/arjunkomath/netdata-ios/blob/main/screenshots/mockup.png?raw=true)

## Features
Expand All @@ -24,3 +26,22 @@ Open-source iOS, iPadOS and macOS client
## Netdata

[Netdata](https://github.com/netdata/netdata) is distributed, real-time performance and health monitoring for systems and applications. It is a highly-optimized monitoring agent you install on all your systems and containers.

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://github.com/kobimx"><img src="https://avatars3.githubusercontent.com/u/1266640?v=4" width="100px;" alt=""/><br /><sub><b>Mirko Milovanovic</b></sub></a><br /><a href="https://github.com/arjunkomath/netdata-ios/commits?author=kobimx" title="Code">💻</a> <a href="#design-kobimx" title="Design">🎨</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
8 changes: 4 additions & 4 deletions netdata.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = NetdataClip/NetdataClip.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 15;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_ASSET_PATHS = "\"NetdataClip/Preview Content\"";
DEVELOPMENT_TEAM = H78GYE72WQ;
ENABLE_PREVIEWS = YES;
Expand All @@ -1091,7 +1091,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = NetdataClip/NetdataClip.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 15;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_ASSET_PATHS = "\"NetdataClip/Preview Content\"";
DEVELOPMENT_TEAM = H78GYE72WQ;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -1320,7 +1320,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = netdata/netdata.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 15;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_ASSET_PATHS = "\"netdata/Preview Content\"";
DEVELOPMENT_TEAM = H78GYE72WQ;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -1350,7 +1350,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = netdata/netdata.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 15;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_ASSET_PATHS = "\"netdata/Preview Content\"";
DEVELOPMENT_TEAM = H78GYE72WQ;
ENABLE_PREVIEWS = YES;
Expand Down
28 changes: 28 additions & 0 deletions netdata/Core/Network/Agent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import Foundation
import Combine

enum APIError: Error {
case userIsOffline
case authenticationFailed
case somethingWentWrong
}

struct Agent {
struct Response<T> {
let value: T
Expand All @@ -18,9 +24,31 @@ struct Agent {
return URLSession.shared
.dataTaskPublisher(for: request)
.tryMap { result -> Response<T> in
guard let httpResponse = result.response as? HTTPURLResponse,
httpResponse.statusCode > 0 else {
throw URLError(.unknown)
}

// Handle basic authentication error
if httpResponse.statusCode == 401 {
throw URLError(.userAuthenticationRequired)
}

let value = try decoder.decode(T.self, from: result.data)
return Response(value: value, response: result.response)
}
.mapError { error -> APIError in
debugPrint(error)

switch error {
case URLError.notConnectedToInternet:
return .userIsOffline
case URLError.userAuthenticationRequired:
return .authenticationFailed
default:
return .somethingWentWrong
}
}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
Expand Down
33 changes: 19 additions & 14 deletions netdata/Core/Network/NetDataAPI.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// Network.swift
// fiftybest
//
// Created by thomas on 7/1/20.
// Copyright © 2020 thomas. All rights reserved.
Expand All @@ -21,31 +20,37 @@ enum NetDataAPI {
}

extension NetDataAPI {
static func getInfo(baseUrl: String) -> AnyPublisher<ServerInfo, Error> {
let base = URL(string: baseUrl)!
static func getInfo(baseUrl: String, basicAuthBase64: String = "") -> AnyPublisher<ServerInfo, Error> {
let requestUrl = URL(string: baseUrl)!.appendingPathComponent(NetDataEndpoint.info.rawValue)

return run(URLRequest(url: base.appendingPathComponent(NetDataEndpoint.info.rawValue)))
return run(requestUrl: requestUrl, basicAuthBase64: basicAuthBase64)
}

static func getCharts(baseUrl: String) -> AnyPublisher<ServerCharts, Error> {
let base = URL(string: baseUrl)!
static func getCharts(baseUrl: String, basicAuthBase64: String = "") -> AnyPublisher<ServerCharts, Error> {
let requestUrl = URL(string: baseUrl)!.appendingPathComponent(NetDataEndpoint.charts.rawValue)

return run(URLRequest(url: base.appendingPathComponent(NetDataEndpoint.charts.rawValue)))
return run(requestUrl: requestUrl, basicAuthBase64: basicAuthBase64)
}

static func getChartData(baseUrl: String, chart: String) -> AnyPublisher<ServerData, Error> {
let base = URL(string: baseUrl)!
static func getChartData(baseUrl: String, basicAuthBase64: String = "", chart: String) -> AnyPublisher<ServerData, Error> {
let requestUrl = URL(string: baseUrl)!.appendingPathComponent(NetDataEndpoint.data.rawValue + chart)

return run(URLRequest(url: base.appendingPathComponent(NetDataEndpoint.data.rawValue + chart)))
return run(requestUrl: requestUrl, basicAuthBase64: basicAuthBase64)
}

static func getAlarms(baseUrl: String) -> AnyPublisher<ServerAlarms, Error> {
let base = URL(string: baseUrl)!
static func getAlarms(baseUrl: String, basicAuthBase64: String = "") -> AnyPublisher<ServerAlarms, Error> {
let requestUrl = URL(string: baseUrl)!.appendingPathComponent(NetDataEndpoint.alarms.rawValue)

return run(URLRequest(url: base.appendingPathComponent(NetDataEndpoint.alarms.rawValue)))
return run(requestUrl: requestUrl, basicAuthBase64: basicAuthBase64)
}

static func run<T: Decodable>(_ request: URLRequest) -> AnyPublisher<T, Error> {
static func run<T: Decodable>(requestUrl: URL, basicAuthBase64: String) -> AnyPublisher<T, Error> {
var request = URLRequest(url: requestUrl)

if !basicAuthBase64.isEmpty {
request.setValue("Basic \(basicAuthBase64)", forHTTPHeaderField: "Authorization")
}

return agent.run(request)
.map(\.value)
.eraseToAnyPublisher()
Expand Down
10 changes: 8 additions & 2 deletions netdata/Models/NDServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public struct NDServer: CloudModel, Equatable, Identifiable {
public let serverInfoJson: String
public let isFavourite: Int

// authentication
public let basicAuthBase64: String

public var record: CKRecord?
public let serverInfo: ServerInfo?

Expand All @@ -26,19 +29,20 @@ public struct NDServer: CloudModel, Equatable, Identifiable {
}

enum RecordKeys: String {
case id, name, description, url, serverInfoJson, isFavourite
case id, name, description, url, serverInfoJson, isFavourite, basicAuthBase64
}

public static func == (lhs: NDServer, rhs: NDServer) -> Bool {
lhs.id == rhs.id
}

public init(name: String, description: String, url: String, serverInfo: ServerInfo?, isFavourite: Int?) {
public init(name: String, description: String, url: String, serverInfo: ServerInfo?, basicAuthBase64: String?, isFavourite: Int?) {
self.id = UUID().uuidString
self.name = name
self.description = description
self.url = url
self.serverInfo = serverInfo
self.basicAuthBase64 = basicAuthBase64 == nil ? "" : basicAuthBase64!
self.isFavourite = isFavourite == nil ? 0 : isFavourite!

if (serverInfo != nil) {
Expand All @@ -56,6 +60,7 @@ public struct NDServer: CloudModel, Equatable, Identifiable {
self.description = record[RecordKeys.description.rawValue] as? String ?? ""
self.url = record[RecordKeys.url.rawValue] as? String ?? ""
self.serverInfoJson = record[RecordKeys.serverInfoJson.rawValue] as? String ?? ""
self.basicAuthBase64 = record[RecordKeys.basicAuthBase64.rawValue] as? String ?? ""
self.isFavourite = record[RecordKeys.isFavourite.rawValue] as? Int ?? 0

self.record = record
Expand All @@ -70,6 +75,7 @@ public struct NDServer: CloudModel, Equatable, Identifiable {
record[RecordKeys.description.rawValue] = description
record[RecordKeys.url.rawValue] = url
record[RecordKeys.serverInfoJson.rawValue] = serverInfoJson
record[RecordKeys.basicAuthBase64.rawValue] = basicAuthBase64
record[RecordKeys.isFavourite.rawValue] = isFavourite
return record
}
Expand Down
7 changes: 5 additions & 2 deletions netdata/Modules/ServerDetail/Components/ChartsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct ChartsListView: View {
@Environment(\.presentationMode) private var presentationMode
var serverCharts: ServerCharts
var serverUrl: String
var basicAuthBase64: String

@State private var searchText = ""

Expand All @@ -23,7 +24,8 @@ struct ChartsListView: View {
ForEach(serverCharts.charts.keys.sorted().filter({ searchText.isEmpty ? true : $0.contains(searchText) }), id: \.self) { key in
if serverCharts.charts[key] != nil && serverCharts.charts[key]!.enabled == true {
NavigationLink(destination: CustomChartDetailView(serverChart: serverCharts.charts[key]!,
serverUrl: serverUrl)) {
serverUrl: serverUrl,
basicAuthBase64: basicAuthBase64)) {
ChartListRow(chart: serverCharts.charts[key]!)
}
}
Expand Down Expand Up @@ -52,6 +54,7 @@ struct ChartsListView_Previews: PreviewProvider {
ChartsListView(serverCharts: ServerCharts(version: "1.0",
release_channel: "beta",
charts: [:]),
serverUrl: "")
serverUrl: "",
basicAuthBase64: "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
struct CustomChartDetailView: View {
var serverChart: ServerChart
var serverUrl: String
var basicAuthBase64: String

@StateObject var viewModel = ServerDetailViewModel()

Expand All @@ -29,7 +30,7 @@ struct CustomChartDetailView: View {
}
.listStyle(InsetGroupedListStyle())
.onAppear {
viewModel.fetchCustomChartData(baseUrl: serverUrl, chart: serverChart.name)
viewModel.fetchCustomChartData(baseUrl: serverUrl, basicAuthBase64: basicAuthBase64, chart: serverChart.name)
}
.onDisappear {
viewModel.destroyCustomChartData()
Expand Down
Loading

0 comments on commit 4c3e781

Please sign in to comment.