-
Notifications
You must be signed in to change notification settings - Fork 34
/
stats-categories.js
92 lines (81 loc) · 4.17 KB
/
stats-categories.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Post from './server/models/post.model';
import Stats from './server/models/stats.model';
import { calculatePayout } from './server/steemitHelpers';
import config from './config/config';
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect(`${config.mongo.host}`);
const conn = mongoose.connection;
conn.once('open', function ()
{
const query = {
reviewed: true,
};
Post
.countAll({ query })
.then(count => {
Post
.list({ skip: 0, limit: count, query })
.then(posts => {
if(posts.length > 0) {
Stats.get().then(stats => {
const categories = {};
posts.forEach((post, index) => {
const categoryType = post.json_metadata.type;
if (!categories[categoryType]) {
categories[categoryType] = {
total_posts: 0,
total_likes: 0,
average_likes_per_post: 0,
total_paid: 0,
total_paid_authors: 0,
average_paid_authors: 0,
total_paid_curators: 0,
average_paid_curators: 0,
total_posts_length: 0,
average_posts_length: 0,
total_images: 0,
average_images_per_post: 0,
total_links: 0,
average_links_per_post: 0,
total_tags: 0,
average_tags_per_post: 0,
}
}
const categoryObj = categories[categoryType];
const isCashedout = post.cashout_time === '1969-12-31T23:59:59';
const payoutDetails = isCashedout ? calculatePayout(post) : null;
const authorPayouts = isCashedout ? payoutDetails.authorPayouts : 0;
const curatorPayouts = isCashedout ? payoutDetails.curatorPayouts : 0;
const images = post.json_metadata.image ? post.json_metadata.image.length : 0;
const links = post.json_metadata.links ? post.json_metadata.links.length : 0;
const tags = post.json_metadata.tags.length;
categoryObj.total_posts = categoryObj.total_posts + 1;
categoryObj.total_likes = categoryObj.total_likes + post.active_votes.length;
categoryObj.average_likes_per_post = categoryObj.total_likes / categoryObj.total_posts;
categoryObj.total_paid = categoryObj.total_paid + authorPayouts + curatorPayouts;
categoryObj.total_paid_authors = categoryObj.total_paid_authors + authorPayouts;
// not counting this post in the average if not yet cashed out.
categoryObj.average_paid_authors = categoryObj.total_paid_authors / (isCashedout ? categoryObj.total_posts : categoryObj.total_posts - 1 || 1);
categoryObj.total_paid_curators = categoryObj.total_paid_curators + curatorPayouts;
// not counting this post in the average if not yet cashed out.
categoryObj.average_paid_curators = categoryObj.total_paid_curators / (isCashedout ? categoryObj.total_posts : categoryObj.total_posts - 1 || 1);
categoryObj.total_posts_length = categoryObj.total_posts_length + post.body.length;
categoryObj.average_posts_length = categoryObj.total_posts_length / categoryObj.total_posts;
categoryObj.total_images = categoryObj.total_images + images;
categoryObj.average_images_per_post = categoryObj.total_images / categoryObj.total_posts;
categoryObj.total_links = categoryObj.total_links + links;
categoryObj.average_links_per_post = categoryObj.total_links / categoryObj.total_posts;
categoryObj.total_tags = categoryObj.total_tags + tags;
categoryObj.average_tags_per_post = categoryObj.total_tags / categoryObj.total_posts;
});
stats.categories = categories;
stats.save().then(savedStats => {
process.exit(0);
conn.close();
});
});
}
});
});
});