634 lines
16 KiB
JavaScript
634 lines
16 KiB
JavaScript
|
|
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class AnalyticsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const analytics = await db.analytics.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
label: data.label
|
|
||
|
|
null
|
|
,
|
|
|
|
date: data.date
|
|
||
|
|
null
|
|
,
|
|
|
|
views: data.views
|
|
||
|
|
null
|
|
,
|
|
|
|
likes: data.likes
|
|
||
|
|
null
|
|
,
|
|
|
|
shares: data.shares
|
|
||
|
|
null
|
|
,
|
|
|
|
comments: data.comments
|
|
||
|
|
null
|
|
,
|
|
|
|
average_watch_time: data.average_watch_time
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await analytics.setVideo( data.video || null, {
|
|
transaction,
|
|
});
|
|
|
|
await analytics.setAccount( data.account || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return analytics;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const analyticsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
label: item.label
|
|
||
|
|
null
|
|
,
|
|
|
|
date: item.date
|
|
||
|
|
null
|
|
,
|
|
|
|
views: item.views
|
|
||
|
|
null
|
|
,
|
|
|
|
likes: item.likes
|
|
||
|
|
null
|
|
,
|
|
|
|
shares: item.shares
|
|
||
|
|
null
|
|
,
|
|
|
|
comments: item.comments
|
|
||
|
|
null
|
|
,
|
|
|
|
average_watch_time: item.average_watch_time
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const analytics = await db.analytics.bulkCreate(analyticsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return analytics;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const analytics = await db.analytics.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.label !== undefined) updatePayload.label = data.label;
|
|
|
|
|
|
if (data.date !== undefined) updatePayload.date = data.date;
|
|
|
|
|
|
if (data.views !== undefined) updatePayload.views = data.views;
|
|
|
|
|
|
if (data.likes !== undefined) updatePayload.likes = data.likes;
|
|
|
|
|
|
if (data.shares !== undefined) updatePayload.shares = data.shares;
|
|
|
|
|
|
if (data.comments !== undefined) updatePayload.comments = data.comments;
|
|
|
|
|
|
if (data.average_watch_time !== undefined) updatePayload.average_watch_time = data.average_watch_time;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await analytics.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.video !== undefined) {
|
|
await analytics.setVideo(
|
|
|
|
data.video,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.account !== undefined) {
|
|
await analytics.setAccount(
|
|
|
|
data.account,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return analytics;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const analytics = await db.analytics.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of analytics) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of analytics) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return analytics;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const analytics = await db.analytics.findByPk(id, options);
|
|
|
|
await analytics.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await analytics.destroy({
|
|
transaction
|
|
});
|
|
|
|
return analytics;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const analytics = await db.analytics.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!analytics) {
|
|
return analytics;
|
|
}
|
|
|
|
const output = analytics.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.video = await analytics.getVideo({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.account = await analytics.getAccount({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.generated_videos,
|
|
as: 'video',
|
|
|
|
where: filter.video ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.video.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.video.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.social_accounts,
|
|
as: 'account',
|
|
|
|
where: filter.account ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.account.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
handle: {
|
|
[Op.or]: filter.account.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.label) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'analytics',
|
|
'label',
|
|
filter.label,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.dateRange) {
|
|
const [start, end] = filter.dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
date: {
|
|
...where.date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
date: {
|
|
...where.date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.viewsRange) {
|
|
const [start, end] = filter.viewsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
views: {
|
|
...where.views,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
views: {
|
|
...where.views,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.likesRange) {
|
|
const [start, end] = filter.likesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
likes: {
|
|
...where.likes,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
likes: {
|
|
...where.likes,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.sharesRange) {
|
|
const [start, end] = filter.sharesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
shares: {
|
|
...where.shares,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
shares: {
|
|
...where.shares,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.commentsRange) {
|
|
const [start, end] = filter.commentsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
comments: {
|
|
...where.comments,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
comments: {
|
|
...where.comments,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.average_watch_timeRange) {
|
|
const [start, end] = filter.average_watch_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
average_watch_time: {
|
|
...where.average_watch_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
average_watch_time: {
|
|
...where.average_watch_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.analytics.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'analytics',
|
|
'label',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.analytics.findAll({
|
|
attributes: [ 'id', 'label' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['label', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.label,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|