UNPKG

4.84 kBJavaScriptView Raw
1'use strict';
2
3const BbPromise = require('bluebird');
4const Config = require('./classes/Config');
5const Exchange = require('./classes/Exchange');
6const TextHelpers = require('./classes/Utils/TextHelpers');
7const Linker = require('./classes/Utils/Linker');
8const PluginManager = require('./classes/PluginManager');
9const Service = require('./classes/Service');
10const Dynatrace = require('./classes/Dynatrace');
11const Users = require('./classes/Users');
12const Utils = require('./classes/Utils');
13const Notifications = require('./classes/Notifications');
14const VB = require('./classes/Utils/VisualBuilder');
15const Filters = require('./classes/Filters');
16const Server = require('./server/Server');
17const Alexa = require('./sources/Alexa');
18const Slack = require('./sources/Slack');
19const Web = require('./sources/Web');
20const Version = require('./../package.json').version;
21const path = require('path');
22const CronJob = require('cron').CronJob;
23const ProblemDetails = require('./models/ProblemDetail');
24const moment = require("moment");
25
26moment.locale("en");
27
28// Allows users to define config defaults
29require('dotenv').config({ silent: true });
30
31class 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 * Returns the version number found in package.json
154 * @returns {string} - Version number
155 * @memberOf Davis
156 */
157 getVersion() {
158 return this.version;
159 }
160
161}
162
163module.exports = Davis;
164module.exports.logError = Utils.logError;