Skip to content

Commit

Permalink
Fixed or added the following:
Browse files Browse the repository at this point in the history
- First Startup - AI-Requests and Fetching? #32
- Assign title and correspondent in manual mode #36
- Paperless-ai making continuouesly Openai requests ? #31
- Completely reworked the dashboard
- Added settings page
- Added new manual mode
- Defined a template for later use
  • Loading branch information
clusterzx committed Jan 6, 2025
1 parent 6b901d3 commit d5b1e90
Show file tree
Hide file tree
Showing 16 changed files with 3,730 additions and 199 deletions.
66 changes: 63 additions & 3 deletions models/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const Database = require('better-sqlite3');
const path = require('path');
const fs = require('fs');
const { get } = require('http');

// Ensure data directory exists
const dataDir = path.join(__dirname, '..', 'data');
Expand All @@ -16,16 +17,28 @@ const db = new Database(path.join(dataDir, 'documents.db'), {
db.pragma('journal_mode = WAL');

// Create tables
const createTable = db.prepare(`
const createTableMain = db.prepare(`
CREATE TABLE IF NOT EXISTS processed_documents (
id INTEGER PRIMARY KEY,
document_id INTEGER UNIQUE,
title TEXT,
processed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP
)
);
`);
createTable.run();
createTableMain.run();

const createTableMetrics = db.prepare(`
CREATE TABLE IF NOT EXISTS openai_metrics (
id INTEGER PRIMARY KEY,
document_id INTEGER,
promptTokens INTEGER,
completionTokens INTEGER,
totalTokens INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);
createTableMetrics.run();

// Prepare statements for better performance
const insertDocument = db.prepare(`
Expand All @@ -40,6 +53,12 @@ const findDocument = db.prepare(
'SELECT * FROM processed_documents WHERE document_id = ?'
);

const insertMetrics = db.prepare(`
INSERT INTO openai_metrics (document_id, promptTokens, completionTokens, totalTokens)
VALUES (?, ?, ?, ?)
`);


module.exports = {
addProcessedDocument(documentId, title) {
try {
Expand All @@ -57,6 +76,47 @@ module.exports = {
}
},

addOpenAIMetrics(documentId, promptTokens, completionTokens, totalTokens) {
try {
const result = insertMetrics.run(documentId, promptTokens, completionTokens, totalTokens);
if (result.changes > 0) {
console.log(`Metrics added for document ${documentId}`);
return true;
}
return false;
} catch (error) {
console.error('Error adding metrics:', error);
return false;
}
},

async getMetrics() {
try {
return db.prepare('SELECT * FROM openai_metrics').all();
} catch (error) {
console.error('Error getting metrics:', error);
return [];
}
},

async getProcessedDocuments() {
try {
return db.prepare('SELECT * FROM processed_documents').all();
} catch (error) {
console.error('Error getting processed documents:', error);
return [];
}
},

async getProcessedDocumentsCount() {
try {
return db.prepare('SELECT COUNT(*) FROM processed_documents').pluck().get();
} catch (error) {
console.error('Error getting processed documents count:', error);
return 0;
}
},

isDocumentProcessed(documentId) {
try {
const row = findDocument.get(documentId);
Expand Down
Loading

0 comments on commit d5b1e90

Please sign in to comment.