Skip to content

Commit

Permalink
Merge pull request #49 from arjunkomath/main
Browse files Browse the repository at this point in the history
Fix charts and lot of clean up
  • Loading branch information
arjunkomath authored Dec 28, 2023
2 parents 7194011 + ff156e7 commit 78680e1
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 113 deletions.
21 changes: 17 additions & 4 deletions AlarmWidget/AlarmWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import WidgetKit
import Combine
import SwiftUI

extension View {
func widgetBackground(_ backgroundView: some View) -> some View {
if #available(iOSApplicationExtension 17.0, *) {
return containerBackground(for: .widget) {
backgroundView
}
} else {
return background(backgroundView)
}
}
}

struct Provider: TimelineProvider {

func placeholder(in context: Context) -> AlarmsEntry {
Expand Down Expand Up @@ -48,8 +60,8 @@ struct Provider: TimelineProvider {
let serverAlarm = try await NetdataClient.shared.getAlarms(baseUrl: server.url, basicAuthBase64: server.basicAuthBase64)

totalAlarmsCount += serverAlarm.alarms.count
criticalAlarmsCount += serverAlarm.getCriticalAlarmsCount()
alarms[server.name] = serverAlarm.getCriticalAlarmsCount() > 0 ? Color.red : serverAlarm.alarms.count > 0 ? Color.orange : Color.green;
criticalAlarmsCount += serverAlarm.criticalAlarmsCount
alarms[server.name] = serverAlarm.criticalAlarmsCount > 0 ? Color.red : serverAlarm.alarms.count > 0 ? Color.orange : Color.green;
} catch {
debugPrint("Fetch Alarms failed", server.name, error)
}
Expand Down Expand Up @@ -99,7 +111,7 @@ struct AlarmWidgetEntryView : View {
case .systemExtraLarge:
Text("Unsupported")
case .accessoryCircular:
Text("unknown")
AlarmCircularView(entry: entry)
case .accessoryRectangular:
Text("unknown")
case .accessoryInline:
Expand All @@ -116,8 +128,9 @@ struct AlarmWidget: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
AlarmWidgetEntryView(entry: entry)
.widgetBackground(Color.black)
}
.supportedFamilies([.systemSmall, .systemMedium])
.supportedFamilies([.systemSmall, .systemMedium, .accessoryCircular])
.configurationDisplayName("Alarms")
.description("Shows the count of active alarms in your favourite servers")
}
Expand Down
28 changes: 28 additions & 0 deletions AlarmWidget/Vairants/AlarmCircularView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// AlarmCircularView.swift
// AlarmWidgetExtension
//
// Created by Arjun on 28/12/2023.
//

import SwiftUI

struct AlarmCircularView: View {
var entry: Provider.Entry

var body: some View {
Gauge(
value: max(Double(entry.criticalCount), 0),
in: 0...max(Double(entry.count), 0)
) {
Image(systemName: "externaldrive.fill.badge.xmark")
} currentValueLabel: {
Text("\(entry.criticalCount)")
}
.gaugeStyle(.accessoryCircular)
}
}

#Preview {
AlarmCircularView(entry: AlarmsEntry(serverCount: 1, count: 1, criticalCount: 0, alarms: ["CDN77": Color.red, "London": Color.green, "Test": Color.orange], date: Date()))
}
File renamed without changes.
File renamed without changes.
36 changes: 30 additions & 6 deletions netdata.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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 */; };
558843052B3D432600C843C4 /* AlarmCircularView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 558843042B3D432600C843C4 /* AlarmCircularView.swift */; };
558CABAC24B99931002D115B /* CloudModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 558CABAB24B99931002D115B /* CloudModel.swift */; };
558CABAF24B999F7002D115B /* ServerService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 558CABAE24B999F7002D115B /* ServerService.swift */; };
558CABB124B99B42002D115B /* CloudService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 558CABB024B99B42002D115B /* CloudService.swift */; };
Expand Down Expand Up @@ -75,6 +76,7 @@
55E91EBD24D99D6900218F57 /* WelcomeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E91EBC24D99D6900218F57 /* WelcomeScreen.swift */; };
55EE682A24CD962100546B1A /* UserSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55EE682924CD962100546B1A /* UserSettings.swift */; };
55F1B1F12B35951200A97F3B /* SupportOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55F1B1F02B35951200A97F3B /* SupportOptions.swift */; };
55FA3AB12B3CCF30002E6DE1 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 55FA3AB02B3CCF30002E6DE1 /* Alamofire */; };
55FC8F6B24BAD8F100CA8751 /* ServerDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FC8F6A24BAD8F100CA8751 /* ServerDetailView.swift */; };
55FC8F6E24BADA5B00CA8751 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FC8F6D24BADA5B00CA8751 /* SettingsView.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -133,6 +135,7 @@
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>"; };
558843042B3D432600C843C4 /* AlarmCircularView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlarmCircularView.swift; sourceTree = "<group>"; };
558CABAB24B99931002D115B /* CloudModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudModel.swift; sourceTree = "<group>"; };
558CABAE24B999F7002D115B /* ServerService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerService.swift; sourceTree = "<group>"; };
558CABB024B99B42002D115B /* CloudService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudService.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -182,6 +185,7 @@
buildActionMask = 2147483647;
files = (
5514045725C6969C0081E4A4 /* CloudKit.framework in Frameworks */,
55FA3AB12B3CCF30002E6DE1 /* Alamofire in Frameworks */,
5514041A25C68F4F0081E4A4 /* SwiftUI.framework in Frameworks */,
5514041925C68F4F0081E4A4 /* WidgetKit.framework in Frameworks */,
);
Expand Down Expand Up @@ -262,11 +266,10 @@
5514041B25C68F4F0081E4A4 /* AlarmWidget */ = {
isa = PBXGroup;
children = (
558843032B3D42EC00C843C4 /* Vairants */,
5514041C25C68F4F0081E4A4 /* AlarmWidget.swift */,
5514041E25C68F510081E4A4 /* Assets.xcassets */,
5514042025C68F510081E4A4 /* Info.plist */,
55A8FE6B25C7E4AD00E2C2F8 /* SmallWidget.swift */,
556064BD25C7E8CA0018FA14 /* MediumWidget.swift */,
);
path = AlarmWidget;
sourceTree = "<group>";
Expand All @@ -282,6 +285,16 @@
path = Charts;
sourceTree = "<group>";
};
558843032B3D42EC00C843C4 /* Vairants */ = {
isa = PBXGroup;
children = (
556064BD25C7E8CA0018FA14 /* MediumWidget.swift */,
55A8FE6B25C7E4AD00E2C2F8 /* SmallWidget.swift */,
558843042B3D432600C843C4 /* AlarmCircularView.swift */,
);
path = Vairants;
sourceTree = "<group>";
};
558CABAA24B99923002D115B /* Protocols */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -461,6 +474,9 @@
dependencies = (
);
name = AlarmWidgetExtension;
packageProductDependencies = (
55FA3AB02B3CCF30002E6DE1 /* Alamofire */,
);
productName = AlarmWidgetExtension;
productReference = 5514041825C68F4F0081E4A4 /* AlarmWidgetExtension.appex */;
productType = "com.apple.product-type.app-extension";
Expand Down Expand Up @@ -572,6 +588,7 @@
5514043725C6905A0081E4A4 /* CloudService.swift in Sources */,
556064BE25C7E8CA0018FA14 /* MediumWidget.swift in Sources */,
5514047B25C6AF750081E4A4 /* ServerCharts.swift in Sources */,
558843052B3D432600C843C4 /* AlarmCircularView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -658,7 +675,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,6";
Expand Down Expand Up @@ -686,7 +703,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,6";
Expand Down Expand Up @@ -747,6 +764,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
MACOSX_DEPLOYMENT_TARGET = 14.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -804,6 +822,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
MACOSX_DEPLOYMENT_TARGET = 14.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -836,7 +855,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.techulus.netdata;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,6";
Expand Down Expand Up @@ -866,7 +885,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.techulus.netdata;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,6";
Expand Down Expand Up @@ -958,6 +977,11 @@
package = 55E2FCCE2B370ACD00AB15A2 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */;
productName = FirebaseMessaging;
};
55FA3AB02B3CCF30002E6DE1 /* Alamofire */ = {
isa = XCSwiftPackageProductDependency;
package = 5525DA1B2B395C4100F56457 /* XCRemoteSwiftPackageReference "Alamofire" */;
productName = Alamofire;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 55D1C4F624B95E6D0087F8A2 /* Project object */;
Expand Down
11 changes: 1 addition & 10 deletions netdata/Core/Data/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ class UserSettings: ObservableObject {
UserDefaults.standard.set(hapticFeedback, forKey: "hapticFeedback")
}
}

