source: trunk/grails-app/controllers/HomeController.groovy @ 1590

Last change on this file since 1590 was 1590, checked in by work@…, 12 years ago
  • fixing remaining test issues
  • Property svn:keywords set to Rev Author Date
File size: 6.2 KB
Line 
1import dbnp.studycapturing.*
2import dbnp.authentication.*
3import org.codehaus.groovy.grails.commons.GrailsApplication
4import org.codehaus.groovy.grails.commons.ConfigurationHolder
5import grails.converters.JSON
6import org.dbnp.gdt.Template
7
8/**
9 * Home Controler
10 *
11 * My Description
12 *
13 * @author  Kees van Bochove
14 * @since   20091102
15 * @package studycapturing
16 *
17 * Revision information:
18 * $Rev: 1590 $
19 * $Author: work@osx.eu $
20 * $Date: 2011-03-04 15:28:45 +0000 (vr, 04 mrt 2011) $
21 */
22class HomeController {
23        def authenticationService
24        def dataSource
25        def gdtService
26
27        def index = {
28                // got a nostats parameter?
29                if (params.get('nostats')) {
30                        session.nostats = (params.get('nostats') == "true") ? true : false
31                }
32
33                // create sql instance for advanced queries
34                def config = ConfigurationHolder.config
35                def sql = new groovy.sql.Sql(dataSource)
36                def db  = config.dataSource.driverClassName
37
38                // get study statistics
39                def user = authenticationService.getLoggedInUser()
40                def studyCount                                          = Study.count()
41                def readableStudyCount                          = Study.countReadableStudies(user)
42                def readableAndWritableStudyCount       = Study.countReadableAndWritableStudies(user)
43                def readOnlyStudyCount                          = (readableStudyCount - readableAndWritableStudyCount)
44                def noAccessStudyCount                          = studyCount - readableAndWritableStudyCount
45                def publishedPublicStudyCount           = Study.countPublicStudies(true)
46                def unPublishedPublicStudyCount         = Study.countPublicStudies(false)
47                def publicStudyCount                            = publishedPublicStudyCount + unPublishedPublicStudyCount
48                def publishedPrivateStudyCount          = Study.countPrivateStudies(true)
49                def unPublishedPrivateStudyCount        = Study.countPrivateStudies(false)
50                def privateStudyCount                           = publishedPrivateStudyCount + unPublishedPrivateStudyCount
51
52                // daily statistics
53                def startDate, endDate, date, userTotal, studyTotal, templateTotal
54                def dailyStatistics = [:]
55                if (db == "org.postgresql.Driver") {
56                        //sql.eachRow("SELECT b.*,(select cast( now() - '120 day'::interval * random() as date) FROM Template c WHERE c.id=b.id) as newDate FROM Template b WHERE b.id IN (SELECT id FROM Template a)") {sql.execute(sprintf("UPDATE Template SET date_created='%s' WHERE id=%s", it.newDate, it.id))}
57                        def studiesPerDay       = sql.rows("SELECT DISTINCT date_trunc('day', a.date_created) as day, (SELECT count(b.*) FROM study b WHERE date_trunc('day', b.date_created) = date_trunc('day', a.date_created)) as count FROM study a ORDER BY day ASC")
58                        def usersPerDay         = sql.rows("SELECT DISTINCT date_trunc('day', a.date_created) as day, (SELECT count(b.*) FROM sec_user b WHERE date_trunc('day', b.date_created) = date_trunc('day', a.date_created)) as count FROM sec_user a ORDER BY day ASC")
59                        def templatesPerDay     = sql.rows("SELECT DISTINCT date_trunc('day', a.date_created) as day, (SELECT count(b.*) FROM template b WHERE date_trunc('day', b.date_created) = date_trunc('day', a.date_created)) as count FROM template a ORDER BY day ASC")
60
61                        // combine daily statistics
62                        dailyStatistics = [:]
63                        long oneDay             = (1 * 24 * 60 * 60 * 1000)
64                        startDate               = (usersPerDay[0].day <= studiesPerDay[0].day) ? usersPerDay[0].day : studiesPerDay[0].day
65                        startDate               = (templatesPerDay[0].day != null && templatesPerDay[0].day < startDate) ? templatesPerDay[0].day : startDate
66                        endDate                 = (usersPerDay[usersPerDay.size()-1].day >= studiesPerDay[studiesPerDay.size()-1].day) ? usersPerDay[usersPerDay.size()-1].day : studiesPerDay[studiesPerDay.size()-1].day
67                        endDate                 = (templatesPerDay[0].day != null && templatesPerDay[templatesPerDay.size()-1].day > endDate) ? templatesPerDay[templatesPerDay.size()-1].day : endDate
68                        date                    = startDate.clone()
69
70                        userTotal               = 0
71                        studyTotal              = 0
72                        templateTotal   = 0
73                        while (date <= endDate) {
74                                def userDay             = usersPerDay.find{ it.day == date }
75                                def studyDay    = studiesPerDay.find{ it.day == date }
76                                def templateDay = templatesPerDay.find{ it.day == date }
77                                def users               = (userDay) ? userDay.count : 0
78                                def studies             = (studyDay) ? studyDay.count : 0
79                                def templates   = (templateDay) ? templateDay.count : 0
80
81                                userTotal += users
82                                studyTotal += studies
83                                templateTotal += templates
84
85                                dailyStatistics[ date.clone() ] = [
86                                        users                   : users,
87                                        userTotal               : userTotal,
88                                        studies                 : studies,
89                                        studyTotal              : studyTotal,
90                                        templates               : templates,
91                                        templateTotal   : templateTotal
92                                ]
93                                date.setTime(date.getTime() + oneDay)
94                        }
95                }
96
97                [
98                        // config
99                        showstats                                       : (session?.nostats) ? !session.nostats : true,
100
101                        // daily statistics
102                        startDate                                       : startDate,
103                        dailyStatistics                         : dailyStatistics,
104
105                        // study
106                        studyCount                                      : studyCount,
107                        publishedPublicStudyCount       : publishedPublicStudyCount,
108                        unPublishedPublicStudyCount     : unPublishedPublicStudyCount,
109                        publicStudyCount                        : publicStudyCount,
110                        publishedPrivateStudyCount      : publishedPrivateStudyCount,
111                        unPublishedPrivateStudyCount: unPublishedPrivateStudyCount,
112                        privateStudyCount                       : privateStudyCount,
113                        readOnlyStudyCount                      : readOnlyStudyCount,
114                        readWriteStudyCount                     : readableAndWritableStudyCount,
115                        noAccessStudyCount                      : noAccessStudyCount,
116                        readableTemplates                       : org.dbnp.gdt.Template.count(),
117
118                        // miscelaneous
119                        facebookLikeUrl                         : '/'
120                ]
121        }
122
123        def ajaxQuickSearch = {
124                def query       = params.name_startsWith
125                def result      = [ total: 0, data: [] ]
126                def user        = authenticationService.getLoggedInUser()
127
128                // search studies
129                Study.textSearchReadableStudies(user,query).each { study ->
130                        result.data << [
131                                link            : createLink(controller:'study', action:'show', id:study.id),
132                            name                : "${study.title}",
133                                category        : 'Study'
134                        ]
135                }
136
137                // search templates
138                Template.createCriteria().list {
139                        or {
140                                ilike("name", "%${query}%")
141                                ilike("description", "%${query}%")
142                        }
143                }.each { template ->
144                        def entityName = template.entity.toString().split(/\./)
145                        def encodedEntity = gdtService.encryptEntity(template.entity.toString()).decodeURL()
146
147                        result.data << [
148                                link            : createLink(controller:'templateEditor', action:'template', params:[entity:encodedEntity, standalone:true, template:template.id]),
149                            name                : "${template.name}",
150                                category        : "${entityName[entityName.size()-1]} Template"
151                        ]
152                }
153
154                // set total
155                result.total = result.data.size()
156
157                // render result
158                if (params.callback) {
159                        render "${params.callback}(${result as JSON})"
160                } else {
161                        render result as JSON
162                }
163        }
164}
Note: See TracBrowser for help on using the repository browser.