| 76 | | |
| 77 | | |
| | 83 | {{{ |
| | 84 | SELECT id AS ticket, status, severity, priority, owner, |
| | 85 | time as created, summary FROM ticket |
| | 86 | WHERE status IN ('new', 'assigned', 'reopened') |
| | 87 | ORDER BY priority, time |
| | 88 | }}} |
| | 89 | |
| | 90 | |
| | 91 | ---- |
| | 92 | |
| | 93 | |
| | 94 | == Advanced Reports: Dynamic Variables == |
| | 95 | For more flexible reports, Trac supports the use of ''dynamic variables'' in report SQL statements. |
| | 96 | In short, dynamic variables are ''special'' strings that are replaced by custom data before query execution. |
| | 97 | |
| | 98 | === Using Variables in a Query === |
| | 99 | The syntax for dynamic variables is simple, any upper case word beginning with '$' is considered a variable. |
| | 100 | |
| | 101 | Example: |
| | 102 | {{{ |
| | 103 | SELECT id AS ticket,summary FROM ticket WHERE priority='$PRIORITY' |
| | 104 | }}} |
| | 105 | |
| | 106 | To assign a value to $PRIORITY when viewing the report, you must define it as an argument in the report URL, leaving out the the leading '$'. |
| | 107 | |
| | 108 | Example: |
| | 109 | {{{ |
| | 110 | http://projects.edgewall.com/trac/reports/14?PRIORITY=high |
| | 111 | }}} |
| | 112 | |
| | 113 | To use multiple variables, separate them with an '&'. |
| | 114 | |
| | 115 | Example: |
| | 116 | {{{ |
| | 117 | http://projects.edgewall.com/trac/reports/14?PRIORITY=high&SEVERITY=critical |
| | 118 | }}} |
| | 119 | |
| | 120 | |
| | 121 | === Special/Constant Variables === |
| | 122 | There is one ''magic'' dynamic variable to allow practical reports, its value automatically set without having to change the URL. |
| | 123 | |
| | 124 | * $USER -- Username of logged in user. |
| | 125 | |
| | 126 | Example (''List all tickets assigned to me''): |
| | 127 | {{{ |
| | 128 | SELECT id AS ticket,summary FROM ticket WHERE owner='$USER' |
| | 129 | }}} |
| | 130 | |
| | 131 | |
| | 132 | ---- |
| | 133 | |
| | 134 | |
| | 135 | == Advanced Reports: Custom Formatting == |
| | 136 | Trac is also capable of more advanced reports, including custom layouts, |
| | 137 | result grouping and user-defined CSS styles. To create such reports, we'll use |
| | 138 | specialized SQL statements to control the output of the Trac report engine. |
| | 139 | |
| | 140 | == Special Columns == |
| | 141 | To format reports, TracReports looks for 'magic' column names in the query |
| | 142 | result. These 'magic' names are processed and affect the layout and style of the |
| | 143 | final report. |
| | 144 | |
| | 145 | === Automatically formatted columns === |
| | 146 | * '''ticket''' -- Ticket ID number. Becomes a hyperlink to that ticket. |
| | 147 | * '''created, modified, date, time''' -- Format cell as a date and/or time. |
| | 148 | |
| | 149 | * '''description''' -- Ticket description field, parsed through the wiki engine. |
| | 150 | |
| | 151 | '''Example:''' |
| | 152 | {{{ |
| | 153 | SELECT id as ticket, created, status, summary FROM ticket |
| | 154 | }}} |
| | 155 | |
| | 156 | === Custom formatting columns === |
| | 157 | Columns whose names begin and end with 2 underscores (Example: '''`__color__`''') are |
| | 158 | assumed to be ''formatting hints'', affecting the appearance of the row. |
| | 159 | |
| | 160 | * '''`__group__`''' -- Group results based on values in this column. Each group will have its own header and table. |
| | 161 | * '''`__color__`''' -- Should be a numeric value ranging from 1 to 5 to select a pre-defined row color. Typically used to color rows by issue priority. |
| | 162 | * '''`__style__`''' -- A custom CSS style expression to use for the current row. |
| | 163 | |
| | 164 | '''Example:''' ''List active tickets, grouped by milestone, colored by priority'' |
| | 165 | {{{ |
| | 166 | SELECT p.value AS __color__, |
| | 167 | t.milestone AS __group__, |
| | 168 | (CASE owner WHEN 'daniel' THEN 'font-weight: bold; background: red;' ELSE '' END) AS __style__, |
| | 169 | t.id AS ticket, summary |
| | 170 | FROM ticket t,enum p |
| | 171 | WHERE t.status IN ('new', 'assigned', 'reopened') |
| | 172 | AND p.name=t.priority AND p.type='priority' |
| | 173 | ORDER BY t.milestone, p.value, t.severity, t.time |
| | 174 | }}} |
| | 175 | |
| | 176 | '''Note:''' A table join is used to match ''ticket'' priorities with their |
| | 177 | numeric representation from the ''enum'' table. |
| | 178 | |
| | 179 | === Changing layout of report rows === |
| | 180 | By default, all columns on each row are display on a single row in the HTML |
| | 181 | report, possibly formatted according to the descriptions above. However, it's |
| | 182 | also possible to create multi-line report entries. |
| | 183 | |
| | 184 | * '''`column_`''' -- ''Break row after this''. By appending an underscore ('_') to the column name, the remaining columns will be be continued on a second line. |
| | 185 | |
| | 186 | * '''`_column_`''' -- ''Full row''. By adding an underscore ('_') both at the beginning and the end of a column name, the data will be shown on a separate row. |
| | 187 | |
| | 188 | * '''`_column`''' -- ''Hide data''. Prepending an underscore ('_') to a column name instructs Trac to hide the contents from the HTML output. This is useful for information to be visible only if downloaded in other formats (like CSV or RSS/XML). |
| | 189 | |
| | 190 | '''Example:''' ''List active tickets, grouped by milestone, colored by priority, with description and multi-line layout'' |
| | 191 | |
| | 192 | {{{ |
| | 193 | SELECT p.value AS __color__, |
| | 194 | t.milestone AS __group__, |
| | 195 | (CASE owner |
| | 196 | WHEN 'daniel' THEN 'font-weight: bold; background: red;' |
| | 197 | ELSE '' END) AS __style__, |
| | 198 | t.id AS ticket, summary AS summary_, -- ## Break line here |
| | 199 | component,version, severity, milestone, status, owner, |
| | 200 | time AS created, changetime AS modified, -- ## Dates are formatted |
| | 201 | description AS _description_, -- ## Uses a full row |
| | 202 | changetime AS _changetime, reporter AS _reporter -- ## Hidden from HTML output |
| | 203 | FROM ticket t,enum p |
| | 204 | WHERE t.status IN ('new', 'assigned', 'reopened') |
| | 205 | AND p.name=t.priority AND p.type='priority' |
| | 206 | ORDER BY t.milestone, p.value, t.severity, t.time |
| | 207 | }}} |
| | 208 | |
| | 209 | === Reporting on custom fields === |
| | 210 | |
| | 211 | If you have added custom fields to your tickets (experimental feature in v0.8, see TracTicketsCustomFields), you can write a SQL query to cover them. You'll need to make a join on the ticket_custom table, but this isn't especially easy. |
| | 212 | |
| | 213 | If you have tickets in the database ''before'' you declare the extra fields in trac.ini, there will be no associated data in the ticket_custom table. To get around this, use SQL's "LEFT OUTER JOIN" clauses. See TracIniReportCustomFieldSample for some examples. |