Rev 8185 | Rev 8899 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
17 | |||
8100 | bpr | 18 | /* Computes class connection count (unit: student-minutes) */ |
10 | reyssat | 19 | |
8100 | bpr | 20 | #include "../Lib/libwims.h" |
10 | reyssat | 21 | |
8149 | bpr | 22 | /* The maximal number of sessions within one day */ |
10 | reyssat | 23 | #define MAX_SESSIONS (128*1024) |
8149 | bpr | 24 | /* The maximal number of classes within one day */ |
10 | reyssat | 25 | #define MAX_CLASSES 8192 |
8149 | bpr | 26 | /* At least these minutes will be counted for each session */ |
10 | reyssat | 27 | #define MIN_CONNECT 2 |
8149 | bpr | 28 | /* Add this number of minutes to each session */ |
10 | reyssat | 29 | #define MIN_ADD 1 |
8149 | bpr | 30 | /* Accounting discontinues after this number of idle minutes */ |
10 | reyssat | 31 | #define MAX_LAPSE 15 |
32 | |||
33 | struct { |
||
34 | char s[4], u[32]; |
||
35 | int cl, start, end, cnt; |
||
36 | } ses[MAX_SESSIONS]; |
||
37 | int sescnt; |
||
38 | |||
39 | char *datestr; |
||
40 | |||
41 | struct cls { |
||
42 | int cl, cnt; |
||
43 | } cls[MAX_CLASSES]; |
||
44 | int clscnt; |
||
45 | |||
8149 | bpr | 46 | /* Read/write to a file with variable parms to print filename */ |
10 | reyssat | 47 | void accessfile(char *content, char *type, char *s,...) |
48 | { |
||
49 | va_list vp; |
||
50 | char buf[MAX_LINELEN+1]; |
||
51 | FILE *f; |
||
52 | int l; |
||
53 | |||
54 | va_start(vp,s); |
||
55 | vsnprintf(buf,sizeof(buf),s,vp); |
||
56 | va_end(vp); |
||
57 | f=fopen(buf,type); if(f==NULL) { |
||
8879 | bpr | 58 | if(*type=='r') content[0]=0; return; |
10 | reyssat | 59 | } |
60 | switch(*type) { |
||
8879 | bpr | 61 | case 'a': |
62 | case 'w': { |
||
7079 | bpr | 63 | l=strlen(content); fwrite(content,1,l,f); break; |
8879 | bpr | 64 | } |
65 | case 'r': { |
||
7079 | bpr | 66 | l=fread(content,1,MAX_LINELEN-1,f); |
67 | if(l>0 && l<MAX_LINELEN) content[l]=0; |
||
68 | else content[0]=0; |
||
69 | break; |
||
8879 | bpr | 70 | } |
71 | default: { |
||
7079 | bpr | 72 | content[0]=0; break; |
8879 | bpr | 73 | } |
7079 | bpr | 74 | } |
10 | reyssat | 75 | fclose(f); |
76 | } |
||
8149 | bpr | 77 | /* recursively generate a directory structure */ |
8100 | bpr | 78 | void mkdirs2(char *s) |
10 | reyssat | 79 | { |
80 | struct stat st; |
||
81 | char *buf; |
||
82 | if(stat(s,&st)==-1) { |
||
7079 | bpr | 83 | if(strrchr(s,'/')!=NULL) { |
84 | buf=xmalloc(strlen(s)+1); |
||
85 | ovlstrcpy(buf,s); *strrchr(buf,'/')=0; |
||
8100 | bpr | 86 | mkdirs2(buf); free(buf); |
10 | reyssat | 87 | } |
7079 | bpr | 88 | mkdir(s,-1); |
89 | } |
||
10 | reyssat | 90 | } |
91 | |||
92 | void oneline(char *p) |
||
93 | { |
||
94 | char tbuf[8], sbuf[8], ubuf[256], cbuf[64]; |
||
95 | char *p1, *p2; |
||
96 | int i,t,cl; |
||
97 | memmove(tbuf,p+9,6); tbuf[2]=tbuf[5]=0; |
||
98 | t=atoi(tbuf)*60+atoi(tbuf+3); |
||
99 | memmove(sbuf,p+18,4); sbuf[4]=0; |
||
100 | p1=strchr(p,','); if(p1==NULL) return; |
||
101 | if(!isdigit(*(p1+1))) return; |
||
102 | snprintf(cbuf,sizeof(cbuf),"%s",p1+1); |
||
7079 | bpr | 103 | for(p2=cbuf;isdigit(*p2); p2++){}; |
7076 | obado | 104 | *p2=0; cl=atoi(cbuf); |
10 | reyssat | 105 | *p1=0; for(p1--;p1>p && !isspace(*(p1-1)); p1--); |
106 | snprintf(ubuf,sizeof(ubuf),"%s",p1); |
||
107 | for(i=0;i<sescnt;i++) { |
||
7079 | bpr | 108 | if(cl==ses[i].cl && memcmp(sbuf,ses[i].s,4)==0 && |
109 | ses[i].end>=t-MAX_LAPSE) { |
||
110 | ses[i].end=t; return; |
||
10 | reyssat | 111 | } |
7079 | bpr | 112 | } |
10 | reyssat | 113 | if(sescnt>=MAX_SESSIONS) return; |
114 | memmove(ses[sescnt].s,sbuf,4); ses[sescnt].cl=cl; |
||
115 | ses[sescnt].start=ses[sescnt].end=t; |
||
116 | snprintf(ses[sescnt].u,sizeof(ses[sescnt].u),"%s",ubuf); |
||
117 | sescnt++; |
||
118 | } |
||
119 | |||
120 | void onefile(char *fname) |
||
121 | { |
||
122 | FILE *f; |
||
123 | long l; |
||
124 | char *fbuf, *p1, *p2, *p3; |
||
125 | l=filelength(fname); if(l<=0) return; |
||
126 | f=fopen(fname,"r"); if(f==NULL) return; |
||
3840 | kbelabas | 127 | fbuf=xmalloc(l+16); (void)fread(fbuf,1,l,f); fclose(f); fbuf[l]=0; |
10 | reyssat | 128 | for(p1=fbuf; *p1; p1=p2) { |
7079 | bpr | 129 | p2=strchr(p1,'\n'); if(p2==NULL) p2=p1+strlen(p1); else *p2++=0; |
130 | p3=strchr(p1,','); if(p3==NULL) continue; |
||
131 | if(strncmp(p1,datestr,8)!=0) continue; |
||
132 | oneline(p1); |
||
10 | reyssat | 133 | } |
134 | } |
||
135 | |||
136 | void classaccount(void) |
||
137 | { |
||
138 | int i,j; |
||
139 | clscnt=0; |
||
140 | for(i=0;i<sescnt;i++) { |
||
7079 | bpr | 141 | ses[i].cnt=ses[i].end-ses[i].start+MIN_ADD; |
142 | if(ses[i].cnt<MIN_CONNECT) ses[i].cnt=MIN_CONNECT; |
||
143 | for(j=0;j<clscnt && ses[i].cl!=cls[j].cl;j++); |
||
144 | if(j<clscnt) cls[j].cnt+=ses[i].cnt; |
||
145 | else if(clscnt<MAX_CLASSES) { |
||
146 | cls[clscnt].cl=ses[i].cl; |
||
147 | cls[clscnt].cnt=ses[i].cnt; |
||
148 | clscnt++; |
||
10 | reyssat | 149 | } |
7079 | bpr | 150 | } |
10 | reyssat | 151 | } |
152 | |||
153 | int clscmp(const void *c1, const void *c2) |
||
154 | { |
||
155 | struct cls *cl1, *cl2; |
||
156 | cl1=(struct cls *) c1; cl2=(struct cls *) c2; |
||
157 | return cl1->cl-cl2->cl; |
||
158 | } |
||
159 | |||
160 | void output(void) |
||
161 | { |
||
162 | char *p, buf[1024], dbuf[1024]; |
||
163 | int i,t; |
||
164 | p=getenv("ccsum_outdir"); if(p==NULL || *p==0) return; |
||
165 | for(i=0;i<sescnt;i++) { |
||
7079 | bpr | 166 | snprintf(dbuf,sizeof(dbuf),"%s/%d",p,ses[i].cl); |
8100 | bpr | 167 | mkdirs2(dbuf); |
7079 | bpr | 168 | snprintf(buf,sizeof(buf),"%s.%02d:%02d %d\n", |
169 | datestr,ses[i].start/60,ses[i].start%60,ses[i].cnt); |
||
170 | accessfile(buf,"a","%s/%s",dbuf,ses[i].u); |
||
10 | reyssat | 171 | } |
172 | snprintf(dbuf,sizeof(dbuf),"%s/bydate/%.4s",p,datestr); |
||
8100 | bpr | 173 | mkdirs2(dbuf); |
10 | reyssat | 174 | snprintf(dbuf+strlen(dbuf),sizeof(dbuf)-strlen(dbuf),"/%.2s",datestr+4); |
175 | t=0; |
||
176 | qsort(cls,clscnt,sizeof(cls[0]),clscmp); |
||
177 | for(i=0;i<clscnt;i++) { |
||
7079 | bpr | 178 | snprintf(buf,sizeof(buf),"%s %d\n",datestr,cls[i].cnt); |
179 | accessfile(buf,"a","%s/%d/.total",p,cls[i].cl); |
||
180 | snprintf(buf,sizeof(buf),"%s %d %d\n",datestr+4,cls[i].cl,cls[i].cnt); |
||
181 | accessfile(buf,"a","%s",dbuf); |
||
182 | t+=cls[i].cnt; |
||
10 | reyssat | 183 | } |
184 | snprintf(buf,sizeof(buf),"%s %d %d\n",datestr,t,(t+30)/60); |
||
185 | accessfile(buf,"a","%s/done",p); |
||
186 | } |
||
187 | |||
188 | int main(int argc, char *argv[]) |
||
189 | { |
||
190 | sescnt=0; |
||
191 | if(argc<2) return 1; |
||
192 | datestr=getenv("ccsum_date"); |
||
193 | if(datestr==NULL || strlen(datestr)!=8) return 2; |
||
194 | onefile(argv[1]); |
||
195 | classaccount(); |
||
196 | output(); |
||
197 | return 0; |
||
198 | } |
||
199 |