rozetko
2 years ago
23 changed files with 164 additions and 170 deletions
@ -0,0 +1,31 @@ |
|||||||
|
import { InfluxdbMetric } from './influxdb'; |
||||||
|
import { GraphiteMetric } from './graphite'; |
||||||
|
import { DatasourceConnector, DatasourceType } from '.'; |
||||||
|
import { PrometheusMetric } from './prometheus'; |
||||||
|
import { PostgresMetric } from './postgres'; |
||||||
|
import { ElasticsearchMetric } from './elasticsearch'; |
||||||
|
import { MysqlMetric } from './mysql'; |
||||||
|
|
||||||
|
import { QueryConfig } from '../models/query_config'; |
||||||
|
|
||||||
|
|
||||||
|
export function connectorFactory( |
||||||
|
queryConfig: QueryConfig, |
||||||
|
): DatasourceConnector { |
||||||
|
const classMap = { |
||||||
|
[DatasourceType.INFLUXDB]: InfluxdbMetric, |
||||||
|
[DatasourceType.GRAPHITE]: GraphiteMetric, |
||||||
|
[DatasourceType.PROMETHEUS]: PrometheusMetric, |
||||||
|
[DatasourceType.POSTGRES]: PostgresMetric, |
||||||
|
[DatasourceType.ELASTICSEARCH]: ElasticsearchMetric, |
||||||
|
[DatasourceType.MYSQL]: MysqlMetric, |
||||||
|
}; |
||||||
|
const datasource = queryConfig.datasource; |
||||||
|
const targets = queryConfig.targets; |
||||||
|
if(classMap[datasource.type] === undefined) { |
||||||
|
console.error(`Datasources of type ${datasource.type} are not supported currently`); |
||||||
|
throw new Error(`Datasources of type ${datasource.type} are not supported currently`); |
||||||
|
} else { |
||||||
|
return new classMap[datasource.type](datasource, targets); |
||||||
|
} |
||||||
|
} |
@ -1,4 +1,4 @@ |
|||||||
import { SqlMetric } from './sql_metric'; |
import { SqlMetric } from './sql'; |
||||||
|
|
||||||
export class MysqlMetric extends SqlMetric { |
export class MysqlMetric extends SqlMetric { |
||||||
|
|
@ -1,4 +1,4 @@ |
|||||||
import { SqlMetric } from './sql_metric'; |
import { SqlMetric } from './sql'; |
||||||
|
|
||||||
export class PostgresMetric extends SqlMetric { |
export class PostgresMetric extends SqlMetric { |
||||||
|
|
@ -1,75 +0,0 @@ |
|||||||
import { InfluxdbMetric } from './influxdb_metric'; |
|
||||||
import { GraphiteMetric } from './graphite_metric'; |
|
||||||
import { AbstractMetric, Datasource, DatasourceType, MetricId } from './metric'; |
|
||||||
import { PrometheusMetric } from './prometheus_metric'; |
|
||||||
import { PostgresMetric } from './postgres_metric'; |
|
||||||
import { ElasticsearchMetric } from './elasticsearch_metric'; |
|
||||||
import { MysqlMetric } from './mysql_metric'; |
|
||||||
|
|
||||||
export function metricFactory( |
|
||||||
datasource: Datasource, |
|
||||||
targets: any[], |
|
||||||
id?: MetricId |
|
||||||
): AbstractMetric { |
|
||||||
|
|
||||||
const classMap = { |
|
||||||
[DatasourceType.INFLUXDB]: InfluxdbMetric, |
|
||||||
[DatasourceType.GRAPHITE]: GraphiteMetric, |
|
||||||
[DatasourceType.PROMETHEUS]: PrometheusMetric, |
|
||||||
[DatasourceType.POSTGRES]: PostgresMetric, |
|
||||||
[DatasourceType.ELASTICSEARCH]: ElasticsearchMetric, |
|
||||||
[DatasourceType.MYSQL]: MysqlMetric, |
|
||||||
}; |
|
||||||
if(classMap[datasource.type] === undefined) { |
|
||||||
console.error(`Datasources of type ${datasource.type} are not supported currently`); |
|
||||||
throw new Error(`Datasources of type ${datasource.type} are not supported currently`); |
|
||||||
} else { |
|
||||||
return new classMap[datasource.type](datasource, targets, id); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
export class Metric { |
|
||||||
datasource: Datasource; |
|
||||||
targets: any[]; |
|
||||||
id?: MetricId; |
|
||||||
private _metricQuery?: AbstractMetric; |
|
||||||
|
|
||||||
constructor(datasource: Datasource, targets: any[], id?: MetricId) { |
|
||||||
if(datasource === undefined) { |
|
||||||
throw new Error('datasource is undefined'); |
|
||||||
} |
|
||||||
if(targets === undefined) { |
|
||||||
throw new Error('targets is undefined'); |
|
||||||
} |
|
||||||
this.datasource = datasource; |
|
||||||
this.targets = targets; |
|
||||||
this.id = id; |
|
||||||
} |
|
||||||
|
|
||||||
public get metricQuery() { |
|
||||||
if(this._metricQuery === undefined) { |
|
||||||
this._metricQuery = metricFactory(this.datasource, this.targets, this.id); |
|
||||||
} |
|
||||||
return this._metricQuery; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
public toObject() { |
|
||||||
return { |
|
||||||
datasource: this.datasource, |
|
||||||
targets: this.targets, |
|
||||||
_id: this.id |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
static fromObject(obj: any): Metric { |
|
||||||
if(obj === undefined) { |
|
||||||
throw new Error('obj is undefined'); |
|
||||||
} |
|
||||||
return new Metric( |
|
||||||
obj.datasource, |
|
||||||
obj.targets, |
|
||||||
obj._id |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,45 @@ |
|||||||
|
import { Datasource, DatasourceConnector } from '../connectors'; |
||||||
|
import { connectorFactory } from '../connectors/connector_factory'; |
||||||
|
|
||||||
|
|
||||||
|
export class QueryConfig { |
||||||
|
datasource: Datasource; |
||||||
|
// TODO: Target type (depends on datasource type)
|
||||||
|
targets: any[]; |
||||||
|
private _datasourceConnector?: DatasourceConnector; |
||||||
|
|
||||||
|
constructor(datasource: Datasource, targets: any[]) { |
||||||
|
if(datasource === undefined) { |
||||||
|
throw new Error('datasource is undefined'); |
||||||
|
} |
||||||
|
if(targets === undefined) { |
||||||
|
throw new Error('targets is undefined'); |
||||||
|
} |
||||||
|
this.datasource = datasource; |
||||||
|
this.targets = targets; |
||||||
|
} |
||||||
|
|
||||||
|
get datasourceConnector(): DatasourceConnector { |
||||||
|
if(this._datasourceConnector === undefined) { |
||||||
|
this._datasourceConnector = connectorFactory(this); |
||||||
|
} |
||||||
|
return this._datasourceConnector; |
||||||
|
} |
||||||
|
|
||||||
|
public toObject() { |
||||||
|
return { |
||||||
|
datasource: this.datasource, |
||||||
|
targets: this.targets, |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
static fromObject(obj: any): QueryConfig { |
||||||
|
if(obj === undefined) { |
||||||
|
throw new Error('obj is undefined'); |
||||||
|
} |
||||||
|
return new QueryConfig( |
||||||
|
obj.datasource, |
||||||
|
obj.targets, |
||||||
|
); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue