function getProfileSettingQsFromStore(){let qs=app.application.$store.getters.getQuerySet('user/'+my_user_id+'/settings');if(!qs){return;}
return qs.copy();}
function updateProfileSettingsQsAndSave(qs){qs.formQueryAndSend('post',qs.cache.data).then(response=>{app.application.$store.commit('setQuerySet',{url:qs.url,queryset:qs,});}).catch(error=>{debugger;});}
function guiCustomizerSkinOnChangeHandler(customizer){let qs=getProfileSettingQsFromStore();if(!qs){return;}
qs.cache.data.selectedSkin=customizer.skin.name;return updateProfileSettingsQsAndSave(qs);}
function guiCustomizerCustomSettingsOnSaveHandler(customizer){let qs=getProfileSettingQsFromStore();if(!qs){return;}
qs.cache.data.skinsSettings=customizer.skins_custom_settings;return updateProfileSettingsQsAndSave(qs);}
guiWidgets.history_chart=class HistoryChart extends guiWidgets.line_chart{constructor(options){super(options);this.format='history_chart';this.period=options.period;this.date_format='DD.MM.YY';Object.defineProperty(this,'chart_options',{get:function(){return{maintainAspectRatio:false,legend:{labels:{fontColor:guiCustomizer.skin.settings.chart_legend_text_color,},},scales:{yAxes:[{ticks:{beginAtZero:true,fontColor:guiCustomizer.skin.settings.chart_axes_text_color,},gridLines:{color:guiCustomizer.skin.settings.chart_axes_lines_color,}}],xAxes:[{ticks:{fontColor:guiCustomizer.skin.settings.chart_axes_text_color,},gridLines:{color:guiCustomizer.skin.settings.chart_axes_lines_color,}}]}};}});}
_getChartStartTime(){let monthNum=moment().format("MM");let yearNum=moment().format("YYYY");let dayNum=moment().format("DD");let hourNum="T00:00:00";let startTimeOrg="";switch(this.period.type){case"year":startTimeOrg=yearNum+"-01-01"+hourNum;break;case"month":startTimeOrg=yearNum+"-"+monthNum+"-01"+hourNum;break;case"day":startTimeOrg=yearNum+"-"+monthNum+"-"+dayNum+hourNum;break;}
return Number(moment(startTimeOrg).subtract(this.period.amount-1,this.period.type).tz(window.timeZone).format("x"),);}
_formChartDataLabels(raw_data){let labels=[];let start_time=this._getChartStartTime();for(let i=-1;i<this.period.amount;i++){let time=+moment(start_time).add(i,this.period.type).tz(window.timeZone).format("x");time=moment(time).tz(window.timeZone).format(this.date_format);labels.push(time);}
return labels;}
_formChartDataDatasets_oneLine(line,raw_data,labels){let data={};for(let index=0;index<labels.length;index++){data[labels[index]]=0;}
for(let index=0;index<raw_data[this.period.type].length;index++){let item=raw_data[this.period.type][index];let time=+moment(item[this.period.type]).tz(window.timeZone).format("x");time=moment(time).tz(window.timeZone).format(this.date_format);if(data[time]===undefined){continue;}
if(line.name.toLowerCase()=='all_tasks'){data[time]=item.all;}else if(line.name.toLowerCase()==item.status.toLowerCase()){data[time]=item.sum;}}
return Object.values(data).map(item=>Number(item));}
_formChartDataDatasets(raw_data,labels){let datasets=[];for(let key in this.lines){if(this.lines.hasOwnProperty(key)){let line=this.lines[key];if(!line.active){continue;}
datasets.push({label:line.title||line.name,data:this._formChartDataDatasets_oneLine(line,raw_data,labels),borderColor:this._getChartLineColor(line),backgroundColor:this._getChartLineColor(line,true),});}}
return datasets;}
_getChartLineColor(line,bg){let alpha=1;let prop='color';let skin=guiCustomizer.skin.settings;if(bg){alpha=0.3;prop='bg_color';}
if(skin['history_status_'+line.name]){if(skin['history_status_'+line.name][0]=="#"){let color=hexToRgbA(skin['history_status_'+line.name],alpha);return color;}
return skin['history_status_'+line.name];}
return line[prop];}
getProgressBarsData(raw_data){let all=0;let data={};for(let key in this.lines){if(this.lines.hasOwnProperty(key)){let line=this.lines[key];if(line.name=='all_tasks'){continue;}
data[line.name]={all:all,sum:0,status:line.name.toUpperCase(),};}}
if(raw_data&&raw_data.year){let stats=raw_data.year;for(let index=0;index<stats.length;index++){let record=stats[index];let status=record.status.toLowerCase();if(!data[status]){continue;}
data[status].sum+=record.sum;}
for(let key in data){if(data.hasOwnProperty(key)){all+=data[key].sum;}}
for(let key in data){if(data.hasOwnProperty(key)){data[key].all=all;}}}
return data;}
setChartPeriod(number){let amount,type;let num=Number(number);switch(num){case 1095:amount=3;type="year";break;case 365:amount=13;type="month";break;case 90:amount=3;type="month";break;default:amount=num;type="day";break;}
this.period={type:type,amount:amount,query_amount:num,};guiLocalSettings.set('chart_period',num);}};[{name:'pmwTemplatesCounter',title:'template',sort:1,active:true,},{name:'pmwProjectsCounter',title:'projects',sort:2,active:true,url:'/project'},{name:'pmwInventoriesCounter',title:'inventories',sort:3,active:true,url:'/inventory'},{name:'pmwGroupsCounter',title:'groups',sort:4,active:true,url:'/group'},{name:'pmwHostsCounter',title:'hosts',sort:5,active:true,url:'/host'},{name:'pmwUsersCounter',title:'users',sort:6,active:true,url:'/user'},].forEach(item=>{guiDashboard.widgets[item.name]=new guiWidgets.counter(item);});guiDashboard.widgets.pmwChartWidget=new guiWidgets.history_chart({name:'pmwChartWidget',title:'Tasks history',sort:7,lines:{all_tasks:{name:"all_tasks",title:"All tasks",color:"#1f77b4",bg_color:"rgba(31, 119, 180, 0.3)",active:true,},ok:{name:"ok",title:"OK",color:"#276900",bg_color:"rgba(39, 105, 0, 0.3)",active:true,},error:{name:"error",title:"ERROR",color:"#dc3545",bg_color:"rgba(220, 53, 69, 0.3)",active:true,},interrupted:{name:"interrupted",title:"INTERRUPTED",color:"#9b97e4",bg_color:"rgba(155, 151, 228, 0.3)",active:true,},delay:{name:"delay",title:"DELAY",color:"#808419",bg_color:"rgba(128, 132, 25, 0.3)",active:true,},offline:{name:"offline",title:"OFFLINE",color:"#9e9e9e",bg_color:"rgba(158, 158, 158, 0.3)",active:true,},},period:{type:'day',amount:14,query_amount:14,},},);guiDashboard.updateWidgetSettings=function(settings){for(let key in settings){if(settings.hasOwnProperty(key)){let s_item=settings[key];if(!this.widgets[key]){continue;}
for(let prop in s_item){if(s_item.hasOwnProperty(prop)){this.widgets[key][prop]=s_item[prop];}}}}};guiDashboard.updateChartLineSettings=function(settings){for(let key in settings){if(!this.widgets.pmwChartWidget.lines[key]){continue;}
let s_item=settings[key];for(let prop in s_item){if(s_item.hasOwnProperty(prop)){this.widgets.pmwChartWidget.lines[key][prop]=s_item[prop];}}}};guiDashboard.updateSettings=function(settings){if(settings.autoupdateInterval){guiLocalSettings.set('page_update_interval',settings.autoupdateInterval);}
if(settings.selectedSkin){guiLocalSettings.set('skin',settings.selectedSkin);guiCustomizer.skin.name=settings.selectedSkin;}
if(settings.skinsSettings){guiLocalSettings.set('skins_settings',settings.skinsSettings);guiCustomizer.skins_custom_settings=settings.skinsSettings;}
if(settings.widgetSettings){guiLocalSettings.set('widget_settings',settings.widgetSettings);guiDashboard.updateWidgetSettings(settings.widgetSettings);}
if(settings.chartLineSettings&&guiDashboard.widgets.pmwChartWidget){guiLocalSettings.set('chart_line_settings',settings.chartLineSettings);guiDashboard.updateChartLineSettings(settings.chartLineSettings);}};if(guiLocalSettings.get('widget_settings')){guiDashboard.updateWidgetSettings(guiLocalSettings.get('widget_settings'));}
if(guiLocalSettings.get('chart_line_settings')){guiDashboard.updateChartLineSettings(guiLocalSettings.get('chart_line_settings'));}
if(guiLocalSettings.get('chart_period')){guiDashboard.widgets.pmwChartWidget.setChartPeriod(guiLocalSettings.get('chart_period'));}
customRoutesComponentsTemplates.home={mixins:[the_basest_view_mixin,view_with_autoupdate_mixin],template:"#template_pm_dashboard",data(){return{widgets:guiDashboard.widgets,widgets_data:{},};},watch:{'widgets.pmwChartWidget.period':function(value){this.setWidgetsData().then(data=>{this.widgets_data=data;});}},created(){this.fetchData();},computed:{title(){return'Dashboard';},sorted_widgets(){return Object.values(this.widgets).sort((a,b)=>{return a.sort-b.sort;});},},methods:{fetchData(){this.initLoading();this.getWidgetsData().then(data=>{this.widgets_data=data;this.setLoadingSuccessful();this.startAutoUpdate();}).catch(error=>{debugger;this.setLoadingError(error);});},getAutoUpdateInterval(){return 15000;},updateData(){return this.setWidgetsData().then(data=>{if(!deepEqual(this.widgets_data,data)){this.widgets_data=data;}
return data;});},getWidgetsData(){let data=this.$store.getters.getWidgets(this.$route.path);if(data){return Promise.resolve(data);}
return this.setWidgetsData();},setWidgetsData(){return this.loadStats().then(response=>{let exclude_stats=['jobs'];let w_data={};for(let key in response.data){if(response.data.hasOwnProperty(key)){if(exclude_stats.includes(key)){w_data.pmwChartWidget=response.data[key];}
w_data['pmw'+capitalizeString(key)+'Counter']=response.data[key];}}
this.$store.commit('setWidgets',{url:this.$route.path,data:w_data,});return w_data;});},loadStats(){return app.api.bulkQuery(this.formBulkStats());},formBulkStats(){return{type:'get',item:'stats',filters:"last="+this.widgets.pmwChartWidget.period.query_amount,};},},};tabSignal.connect('app.afterInit',(obj)=>{let app=obj.app;let setting_view=app.views["/profile/settings/"];let qs=setting_view.objects.clone();let f_obj={};f_obj[path_pk_key]=my_user_id;qs.url=qs.url.format(f_obj).replace(/^\/|\/$/g,"");qs.get().then(instance=>{guiDashboard.updateSettings(instance.data);let qs_1=app.application.$store.getters.getQuerySet(qs.url);if(!qs_1){app.application.$store.commit('setQuerySet',{url:qs.url,queryset:qs,});}
tabSignal.connect('GuiCustomizer.skin.name.changed',guiCustomizerSkinOnChangeHandler);tabSignal.connect('GuiCustomizer.skins_custom_settings.saved',guiCustomizerCustomSettingsOnSaveHandler);tabSignal.connect('GuiCustomizer.skins_custom_settings.reseted',guiCustomizerCustomSettingsOnSaveHandler);});});