Browse Source

WIP: Server info v2 #167 (#194)

improve server info
pull/1/head
Evgeny Smyshlyaev 6 years ago committed by GitHub
parent
commit
840cc1a035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 49
      server/src/config.ts
  2. 1
      server/src/controllers/analytics_controller.ts
  3. 14
      server/src/index.ts
  4. 1
      server/src/models/analytic_unit_model.ts
  5. 26
      server/src/services/analytics_service.ts

49
server/src/config.ts

@ -17,12 +17,17 @@ export const ANALYTIC_UNIT_CACHES_DATABASE_PATCH = path.join(DATA_PATH, 'analyti
export const HASTIC_PORT = getConfigField('HASTIC_PORT', '8000');
export const ZMQ_CONNECTION_STRING = getConfigField('ZMQ_CONNECTION_STRING', null);
export const ZMQ_IPC_PATH = getConfigField('ZMQ_IPC_PATH', path.join(os.tmpdir(), 'hastic'));
export const ZMQ_DEV_PORT = getConfigField('ZMQ_DEV_PORT', '8002');
export const ZMQ_HOST = getConfigField('ZMQ_HOST', '127.0.0.1');
export const HASTIC_API_KEY = getConfigField('HASTIC_API_KEY');
export const ANLYTICS_PING_INTERVAL = 500; // ms
export const PACKAGE_VERSION = getPackageVersion();
export const GIT_INFO = getGitInfo();
export const INSIDE_DOCKER = process.env.INSIDE_DOCKER !== undefined;
export const PRODUCTION_MODE = process.env.NODE_ENV !== 'development';
export const ZMQ_CONNECTION_STRING = createZMQConnectionString();
function getConfigField(field: string, defaultVal?: any) {
@ -43,3 +48,45 @@ function getConfigField(field: string, defaultVal?: any) {
}
return val;
}
function getPackageVersion() {
if(process.env.npm_package_version !== undefined) {
return process.env.npm_package_version;
} else {
let packageFile = path.join(__dirname, '../package.json');
if(fs.existsSync(packageFile)) {
let packageJson: any = getJsonDataSync(packageFile);
return packageJson.version;
} else {
console.debug(`Can't find package file ${packageFile}`);
return null;
}
}
}
function getGitInfo() {
let gitRoot = path.join(__dirname, '../../.git');
let gitHeadFile = path.join(gitRoot, 'HEAD');
if(!fs.existsSync(gitHeadFile)) {
console.debug(`Can't find git HEAD file ${gitHeadFile}`);
return null;
}
const rev = fs.readFileSync(gitHeadFile).toString();
let branchPath = rev.indexOf(':') === -1 ? rev : rev.slice(5, -1);
let branch = branchPath.split('/').pop();
let commitHash = fs.readFileSync(`${gitRoot}/${branchPath}`).toString().slice(0, -1);
return { branch, commitHash };
}
function createZMQConnectionString() {
let zmq =`tcp://${ZMQ_HOST}:${ZMQ_DEV_PORT}`; //debug mode
let zmqConf = getConfigField('ZMQ_CONNECTION_STRING', null);
if(INSIDE_DOCKER) {
return zmqConf;
} else if(PRODUCTION_MODE) {
if(zmqConf === null) {
return 'ipc://' + `${path.join(ZMQ_IPC_PATH, process.pid.toString())}.ipc`;
}
}
return zmq;
}

1
server/src/controllers/analytics_controller.ts

@ -235,6 +235,7 @@ export function isAnalyticReady(): boolean {
return analyticsService.ready;
}
export async function createAnalyticUnitFromObject(obj: any): Promise<AnalyticUnit.AnalyticUnitId> {
if(obj.datasource !== undefined) {
obj.metric.datasource = obj.datasource;

14
server/src/index.ts

@ -6,11 +6,13 @@ import * as AnalyticsController from './controllers/analytics_controller';
import * as ProcessService from './services/process_service';
import { HASTIC_PORT } from './config';
import { HASTIC_PORT, PACKAGE_VERSION, GIT_INFO, ZMQ_CONNECTION_STRING } from './config';
import * as Koa from 'koa';
import * as Router from 'koa-router';
import * as bodyParser from 'koa-bodyparser';
import * as fs from 'fs';
AnalyticsController.init();
@ -38,12 +40,18 @@ var rootRouter = new Router();
rootRouter.use('/analyticUnits', anomaliesRouter.routes(), anomaliesRouter.allowedMethods());
rootRouter.use('/segments', segmentsRouter.routes(), segmentsRouter.allowedMethods());
//rootRouter.use('/alerts', alertsRouter.routes(), alertsRouter.allowedMethods());
rootRouter.get('/', async (ctx) => {
ctx.response.body = {
server: 'OK',
analyticsReady: AnalyticsController.isAnalyticReady(),
version: process.env.npm_package_version,
env: process.env.npm_config_user_agent
nodeVersion: process.version,
packageVersion: PACKAGE_VERSION,
npmUserAgent: process.env.npm_config_user_agent,
docker: process.env.INSIDE_DOCKER !== undefined,
zmqConectionString: ZMQ_CONNECTION_STRING,
serverPort: HASTIC_PORT,
git: GIT_INFO
};
});

1
server/src/models/analytic_unit_model.ts

@ -80,6 +80,7 @@ export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> {
return AnalyticUnit.fromObject(obj);
}
/**
* Creates and updates new unit.id
*

26
server/src/services/analytics_service.ts

@ -22,8 +22,8 @@ export class AnalyticsService {
private _inDocker = false;
constructor(private _onMessage: (message: AnalyticsMessage) => void) {
this._productionMode = process.env.NODE_ENV !== 'development';
this._inDocker = process.env.INSIDE_DOCKER !== undefined;
this._productionMode = config.PRODUCTION_MODE;
this._inDocker = config.INSIDE_DOCKER;
this._init();
}
@ -72,17 +72,10 @@ export class AnalyticsService {
private async _init() {
this._requester = zmq.socket('pair');
this._zmqConnectionString = `tcp://${config.ZMQ_HOST}:${config.ZMQ_DEV_PORT}`; // debug mode
this._zmqConnectionString = config.ZMQ_CONNECTION_STRING;
if(this._inDocker) {
this._zmqConnectionString = config.ZMQ_CONNECTION_STRING;
} else if(this._productionMode && !this._inDocker) {
this._zmqConnectionString = config.ZMQ_CONNECTION_STRING;
if(this._zmqConnectionString === null) {
var createResult = await AnalyticsService.createIPCAddress();
this._zmqConnectionString = createResult.address;
this._ipcPath = createResult.file;
}
if(this._zmqConnectionString.startsWith('ipc')) {
this._ipcPath = AnalyticsService.createIPCAddress(this._zmqConnectionString);
}
console.log("Binding to zmq... %s", this._zmqConnectionString);
@ -214,11 +207,10 @@ export class AnalyticsService {
}, config.ANLYTICS_PING_INTERVAL);
}
private static async createIPCAddress(): Promise<{ address: string, file: string }> {
let filename = `${process.pid}.ipc`;
let p = path.join(config.ZMQ_IPC_PATH, filename);
fs.writeFileSync(p, '');
return Promise.resolve({ address: 'ipc://' + p, file: p });
private static createIPCAddress(zmqConnectionString: string): string {
let filename = zmqConnectionString.substring(6); //without 'ipc://'
fs.writeFileSync(filename, '');
return filename;
}
}

Loading…
Cancel
Save