38190-vm/backend/src/db/models/usage_events.js
2026-02-04 15:37:22 +00:00

173 lines
2.2 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const usage_events = sequelize.define(
'usage_events',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
event_type: {
type: DataTypes.ENUM,
values: [
"chat_completion",
"tool_call",
"code_execution",
"preview_build"
],
},
input_tokens: {
type: DataTypes.INTEGER,
},
output_tokens: {
type: DataTypes.INTEGER,
},
cost: {
type: DataTypes.DECIMAL,
},
occurred_at: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
usage_events.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.usage_events.belongsTo(db.workspaces, {
as: 'workspace',
foreignKey: {
name: 'workspaceId',
},
constraints: false,
});
db.usage_events.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.usage_events.belongsTo(db.ai_models, {
as: 'model',
foreignKey: {
name: 'modelId',
},
constraints: false,
});
db.usage_events.belongsTo(db.conversations, {
as: 'conversation',
foreignKey: {
name: 'conversationId',
},
constraints: false,
});
db.usage_events.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.usage_events.belongsTo(db.users, {
as: 'createdBy',
});
db.usage_events.belongsTo(db.users, {
as: 'updatedBy',
});
};
return usage_events;
};