]> git.evergreen-ils.org Git - Evergreen.git/blob - docs-antora/modules/reports/pages/reporter_add_data_source.adoc
LP#1848524: Docs: Fix sections for Antora: "reports"
[Evergreen.git] / docs-antora / modules / reports / pages / reporter_add_data_source.adoc
1 = Adding Data Sources to Reporter =
2 :toc:
3
4 indexterm:[reports, adding data sources]
5
6 You can further customize your Evergreen reporting environment by adding 
7 additional data sources.
8
9 The Evergreen reporter module does not build and execute SQL queries directly, 
10 but instead uses a data abstraction layer called *Fieldmapper* to mediate queries 
11 on the Evergreen database.Fieldmapper is also used by other core Evergreen DAO 
12 services, including cstore and permacrud. The configuration file _fm_IDL.xml_ 
13 contains the mapping between _Fieldmapper_ class definitions and the database. 
14 The _fm_IDL.xml_ file is located in the _/openils/conf_ directory.
15
16 indexterm:[fm_IDL.xml]
17
18 There are 3 basic steps to adding a new data source. Each step will be discussed 
19 in more detail in the
20
21 . Create a PostgreSQL query, view, or table that will provide the data for your 
22 data source.
23 . Add a new class to _fm_IDL.xml_ for your data source.
24 . Restart the affected services to see the new data source in Reporter.
25
26 There are two possible sources for new data sources:
27
28 indexterm:[PostgreSQL]
29
30 indexterm:[SQL]
31
32 * An SQL query built directly into the class definition in _fm_IDL.xml_. You can 
33 use this method if you are only going to access this data source through the 
34 Evergreen reporter and/or cstore code that you write.
35 * A new table or view in the Evergreen PostgreSQL database on which a class 
36 definition in _fm_IDL.xml_. You can use this method if you want to be able to 
37 access this data source through directly through SQL or using other reporting tool.
38
39 == Create a PostgreSQL query, view, or table for your data source ==
40
41 indexterm:[PostgreSQL]
42
43 You need to decide whether you will create your data source as a query, a view, 
44 or a table.
45
46 . Create a query if you are planning to access this data source only through the 
47 Evergreen reporter and/or cstore code that you write. You will use this query to 
48 create an IDL only view.
49 . Create a view if you are planning to access this data source through other 
50 methods in addition to the Evergreen reporter, or if you may need to do 
51 performance tuning to optimize your query.
52 . You may also need to use an additional table as part of your data source if 
53 you have additional data that's not included in the base Evergreen, or if you 
54 need to use a table to store the results of a query for performance reasons.
55
56 To develop and test queries, views, and tables, you will need
57
58 * Access to the Evergreen PostgreSQL database at the command line. This is 
59 normally the psql application. You 
60 can access the Postgres documentation at the 
61 https://www.postgresql.org/docs/[Official Postgres documentation] for 
62 more information about PostgreSQL.
63 * Knowledge of the Evergreen database structure for the data that you want to 
64 access. You can find this information by looking at the Evergreen schema
65 http://docs.evergreen-ils.org/2.2/schema/[Evergreen schema] 
66
67 indexterm:[database schema]
68
69 If the views that you are creating are purely local in usage and are not intended 
70 for contribution to the core Evergreen code, create the Views and Tables in the 
71 extend_reporter schema. This schema is intended to be used for local 
72 customizations and will not be modified during upgrades to the Evergreen system.
73
74 You should make sure that you have an appropriate version control process for the SQL 
75 used to create your data sources.
76
77 Here's an example of a view created to incorporate some locally defined user 
78 statistical categories:
79
80 .example view for reports
81 ------------------------------------------------------------
82 create view extend_reporter.patronstats as
83 select u.id, 
84 grp.name as "ptype",
85 rl.stat_cat_entry as "reg_lib",
86 gr.stat_cat_entry as "gender",
87 ag.stat_cat_entry as "age_group",
88 EXTRACT(YEAR FROM age(u.dob)) as "age",
89 hl.id as "home_lib",
90 u.create_date,
91 u.expire_date,
92 ms_balance_owed
93 from actor.usr u
94 join permission.grp_tree grp 
95     on (u.profile = grp.id and (grp.parent = 2 or grp.name = 'patron')) 
96 join actor.org_unit hl on (u.home_ou = hl.id)
97 left join money.open_usr_summary ms 
98     on (ms.usr = u.id) 
99 left join actor.stat_cat_entry_usr_map rl 
100     on (u.id = rl.target_usr and rl.stat_cat = 4) 
101 left join actor.stat_cat_entry_usr_map bt 
102     on (u.id = bt.target_usr and bt.stat_cat = 3) 
103 left join actor.stat_cat_entry_usr_map gr 
104     on (u.id = gr.target_usr and gr.stat_cat = 2) 
105 left join actor.stat_cat_entry_usr_map gr 
106     on (u.id = gr.target_usr and gr.stat_cat = 2) 
107 left join actor.stat_cat_entry_usr_map ag 
108     on (u.id = ag.target_usr and ag.stat_cat = 1) 
109 where u.active = 't' and u.deleted <> 't';
110 ------------------------------------------------------------
111
112 == Add a new class to fm_IDL.xml for your data source ==
113
114 Once you have your data source, the next step is to add that data source as a 
115 new class in _fm_IDL.xml_.
116
117 indexterm:[fm_IDL.xml]
118 indexterm:[fieldmapper]
119 indexterm:[report sources]
120
121 You will need to add the following attributes for the class definition:
122
123 * *id*. You should follow a consistent naming convention for your class names 
124 that won't create conflicts in the future with any standard classes added in 
125 future upgrades. Evergreen normally names each class with the first letter of 
126 each word in the schema and table names. You may want to add a local prefix or 
127 suffix to your local class names.
128 * *controller=”open-ils.cstore”*
129 * *oils_obj:fieldmapper=”extend_reporter::long_name_of_view”*
130 * *oils_persist.readonly=”true”*
131 * *reporter:core=”true”* (if you want this to show up as a “core” reporting source)
132 * *reporter:label*. This is the name that will appear on the data source list in 
133 the Evergreen reporter.
134 * *oils_persist:source_definition*. If this is an IDL-only view, add the SQL query 
135 here. You don't need this attribute if your class is based on a PostgreSQL view 
136 or table.
137 * *oils_persist:tablename="schemaname.viewname or tablename"* If this class is 
138 based on a PostgreSQL view or table, add the table name here. You don't need 
139 this attribute is your class is an IDL-only view.
140
141 For each column in the view or query output, add field element and set the 
142 following attributes. The fields should be wrapped with _<field> </field>_:
143
144 * *reporter:label*. This is the name that appears in the Evergreen reporter.
145 * *name*. This should match the column name in the view or query output.
146 * *reporter:datatype* (which can be id, bool, money, org_unit, int, number, 
147 interval, float, text, timestamp, or link)
148
149 For each linking field, add a link element with the following attributes. The 
150 elements should be wrapped with _<link> </link>_:
151
152 * *field* (should match field.name)
153 * *reltype* (“has_a”, “might_have”, or “has_many”)
154 * *map* (“”)
155 * *key* (name of the linking field in the foreign table)
156 * *class* (ID of the IDL class of the table that is to be linked to)
157
158 The following example is a class definition for the example view that was created 
159 in the previous section.
160
161 .example class definition for reports
162 ------------------------------------------------------------
163 <class id="erpstats" controller="open-ils.reporter-store" 
164 oils_obj:fieldmapper="extend_reporter::patronstats" 
165 oils_persist:tablename="extend_reporter.patronstats" oils_persist:readonly="true" 
166 reporter:label="Patron Statistics" reporter:core="true">
167   <fields oils_persist:primary="id">
168   <field reporter:label="Patron ID" name="id" reporter:datatype="link" />
169   <field reporter:label="Patron Type" name="ptype" reporter:datatype="text" />
170   <field reporter:label="Reg Lib" name="reg_lib" reporter:datatype="text" />
171   <field reporter:label="Boro/Twp" name="boro_twp" reporter:datatype="text" />
172   <field reporter:label="Gender" name="gender" reporter:datatype="text" />
173   <field reporter:label="Age Group" name="age_group" reporter:datatype="text" />
174   <field reporter:label="Age" name="age" reporter:datatype="int" />
175   <field reporter:label="Home Lib ID" name="home_lib_id" 
176     reporter:datatype="link" />
177   <field reporter:label="Home Lib Code" name="home_lib_code" 
178     reporter:datatype="text" />
179   <field reporter:label="Home Lib" name="home_lib" reporter:datatype="text" />
180   <field reporter:label="Create Date" name="create_date" 
181     reporter:datatype="timestamp" />
182   <field reporter:label="Expire Date" name="expire_date" 
183     reporter:datatype="timestamp" />
184   <field reporter:label="Balance Owed" name="balance_owed" 
185     reporter:datatype="money" />
186 </fields>
187 <links>
188   <link field="id" reltype="has_a" key="id" map="" class="au"/>
189   <link field="home_lib_id" reltype="has_a" key="id" map="" class="aou"/>
190 </links>
191 </class>
192 ------------------------------------------------------------
193
194 NOTE: _fm_IDL.xml_ is used by other core Evergreen DAO services, including cstore 
195 and permacrud. So changes to this file can affect the entire Evergreen 
196 application, not just reporter. After making changes fm_IDL.xml, it is a good 
197 idea to ensure that it is valid XML by using a utility such as *xmllint* – a 
198 syntax error can render much of Evergreen nonfunctional. Set up a good change 
199 control system for any changes to fm_IDL.xml. You will need to keep a separate 
200 copy of you local class definitions so that you can reapply the changes to 
201 _fm_IDL.xml_ after Evergreen upgrades.
202
203 == Restart the affected services to see the new data source in the reporter ==
204
205 The following steps are needed to for Evergreen to recognize the changes to 
206 _fm_IDL.xml_
207
208 . Copy the updated _fm_IDL.xml_ into place:
209 +
210 -------------
211 cp fm_IDL.xml /openils/conf/.
212 -------------
213 +
214 . (Optional) Make the reporter version of fm_IDL.xml match the core version.
215 Evergreen systems supporting only one interface language will normally find
216 that _/openils/var/web/reports/fm_IDL.xml_ is a symbolic link pointing to
217 _/openils/conf/fm_IDL.xml_, so no action will be required. However, systems
218 supporting multiple interfaces will have a different version of _fm_IDL.xml_ in
219 the _/openils/var/web/reports_ directory. The _right_ way to update this is to
220 go through the Evergreen internationalization build process to create the
221 entity form of _fm_IDL.xml_ and the updated _fm_IDL.dtd_ files for each
222 supported language. However, that is outside the scope of this document. If you
223 can accept the reporter interface supporting only one language, then you can
224 simply copy your updated version of _fm_IDL.xml_ into the
225 _/openils/var/web/reports_ directory:
226 +
227 -------------
228 cp /openils/conf/fm_IDL.xml /openils/var/web/reports/.
229 -------------
230 +
231 . As the *opensrf* user, run Autogen to to update the Javascript versions of
232 the fieldmapper definitions.
233 +
234 -------------
235 /openils/bin/autogen.sh
236 -------------
237 +    
238 . As the *opensrf* user, restart services:
239 +
240 -------------
241 osrf_control --localhost --restart-services
242 -------------
243 +
244 . As the *root* user, restart the Apache web server:
245 +
246 -------------
247 service apache2 restart
248 -------------
249 +
250 . As the *opensrf* user, restart the Evergreen reporter. You may need to modify
251 this command depending on your system configuration and PID path:
252 +
253 ------------
254 opensrf-perl.pl -l -action restart -service open-ils.reporter \
255 -config /openils/conf/opensrf_core.xml -pid-dir /openils/var/run
256 ------------
257 +
258 . Restart the Evergreen staff client, or use *Admin --> For Developers -->
259   Clear Cache*
260