You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.1 KiB
75 lines
2.1 KiB
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 |
|
); |
|
} |
|
}
|
|
|