]> git.evergreen-ils.org Git - working/Evergreen.git/blob - docs/reports/reporter_add_data_source.txt
Resolve doc PDF transform error
[working/Evergreen.git] / docs / reports / reporter_add_data_source.txt
1 Adding Data Sources to Reporter
2 -------------------------------
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 possbile 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 PostgresSQL 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
42 indexterm:[PostgreSQL]
43
44 You need to decide whether you will create your data source as a query, a view, 
45 or a table.
46
47 . Create a query if you are planning to access this data source only through the 
48 Evergreen reporter and/or cstore code that you write. You will use this query to 
49 create an IDL only view.
50 . Create a view if you are planning to access this data source through other 
51 methods in addition to the Evergreen reporter, or if you may need to do 
52 performance tuning to optimize your query.
53 . You may also need to use an additional table as part of your data source if 
54 you have additional data that's not included in the base Evergreen, or if you 
55 need to use a table to store the results of a query for performance reasons.
56
57 To develop and test queries, views, and tables, you will need
58
59 * Access to the Evergree PostgreSQL database at the command line. This is 
60 normally the psql application. You 
61 can access the Postgres documentation at the 
62 http://http://www.postgresql.org/docs/[Official Postgres documentation] for 
63 more information about PostgreSQL.
64 * Knowledge of the Evergreen database structure for the data that you want to 
65 access. You can find this information by looking at the Evergreen schema
66 http://docs.evergreen-ils.org/2.2/schema/[Evergreen schema] 
67
68 indexterm:[database schema]
69
70 If the views that you are creating are purely local in usage and are not intended 
71 for contribution to the core Evergreen code, create the Views and Tables in the 
72 extend_reporter schema. This schema is intended to be used for local 
73 customizations and will not be modified during upgrades to the Evergreen system.
74
75 You should make that you have an appropriate version control pocess for the SQL 
76 used to create you data sources.
77
78 Here's an example of a view created to incorporate some locally defined user 
79 statistical categories:
80
81 .example view for reports
82 ----------
83 create view extend_reporter.patronstats as
84 select u.id, 
85 grp.name as "ptype",
86 rl.stat_cat_entry as "reg_lib",
87 gr.stat_cat_entry as "gender",
88 ag.stat_cat_entry as "age_group",
89 EXTRACT(YEAR FROM age(u.dob)) as "age",
90 hl.id as "home_lib",
91 u.create_date,
92 u.expire_date,
93 ms_balance_owed
94 from actor.usr u
95 join permission.grp_tree grp 
96         on (u.profile = grp.id and (grp.parent = 2 or grp.name = 'patron')) 
97 join actor.org_unit hl on (u.home_ou = hl.id)
98 left join money.open_usr_summary ms 
99         on (ms.usr = u.id) 
100 left join actor.stat_cat_entry_usr_map rl 
101         on (u.id = rl.target_usr and rl.stat_cat = 4) 
102 left join actor.stat_cat_entry_usr_map bt 
103         on (u.id = bt.target_usr and bt.stat_cat = 3) 
104 left join actor.stat_cat_entry_usr_map gr 
105         on (u.id = gr.target_usr and gr.stat_cat = 2) 
106 left join actor.stat_cat_entry_usr_map gr 
107         on (u.id = gr.target_usr and gr.stat_cat = 2) 
108 left join actor.stat_cat_entry_usr_map ag 
109         on (u.id = ag.target_usr and ag.stat_cat = 1) 
110 where u.active = 't' and u.deleted <> 't';
111 -----------
112
113 Add a new class to fm_IDL.xml for your data source
114 --------------------------------------------------
115
116 Once you have your data source, the next step is to add that data source as a 
117 new class in _fm_IDL.xml_.
118
119 indexterm:[fm_IDL.xml]
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 * *field* (should match field.name)
152 * *reltype* (“has_a”, “might_have”, or “has_many”)
153 * *map* (“”)
154 * *key* (name of the linking field in the foreign table)
155 * *class* (ID of the IDL class of the table that is to be linked to)
156
157 The following example is a class definition for the example view that was created 
158 in the previous section.
159
160 .example class definition for reports
161 ----------
162 <class id="erpstats" controller="open-ils.reporter-store" 
163 oils_obj:fieldmapper="extend_reporter::patronstats" 
164 oils_persist:tablename="extend_reporter.patronstats" oils_persist:readonly="true" 
165 reporter:label="Patron Statistics" reporter:core="true">
166   <fields oils_persist:primary="id">
167   <field reporter:label="Patron ID" name="id" reporter:datatype="link" />
168   <field reporter:label="Patron Type" name="ptype" reporter:datatype="text" />
169   <field reporter:label="Reg Lib" name="reg_lib" reporter:datatype="text" />
170   <field reporter:label="Boro/Twp" name="boro_twp" reporter:datatype="text" />
171   <field reporter:label="Gender" name="gender" reporter:datatype="text" />
172   <field reporter:label="Age Group" name="age_group" reporter:datatype="text" />
173   <field reporter:label="Age" name="age" reporter:datatype="int" />
174   <field reporter:label="Home Lib ID" name="home_lib_id" 
175         reporter:datatype="link" />
176   <field reporter:label="Home Lib Code" name="home_lib_code" 
177         reporter:datatype="text" />
178   <field reporter:label="Home Lib" name="home_lib" reporter:datatype="text" />
179   <field reporter:label="Create Date" name="create_date" 
180         reporter:datatype="timestamp" />
181   <field reporter:label="Expire Date" name="expire_date" 
182         reporter:datatype="timestamp" />
183   <field reporter:label="Balance Owed" name="balance_owed" 
184         reporter:datatype="money" />
185 </fields>
186 <links>
187   <link field="id" reltype="has_a" key="id" map="" class="au"/>
188   <link field="home_lib_id" reltype="has_a" key="id" map="" class="aou"/>
189 </links>
190 </class>
191 ---------
192
193 NOTE: _fm_IDL.xml_ is used by other core Evergreen DAO services, including cstore 
194 and permacrud. So changes to this file can affect the entire Evergreen 
195 application, not just reporter. After making changes fm_IDL.xml, it is a good 
196 idea to ensure that it is valid XML by using a utility such as *xmllint* – a 
197 syntax error can render much of Evergreen nonfunctional. Set up a good change 
198 control system for any changes to fm_IDL.xml. You will need to keep a separate 
199 copy of you local class definitions so that you can reapply the changes to 
200 _fm_IDL.xml_ after Evergreen upgrades.
201
202 Restart the affected services to see the new data source in the reporter
203 ------------------------------------------------------------------------
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_ Update _/openils/conf/fm_IDL.xml_ to 
209 _/openils/var/web/reports/fm_IDL.xml_
210 +
211 -------------
212 cp _/openils/conf/fm_IDL.xml /openils/var/web/reports/fm_IDL.xml
213 -------------
214 +
215 . Run Autogen to to update the Javascript versions of the fieldmapper definitions.
216 +
217 -------------
218 /openils/bin/autogen.sh
219 -------------
220 +    
221 . Restart C services
222 +
223 -------------
224 osrf_ctl.sh -l -a restart_c
225 -------------
226 +
227 . Restart the Evergreen reporter. You may need to modify this command depending 
228 on your system configuration and pid path
229 +
230 ------------
231 opensrf-perl.pl -l -action restart -service open-ils.reporter \
232 -config /openils/conf/opensrf_core.xml -pid-dir /openils/var/run
233 ------------
234 +
235 . Restart the Evergreen application or _use Admin --> For Developers --> Clear 
236 Cache_
237
238
239