(()=>{function e(g,l,t,u,s,e,n){var o,d,a,f={},i=[],r=!1,c=[{value:"NONE",name:e("No aggregation")},{value:"MINUTELY",name:e("Minutely"),disabledWhenLessThan:{qty:1,unit:"minutes"}},{value:"HOURLY",name:e("Hourly"),disabledWhenLessThan:{qty:1,unit:"hours"}},{value:"DAILY",name:e("Daily"),disabledWhenLessThan:{qty:1,unit:"days"}}],m=[{minDuration:moment.duration(1,"months"),maxDuration:null,aggregationType:"DAILY"},{minDuration:moment.duration(1,"days"),maxDuration:moment.duration(1,"months"),aggregationType:"HOURLY"},{minDuration:moment.duration(1,"minutes"),maxDuration:moment.duration(1,"days"),aggregationType:"MINUTELY"},{minDuration:null,maxDuration:moment.duration(1,"minutes"),aggregationType:"NONE"}],p=!1,y=n(function(){var e;p||(e=moment(g.filter.dateTo).diff(g.filter.dateFrom),g.filter.dateTo=new Date,g.filter.dateFrom=moment().subtract(e).toDate(),g.$broadcast("newData"))});function T(){g.filter.dateFrom&&g.filter.dateTo&&g.filter.aggregationType&&v()}function h(e){g.series=e,i.length&&t(function(){$(_.flattenDeep(i)),i.length=0}),D()}function D(){g.loading=!1}function v(){var n=f.dateFrom,a=f.dateTo,i=f.aggregationType.value,e=g.deviceId,o=g.frag,t=g.selectedKpis,r=g.datapoints;g.title=s.humanizeFragment(o||""),n&&a&&(e||r)&&(o||t||r)&&(g.loading=!0,r?(r=_.groupBy(r,function(e){return e.__target.id}),l.all(_.map(r,function(e,t){return u.getSeries(t,e,n,a,e.aggregation||i)})).then(function(e){return d=d||g.$watch("datapoints",v,!0),h(_.flattenDeep(e))}).finally(D)):t?u.getSeries(e,t,n,a,i).then(h).finally(D):o&&u.getKpi(e).then(function(t){u.getSeries(e,o,n,a,i).then(function(e){return g.truncated=e.truncated,_.map(e,function(e){return e.kpi=u.getKpiOfSerie(e,t),e})}).then(h)}).finally(D))}function $(e){!g.loading&&g.series&&g.series.length?u.appendMeasurement(g.frag||g.kpis,e,g.series).hasNew&&t(_.bind(g.$broadcast,g,"newData")):(!g.series||g.series.length||g.loading||(g.$broadcast("updateInterval"),t(v)),i.push(e))}function L(){return!!(g.filter&&g.filter.dateFrom&&g.filter.dateTo&&g.filter.aggregationType&&(g.deviceId||g.datapoints)&&(g.selectedKpis||g.frag||g.datapoints))}function b(e){g.filter.aggregationType=_.find(c,{value:e})||c[0],g.aggregationTypeDescription=g.filter.aggregationType.name.toLowerCase()}function w(){var t,n,e=moment(g.filter.dateFrom),a=moment(g.filter.dateTo),i=moment.duration(a.diff(e));g.autoAggregationOff||r||!o||o.asMilliseconds()===i.asMilliseconds()||_.forEach(m,function(e){t=!e.minDuration||(e.minDuration,e.minDuration.asMilliseconds()<=i.asMilliseconds()),n=!e.maxDuration||(e.maxDuration,e.maxDuration.asMilliseconds()>i.asMilliseconds()),t&&n&&b(e.aggregationType)}),o=i}g.onDatepickerOpen=function(e){e?t(function(){p=!0},50):p=!1},g.filter=f,g.aggregationTypes=c,g.onAggregationTypeChangedManually=function(e){g.filter.aggregationType=_.find(c,{value:e})||c[0],r=!0,T()},g.isAggregationTypeEnabled=function(e){return!(e.disabledWhenLessThan&&(e=e.disabledWhenLessThan,moment(g.dateTo).diff(g.dateFrom,e.unit)<e.qty))},g.getData=v,g.onData=$,g.$watch("realtimeState",function(e){e&&L()?(g.$broadcast("updateInterval"),t(T),y.start()):y.cancel()}),g.$watch("aggregationType",b),g.$watch("filter.dateFrom",w),g.$watch("filter.dateTo",w),g.$watch("filter.aggregationType",function(){g.aggregationType=g.filter.aggregationType.value,g.selectedAggregationTypeValue=g.filter.aggregationType.value}),g.$on("connect",function(e,t){125<t.fromLastConnect&&v()}),g.$on("changeFilters",function(){g.dateFrom=g.filter.dateFrom,g.dateTo=g.filter.dateTo,g.aggregationType=g.filter.aggregationType.value,g.interval=g.filter.interval}),g.$on("$destroy",y.cancel),a=g.$watch(L,function(e){e&&(T(),g.$on("search",T),a())})}e.$inject=["$scope","$q","$timeout","c8yLineChartSvc","c8yBase","gettext","RTAutoMove"],angular.module("c8y.core.measurements2").controller("c8yLineChartCtrl",e)})();