feat: init server plugin

This commit is contained in:
Serhiy Mytrovtsiy
2021-07-06 18:17:37 +02:00
parent dc017c866d
commit 08d8d84ceb
5 changed files with 87 additions and 1 deletions

75
Kit/plugins/Server.swift Normal file
View File

@@ -0,0 +1,75 @@
//
// Server.swift
// Kit
//
// Created by Serhiy Mytrovtsiy on 04/07/2021.
// Using Swift 5.0.
// Running on macOS 10.15.
//
// Copyright © 2021 Serhiy Mytrovtsiy. All rights reserved.
//
import Foundation
struct event: Codable {
var ID: String
var version: String
var build: String
var modules: [String]
var device: String
var os: String
var language: String
}
public class Server {
public static let shared = Server(url: URL(string: "https://api.serhiy.io/v1/stats")!)
public var ID: String {
get {
return Store.shared.string(key: "id", defaultValue: UUID().uuidString)
}
}
private let url: URL
public init(url: URL) {
self.url = url
if !Store.shared.exist(key: "id") {
Store.shared.set(key: "id", value: self.ID)
}
}
public func sendEvent(modules: [String]) {
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String
let systemVersion = ProcessInfo().operatingSystemVersion
let e = event(
ID: self.ID,
version: version ?? "unknown",
build: build ?? "unknown",
modules: modules, device: SystemKit.shared.modelName() ?? "unknown",
os: systemVersion.getFullVersion(),
language: Locale.current.languageCode ?? "unknown"
)
var request = URLRequest(url: self.url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
request.httpBody = try JSONEncoder().encode(e)
} catch let err {
error("failed to encode json: \(err)")
}
let task = URLSession.shared.dataTask(with: request) { (_, _, err) in
if err != nil {
error("send report \(String(describing: err))")
}
}
task.resume()
}
}

View File

@@ -74,6 +74,7 @@
9A81C75E2449A41400825D92 /* RAM.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9A81C7562449A41400825D92 /* RAM.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9A81C7692449A43600825D92 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A81C7672449A43600825D92 /* main.swift */; };
9A81C76A2449A43600825D92 /* readers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A81C7682449A43600825D92 /* readers.swift */; };
9A8AE0A326921A2A00B13054 /* Server.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A8AE0A226921A2A00B13054 /* Server.swift */; };
9A8DE58E253DEFA9006A748F /* Fans.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A8DE587253DEFA9006A748F /* Fans.framework */; };
9A8DE58F253DEFA9006A748F /* Fans.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9A8DE587253DEFA9006A748F /* Fans.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9A8DE5E4253DF4E2006A748F /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A8DE5E3253DF4E2006A748F /* main.swift */; };
@@ -376,6 +377,7 @@
9A81C7592449A41400825D92 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9A81C7672449A43600825D92 /* main.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
9A81C7682449A43600825D92 /* readers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = readers.swift; sourceTree = "<group>"; };
9A8AE0A226921A2A00B13054 /* Server.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Server.swift; sourceTree = "<group>"; };
9A8DE587253DEFA9006A748F /* Fans.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Fans.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9A8DE58A253DEFA9006A748F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9A8DE5E3253DF4E2006A748F /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
@@ -769,6 +771,7 @@
9A2848032666AB2F00EC1F6D /* SystemKit.swift */,
9A2848072666AB3000EC1F6D /* Updater.swift */,
9A6EEBBD2685259500897371 /* Logger.swift */,
9A8AE0A226921A2A00B13054 /* Server.swift */,
);
path = plugins;
sourceTree = "<group>";
@@ -1421,6 +1424,7 @@
9A28480E2666AB3000EC1F6D /* Updater.swift in Sources */,
9A2847622666AA2700EC1F6D /* Label.swift in Sources */,
9A28477C2666AA5000EC1F6D /* reader.swift in Sources */,
9A8AE0A326921A2A00B13054 /* Server.swift in Sources */,
9A2847652666AA2700EC1F6D /* Memory.swift in Sources */,
9A2847642666AA2700EC1F6D /* Battery.swift in Sources */,
9A28480B2666AB3000EC1F6D /* Charts.swift in Sources */,

View File

@@ -52,6 +52,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
self.defaultValues()
self.updateCron()
info("Stats started in \((startingPoint.timeIntervalSinceNow * -1).rounded(toPlaces: 4)) seconds")
Server.shared.sendEvent(modules: modules.filter({ $0.enabled != false && $0.available != false }).map({ $0.config.name }))
}
func applicationWillTerminate(_ aNotification: Notification) {

View File

@@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>286</string>
<string>287</string>
<key>Description</key>
<string>Simple macOS system monitor in your menu bar</string>
<key>LSApplicationCategoryType</key>
@@ -26,6 +26,11 @@
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.</string>
<key>NSPrincipalClass</key>

View File

@@ -96,6 +96,7 @@ class ApplicationSettings: NSScrollView {
statsName.font = NSFont.systemFont(ofSize: 20, weight: .regular)
statsName.stringValue = "Stats"
statsName.isSelectable = true
statsName.toolTip = Server.shared.ID
let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String