function historyModelsFieldsHandler(model){let str="models[{0}].fields.beforeInit".format([model]);tabSignal.connect(str,(fields)=>{fields.start_time.format='one_history_date_time';fields.stop_time.format='one_history_date_time';if(fields.inventory){fields.inventory.format='fk';fields.inventory.additionalProperties={model:{$ref:"#/definitions/Inventory"},value_field:"id",view_field:"name",};}
if(fields.executor){fields.executor.format='history_executor';fields.executor.additionalProperties={model:{$ref:"#/definitions/User"},value_field:"id",view_field:"username",};}
if(fields.project){fields.project.format='fk';fields.project.additionalProperties={model:{$ref:"#/definitions/Project"},value_field:"id",view_field:"name",};}
if(fields.options){fields.options.hidden=true;}
if(fields.initiator){fields.initiator.format='history_initiator';fields.initiator.additionalProperties={list_paths:Object.values(history_initiator_types),view_field:'name',value_field:'id',};}
if(fields.initiator_type){fields.initiator_type.hidden=true;}
if(fields.revision){fields.revision.format='one_history_revision';}});}
function OneHistoryFieldsHandler(model){tabSignal.connect("models["+model+"].fields.beforeInit",(fields)=>{for(let field in fields){fields[field].format='one_history_string';if(['kind','raw_args','raw_stdout','initiator_type'].includes(field)){fields[field].format='hidden';}else if(['start_time','stop_time'].includes(field)){fields[field].format='one_history_date_time';}}
fields.executor.format='one_history_executor';fields.initiator.format='one_history_initiator';fields.project.format='one_history_fk';fields.inventory.format='one_history_fk';fields.inventory.hidden=true;fields.execute_args.format='one_history_execute_args';fields.execution_time.format='one_history_uptime';fields.revision.format='one_history_revision';fields.status.format='one_history_choices';fields.raw_inventory.format='one_history_raw_inventory';fields.raw_inventory.hidden=true;fields.mode.format='dynamic';fields.mode.additionalProperties={callback:OneHistory_kind_mode_callback,field:['kind'],};});}
function historyPathsFiltersHandler(path){tabSignal.connect("views["+path+"].filters.beforeInit",filters=>{for(let index in filters){let filter=filters[index];if(filter.name=='status'){filter.type='choices';filter.enum=app.models['History'].fields.status.options.enum;}}});}
function historyPathsViewsHandler(path){tabSignal.connect("views["+path+"].afterInit",(obj)=>{if(obj.view.schema.type=='page'){obj.view.mixins=obj.view.mixins.concat(history_pk_mixin);obj.view.template='#template_view_history_one';}});tabSignal.connect('views['+path+'].created',obj=>{if(obj.view.schema.type=='list'&&obj.view.schema.operations&&obj.view.schema.operations.add){delete obj.view.schema.operations.add;}});}
function OneHistory_kind_mode_callback(parent_values={}){let obj={format:'one_history_string',save_value:true,};if(parent_values['kind']){obj.title=parent_values['kind'].toLowerCase();}
return obj;}
function addHistorySignals(){history_models.forEach(historyModelsFieldsHandler);history_paths.forEach(historyPathsViewsHandler);history_paths.forEach(historyPathsFiltersHandler);OneHistoryFieldsHandler('OneHistory');}
var history_models=['History','OneHistory','ProjectHistory'];var history_initiator_types={project:'/project/',template:'/project/{'+path_pk_key+'}/template/',scheduler:'/project/{'+path_pk_key+'}/periodic_task/',};var history_paths=['/history/','/history/{'+path_pk_key+'}/','/project/{'+path_pk_key+'}/history/','/project/{'+path_pk_key+'}/history/{history_id}/',];var history_pk_mixin={data:function(){return{inventory_toggle:false,output_toggle:true,information_toggle:true,was_cleared:undefined,}},beforeRouteUpdate(to,from,next){this.stopChildrenAutoUpdate();this.stopAutoUpdate();next();},beforeRouteLeave(to,from,next){this.stopChildrenAutoUpdate();this.stopAutoUpdate();this.$destroy();next();},computed:{inventory_field(){let options=$.extend(true,{},this.view.objects.model.fields.inventory.options);options.format='fk_just_value';options.hidden=false;let field=new guiFields.fk_just_value(options);field=guiFields.fk_just_value.prepareField(field,this.$route.name);return field;},inventory_field_prop_data(){return this.data.instance.data;},inventory_field_wrapper_opt(){return{use_prop_data:true,readOnly:true,};},raw_inventory_field(){let options=$.extend(true,{},this.view.objects.model.fields.raw_inventory.options);options.format='one_history_raw_inventory';options.hidden=false;let field=new guiFields.one_history_raw_inventory(options);return field;},clear_button_show(){if(this.data.instance.data&&!["RUN","DELAY"].includes(this.data.instance.data.status)){return true;}
return false;},clear_button_options(){return{name:'clear',}},clear_button_look(){return{icon_classes:['fa','fa-trash','fa-lg'],title_classes:['clear-btn-title'],classes:['btn','btn-card-tool','btn-sm','btn-light','btn-icon','clear-btn','hidden-button'],};}},methods:{stopChildrenAutoUpdate(){this.$children.forEach(child=>{if(child.stopAutoUpdate&&typeof child.stopAutoUpdate=='function'){child.stopAutoUpdate();}});},clearInstance(){let qs=this.getQuerySet(this.view,this.qs_url).clone({url:this.qs_url+'/clear'});qs.formQueryAndSend('delete').then(response=>{guiPopUp.success(pop_up_msg.instance.success.execute.format(['clear',this.view.schema.name]));this.was_cleared=true;}).catch(error=>{let str=app.error_handler.errorToString(error);let srt_to_show=pop_up_msg.instance.error.execute.format(['clear',this.view.schema.name,str],);app.error_handler.showError(srt_to_show,str);});},},components:{clear_button:{mixins:[base_button_mixin],},history_stdout:{template:"#template_history_stdout",mixins:[view_with_autoupdate_mixin],props:['instance','url','cleared'],data(){return{lines:[],lines_limit:500,loading:false,lines_qs:undefined,last_status:undefined,stdout_el:undefined,};},watch:{'cleared':function(value){if(value){this.sendLinesApiRequest({limit:this.lines_limit}).then(response=>{this.lines=[];this.saveNewLines(response.data.results);});}},},computed:{raw_stdout_link(){let url=this.url.replace(/^\/|\/$/g,"");return'/api/'+api_version+'/'+url+'/raw';},lines_url(){let url=this.url.replace(/^\/|\/$/g,"");return"/"+url+"/lines/";},gluedLines(){let obj={};for(let index in this.lines){let subline=this.lines[index];if(!obj[subline.line_gnumber]){obj[subline.line_gnumber]={id:subline.line_gnumber,text:subline.line,};}else{obj[subline.line_gnumber].text+=subline.line;}}
return obj;},linesHTML(){let html=[];for(let key in this.gluedLines){html.push(this.ansiUp(this.gluedLines[key].text));}
return html.join("");},needLoadAdditionalData(){if(["RUN","DELAY"].includes(this.instance.data.status)){return false;}
let lines=[...this.lines].sort((a,b)=>{return b.line_gnumber-a.line_gnumber;});if(lines.last.line_gnumber==1||lines.last.line_gnumber==0){return false;}
return true;},},created(){this.lines_qs=new guiQuerySets.QuerySet(app.models.OneHistory,this.lines_url);this.getLines().then(responce=>{setTimeout(()=>{$(this.stdout_el).scrollTop($(this.stdout_el).prop('scrollHeight'));},300);this.startAutoUpdate();});},mounted(){this.stdout_el=$(this.$el).find('.history-stdout')[0];},methods:{updateData(){let instance_data=this.instance.data;if(["RUN","DELAY"].includes(instance_data.status)){this.last_status=instance_data.status;return this.loadLinesFromBeginning().then(response=>{setTimeout(()=>{$(this.stdout_el).scrollTop($(this.stdout_el).prop('scrollHeight'));},300);});}else{if(["RUN","DELAY"].includes(this.last_status)){this.last_status=instance_data.status;this.sendLinesApiRequest({limit:this.lines_limit}).then(response=>{this.saveNewLines(response.data.results);setTimeout(()=>{$(this.stdout_el).scrollTop($(this.stdout_el).prop('scrollHeight'));},300);});}
return Promise.resolve();}},updateDataOnScroll(){if(this.needLoadAdditionalData){let height=$(this.stdout_el).prop('scrollHeight');return this.loadLinesFromEnd().then(response=>{setTimeout(()=>{$(this.stdout_el).scrollTop($(this.stdout_el).prop('scrollHeight')-height);},300);});}},scrollHandler(){if($(this.stdout_el).scrollTop()<100){this.updateDataOnScroll();}},getLines(){return this.loadLines();},loadLines(){let instance_data=this.instance.data;if(["RUN","DELAY"].includes(instance_data.status)){return this.loadLinesFromBeginning();}else{return this.loadLinesFromEnd();}},loadLinesFromBeginning(){let query={ordering:'line_gnumber',limit:this.lines_limit,};if(this.lines.last){query.after=this.lines.last.line_gnumber;}
return this.sendLinesApiRequest(query).then(response=>{this.saveNewLines(response.data.results);return response;});},loadLinesFromEnd(){let query={ordering:'-line_gnumber',limit:this.lines_limit,};if(this.lines.last){query.before=this.lines.last.line_gnumber;}
return this.sendLinesApiRequest(query).then(response=>{this.saveNewLines(response.data.results);return response;});},sendLinesApiRequest(query={}){let qs=this.lines_qs.clone({query:query});this.loading=true;return qs.formQueryAndSend('get').then(response=>{this.loading=false;return response;}).catch(error=>{throw error;});},saveNewLines(new_lines=[]){for(let index in new_lines){let new_line=new_lines[index];let filtered=this.lines.filter(line=>{if(deepEqual(line,new_line)){return line;}});if(filtered.length==0){this.lines.push(new_line);}}},ansiUp(line_content){let comments=[];let strings=[];let res=[];let all={'C':comments,'S':strings,'R':res};let safe={'<':'<','>':'>','&':'&'};let ansi_up=new AnsiUp;ansi_up.use_classes=true;let html=ansi_up.ansi_to_html(line_content);return html.replace(/\t/g,'&nbsp;&nbsp;&nbsp;&nbsp;');},},},},};addHistorySignals();tabSignal.connect('allViews.inited',obj=>{let views=obj.views;history_paths.forEach(path=>{views[path].getViewSublinkButtons=function(type,buttons,instance){let data=instance.data;let btns=$.extend(true,{},buttons);if(!data){return btns;}
if(type=='actions'||type=='child_links'){if(!['RUN','DELAY'].includes(data['status'])){btns['cancel'].hidden=true;}
if(!(data['status']=='OK'&&data['kind']=='MODULE'&&data['mode']=='setup')){btns['facts'].hidden=true;}
btns['clear'].hidden=true;}
return btns;}});});