source: trunk/grails-app/services/CreateBulkDataService.groovy @ 532

Last change on this file since 532 was 418, checked in by gav, 15 years ago

Add attentionFlag to Task domain along with views and logic to suite.
Add entry type 'cause', refactor as required.
Refactor task types.
Move createBreakin to createImmediateCallout.

File size: 6.9 KB
RevLine 
[258]1import grails.util.GrailsUtil
2
3/**
4* Provides a data service to create a large volume of test data for load testing.
5*/
6class  CreateBulkDataService {
7
8    boolean transactional = false
9
[291]10    def authService
[258]11    def taskService
12    def dateUtilService
13    def appConfigService
14    def assignedGroupService
15    def assignedPersonService
16
17    def sessionFactory
18    def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP
19
[261]20    def startTime
21    def lastBatchStarted
[258]22
23/*******************************************
24Start of Group methods.
25Generally use these methods to create data.
26*******************************************/
27
28    /**
29    * Make a run of data creation.
30    */
31    def create() {
32        if(!GrailsUtil.environment == "development") {
33            log.error "Dev environment not detected, will NOT create bulk data."
34            return false
35        }
36
37        log.info "Creating BULK data..."
38
39        // Person and Utils
40        log.info "Creating persons..."
41        createBulkTestPersons()
42
43//         createBulkTestSites()
44//         createBulkTestDepartments()
45//         createBulkTestSuppliers()
46//         createBulkTestManufacturers()
47
48        // Tasks
49        log.info "Creating tasks..."
50        createBulkTestTasks()
51
52//         createBulkTestEntries()
53//         createBulkTestAssignedGroups()
54//         createBulkTestAssignedPersons()
55//         createBulkTestTaskRecurringSchedules()
56
57        // Inventory
58//         createBulkTestInventoryStores()  /// @todo: Perhaps a 'createQuickStartData' method?
59//         createBulkTestInventoryLocations()
60//         createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method?
61//         createBulkTestInventoryItems()
62
63        // Assets
64//         createBulkTestLifePlan()
65//         createBulkTestTaskProcedure()
66//         createBulkTestMaintenanceActions()
[269]67//         createBulkTestSections()
[258]68//         createBulkTestAssets()
69//         createBulkTestAssetExtenedAttributes()
[269]70//         createBulkTestAssetSubItems()
71//         createBulkTestAssetSubItemExtenedAttributes()
[258]72
73        log.info "Creating BULK data...complete."
74        return true
75
76    }
77
78/******************
79Start of Person
80*******************/
81
82    def createBulkTestPersons() {
83        //Person
84        def passClearText = "pass"
[291]85        def passwordEncoded = authService.encodePassword(passClearText)
[258]86        def personInstance
87
[261]88        def start = Person.count() + 1
89        def end = start + 100
[258]90
[261]91        def range = start..end
92
[258]93        def loginName = "BtLoginName"
94        String btLoginName
95        def firstName = "BtFirstName"
96        String btFirstName
97        def lastName = "BtLastName"
98
99        def authority2 = Authority.get(2)
100        def authority3 = Authority.get(3)
101        def personGroup1 = PersonGroup.get(1)
102        def personGroup2 = PersonGroup.get(2)
103        def personGroup3 = PersonGroup.get(3)
104        def personGroup4 = PersonGroup.get(4)
105        def personGroup5 = PersonGroup.get(5)
106
107        range.each() {
108
109            btLoginName = loginName + it
110            btFirstName = firstName + it
111
112            personInstance = new Person(loginName: btLoginName,
113                                        firstName: btFirstName,
114                                        lastName: lastName,
115                                        pass: passClearText,
[399]116                                        password: passwordEncoded)
[258]117            saveAndTest(personInstance)
118            personInstance.addToAuthorities(authority2)
119            personInstance.addToAuthorities(authority3)
120            personInstance.addToPersonGroups(personGroup1)
121            personInstance.addToPersonGroups(personGroup2)
122            personInstance.addToPersonGroups(personGroup3)
123            personInstance.addToPersonGroups(personGroup4)
124            personInstance.addToPersonGroups(personGroup5)
125
126        }
127
128    } // createBulkTestPersons()
129
130/*********************
131START OF TASK
132*********************/
133
134    def createBulkTestTasks() {
135
136        def taskResult
137        def p = [:]
138
[261]139        def start = Task.count() + 1
140        def end = start + 10000
[258]141
[261]142        def range = start..end
[258]143
[261]144
[258]145        def taskGroup1 = TaskGroup.get(1)
146        def taskPriority2 = TaskPriority.get(2)
[418]147        def taskType3 = TaskType.get(3)
[258]148        def leadPerson2 = Person.get(2)
149
150        def description = "Bulk test data "
151        String btDescription
152        def comment1 = "Has been noted as problematic, try recalibrating."
153        def today = dateUtilService.today
154
[261]155        startTime = System.currentTimeMillis()
156        lastBatchStarted = startTime
157
[258]158        range.each() {
159
[261]160            if(it % 100 == 0) {
[258]161                logStatus("Creating task #" + it)
162                cleanUpGorm()
163            }
164
165            btDescription = description + it
166
167            //Task #1
168            p = [taskGroup: taskGroup1,
169                    taskPriority: taskPriority2,
[418]170                    taskType: taskType3,
[258]171                    leadPerson: leadPerson2,
172                    description: btDescription,
173                    comment: comment1,
174                    targetStartDate: today]
175
[394]176            taskResult = taskService.save(p)
[258]177        }
178
179    }
180
181    def createBulkTestEntries() {
182
183        def entryResult
184        def p = [:]
185
186        def range = 1..10
187        def task1 = Task.get(1)
188        def entryType1 = EntryType.get(1)
189        def comment1 = "This is a bulk test entry."
190        def durationMinute1 = 20
191
192        range.each() {
193
194            p = [task: task1,
195                    entryType: entryType1,
196                    comment: comment1,
197                    durationMinute: durationMinute1]
198
[394]199            entryResult = taskService.saveEntry(p)
[258]200
201        }
202
203    } // createBulkTestEntries()
204
205    /**
206    * This cleans up the hibernate session and a grails map.
207    * For more info see: http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/
208    * The hibernate session flush is normal for hibernate.
209    * The map is apparently used by grails for domain object validation errors.
210    * A starting point for clean up is every 100 objects.
211    */
212    def cleanUpGorm() {
213        def session = sessionFactory.currentSession
214        session.flush()
215        session.clear()
216        propertyInstanceMap.get().clear()
217    }
218
219    def logStatus(String message) {
220        def batchEnded = System.currentTimeMillis()
221        def seconds = (batchEnded-lastBatchStarted)/1000
222        def total = (batchEnded-startTime)/1000
223        log.info "${message}, last: ${seconds}s, total: ${total}s"
224        lastBatchStarted = batchEnded
225    }
226
227
228/****************************************
229Call this function instead of .save()
230*****************************************/
231    private boolean saveAndTest(object) {
232        if(!object.save()) {
233//             BulkTestDataSuccessful = false
234            log.error "'${object}' failed to save!"
235            log.error object.errors
236            return false
237        }
238        return true
239    }
240
241} // end class.
Note: See TracBrowser for help on using the repository browser.