| [258] | 1 | import grails.util.GrailsUtil |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Provides a data service to create a large volume of test data for load testing. |
|---|
| 5 | */ |
|---|
| 6 | class CreateBulkDataService { |
|---|
| 7 | |
|---|
| 8 | boolean transactional = false |
|---|
| 9 | |
|---|
| [291] | 10 | def authService |
|---|
| [258] | 11 | def taskService |
|---|
| 12 | def dateUtilService |
|---|
| 13 | def appConfigService |
|---|
| [622] | 14 | def createDataService |
|---|
| [572] | 15 | def searchableService |
|---|
| [258] | 16 | def assignedGroupService |
|---|
| 17 | def assignedPersonService |
|---|
| [548] | 18 | def inventoryItemService |
|---|
| [258] | 19 | |
|---|
| 20 | def sessionFactory |
|---|
| [548] | 21 | def grailsApplication |
|---|
| [258] | 22 | def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP |
|---|
| 23 | |
|---|
| [261] | 24 | def startTime |
|---|
| 25 | def lastBatchStarted |
|---|
| [258] | 26 | |
|---|
| 27 | /******************************************* |
|---|
| 28 | Start of Group methods. |
|---|
| 29 | Generally use these methods to create data. |
|---|
| 30 | *******************************************/ |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Make a run of data creation. |
|---|
| 34 | */ |
|---|
| [548] | 35 | def createAll() { |
|---|
| 36 | def result = [:] |
|---|
| 37 | |
|---|
| 38 | def fail = { Map m -> |
|---|
| 39 | result.error = [ code: m.code, args: m.args ] |
|---|
| 40 | return result |
|---|
| [258] | 41 | } |
|---|
| 42 | |
|---|
| [548] | 43 | if(GrailsUtil.environment != "development") |
|---|
| 44 | return fail(code: 'default.not.development.environment.failure') |
|---|
| 45 | |
|---|
| [622] | 46 | createDataService.stopSearchableIndex() |
|---|
| [572] | 47 | |
|---|
| [258] | 48 | log.info "Creating BULK data..." |
|---|
| 49 | |
|---|
| 50 | // Person and Utils |
|---|
| 51 | log.info "Creating persons..." |
|---|
| 52 | createBulkTestPersons() |
|---|
| 53 | // createBulkTestSites() |
|---|
| 54 | // createBulkTestDepartments() |
|---|
| 55 | // createBulkTestSuppliers() |
|---|
| 56 | // createBulkTestManufacturers() |
|---|
| 57 | |
|---|
| [548] | 58 | // Assets |
|---|
| 59 | // createBulkTestLifePlan() |
|---|
| 60 | // createBulkTestTaskProcedure() |
|---|
| 61 | // createBulkTestMaintenanceActions() |
|---|
| 62 | // createBulkTestSections() |
|---|
| 63 | // createBulkTestAssets() |
|---|
| 64 | // createBulkTestAssetExtenedAttributes() |
|---|
| 65 | // createBulkTestAssetSubItems() |
|---|
| 66 | // createBulkTestAssetSubItemExtenedAttributes() |
|---|
| 67 | |
|---|
| 68 | // Inventory |
|---|
| 69 | log.info "Creating inventory..." |
|---|
| 70 | // createBulkTestInventoryStores() /// @todo: Perhaps a 'createQuickStartData' method? |
|---|
| 71 | createBulkTestInventoryLocations() |
|---|
| 72 | // createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method? |
|---|
| 73 | createBulkTestInventoryItems() |
|---|
| 74 | |
|---|
| [258] | 75 | // Tasks |
|---|
| 76 | log.info "Creating tasks..." |
|---|
| 77 | createBulkTestTasks() |
|---|
| 78 | // createBulkTestEntries() |
|---|
| 79 | // createBulkTestAssignedGroups() |
|---|
| 80 | // createBulkTestAssignedPersons() |
|---|
| 81 | // createBulkTestTaskRecurringSchedules() |
|---|
| 82 | |
|---|
| [548] | 83 | log.info "Creating BULK data...complete." |
|---|
| [572] | 84 | |
|---|
| [622] | 85 | createDataService.startSearchableIndex() |
|---|
| [572] | 86 | |
|---|
| [548] | 87 | return result |
|---|
| 88 | |
|---|
| 89 | } // create() |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Make a run of inventory data creation. |
|---|
| 93 | */ |
|---|
| 94 | def createBulkInventoryTestData() { |
|---|
| 95 | def result = [:] |
|---|
| 96 | |
|---|
| 97 | def fail = { Map m -> |
|---|
| 98 | result.error = [ code: m.code, args: m.args ] |
|---|
| 99 | return result |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if(GrailsUtil.environment != "development") |
|---|
| 103 | return fail(code: 'default.not.development.environment.failure') |
|---|
| 104 | |
|---|
| [622] | 105 | createDataService.stopSearchableIndex() |
|---|
| [572] | 106 | |
|---|
| [548] | 107 | log.info "Creating BULK data..." |
|---|
| 108 | |
|---|
| [258] | 109 | // Inventory |
|---|
| [548] | 110 | log.info "Creating inventory..." |
|---|
| [258] | 111 | // createBulkTestInventoryStores() /// @todo: Perhaps a 'createQuickStartData' method? |
|---|
| [548] | 112 | createBulkTestInventoryLocations() |
|---|
| [258] | 113 | // createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method? |
|---|
| [548] | 114 | createBulkTestInventoryItems() |
|---|
| [258] | 115 | |
|---|
| 116 | log.info "Creating BULK data...complete." |
|---|
| [572] | 117 | |
|---|
| [622] | 118 | createDataService.startSearchableIndex() |
|---|
| [572] | 119 | |
|---|
| [548] | 120 | return result |
|---|
| [258] | 121 | |
|---|
| [548] | 122 | } // createBulkInventoryTestData() |
|---|
| [258] | 123 | |
|---|
| 124 | /****************** |
|---|
| 125 | Start of Person |
|---|
| 126 | *******************/ |
|---|
| 127 | |
|---|
| 128 | def createBulkTestPersons() { |
|---|
| 129 | //Person |
|---|
| 130 | def passClearText = "pass" |
|---|
| [291] | 131 | def passwordEncoded = authService.encodePassword(passClearText) |
|---|
| [258] | 132 | def personInstance |
|---|
| 133 | |
|---|
| [261] | 134 | def start = Person.count() + 1 |
|---|
| 135 | def end = start + 100 |
|---|
| [258] | 136 | |
|---|
| [261] | 137 | def range = start..end |
|---|
| 138 | |
|---|
| [258] | 139 | def loginName = "BtLoginName" |
|---|
| 140 | String btLoginName |
|---|
| 141 | def firstName = "BtFirstName" |
|---|
| 142 | String btFirstName |
|---|
| 143 | def lastName = "BtLastName" |
|---|
| 144 | |
|---|
| 145 | def authority2 = Authority.get(2) |
|---|
| 146 | def authority3 = Authority.get(3) |
|---|
| 147 | def personGroup1 = PersonGroup.get(1) |
|---|
| 148 | def personGroup2 = PersonGroup.get(2) |
|---|
| 149 | def personGroup3 = PersonGroup.get(3) |
|---|
| 150 | def personGroup4 = PersonGroup.get(4) |
|---|
| 151 | def personGroup5 = PersonGroup.get(5) |
|---|
| 152 | |
|---|
| 153 | range.each() { |
|---|
| 154 | |
|---|
| 155 | btLoginName = loginName + it |
|---|
| 156 | btFirstName = firstName + it |
|---|
| 157 | |
|---|
| 158 | personInstance = new Person(loginName: btLoginName, |
|---|
| 159 | firstName: btFirstName, |
|---|
| 160 | lastName: lastName, |
|---|
| 161 | pass: passClearText, |
|---|
| [399] | 162 | password: passwordEncoded) |
|---|
| [258] | 163 | saveAndTest(personInstance) |
|---|
| 164 | personInstance.addToAuthorities(authority2) |
|---|
| 165 | personInstance.addToAuthorities(authority3) |
|---|
| 166 | personInstance.addToPersonGroups(personGroup1) |
|---|
| 167 | personInstance.addToPersonGroups(personGroup2) |
|---|
| 168 | personInstance.addToPersonGroups(personGroup3) |
|---|
| 169 | personInstance.addToPersonGroups(personGroup4) |
|---|
| 170 | personInstance.addToPersonGroups(personGroup5) |
|---|
| 171 | |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | } // createBulkTestPersons() |
|---|
| 175 | |
|---|
| 176 | /********************* |
|---|
| 177 | START OF TASK |
|---|
| 178 | *********************/ |
|---|
| 179 | |
|---|
| 180 | def createBulkTestTasks() { |
|---|
| 181 | |
|---|
| 182 | def taskResult |
|---|
| 183 | def p = [:] |
|---|
| 184 | |
|---|
| [261] | 185 | def start = Task.count() + 1 |
|---|
| 186 | def end = start + 10000 |
|---|
| [258] | 187 | |
|---|
| [261] | 188 | def range = start..end |
|---|
| [258] | 189 | |
|---|
| [261] | 190 | |
|---|
| [258] | 191 | def taskGroup1 = TaskGroup.get(1) |
|---|
| 192 | def taskPriority2 = TaskPriority.get(2) |
|---|
| [418] | 193 | def taskType3 = TaskType.get(3) |
|---|
| [258] | 194 | def leadPerson2 = Person.get(2) |
|---|
| 195 | |
|---|
| 196 | def description = "Bulk test data " |
|---|
| 197 | String btDescription |
|---|
| 198 | def comment1 = "Has been noted as problematic, try recalibrating." |
|---|
| 199 | def today = dateUtilService.today |
|---|
| 200 | |
|---|
| [261] | 201 | startTime = System.currentTimeMillis() |
|---|
| 202 | lastBatchStarted = startTime |
|---|
| 203 | |
|---|
| [258] | 204 | range.each() { |
|---|
| 205 | |
|---|
| [261] | 206 | if(it % 100 == 0) { |
|---|
| [258] | 207 | logStatus("Creating task #" + it) |
|---|
| 208 | cleanUpGorm() |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | btDescription = description + it |
|---|
| 212 | |
|---|
| 213 | //Task #1 |
|---|
| 214 | p = [taskGroup: taskGroup1, |
|---|
| 215 | taskPriority: taskPriority2, |
|---|
| [418] | 216 | taskType: taskType3, |
|---|
| [258] | 217 | leadPerson: leadPerson2, |
|---|
| 218 | description: btDescription, |
|---|
| 219 | comment: comment1, |
|---|
| 220 | targetStartDate: today] |
|---|
| 221 | |
|---|
| [394] | 222 | taskResult = taskService.save(p) |
|---|
| [258] | 223 | } |
|---|
| 224 | |
|---|
| [548] | 225 | } // createBulkTestTasks() |
|---|
| [258] | 226 | |
|---|
| 227 | def createBulkTestEntries() { |
|---|
| 228 | |
|---|
| 229 | def entryResult |
|---|
| 230 | def p = [:] |
|---|
| 231 | |
|---|
| 232 | def range = 1..10 |
|---|
| 233 | def task1 = Task.get(1) |
|---|
| 234 | def entryType1 = EntryType.get(1) |
|---|
| 235 | def comment1 = "This is a bulk test entry." |
|---|
| 236 | def durationMinute1 = 20 |
|---|
| 237 | |
|---|
| 238 | range.each() { |
|---|
| 239 | |
|---|
| 240 | p = [task: task1, |
|---|
| 241 | entryType: entryType1, |
|---|
| 242 | comment: comment1, |
|---|
| 243 | durationMinute: durationMinute1] |
|---|
| 244 | |
|---|
| [394] | 245 | entryResult = taskService.saveEntry(p) |
|---|
| [258] | 246 | |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | } // createBulkTestEntries() |
|---|
| 250 | |
|---|
| [548] | 251 | |
|---|
| 252 | /************************** |
|---|
| 253 | START OF INVENTORY |
|---|
| 254 | **************************/ |
|---|
| 255 | |
|---|
| 256 | def createBulkTestInventoryLocations() { |
|---|
| 257 | |
|---|
| 258 | def inventoryLocationResult |
|---|
| 259 | def p = [:] |
|---|
| 260 | |
|---|
| 261 | def start = InventoryLocation.count() + 1 |
|---|
| 262 | def end = start + 50 |
|---|
| 263 | |
|---|
| 264 | def range = start..end |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | def inventoryStore1 = InventoryStore.read(1) |
|---|
| 268 | |
|---|
| 269 | def name = "Bulk test location " |
|---|
| 270 | def btName = '' |
|---|
| 271 | |
|---|
| 272 | startTime = System.currentTimeMillis() |
|---|
| 273 | lastBatchStarted = startTime |
|---|
| 274 | |
|---|
| 275 | range.each() { |
|---|
| 276 | |
|---|
| [572] | 277 | if(it % 25 == 0) { |
|---|
| [548] | 278 | logStatus("Creating inventory location #" + it) |
|---|
| 279 | cleanUpGorm() |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | btName = name + it |
|---|
| 283 | |
|---|
| 284 | p = [inventoryStore: inventoryStore1, |
|---|
| 285 | name: btName] |
|---|
| 286 | |
|---|
| 287 | inventoryLocationResult = new InventoryLocation(p).save() |
|---|
| 288 | } // each() |
|---|
| 289 | |
|---|
| 290 | } // createBulkTestInventoryLocations() |
|---|
| 291 | |
|---|
| 292 | def createBulkTestInventoryItems() { |
|---|
| 293 | |
|---|
| 294 | def inventoryItemInstance |
|---|
| 295 | def p = [:] |
|---|
| 296 | |
|---|
| 297 | def pictureResource = grailsApplication.mainContext.getResource('images/logo.png') |
|---|
| 298 | |
|---|
| 299 | def start = InventoryItem.count() + 1 |
|---|
| 300 | def end = start + 250 |
|---|
| 301 | |
|---|
| 302 | def range = start..end |
|---|
| 303 | |
|---|
| 304 | def inventoryLocation |
|---|
| 305 | def inventoryLocationIndex = 0 |
|---|
| 306 | def inventoryLocationList = InventoryLocation.findAll() |
|---|
| 307 | def unitOfMeasure2 = UnitOfMeasure.read(2) |
|---|
| 308 | def inventoryType1 = InventoryType.read(1) |
|---|
| 309 | def inventoryGroup1 = InventoryGroup.read(1) |
|---|
| 310 | |
|---|
| 311 | def name = "Bulk test inventory item " |
|---|
| 312 | def btName = '' |
|---|
| 313 | |
|---|
| 314 | startTime = System.currentTimeMillis() |
|---|
| 315 | lastBatchStarted = startTime |
|---|
| 316 | |
|---|
| 317 | range.each() { |
|---|
| 318 | |
|---|
| [572] | 319 | if(it % 50 == 0) { |
|---|
| [548] | 320 | logStatus("Creating inventory item #" + it) |
|---|
| 321 | cleanUpGorm() |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | // Spread the inventoryItems across all available locations. |
|---|
| 325 | if(inventoryLocationIndex < inventoryLocationList.size()) { |
|---|
| 326 | inventoryLocation = inventoryLocationList[inventoryLocationIndex] |
|---|
| 327 | } |
|---|
| 328 | else { |
|---|
| 329 | inventoryLocationIndex = 0 |
|---|
| 330 | inventoryLocation = inventoryLocationList[inventoryLocationIndex] |
|---|
| 331 | } |
|---|
| 332 | inventoryLocationIndex++ |
|---|
| 333 | |
|---|
| 334 | // Change the name for each inventoryItem. |
|---|
| 335 | btName = name + it |
|---|
| 336 | |
|---|
| 337 | p = [inventoryGroup: inventoryGroup1, |
|---|
| 338 | inventoryType: inventoryType1, |
|---|
| 339 | unitOfMeasure: unitOfMeasure2, |
|---|
| 340 | inventoryLocation: inventoryLocation, |
|---|
| 341 | name: btName, |
|---|
| 342 | description: "Bulk test data", |
|---|
| 343 | unitsInStock: 2, |
|---|
| 344 | reorderPoint: 0] |
|---|
| 345 | |
|---|
| 346 | inventoryItemInstance = new InventoryItem(p) |
|---|
| 347 | saveAndTest(inventoryItemInstance) |
|---|
| 348 | |
|---|
| 349 | def pictureResult = inventoryItemService.savePicture(inventoryItemInstance, pictureResource) |
|---|
| 350 | |
|---|
| 351 | if(pictureResult.error) |
|---|
| 352 | log.error pictureResult.error |
|---|
| 353 | } // each() |
|---|
| 354 | |
|---|
| 355 | } // createBulkTestInventoryItems() |
|---|
| 356 | |
|---|
| [258] | 357 | /** |
|---|
| 358 | * This cleans up the hibernate session and a grails map. |
|---|
| 359 | * For more info see: http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/ |
|---|
| 360 | * The hibernate session flush is normal for hibernate. |
|---|
| 361 | * The map is apparently used by grails for domain object validation errors. |
|---|
| 362 | * A starting point for clean up is every 100 objects. |
|---|
| 363 | */ |
|---|
| 364 | def cleanUpGorm() { |
|---|
| 365 | def session = sessionFactory.currentSession |
|---|
| 366 | session.flush() |
|---|
| 367 | session.clear() |
|---|
| 368 | propertyInstanceMap.get().clear() |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | def logStatus(String message) { |
|---|
| 372 | def batchEnded = System.currentTimeMillis() |
|---|
| 373 | def seconds = (batchEnded-lastBatchStarted)/1000 |
|---|
| 374 | def total = (batchEnded-startTime)/1000 |
|---|
| 375 | log.info "${message}, last: ${seconds}s, total: ${total}s" |
|---|
| 376 | lastBatchStarted = batchEnded |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | /**************************************** |
|---|
| 381 | Call this function instead of .save() |
|---|
| 382 | *****************************************/ |
|---|
| 383 | private boolean saveAndTest(object) { |
|---|
| 384 | if(!object.save()) { |
|---|
| 385 | // BulkTestDataSuccessful = false |
|---|
| 386 | log.error "'${object}' failed to save!" |
|---|
| 387 | log.error object.errors |
|---|
| 388 | return false |
|---|
| 389 | } |
|---|
| 390 | return true |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | } // end class. |
|---|