统计工单重开次数

先决条件:您必须熟悉教程中使用的语法 并且已经创建了一个扩展.

学习:计数过渡发生

水平:初学者

域:XML,  PHPAutomationDictionaryLifecycle 

最低版本:2.1.0

在此用例中,我们要计数重新打开用户请求的次数。为此:

向UserRequest添加一个计数器字段,

停用历史记录跟踪(可选)

在UserRequest上定义PHP方法以增加计数器,

修改UserRequest生命周期以在转换期间将方法从被解决调用为已分配

修改演示文稿以显示计数器

为计数器字段添加标签

为了提高可读性,我们用6个不同的XML块介绍了这6个步骤,但是实际上,您只需要编写一个XML结构即可。

iTop XML编译流程,支持具有重复节点的非最佳树结构,他将合并它们

添加计数器字段

itop_design
 
  <classes>
    <class id="UserRequest" _delta="must_exist">
      <fields>
        <field id="reopen_count" xsi:type="AttributeInteger" _delta="define">
          <sql>reopen_count</sql>
          <default_value>0</default_value>
          <is_null_allowed>true</is_null_allowed>
        </field>
      </fields>
    </class>
  </classes>

停用历史记录跟踪

每次更改reopen_计数计数器时,您都可以确定它对历史记录中的记录毫无用处
然后将<tracking_level> none << tracking_level>添加到字段定义中,它将完成此工作。这只是一个选择,您不必这样做。如果您未定义标签,则iTop将跟踪在该字段上所做的更改,因为省略时它是默认设置。

itop_design / classes / class@UserRequest / fields
 
        <field id="reopen_count">
          <!-- No tracking level, mean no entry in History log for this field -->
          <tracking_level>none</tracking_level>
        </field>

删除字段上的历史记录跟踪可以避免污染对象历史记录。

不断变化的价值,由外部来源掌握

计算的计数器,其修改日期可能没有用

声明PHP方法

class:UserRequest
 
public function IncrementCounter($sAttCode, $iIncrement=1)
{
   // Defensive programming, ensure that: the field code is valid on current class
   if (MetaModel::IsValidAttCode(($this), $sAttCode)
   // and the increment provided is numeric
   && ($iIncrement) 
   // and the field code correspond to an Integer type of attribute 
   && (MetaModel::GetAttributeDef(($this),$sAttCode) instanceof AttributeInteger))
   {
       $iNew = $this->Get($sAttCode) + $iIncrement;
       $this->Set($sAttCode, $iNew);
   }
   return true;
}         

请记住,PHP方法需要嵌入到

调用转换上的方法

每次工单从被解决移到分配的状态时,我们就增加计数器。

itop_design / classes / class@UserRequest / lifecycle / states
 
          <!-- When we are in "resolved" state of UserRequest lifecycle -->
          <state id="resolved">
            <!-- We look at the possible transitions from that state -->
            <transitions>
              <!-- if an "ev_reopen" stimulus is applied -->
              <transition id="ev_reopen">
                <actions _delta="redefine">
                  <!-- Because an action has no id, you must redefine ALL actions for that transition -->
                  <action>
                    <!-- Execute on current object the method with id="IncrementCounter" -->
                    <verb>IncrementCounter</verb>
                    <!-- with the following parameters -->
                    <params>
                      <!-- attribute code of the counter to increment -->
                      <param xsi:type="attcode">reopen_count</param>
                      <!-- also the default value is set in PHP, it must be done again in XML -->
                      <param xsi:type="int">1</param>
                    </params>
                  </action>
                </actions>
              </transition>
            </transitions>
          </state>

在演示文稿中添加计数器

让我们在UserRequest详细信息页面的第3列和SLA报告字段集中添加此reopen_计数。

为了能够编写此类增量XML,您需要确定

在详细信息的哪个列中显示该字段:上校:col1 col:col2 col:col3(如果定义了列)。

检索字段集代码(如果已定义)

猜测一个等级,该等级用于将特定字段定位在另一个字段之前或之后。在默认数据模型中,等级在公共容器内的字段之间有10的增量:字段集,列或页面(?)。

要获取当前的XML演示文稿,请检查已安装的iTop服务器,文件itop/data/datamodel-production.xml,其中包含iTop的已编译XML。

itop_design / classes
 
    <class id="UserRequest" _delta="must_exist">
      <presentation>
        <details>
          <items>
            <item id="col:col3">
              <items>
                <item id="fieldset:Ticket:SLA">
                  <items>
                    <item id="reopen_count" _delta="define">
                      <rank>80</rank>
                    </item>
                  </items>
                </item>
              </items>
            </item>
          </items>
        </details>
      </presentation>
    </class>

添加柜台标签

itop_design / dictionaries / dictionary@EN US / entries
 
<entry id="Class:UserRequest/Attribute:reopen_count" _delta="define">Reopening counter</entry>

该方法是通用的,因此可以用于其他计数器
如果您在类工单而不是UserRequest上声明了它,那么它也可以在任何变更类上使用

原贴链接:https://www.itophub.io/wiki/page?id=2_7_0%3Acustomization%3Aadd-counter


