/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

define(["angular","lodash","app/core/utils/kbn"],function(a,b,c){"use strict";var d=a.module("grafana.controllers");d.controller("GraphiteImportCtrl",["$scope","datasourceSrv","dashboardSrv","$location",function(d,e,f,g){function h(d,e){var h,i,j,k=2,l=300;h={title:"",panels:[],height:l},i=a.copy(h);var m=f.create({});m.rows=[],m.title=d.name,m.rows.push(i),b.each(d.graphs,function(c,d){i.panels.length===k&&(i=a.copy(h),m.rows.push(i)),j={type:"graph",span:12/k,title:c[1].title,targets:[],datasource:e,id:d+1},b.each(c[1].target,function(a){j.targets.push({target:a})}),i.panels.push(j)}),window.grafanaImportDashboard=m,g.path("/dashboard-import/"+c.slugifyForUrl(m.title))}d.options={},d.init=function(){d.datasources=[],b.each(e.getAll(),function(a){"graphite"===a.type&&(d.options.sourceName=a.name,d.datasources.push(a.name))})},d.listAll=function(){e.get(d.options.sourceName).then(function(a){d.datasource=a,d.datasource.listDashboards("").then(function(a){d.dashboards=a},function(a){var b=a.message||a.statusText||"Error";d.appEvent("alert-error",["Failed to load dashboard list from graphite",b])})})},d["import"]=function(a){d.datasource.loadDashboard(a).then(function(a){if(!a.data||!a.data.state)throw{message:"no dashboard state received from graphite"};h(a.data.state,d.datasource.name)},function(a){var b=a.message||a.statusText||"Error";d.appEvent("alert-error",["Failed to load dashboard from graphite",b])})}}])});