How to use the midway-web.inject function in midway-web

To help you get started, we’ve selected a few midway-web examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github midwayjs / sandbox / packages / sandbox-core / src / core / service / metricsService.ts View on Github external
import {TSDB} from '../dataSource/tsdb';
import * as _ from 'lodash';
import {MetricsManager} from '../manager/metricsManager';
import {ApplicationService} from './applicationService';
import {IMetricsService} from '../../interface/services/IMetricsService';

const BUILT_IN_PREFIXS = ['system.', 'node.', 'error.', 'middleware.'];
const BUILD_IN_REG = new RegExp(`^(${BUILT_IN_PREFIXS.join('|')})`);

@provide('metricsService')
export class MetricsService implements IMetricsService {

  @inject('tsdb')
  protected tsdb: TSDB;

  @inject('metricsManager')
  protected metricsManager: MetricsManager;

  @inject('applicationService')
  protected applicationService: ApplicationService;

  async getMetricsNames(options: ComplexSelector & AppSelector & TimeWindowOptions): Promise {
    return this.metricsManager.getMetricsNames(options);
  }

  async getCustomMetricsNames(options: ComplexSelector & AppSelector & TimeWindowOptions): Promise {
    const allMetricsName = await this.metricsManager.getMetricsNames(options);
    return allMetricsName.filter((name) => !BUILD_IN_REG.test(name));
  }

