-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Since the OpenAI usage API(/v1/dashboard/billing/usage) is no lo…
…nger available in July 22, the cost usage on the openai settings page has been replaced with token usage. * Also fixed the API key validation error .
- Loading branch information
Showing
10 changed files
with
292 additions
and
105 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/schemas/com.crossbowffs.quotelock.data.modules.openai.OpenAIUsageDatabase/1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"formatVersion": 1, | ||
"database": { | ||
"version": 1, | ||
"identityHash": "56f49147e19f56a03019b2fa3a1bb569", | ||
"entities": [ | ||
{ | ||
"tableName": "usage", | ||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `api_key` TEXT NOT NULL, `model` TEXT NOT NULL, `tokens` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL)", | ||
"fields": [ | ||
{ | ||
"fieldPath": "id", | ||
"columnName": "_id", | ||
"affinity": "INTEGER", | ||
"notNull": false | ||
}, | ||
{ | ||
"fieldPath": "apiKey", | ||
"columnName": "api_key", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "model", | ||
"columnName": "model", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "tokens", | ||
"columnName": "tokens", | ||
"affinity": "INTEGER", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "timestamp", | ||
"columnName": "timestamp", | ||
"affinity": "INTEGER", | ||
"notNull": true | ||
} | ||
], | ||
"primaryKey": { | ||
"autoGenerate": true, | ||
"columnNames": [ | ||
"_id" | ||
] | ||
}, | ||
"indices": [], | ||
"foreignKeys": [] | ||
} | ||
], | ||
"views": [], | ||
"setupQueries": [ | ||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", | ||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '56f49147e19f56a03019b2fa3a1bb569')" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
app/src/main/java/com/crossbowffs/quotelock/data/api/OpenAIAccount.kt
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
app/src/main/java/com/crossbowffs/quotelock/data/modules/openai/OpenAIDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.crossbowffs.quotelock.data.modules.openai | ||
|
||
import android.content.Context | ||
import android.provider.BaseColumns | ||
import androidx.room.ColumnInfo | ||
import androidx.room.Dao | ||
import androidx.room.Database | ||
import androidx.room.Delete | ||
import androidx.room.Entity | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.PrimaryKey | ||
import androidx.room.Query | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import com.crossbowffs.quotelock.data.modules.openai.OpenAIUsageContract.DATABASE_NAME | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
const val OPENAI_USAGE_DB_VERSION = 1 | ||
|
||
object OpenAIUsageContract { | ||
const val DATABASE_NAME = "openai_usage.db" | ||
|
||
const val TABLE = "usage" | ||
|
||
const val ID = BaseColumns._ID | ||
const val API_KEY = "api_key" | ||
const val MODEL = "model" | ||
const val TOKENS = "tokens" | ||
const val TIMESTAMP = "timestamp" | ||
} | ||
|
||
@Entity(tableName = OpenAIUsageContract.TABLE) | ||
data class OpenAIUsageEntity( | ||
@PrimaryKey(autoGenerate = true) | ||
@ColumnInfo(name = OpenAIUsageContract.ID) | ||
val id: Int? = null, | ||
@ColumnInfo(name = OpenAIUsageContract.API_KEY) | ||
val apiKey: String, | ||
@ColumnInfo(name = OpenAIUsageContract.MODEL) | ||
val model: String, | ||
@ColumnInfo(name = OpenAIUsageContract.TOKENS) | ||
val tokens: Int, | ||
@ColumnInfo(name = OpenAIUsageContract.TIMESTAMP) | ||
val timestamp: Long = System.currentTimeMillis(), | ||
) | ||
|
||
@Dao | ||
interface OpenAIUsageDao { | ||
|
||
@Query( | ||
"SELECT * FROM ${OpenAIUsageContract.TABLE} WHERE ${OpenAIUsageContract.API_KEY} = :apiKey " + | ||
"AND ${OpenAIUsageContract.TIMESTAMP} >= (:startDate / 1000)" | ||
) | ||
fun getUsageByApiKeyStream(apiKey: String, startDate: Long): Flow<List<OpenAIUsageEntity>> | ||
|
||
@Insert(onConflict = OnConflictStrategy.IGNORE) | ||
suspend fun insert(quote: OpenAIUsageEntity): Long? | ||
|
||
@Delete | ||
suspend fun delete(quote: OpenAIUsageEntity): Int | ||
|
||
@Query("DELETE FROM ${OpenAIUsageContract.TABLE} WHERE ${OpenAIUsageContract.ID} = :id") | ||
suspend fun delete(id: Long): Int | ||
|
||
@Query("DELETE FROM ${OpenAIUsageContract.TABLE}") | ||
suspend fun deleteAll() | ||
} | ||
|
||
@Database(entities = [OpenAIUsageEntity::class], version = OPENAI_USAGE_DB_VERSION) | ||
abstract class OpenAIUsageDatabase : RoomDatabase() { | ||
abstract fun dao(): OpenAIUsageDao | ||
|
||
companion object { | ||
@Volatile | ||
private var INSTANCE: OpenAIUsageDatabase? = null | ||
|
||
fun getDatabase(context: Context): OpenAIUsageDatabase { | ||
// if the INSTANCE is not null, then return it, | ||
// if it is, then create the database | ||
return INSTANCE ?: synchronized(this) { | ||
val instance = Room.databaseBuilder( | ||
context.applicationContext, | ||
OpenAIUsageDatabase::class.java, | ||
DATABASE_NAME | ||
).setJournalMode(JournalMode.TRUNCATE).build() | ||
INSTANCE = instance | ||
// return instance | ||
instance | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.