Count Ticket reopening

Prerequisite: You must be familiar with the Syntax used in Tutorials and have already created an extension.

learning:
Count transitions occurence
level:
Beginner
domains:
XMLPHPAutomationDictionaryLifecycle
min version:
2.1.0

In this usecase, we want to count how many times a User Request was re-open. For this will:

  1. add a counter field to UserRequest,

  2. desactivate history tracking (optional)

  3. define a PHP method on UserRequest to increment a counter,

  4. modify UserRequest lifecycle to call the method during the transition from resolved to assigned

  5. modify the presentation to display the counter

  6. add a label for the counter field

For readability, we present this 6 steps with 6 different blocks of XML, but in reality, you just need to write one XML structure with all of them.

iTop XML compilation process, supports an non-optimal tree structure with nodes duplicated, he will merge them

Add a counter field

itop_design
 
  <classes>
    <class id="UserRequest" _delta="must_exist">
      <fields>
        <field id="reopen_count" xsi:type="AttributeInteger" _delta="define">
          <sql>reopen_count</sql>
          <default_value>0</default_value>
          <is_null_allowed>true</is_null_allowed>
        </field>
      </fields>
    </class>
  </classes>

Deactivate history tracking

You may decide that it is useless to record in the history, each time reopen_count counter is changed
Then add <tracking_level>none</tracking_level> to the field definition and it will do the job. This is just an option, you don't have to do it. If you don't define that tag, iTop will track the changes made on that field, as it is the default when omitted.

itop_design / classes / class@UserRequest / fields
 
        <field id="reopen_count">
          <!-- No tracking level, mean no entry in History log for this field -->
          <tracking_level>none</tracking_level>
        </field>

Removing history tracking on fields avoid polluting the object history.

  • an ever changing value, mastered by an external source

  • a computed counter, whose modification dates might be useless

Declare the PHP method

class:UserRequest
 
public function IncrementCounter($sAttCode, $iIncrement=1)
{
   // Defensive programming, ensure that: the field code is valid on current class
   if (MetaModel::IsValidAttCode(($this), $sAttCode)
   // and the increment provided is numeric
   && ($iIncrement) 
   // and the field code correspond to an Integer type of attribute 
   && (MetaModel::GetAttributeDef(($this),$sAttCode) instanceof AttributeInteger))
   {
       $iNew = $this->Get($sAttCode) + $iIncrement;
       $this->Set($sAttCode, $iNew);
   }
   return true;
}         

Remember that PHP method need to be embeded in a XML structure

Call the method on transition

Let's increment the counter each time the ticket move from resolved to assigned states.

itop_design / classes / class@UserRequest / lifecycle / states
 
          <!-- When we are in "resolved" state of UserRequest lifecycle -->
          <state id="resolved">
            <!-- We look at the possible transitions from that state -->
            <transitions>
              <!-- if an "ev_reopen" stimulus is applied -->
              <transition id="ev_reopen">
                <actions _delta="redefine">
                  <!-- Because an action has no id, you must redefine ALL actions for that transition -->
                  <action>
                    <!-- Execute on current object the method with id="IncrementCounter" -->
                    <verb>IncrementCounter</verb>
                    <!-- with the following parameters -->
                    <params>
                      <!-- attribute code of the counter to increment -->
                      <param xsi:type="attcode">reopen_count</param>
                      <!-- also the default value is set in PHP, it must be done again in XML -->
                      <param xsi:type="int">1</param>
                    </params>
                  </action>
                </actions>
              </transition>
            </transitions>
          </state>

Add counter to presentation

Let's add this reopen_count on the UserRequest details page, in the 3rd column and in the SLA report fieldset

In order to be able to write such delta XML, you need to decide

  • in which column of the details to display that field: col:col1 / col:col2 / col:col3, if there are columns defined.

  • retrieve the fieldset code, if any defined

  • guess a rank to use to position a particular field after or before another one, in default datamodel rank have a 10 increment between fields within a common container: fieldset, column or page help.

To get the current XML presentation, check on your installed iTop server, the fileitop/data/datamodel-production.xml which contains the compiled XML of your iTop

itop_design / classes
 
    <class id="UserRequest" _delta="must_exist">
      <presentation>
        <details>
          <items>
            <item id="col:col3">
              <items>
                <item id="fieldset:Ticket:SLA">
                  <items>
                    <item id="reopen_count" _delta="define">
                      <rank>80</rank>
                    </item>
                  </items>
                </item>
              </items>
            </item>
          </items>
        </details>
      </presentation>
    </class>

Add counter label

itop_design / dictionaries / dictionary@EN US / entries
 
<entry id="Class:UserRequest/Attribute:reopen_count" _delta="define">Reopening counter</entry>

The method is generic, so can be used for another counter
If you declared it on class Ticket instead of UserRequest, then it can be used on any Change classes as well

标签:
由 superadmin 在 2020/08/27, 17:26 创建
    

需要帮助?

如果您需要有关XWiki的帮助,可以联系:

深圳市艾拓先锋企业管理咨询有限公司