1 | 'use strict';
|
2 |
|
3 | const BbPromise = require('bluebird');
|
4 | const Config = require('./classes/Config');
|
5 | const Exchange = require('./classes/Exchange');
|
6 | const TextHelpers = require('./classes/Utils/TextHelpers');
|
7 | const Linker = require('./classes/Utils/Linker');
|
8 | const PluginManager = require('./classes/PluginManager');
|
9 | const Service = require('./classes/Service');
|
10 | const Dynatrace = require('./classes/Dynatrace');
|
11 | const Users = require('./classes/Users');
|
12 | const Utils = require('./classes/Utils');
|
13 | const Notifications = require('./classes/Notifications');
|
14 | const VB = require('./classes/Utils/VisualBuilder');
|
15 | const Filters = require('./classes/Filters');
|
16 | const Server = require('./server/Server');
|
17 | const Alexa = require('./sources/Alexa');
|
18 | const Slack = require('./sources/Slack');
|
19 | const Web = require('./sources/Web');
|
20 | const Version = require('./../package.json').version;
|
21 | const path = require('path');
|
22 | const CronJob = require('cron').CronJob;
|
23 | const ProblemDetails = require('./models/ProblemDetail');
|
24 | const moment = require("moment");
|
25 |
|
26 | moment.locale("en");
|
27 |
|
28 |
|
29 | require('dotenv').config({ silent: true });
|
30 |
|
31 | class Davis {
|
32 | constructor(config) {
|
33 | const configObject = config || {};
|
34 |
|
35 | this.version = Version;
|
36 | this.dir = path.join(__dirname, '..');
|
37 |
|
38 | this.logger = new Utils.Logger(this, configObject.logLevel);
|
39 | this.event = new Utils.Events(this);
|
40 | this.notifications = new Notifications(this);
|
41 | this.utils = Utils;
|
42 |
|
43 | this.config = new Config(this, configObject);
|
44 | this.service = new Service(this);
|
45 | this.filters = new Filters(this);
|
46 | this.dynatrace = new Dynatrace(this);
|
47 | this.server = new Server(this);
|
48 | this.users = new Users(this);
|
49 | this.textHelpers = new TextHelpers(this);
|
50 | this.linker = new Linker(this);
|
51 |
|
52 | this.pluginManager = new PluginManager(this);
|
53 | this.userPlugins = configObject.userPlugins || [];
|
54 |
|
55 | this.classes = {};
|
56 | this.classes.Error = Utils.DError;
|
57 | this.classes.Exchange = Exchange;
|
58 | this.classes.VB = VB;
|
59 |
|
60 | this.sources = {};
|
61 | this.sources.alexa = new Alexa(this);
|
62 | this.sources.slack = new Slack(this);
|
63 | this.sources.web = new Web(this);
|
64 |
|
65 | this.models = {};
|
66 | this.models.ProblemDetails = ProblemDetails;
|
67 |
|
68 | this.cronJobs = {};
|
69 |
|
70 | process.stdin.resume();
|
71 | process.on('SIGINT', () => {
|
72 | this.sources.slack.stop()
|
73 | .then(() => {
|
74 | console.log();
|
75 | process.exit(0);
|
76 | });
|
77 | });
|
78 | }
|
79 |
|
80 | addCron(cronTime, onTick, name, run) {
|
81 | const fname = name || 'anonymous cron';
|
82 |
|
83 | const fn = function () {
|
84 | try {
|
85 | this.logger.debug(`CRON: ${fname}`);
|
86 | onTick.call(this);
|
87 | } catch (e) {
|
88 | this.logger.warn(`CRON: ${fname} failed`);
|
89 | }
|
90 | };
|
91 |
|
92 | const cron = new CronJob({
|
93 | cronTime,
|
94 | onTick: fn,
|
95 | start: true,
|
96 | context: this,
|
97 | runOnInit: Boolean(run),
|
98 | });
|
99 |
|
100 | this.cronJobs[fname] = cron;
|
101 | }
|
102 |
|
103 | removeCron(name) {
|
104 | if (this.cronJobs[name]) {
|
105 | this.cronJobs[name].stop();
|
106 | delete this.cronJobs[name];
|
107 | }
|
108 | }
|
109 |
|
110 | run() {
|
111 | const startTime = Date.now();
|
112 | return BbPromise.resolve()
|
113 | .then(() => this.logger.asciiGreeting())
|
114 | .then(() => this.service.connectToMongoDB())
|
115 | .then(() => this.config.load())
|
116 | .then(() => {
|
117 | const admin = this.users.adminExists()
|
118 | .then((exists) => {
|
119 | if (!exists) {
|
120 | return this.users.createDefaultUser();
|
121 | }
|
122 | return null;
|
123 | });
|
124 | return admin;
|
125 | })
|
126 | .then((admin) => {
|
127 | if (admin) this.logger.info('A default user has been created.');
|
128 |
|
129 | let n;
|
130 | this.logger.info('Learning everything there is to know about APM.');
|
131 | n = this.pluginManager.loadCorePlugins();
|
132 | this.pluginManager.stats.plugins.core += n;
|
133 | this.logger.info(`Loaded ${n} core plugins`);
|
134 | n = this.pluginManager.loadUserPlugins(this.userPlugins);
|
135 | this.pluginManager.stats.plugins.user += n;
|
136 | this.logger.info(`Loaded ${n} user plugins`);
|
137 | })
|
138 | .then(() => this.pluginManager.train())
|
139 | .then(() => this.logger.info("I would say it's safe to consider me an APM expert now!"))
|
140 | .then(() => this.pluginManager.loadEntities())
|
141 | .then(() => {
|
142 | this.addCron('0 0 0 * * *', () => {
|
143 | this.pluginManager.loadEntities();
|
144 | }, 'update entities');
|
145 | })
|
146 | .then(() => this.server.start())
|
147 | .then(() => this.sources.slack.start())
|
148 | .then(() => this.logger.info(`Server started successfully after ${Date.now() - startTime} ms.`));
|
149 | }
|
150 |
|
151 |
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 | getVersion() {
|
158 | return this.version;
|
159 | }
|
160 |
|
161 | }
|
162 |
|
163 | module.exports = Davis;
|
164 | module.exports.logError = Utils.logError;
|