/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["lodash","./response_parser"],function(a,b){"use strict";var c,d,e;b&&b.id;return{setters:[function(a){c=a},function(a){d=a}],execute:function(){e=function(){function a(a,b,c,e){this.backendSrv=b,this.$q=c,this.templateSrv=e,this.name=a.name,this.id=a.id,this.responseParser=new d.default(this.$q)}return a.$inject=["instanceSettings","backendSrv","$q","templateSrv"],a.prototype.interpolateVariable=function(a){if("string"==typeof a)return"'"+a+"'";var b=c.default.map(a,function(a){return"'"+a+"'"});return b.join(",")},a.prototype.query=function(a){var b=this,d=c.default.filter(a.targets,function(a){return a.hide!==!0}).map(function(c){return{refId:c.refId,intervalMs:a.intervalMs,maxDataPoints:a.maxDataPoints,datasourceId:b.id,rawSql:b.templateSrv.replace(c.rawSql,a.scopedVars,b.interpolateVariable),format:c.format}});return 0===d.length?this.$q.when({data:[]}):this.backendSrv.datasourceRequest({url:"/api/tsdb/query",method:"POST",data:{from:a.range.from.valueOf().toString(),to:a.range.to.valueOf().toString(),queries:d}}).then(this.responseParser.processQueryResult)},a.prototype.annotationQuery=function(a){var b=this;if(!a.annotation.rawQuery)return this.$q.reject({message:"Query missing in annotation definition"});var c={refId:a.annotation.name,datasourceId:this.id,rawSql:this.templateSrv.replace(a.annotation.rawQuery,a.scopedVars,this.interpolateVariable),format:"table"};return this.backendSrv.datasourceRequest({url:"/api/tsdb/query",method:"POST",data:{from:a.range.from.valueOf().toString(),to:a.range.to.valueOf().toString(),queries:[c]}}).then(function(c){return b.responseParser.transformAnnotationResponse(a,c)})},a.prototype.metricFindQuery=function(a,b){var c=this,d="tempvar";b&&b.variable&&b.variable.name&&(d=b.variable.name);var e={refId:d,datasourceId:this.id,rawSql:this.templateSrv.replace(a,{},this.interpolateVariable),format:"table"};return this.backendSrv.datasourceRequest({url:"/api/tsdb/query",method:"POST",data:{queries:[e]}}).then(function(a){return c.responseParser.parseMetricFindQueryResult(d,a)})},a.prototype.testDatasource=function(){return this.backendSrv.datasourceRequest({url:"/api/tsdb/query",method:"POST",data:{from:"5m",to:"now",queries:[{refId:"A",intervalMs:1,maxDataPoints:1,datasourceId:this.id,rawSql:"SELECT 1",format:"table"}]}}).then(function(a){return{status:"success",message:"Database Connection OK"}}).catch(function(a){return console.log(a),a.data&&a.data.message?{status:"error",message:a.data.message}:{status:"error",message:a.status}})},a}(),a("MysqlDatasource",e)}}});