@Published var enableCharts: Bool {
didSet {
UserDefaults.standard.set(enableCharts, forKey: "enableCharts")
}
}


// MARK: - Charts

@Published var bookmarks: [String] {
Expand All @@ -73,9 +67,6 @@ class UserSettings: ObservableObject {
Color(UserDefaults.standard.colorForKey(key: "appTintColor")!) : Color.blue
self.hapticFeedback = UserDefaults.standard.object(forKey: "hapticFeedback") as? Bool ?? true

// Features
self.enableCharts = UserDefaults.standard.object(forKey: "enableCharts") as? Bool ?? false

// Charts
self.bookmarks = NSUbiquitousKeyValueStore.default.array(forKey: "bookmarks") as? [String] ?? []

Expand Down
4 changes: 2 additions & 2 deletions netdata/Core/Network/AuthAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ struct AuthAPI {
static let API_BASE_URL = "https://netdata.techulus.com/auth"
#endif

static func createToken(uid: String) async throws -> AuthTokenResult{
static func createToken(uid: String) async throws -> AuthTokenResult {
let parameters: [String: String] = [
"userId": uid
]
]

return try await withCheckedThrowingContinuation { continuation in
AF.request(
Expand Down
44 changes: 26 additions & 18 deletions netdata/Core/Network/NetdataClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
import Combine
import Alamofire

enum NetDataEndpoint: String {
case info = "/api/v1/info"
Expand All @@ -22,9 +22,6 @@ enum APIError: Error {
case invalidRequest
}

@available(iOS 15.0, *)
@available(macOS 12.0, *)
@available(tvOS 15.0, *)
public class NetdataClient {
public static let shared = NetdataClient()

Expand Down Expand Up @@ -52,8 +49,8 @@ public class NetdataClient {
// after=-10&points=10
var request = URLComponents(string: baseUrl + NetDataEndpoint.data.rawValue)!
request.queryItems = [
URLQueryItem(name: "after", value: "-10"),
URLQueryItem(name: "points", value: "10"),
URLQueryItem(name: "after", value: "-1"),
URLQueryItem(name: "points", value: "1"),
URLQueryItem(name: "chart", value: chart)
];

Expand All @@ -65,11 +62,12 @@ public class NetdataClient {
}

func getChartDataWithHistory(baseUrl: String, basicAuthBase64: String = "", chart: String) async throws -> ServerData {
// after=-900&points=900
var request = URLComponents(string: baseUrl + NetDataEndpoint.data.rawValue)!

request.queryItems = [
URLQueryItem(name: "after", value: "-900"),
URLQueryItem(name: "points", value: "900"),
URLQueryItem(name: "after", value: "-15"),
URLQueryItem(name: "points", value: "15"),
URLQueryItem(name: "gtime", value: "60"),
URLQueryItem(name: "chart", value: chart)
];

Expand All @@ -81,19 +79,29 @@ public class NetdataClient {
}

private func run<T: Decodable>(requestUrl: URL, basicAuthBase64: String) async throws -> T {
var request = URLRequest(url: requestUrl)
var headers: HTTPHeaders = [
"Cache-Control": "no-cache"
]

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

do {
let (jsonData, _) = try await session.data(for: request)
let response = try JSONDecoder().decode(T.self, from: jsonData)
return response
} catch {
print("Decoding error: \(error)")
throw error
return try await withCheckedThrowingContinuation { continuation in
AF.request(
requestUrl.absoluteString,
method: .get,
encoding: JSONEncoding.default,
headers: headers
)
.responseDecodable(of: T.self) { response in
switch(response.result) {
case let .success(data):
continuation.resume(returning: data)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
}
}
8 changes: 5 additions & 3 deletions netdata/Models/ServerAlarms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public struct ServerAlarms: Encodable, Decodable {
var status: Bool;
var alarms: [String: ServerAlarm];

public func getCriticalAlarmsCount() -> Int {
return self.alarms.keys.sorted()
.reduce(0, { acc, key in acc + (self.alarms[key]?.status == "CRITICAL" ? 1 : 0) })
public var criticalAlarmsCount: Int {
get {
return self.alarms.keys.sorted()
.reduce(0, { acc, key in acc + (self.alarms[key]?.status == "CRITICAL" ? 1 : 0) })
}
}
}
22 changes: 11 additions & 11 deletions netdata/Modules/ServerDetail/Components/AlarmListRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ struct AlarmListRow: View {

var body: some View {
VStack(alignment: .leading, spacing: 10) {
HStack {
Text(alarm.name)
.bold()
Text(alarm.status)
.font(.caption)
.bold()
.foregroundColor(self.isCritical() ? Color.red : Color.orange)
.padding(5)
.background(RoundedRectangle(cornerRadius: 14, style: .continuous)
.foregroundColor((self.isCritical() ? Color.red : Color.orange).opacity(0.2)))
}
Text(alarm.status)
.font(.caption)
.bold()
.foregroundColor(self.isCritical() ? Color.red : Color.orange)
.padding(.horizontal, 12)
.padding(.vertical, 4)
.background(RoundedRectangle(cornerRadius: 14, style: .continuous)
.foregroundColor((self.isCritical() ? Color.red : Color.orange).opacity(0.1)))

Text(alarm.name)
.bold()

Text(alarm.info)
.font(.subheadline)
Expand Down
Loading

0 comments on commit 78680e1

Please sign in to comment.