Skip to content
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"typescript": "^4.1.5",
"typescript-is": "^0.18.2",
"uuid": "^8.3.2",
"vdata-parser": "^0.1.5",
"yauzl": "^2.10.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Discord from '../Discord'
import Calculator from '../../statClasses/Transaction';
import { StatisticType } from '../../../../../types/schemas/Statistic';

Discord.prototype.getTransactionsStatistics = async function getTransactionsStatistics() {
const calculator = new Calculator()
let chunk
const size = 50
let offset = 0
do {
// eslint-disable-next-line no-await-in-loop
chunk = await this.getTransactions({
parsingOptions: {
pagination: {
offset,
items: size,
},
},
})
calculator.process_record(chunk?.data ?? [])
offset += size
} while (chunk?.data?.length === 50)

return {
statistics: [
calculator.total,
{
type: StatisticType.RANKING,
name: 'AverageMonth',
value: await calculator.averages,
},
],
parsedFiles: chunk?.parsedFiles ?? [],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Facebook from '../Facebook'
import Calculator from '../../statClasses/Notification';
import { StatisticType } from '../../../../../types/schemas/Statistic'

Facebook.prototype.getNotificationsStatistics = async function getNotificationsStatistics() {
const calculator = new Calculator()
let chunk
const size = 50
let offset = 0
do {
// eslint-disable-next-line no-await-in-loop
chunk = await this.getNotifications({
parsingOptions: {
pagination: {
offset,
items: size,
},
},
})
calculator.process_record(chunk?.data ?? [])
offset += size
} while (chunk?.data?.length === 50)

return {
statistics: [
{
name: 'Daily Average',
type: StatisticType.NUMBER,
value: await calculator.average,
description: 'average number of notifications per day',
},
],
parsedFiles: chunk?.parsedFiles ?? [],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Google from '../Google'
import Calculator from '../../statClasses/Connection';
import { StatisticType } from '../../../../../types/schemas/Statistic'

Google.prototype.getConnectionsStatistics = async function getConnectionsStatistics() {
const calculator = new Calculator()
let chunk
const size = 50
let offset = 0
do {
// eslint-disable-next-line no-await-in-loop
chunk = await this.getConnections({
parsingOptions: {
pagination: {
offset,
items: size,
},
},
})
calculator.process_record(chunk?.data ?? [])
offset += size
} while (chunk?.data?.length === 50)

return {
statistics: [
{
name: 'Monthly Average',
type: StatisticType.NUMBER,
value: await calculator.average,
description: 'average number of connections per month',
},
{
name: 'Top IPs',
type: StatisticType.RANKING,
value: calculator.ips,
},
{
name: 'Top locations',
type: StatisticType.RANKING,
value: calculator.locations,
},
],
parsedFiles: chunk?.parsedFiles ?? [],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Google from '../Google'
import Calculator from '../../statClasses/Task';
import { StatisticType } from '../../../../../types/schemas/Statistic'

Google.prototype.getTasksStatistics = async function getTasksStatistics() {
const calculator = new Calculator()
let chunk
const size = 50
let offset = 0
do {
// eslint-disable-next-line no-await-in-loop
chunk = await this.getTasks({
parsingOptions: {
pagination: {
offset,
items: size,
},
},
})
calculator.process_record(chunk?.data ?? [])
offset += size
} while (chunk?.data?.length === 50)

return {
statistics: [
{
name: 'Monthly Average',
type: StatisticType.NUMBER,
value: await calculator.average,
description: 'average number of tasks created per Month',
},
{
name: 'Tasks Done',
type: StatisticType.PERCENTAGE,
value: calculator.percentDone,
description: 'percentage of tasks done',
},
],
parsedFiles: chunk?.parsedFiles ?? [],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Google from '../Google'
import Calculator from '../../statClasses/Transaction';
import { StatisticType } from '../../../../../types/schemas/Statistic';

Google.prototype.getTransactionsStatistics = async function getTransactionsStatistics() {
const calculator = new Calculator()
let chunk
const size = 50
let offset = 0
do {
// eslint-disable-next-line no-await-in-loop
chunk = await this.getTransactions({
parsingOptions: {
pagination: {
offset,
items: size,
},
},
})
calculator.process_record(chunk?.data ?? [])
offset += size
} while (chunk?.data?.length === 50)

return {
statistics: [
calculator.total,
{
type: StatisticType.RANKING,
name: 'AverageMonth',
value: await calculator.averages,
},
],
parsedFiles: chunk?.parsedFiles ?? [],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Reddit from '../Reddit'
import Calculator from '../../statClasses/Transaction';
import { StatisticType } from '../../../../../types/schemas/Statistic';

Reddit.prototype.getTransactionsStatistics = async function getTransactionsStatistics() {
const calculator = new Calculator()
let chunk
const size = 50
let offset = 0
do {
// eslint-disable-next-line no-await-in-loop
chunk = await this.getTransactions({
parsingOptions: {
pagination: {
offset,
items: size,
},
},
})
calculator.process_record(chunk?.data ?? [])
offset += size
} while (chunk?.data?.length === 50)

return {
statistics: [
calculator.total,
{
type: StatisticType.RANKING,
name: 'AverageMonth',
value: await calculator.averages,
},
],
parsedFiles: chunk?.parsedFiles ?? [],
}
}
92 changes: 92 additions & 0 deletions src/classes/Standardizer/plugins/statClasses/Connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import moment from 'moment';
import { Connection } from '../../../../types/schemas';

export default class Calculator {
constructor() {
this.calendar = {}
this.ip = {}
this.location = {}
}

process_record(chunk: Array<Connection>) {
this.updateAverage(chunk)
this.updateRanking(chunk)
}

private readonly calendar: Record<string, any>

private readonly ip: Record<string, number>

private readonly location: Record<string, number>

private updateRanking(chunk: Array<Connection>) {
chunk.forEach(connection => {
// eslint-disable-next-line no-prototype-builtins
if (!this.ip.hasOwnProperty(connection.ipAddress)) {
this.ip[connection.ipAddress] = 1
} else {
this.ip[connection.ipAddress] += 1
}
const location = connection.location?.relativePosition?.city
if (!location) return
// eslint-disable-next-line no-prototype-builtins
if (!this.location.hasOwnProperty(location)) {
this.location[location] = 1
} else {
this.ip[location] += 1
}
})
}

private sort(obj: Object) {
return Object.fromEntries(
Object.entries(obj).sort(([,a], [,b]) => b - a),
);
}

get ips() {
return Object.entries(this.sort(this.ip)).map(([ip, nb]) => ({
label: ip,
value: nb,
})).slice(0, 5)
}

get locations() {
return Object.entries(this.sort(this.location)).map(([location, nb]) => ({
label: location,
value: nb,
})).slice(0, 5)
}

private updateAverage(chunk: Array<Connection>) {
/**
* calendar = {
* '09-2021': [0,100,...]
* ...
* }
*/
chunk.forEach(connection => {
const { calendar } = this
const date = moment(connection.timestamp)
// eslint-disable-next-line no-prototype-builtins
if (!calendar.hasOwnProperty(date.format('YYYY-MM'))) {
calendar[date.format('YYYY-MM')] = Array.from(new Array(date.daysInMonth()), () => 0)
}
calendar[date.format('YYYY-MM')][date.date() - 1] += 1
})
}

get average() {
return this.computeAverage()
}

private async computeAverage() {
const { calendar } = this
let sum: Array<number> = []
await Object.keys(calendar).forEach(month => {
sum = sum.concat(calendar[month])
})
const nb = sum.length
return sum.reduce((a, b) => a + b, 0) / nb
}
}
46 changes: 46 additions & 0 deletions src/classes/Standardizer/plugins/statClasses/Notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import moment from 'moment';
import { Notification } from '../../../../types/schemas';

export default class Calculator {
constructor() {
this.calendar = {}
}

process_record(chunk: Array<Notification>) {
this.updateAverage(chunk)
}

private readonly calendar: Record<string, any>

private updateAverage(chunk: Array<Notification>) {
/**
* calendar = {
* '09-2021': [0,100,...]
* ...
* }
*/
chunk.forEach(notification => {
const { calendar } = this
const date = moment(notification.notificationDate)
// eslint-disable-next-line no-prototype-builtins
if (!calendar.hasOwnProperty(date.format('YYYY-MM'))) {
calendar[date.format('YYYY-MM')] = Array.from(new Array(date.daysInMonth()), () => 0)
}
calendar[date.format('YYYY-MM')][date.date() - 1] += 1
})
}

get average() {
return this.computeAverage()
}

private async computeAverage() {
const { calendar } = this
let sum: Array<number> = []
await Object.keys(calendar).forEach(month => {
sum = sum.concat(calendar[month])
})
const nb = sum.length
return sum.reduce((a, b) => a + b, 0) / nb
}
}
Loading