Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0.29 #33

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ Maven:
<dependency>
<groupId>com.simiacryptus</groupId>
<artifactId>skyenet-webui</artifactId>
<version>1.0.28</version>
<version>1.0.29</version>
</dependency>
```

Gradle:

```groovy
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.28'
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.29'
```

```kotlin
implementation("com.simiacryptus:skyenet:1.0.28")
implementation("com.simiacryptus:skyenet:1.0.29")
```

### 🌟 To Use
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.simiacryptus.skyenet.util

import java.nio.file.Files
import java.nio.file.Paths
import java.util.*

object AuthorizationManager {
Expand All @@ -22,7 +20,7 @@ object AuthorizationManager {
if (isUserAuthorized("/permissions/${operationType.name.lowercase(Locale.getDefault())}.txt", user)) {
log.debug("User {} authorized for {} globally", user, operationType)
true
} else if(null != applicationClass) {
} else if (null != applicationClass) {
val packagePath = applicationClass.`package`.name.replace('.', '/')
val opName = operationType.name.lowercase(Locale.getDefault())
if (isUserAuthorized("/$packagePath/$opName.txt", user)) {
Expand All @@ -42,14 +40,10 @@ object AuthorizationManager {
}

private fun isUserAuthorized(permissionPath: String, user: String?): Boolean {
val url = this::class.java.getResource(permissionPath)
if (url == null) {
log.debug("No permissions file found at $permissionPath")
return false
}
val path = Paths.get(url.toURI())
val lines = Files.readAllLines(path)
return lines.any { it.equals(user, ignoreCase = true) || it == "*" }
return this::class.java.getResourceAsStream(permissionPath)?.use { stream ->
val lines = stream.bufferedReader().readLines()
lines.any { it.equals(user, ignoreCase = true) || it == "*" }
} ?: false
}

val log = org.slf4j.LoggerFactory.getLogger(AuthorizationManager::class.java)
Expand Down
43 changes: 37 additions & 6 deletions core/src/main/kotlin/com/simiacryptus/skyenet/util/UsageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ object UsageManager {

private val scheduler = Executors.newSingleThreadScheduledExecutor()
private val txLogFile = File(".skyenet/usage/log.csv")
private val txLogFileWriter by lazy { FileWriter(txLogFile, true) }
@Volatile private var txLogFileWriter: FileWriter
private val usagePerSession = HashMap<String, UsageCounters>()
private val sessionsByUser = HashMap<String, ArrayList<String>>()
private val usersBySession = HashMap<String, ArrayList<String>>()

init {
txLogFile.parentFile.mkdirs()
loadFromLog(txLogFile)
txLogFileWriter = FileWriter(txLogFile, true)
scheduler.scheduleAtFixedRate({ saveCounters() }, 1, 1, TimeUnit.HOURS)
}

Expand All @@ -34,6 +35,7 @@ object UsageManager {
}
}
}

@Suppress("MemberVisibilityCanBePrivate")
fun writeCompactLog(file: File) {
val writer = FileWriter(file)
Expand All @@ -48,8 +50,33 @@ object UsageManager {
}

private fun saveCounters() {
txLogFile.renameTo(File(txLogFile.absolutePath + "." + System.currentTimeMillis()))
writeCompactLog(txLogFile)
txLogFileWriter = FileWriter(txLogFile, true)
val timedFile = File(txLogFile.absolutePath + "." + System.currentTimeMillis())
writeCompactLog(timedFile)
val swapFile = File(txLogFile.absolutePath + ".old")
synchronized(txLogFile) {
try {
txLogFileWriter.close()
} catch (e: Exception) {
log.warn("Error closing log file", e)
}
try {
txLogFile.renameTo(swapFile)
} catch (e: Exception) {
log.warn("Error renaming log file", e)
}
try {
timedFile.renameTo(txLogFile)
} catch (e: Exception) {
log.warn("Error renaming log file", e)
}
try {
swapFile.renameTo(timedFile)
} catch (e: Exception) {
log.warn("Error renaming log file", e)
}
txLogFileWriter = FileWriter(txLogFile, true)
}
File(".skyenet/usage/counters.json").writeText(JsonUtil.toJson(usagePerSession))
}

Expand All @@ -67,9 +94,13 @@ object UsageManager {
}
sessions.add(sessionId)
}
synchronized(txLogFileWriter) {
txLogFileWriter.write("$sessionId,$user,${model.modelName},$tokens\n")
txLogFileWriter.flush()
try {
synchronized(txLogFile) {
txLogFileWriter.write("$sessionId,$user,${model.modelName},$tokens\n")
txLogFileWriter.flush()
}
} catch (e: Exception) {
log.warn("Error incrementing usage", e)
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Gradle Releases -> https://github.com/gradle/gradle/releases
libraryGroup = com.simiacryptus.skyenet
libraryVersion = 1.0.28
libraryVersion = 1.0.29
gradleVersion = 7.6.1

# Opt-out flag for bundling Kotlin standard library -> https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library
Expand Down
Loading