  async queryMetricsLatest(options: ComplexSelector & MetricsNamesSelector & AppSelector): Promise {
github midwayjs / sandbox / packages / sandbox-core / src / core / service / metricsService.ts View on Github external
import { provide, inject } from 'midway-web';
import { ComplexSelector, MetricsNamesSelector, IndicatorResult, TimeWindowOptions,
  AppSelector } from '../../interface/services/common';
import {TSDB} from '../dataSource/tsdb';
import * as _ from 'lodash';
import {MetricsManager} from '../manager/metricsManager';
import {ApplicationService} from './applicationService';
import {IMetricsService} from '../../interface/services/IMetricsService';

const BUILT_IN_PREFIXS = ['system.', 'node.', 'error.', 'middleware.'];
const BUILD_IN_REG = new RegExp(`^(${BUILT_IN_PREFIXS.join('|')})`);

@provide('metricsService')
export class MetricsService implements IMetricsService {

  @inject('tsdb')
  protected tsdb: TSDB;

  @inject('metricsManager')
  protected metricsManager: MetricsManager;

  @inject('applicationService')
  protected applicationService: ApplicationService;

  async getMetricsNames(options: ComplexSelector & AppSelector & TimeWindowOptions): Promise {
    return this.metricsManager.getMetricsNames(options);
  }

  async getCustomMetricsNames(options: ComplexSelector & AppSelector & TimeWindowOptions): Promise {
    const allMetricsName = await this.metricsManager.getMetricsNames(options);
    return allMetricsName.filter((name) => !BUILD_IN_REG.test(name));
  }
github midwayjs / sandbox / packages / sandbox-core / src / app / controller / remoteDebugCtrl.ts View on Github external
import { get, inject } from 'midway-web';
import { IRemoteDebugService } from '../../interface/services/IRemoteDebugService';
import { IPrivilegeAdapter } from '../../interface/adapter/IPrivilegeAdapter';

export class RemoteDebugCtrl {

  @inject('remoteDebugService')
  remoteDebugService: IRemoteDebugService;

  @inject('privilegeAdapter')
  protected privilegeAdapter: IPrivilegeAdapter;

  @get('/getDebuggableHost')
  async getDebuggableHost(ctx) {

    const query = ctx.query;
    const { scope, scopeName, env, ip, hostname } = query;
    const uid: string = ctx.uid;
    const hasPermission = await this.privilegeAdapter.isAppOps(scope, scopeName, uid);
    if (!hasPermission) {
      throw new Error('You have no permission to reach this');
    }
    const data =  await this.remoteDebugService.getDebuggableHost({
      scope, scopeName, env, ip, hostname, uid,
    });
    ctx.body = {
github midwayjs / sandbox / packages / sandbox-core / src / core / manager / metricsManager.ts View on Github external
import { provide, inject } from 'midway-web';
import * as _ from 'lodash';
import {AppSelector, IndicatorResult, MetricNameJSON, TimeWindowOptions} from '../../interface/services/common';
import {TSDB} from '../dataSource/tsdb';
import {ISadMetricsAdapter} from '../../interface/adapter/ISadMetricsAdapter';

@provide('metricsManager')
export class MetricsManager {

  @inject('tsdb')
  protected tsdb: TSDB;

  @inject('sadMetricsAdapter')
  protected sadMetricsAdapter: ISadMetricsAdapter;

  static pickLatestDp (dps) {
    if (_.isEmpty(dps)) {
      return null;
    }
    let times = [];
    for (const key of Object.keys(dps)) {
      times.push(parseInt(key, 10));
    }
    times = times.sort((a, b) => b - a);
    const latest = dps[times[0]];
    return latest;
github midwayjs / sandbox / packages / sandbox-core / src / core / manager / metricsManager.ts View on Github external
import { provide, inject } from 'midway-web';
import * as _ from 'lodash';
import {AppSelector, IndicatorResult, MetricNameJSON, TimeWindowOptions} from '../../interface/services/common';
import {TSDB} from '../dataSource/tsdb';
import {ISadMetricsAdapter} from '../../interface/adapter/ISadMetricsAdapter';

@provide('metricsManager')
export class MetricsManager {

  @inject('tsdb')
  protected tsdb: TSDB;

  @inject('sadMetricsAdapter')
  protected sadMetricsAdapter: ISadMetricsAdapter;

  static pickLatestDp (dps) {
    if (_.isEmpty(dps)) {
      return null;
    }
    let times = [];
    for (const key of Object.keys(dps)) {
      times.push(parseInt(key, 10));
    }
    times = times.sort((a, b) => b - a);
    const latest = dps[times[0]];
    return latest;
  }

  getMetricsNames(options: AppSelector & TimeWindowOptions): Promise {
github midwayjs / sandbox / packages / sandbox-core / src / core / adapter / pandoraAdapter.ts View on Github external
import { inject, config } from 'midway-web';
import {AppSelector, HostSelector} from '../../interface/services/common';
import {IPandoraAdapter} from '../../interface/adapter/IPandoraAdapter';
import {IRemoteExecuteAdapter} from '../../interface/adapter/IRemoteExecuteAdapter';
import {parse as urlparse} from 'url';

export class PandoraAdapter implements IPandoraAdapter {

  @inject('remoteExecuteAdapter')
  protected remoteExecuteAdapter: IRemoteExecuteAdapter;

  @config('pandora')
  protected config;

  async invokeRestful(host: HostSelector, url: string, options?: any) {
    const pandoraRestfulPort = this.config.restfulPort;
    url = url.replace(/^\//, '');
    const cmd = `curl http://127.0.0.1:${pandoraRestfulPort}/${url} 2>/dev/null`;
    const res = await this.remoteExecuteAdapter.exec(host, cmd, options).catch((err) => {
      if (err.message === 'CALL_ERROR: exit 7') {
        err.message = '请求 Pandora 失败,请确保 pandora agent 已正常启动';
      }
      throw err;
    });
    return JSON.parse(res);
github midwayjs / sandbox / packages / sandbox-core / src / app / controller / errorCtrl.ts View on Github external
import * as Interface from '../../interface/services/IErrorService';
import * as moment from 'moment';
import * as _ from 'lodash';

import { inject, get, logger } from 'midway-web';

export class ErrorCtrl {

  @logger()
  logger;

  @inject('errorService')
  errorServ;

  @get('/queryErrors')
  async queryErrors(ctx) {

    const startTime: moment.Moment = moment(parseInt(ctx.request.query.startTime, 10));
    const endTime: moment.Moment = moment(parseInt(ctx.request.query.endTime, 10));
    const ago: moment.Moment = moment().subtract(15, 'minutes');
    const _s: moment.Moment = !startTime.isValid() ? ago : startTime;
    const _e: moment.Moment = !endTime.isValid() ? moment() : endTime;
    const scope: string = _.trim(ctx.request.query.scope || '');
    const scopeName: string = _.trim(ctx.request.query.scopeName || '');
    const env: string = _.trim(ctx.request.query.env || '');
    const logPath: string = _.trim(ctx.request.query.logPath || '');
    const ip: string = _.trim(ctx.request.query.ip || '');
    const keyword: string = _.trim(ctx.request.query.keyword) || '';
github midwayjs / sandbox / packages / sandbox-core / src / app / controller / metricsCtrl.ts View on Github external
import { inject, get, controller, provide, priority } from 'midway-web';

import {MetricsUtils} from '../util/metricsUtils';
import {MetricNameJSON} from '../../interface/services/common';

@priority(0)
@provide()
@controller('/v2/api/metrics/')
export class MetricsCtrl {

  @inject('metricsService')
  private metricsService;

  @get('/getMetricsNames')
  async getMetricsNames(ctx) {

    const query = ctx.query;
    const {scope, scopeName, env} = query;
    const data = await this.metricsService.getMetricsNames({
      scope, scopeName, env,
    });
    ctx.body = {
      success: true,
      data,
    };

  }
github midwayjs / sandbox / packages / sandbox-core / src / app / controller / remoteDebugCtrl.ts View on Github external
import { get, inject } from 'midway-web';
import { IRemoteDebugService } from '../../interface/services/IRemoteDebugService';
import { IPrivilegeAdapter } from '../../interface/adapter/IPrivilegeAdapter';

export class RemoteDebugCtrl {

  @inject('remoteDebugService')
  remoteDebugService: IRemoteDebugService;

  @inject('privilegeAdapter')
  protected privilegeAdapter: IPrivilegeAdapter;

  @get('/getDebuggableHost')
  async getDebuggableHost(ctx) {

    const query = ctx.query;
    const { scope, scopeName, env, ip, hostname } = query;
    const uid: string = ctx.uid;
    const hasPermission = await this.privilegeAdapter.isAppOps(scope, scopeName, uid);
    if (!hasPermission) {
      throw new Error('You have no permission to reach this');
    }
    const data =  await this.remoteDebugService.getDebuggableHost({
github midwayjs / sandbox / packages / sandbox-core / src / app / controller / tracingCtrl.ts View on Github external
import { get, post, inject } from 'midway-web';
import {wrapJson} from '../../core/util/util';

export class TracingCtrl {

  @inject('traceService')
  traceService;

  @get('/listFocusTraces')
  async focusTraces(ctx) {
    const query = ctx.query;
    const result = await this.traceService.listFocusTraces(query);
    ctx.body = wrapJson(result);
  }

  @get('/listTraces')
  async traces(ctx) {
    const query = ctx.query;
    const result = await this.traceService.listTraces(query);
    ctx.body = wrapJson(result);
  }