(()=>{function t(i,t,s,e,n,o,r,u,a,c){var h=this,c=(this.APP_IGNORE=["administration","vendme_plugins","core","c8ydata","tenantadmin","platformadmin"],c.$on("language:loaded",function(){h.fetch()}),c.$on("language:change",function(){h.fetch()}),{requestCount:r("calculating number of API requests"),deviceRequestCount:r("calculating number of device API requests"),totalResourceCreateAndUpdateCount:r("calculating number of inbound API requests"),storageSize:r("calculating storage"),devicesCount:r("calculating number of devices"),usersCount:r("calculating number of users"),deviceWithChildrenCount:r("calculating number of devices")}),d=e.administration.statistics.thisMonthExtraTooltip;function l(){return!!d}function m(t,e){t=u.getString("Usage between {{start}} - {{end}}",t);return d&&"thisMonth"===e&&(t+="\n".concat(u.getString(d))),t}function g(t){return u.getString("Amount of data in use on {{end}}.",t)}function f(t){return u.getString("Number of root devices on {{end}}. Does not include child devices.",t)}function v(t){return u.getString("Number of devices on {{end}}, including child devices.",t)}this.summary={thisMonth:_.clone(c),lastMonth:_.clone(c)},this.tooltips=function(t,e){return{month:_.partial(m,t,e),storageSize:_.partial(g,t),rootDevicesCount:_.partial(f,t),devicesCount:_.partial(v,t)}},this.fetch=function(){a.hasAllRoles(["ROLE_TENANT_STATISTICS_READ"]).then(_.bind(this.fetchStatisticsIfPermission,this)),a.hasAllRoles(["ROLE_APPLICATION_MANAGEMENT_READ"]).then(_.bind(this.fetchSubscribedApps,this))},this.fetchStatisticsIfPermission=function(t){var e;t&&((t=s.monthFilter({},1)).dateFrom=moment(t.dateFrom).subtract(1,"month").endOf("month").format(s.dateFormat),t=t,delete(e=s.monthFilter()).dateTo,e=e,this.formatMonth("thisMonth",e),this.formatMonth("lastMonth",t),this.fetchStatistics("thisMonth",e).then(_.bind(this.fetchUsersCount,this)),this.fetchStatistics("lastMonth",t))},this.formatMonth=function(t,e){this[t]={},e.dateFrom?this[t].start=moment(e.dateFrom).format("ll"):this[t].start="?",this[t].end=(e.dateTo?moment(e.dateTo):moment()).format("ll"),this.assignTooltips(this[t],t)},this.assignTooltips=function(t,e){t.tooltips=this.tooltips(t,e)},this.fetchStatistics=function(t,e){return i.getSummary(e).then(_.bind(this.setStatistics,this,t))},this.fetchSubscribedApps=function(t){t&&n.list().then(_.bind(this.setApps,this))},this.setStatistics=function(t,e){var i,s=u.getString("<Not calculated>"),e=e.data;e.rootDevicesCount=e.deviceCount,e.devicesCount=e.deviceWithChildrenCount,_.isNumber(e.storageSize)&&(e.storageSize=Number(e.storageSize)<0?s:o("bytes")(e.storageSize)),e.storageLimitPerDevice&&(i=Math.max(e.devicesCount,1),e.storageLimit=o("bytes")(e.storageLimitPerDevice*i)),Number(e.rootDevicesCount)<0&&(e.rootDevicesCount=s),Number(e.devicesCount)<0&&(e.devicesCount=s),this.summary[t]={requestCount:_.isNumber(e.requestCount)?u.getPlural(e.requestCount,"1 API request, including:","{{$count}} API requests, including:",e):void 0,deviceRequestCount:_.isNumber(e.deviceRequestCount)?u.getPlural(e.deviceRequestCount,"1 device API request","{{$count}} device API requests",e):void 0,totalResourceCreateAndUpdateCount:_.isNumber(e.totalResourceCreateAndUpdateCount)?u.getPlural(e.totalResourceCreateAndUpdateCount,"1 inbound API request","{{$count}} inbound API requests",e):void 0,storageSize:e.storageSize?u.getString("{{storageSize}} storage",e):void 0,storageLimit:e.storageLimit?u.getString("{{storageLimit}} storage quota",e):void 0,rootDevicesCount:_.isNumber(e.rootDevicesCount)?u.getPlural(e.rootDevicesCount,"1 root device","{{$count}} root devices",e):void 0,devicesCount:_.isNumber(e.devicesCount)?u.getPlural(e.devicesCount,"1 device","{{$count}} devices",e):void 0,show:!0}},this.fetchUsersCount=function(){t.list({pageSize:1}).then(_.bind(this.setUsersCount,this))},this.setUsersCount=function(t){t=t.statistics.totalPages;this.summary.thisMonth.usersCount=u.getPlural(t,"1 user","{{$count}} users",{})},this.setApps=function(t){this.apps=_(t).filter(_.bind(this.marketApps,this)).filter(_.bind(this.hiddenApps,this)).filter(_.bind(this.ignoredApps,this)).value()},this.marketApps=function(t){return"MARKET"===t.availability},this.hiddenApps=function(t){return!(t.manifest&&!0===t.manifest.noAppSwitcher)},this.ignoredApps=function(t){return!_.includes(this.APP_IGNORE,t.name)},this.shouldDisplayCustomTooltip=l}t.$inject=["c8yStatistics","c8yUser","c8yBase","c8yConfig","c8yApplication","$filter","gettext","gettextCatalog","c8yPermissions","$rootScope"],angular.module("c8y.parts.statistics").controller("StatisticsCtrl",t)})();