Subversion Repositories wimsdev

Rev

Rev 11124 | Rev 15779 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 11124 Rev 12248
Line 29... Line 29...
29
#define MIN_ADD 1
29
#define MIN_ADD 1
30
/* Accounting discontinues after this number of idle minutes */
30
/* Accounting discontinues after this number of idle minutes */
31
#define MAX_LAPSE 15
31
#define MAX_LAPSE 15
32
 
32
 
33
struct {
33
struct {
34
    char s[4], u[32];
34
  char s[4], u[32];
35
    int cl, start, end, cnt;
35
  int cl, start, end, cnt;
36
} ses[MAX_SESSIONS];
36
} ses[MAX_SESSIONS];
37
int sescnt;
37
int sescnt;
38
 
38
 
39
char *datestr;
39
char *datestr;
40
 
40
 
41
struct cls {
41
struct cls {
42
    int cl, cnt;
42
  int cl, cnt;
43
} cls[MAX_CLASSES];
43
} cls[MAX_CLASSES];
44
int clscnt;
44
int clscnt;
45
 
45
 
46
/* Read/write to a file with variable parms to print filename */
46
/* Read/write to a file with variable parms to print filename */
47
/* same in Interfaces/common.c and wlogdaccessfile */
47
/* same in Interfaces/common.c and wlogdaccessfile */
48
static
48
static
49
void accessfile(char *content, char *type, char *s,...)
49
void accessfile(char *content, char *type, char *s,...)
50
{
50
{
51
    va_list vp;
51
  va_list vp;
52
    char buf[MAX_LINELEN+1];
52
  char buf[MAX_LINELEN+1];
53
    FILE *f;
53
  FILE *f;
54
    int l;
54
  int l;
55
 
55
 
56
    va_start(vp,s);
56
  va_start(vp,s);
57
    vsnprintf(buf,sizeof(buf),s,vp);
57
  vsnprintf(buf,sizeof(buf),s,vp);
58
    va_end(vp);
58
  va_end(vp);
59
    f=fopen(buf,type); if(f==NULL) {
59
  f=fopen(buf,type); if(f==NULL) {
60
      if(*type=='r') content[0]=0;
60
    if(*type=='r') content[0]=0;
61
      return;
61
    return;
-
 
62
  }
-
 
63
  switch(*type) {
-
 
64
    case 'a':
-
 
65
    case 'w': {
-
 
66
      l=strlen(content); fwrite(content,1,l,f); break;
62
    }
67
    }
63
    switch(*type) {
-
 
64
      case 'a':
-
 
65
      case 'w': {
-
 
66
        l=strlen(content); fwrite(content,1,l,f); break;
-
 
67
      }
-
 
68
      case 'r': {
68
    case 'r': {
69
        l=fread(content,1,MAX_LINELEN-1,f);
69
      l=fread(content,1,MAX_LINELEN-1,f);
70
        if(l>0 && l<MAX_LINELEN) content[l]=0;
70
      if(l>0 && l<MAX_LINELEN) content[l]=0;
71
        else content[0]=0;
71
      else content[0]=0;
72
        break;
72
      break;
73
      }
-
 
74
     default: {
-
 
75
        content[0]=0; break;
-
 
76
     }
-
 
77
    }
73
    }
-
 
74
   default: {
-
 
75
      content[0]=0; break;
-
 
76
   }
-
 
77
  }
78
    fclose(f);
78
  fclose(f);
79
}
79
}
80
/* recursively generate a directory structure */
80
/* recursively generate a directory structure */
81
void mkdirs2(char *s)
81
void mkdirs2(char *s)
82
{
82
{
83
    struct stat st;
83
  struct stat st;
84
    char *buf;
84
  char *buf;
85
    if(stat(s,&st)==-1) {
85
  if(stat(s,&st)==-1) {
86
    if(strrchr(s,'/')!=NULL) {
86
  if(strrchr(s,'/')!=NULL) {
87
        buf=xmalloc(strlen(s)+1);
87
    buf=xmalloc(strlen(s)+1);
88
        ovlstrcpy(buf,s); *strrchr(buf,'/')=0;
88
    ovlstrcpy(buf,s); *strrchr(buf,'/')=0;
89
        mkdirs2(buf); free(buf);
89
    mkdirs2(buf); free(buf);
90
    }
90
  }
91
    mkdir(s,-1);
91
  mkdir(s,-1);
92
    }
92
  }
93
}
93
}
94
 
94
 
95
static
95
static
96
void oneline(char *p)
96
void oneline(char *p)
97
{
97
{
98
    char tbuf[8], sbuf[8], ubuf[256], cbuf[64];
98
  char tbuf[8], sbuf[8], ubuf[256], cbuf[64];
99
    char *p1, *p2;
99
  char *p1, *p2;
100
    int i,t,cl;
100
  int i,t,cl;
101
    memmove(tbuf,p+9,6); tbuf[2]=tbuf[5]=0;
101
  memmove(tbuf,p+9,6); tbuf[2]=tbuf[5]=0;
102
    t=atoi(tbuf)*60+atoi(tbuf+3);
102
  t=atoi(tbuf)*60+atoi(tbuf+3);
103
    memmove(sbuf,p+18,4); sbuf[4]=0;
103
  memmove(sbuf,p+18,4); sbuf[4]=0;
104
    p1=strchr(p,','); if(p1==NULL) return;
104
  p1=strchr(p,','); if(p1==NULL) return;
105
    if(!isdigit(*(p1+1))) return;
105
  if(!isdigit(*(p1+1))) return;
106
    snprintf(cbuf,sizeof(cbuf),"%s",p1+1);
106
  snprintf(cbuf,sizeof(cbuf),"%s",p1+1);
107
    for(p2=cbuf;isdigit(*p2); p2++){};
107
  for(p2=cbuf;isdigit(*p2); p2++){};
108
    *p2=0; cl=atoi(cbuf);
108
  *p2=0; cl=atoi(cbuf);
109
    *p1=0; for(p1--;p1>p && !isspace(*(p1-1)); p1--);
109
  *p1=0; for(p1--;p1>p && !isspace(*(p1-1)); p1--);
110
    snprintf(ubuf,sizeof(ubuf),"%s",p1);
110
  snprintf(ubuf,sizeof(ubuf),"%s",p1);
111
    for(i=0;i<sescnt;i++) {
111
  for(i=0;i<sescnt;i++) {
112
    if(cl==ses[i].cl && memcmp(sbuf,ses[i].s,4)==0 &&
112
    if(cl==ses[i].cl && memcmp(sbuf,ses[i].s,4)==0 &&
113
       ses[i].end>=t-MAX_LAPSE) {
113
        ses[i].end>=t-MAX_LAPSE) {
114
        ses[i].end=t; return;
114
      ses[i].end=t; return;
115
    }
115
    }
116
    }
116
  }
117
    if(sescnt>=MAX_SESSIONS) return;
117
  if(sescnt>=MAX_SESSIONS) return;
118
    memmove(ses[sescnt].s,sbuf,4); ses[sescnt].cl=cl;
118
  memmove(ses[sescnt].s,sbuf,4); ses[sescnt].cl=cl;
119
    ses[sescnt].start=ses[sescnt].end=t;
119
  ses[sescnt].start=ses[sescnt].end=t;
120
    snprintf(ses[sescnt].u,sizeof(ses[sescnt].u),"%s",ubuf);
120
  snprintf(ses[sescnt].u,sizeof(ses[sescnt].u),"%s",ubuf);
121
    sescnt++;
121
  sescnt++;
-
 
122
}
-
 
123
 
-
 
124
void onefile(char *fname)
-
 
125
{
-
 
126
  FILE *f;
-
 
127
  long l;
-
 
128
  char *fbuf, *p1, *p2, *p3;
-
 
129
  l=filelength(fname); if(l<=0) return;
-
 
130
  f=fopen(fname,"r"); if(f==NULL) return;
-
 
131
  fbuf=xmalloc(l+16); (void)fread(fbuf,1,l,f); fclose(f); fbuf[l]=0;
-
 
132
  for(p1=fbuf; *p1; p1=p2) {
-
 
133
  p2=strchr(p1,'\n'); if(p2==NULL) p2=p1+strlen(p1); else *p2++=0;
-
 
134
  p3=strchr(p1,','); if(p3==NULL) continue;
-
 
135
  if(strncmp(p1,datestr,8)!=0) continue;
-
 
136
  oneline(p1);
-
 
137
  }
122
}
138
}
123
 
139
 
124
void onefile(char *fname)
140
void classaccount(void)
125
{
141
{
126
    FILE *f;
-
 
127
    long l;
-
 
128
    char *fbuf, *p1, *p2, *p3;
-
 
129
    l=filelength(fname); if(l<=0) return;
-
 
130
    f=fopen(fname,"r"); if(f==NULL) return;
-
 
131
    fbuf=xmalloc(l+16); (void)fread(fbuf,1,l,f); fclose(f); fbuf[l]=0;
-
 
132
    for(p1=fbuf; *p1; p1=p2) {
-
 
133
    p2=strchr(p1,'\n'); if(p2==NULL) p2=p1+strlen(p1); else *p2++=0;
-
 
134
    p3=strchr(p1,','); if(p3==NULL) continue;
-
 
135
    if(strncmp(p1,datestr,8)!=0) continue;
-
 
136
    oneline(p1);
-
 
137
    }
-
 
138
}
-
 
139
 
-
 
140
void classaccount(void)
-
 
141
{
-
 
142
    int i,j;
142
  int i,j;
143
    clscnt=0;
143
  clscnt=0;
144
    for(i=0;i<sescnt;i++) {
144
  for(i=0;i<sescnt;i++) {
145
    ses[i].cnt=ses[i].end-ses[i].start+MIN_ADD;
145
  ses[i].cnt=ses[i].end-ses[i].start+MIN_ADD;
146
    if(ses[i].cnt<MIN_CONNECT) ses[i].cnt=MIN_CONNECT;
146
  if(ses[i].cnt<MIN_CONNECT) ses[i].cnt=MIN_CONNECT;
147
    for(j=0;j<clscnt && ses[i].cl!=cls[j].cl;j++);
147
  for(j=0;j<clscnt && ses[i].cl!=cls[j].cl;j++);
148
    if(j<clscnt) cls[j].cnt+=ses[i].cnt;
148
  if(j<clscnt) cls[j].cnt+=ses[i].cnt;
-
 
149
  else
149
    else if(clscnt<MAX_CLASSES) {
150
    if(clscnt<MAX_CLASSES) {
150
        cls[clscnt].cl=ses[i].cl;
151
      cls[clscnt].cl=ses[i].cl;
151
        cls[clscnt].cnt=ses[i].cnt;
152
      cls[clscnt].cnt=ses[i].cnt;
152
        clscnt++;
153
      clscnt++;
153
    }
-
 
154
    }
154
    }
-
 
155
  }
155
}
156
}
156
 
157
 
157
int clscmp(const void *c1, const void *c2)
158
int clscmp(const void *c1, const void *c2)
158
{
159
{
159
    struct cls *cl1, *cl2;
160
  struct cls *cl1, *cl2;
160
    cl1=(struct cls *) c1; cl2=(struct cls *) c2;
161
  cl1=(struct cls *) c1; cl2=(struct cls *) c2;
161
    return cl1->cl-cl2->cl;
162
  return cl1->cl-cl2->cl;
162
}
163
}
163
 
164
 
164
void output(void)
165
void output(void)
165
{
166
{
166
    char *p, buf[1024], dbuf[1024];
167
  char *p, buf[1024], dbuf[1024];
167
    int i,t;
168
  int i,t;
168
    p=getenv("ccsum_outdir"); if(p==NULL || *p==0) return;
169
  p=getenv("ccsum_outdir"); if(p==NULL || *p==0) return;
169
    for(i=0;i<sescnt;i++) {
170
  for(i=0;i<sescnt;i++) {
170
    snprintf(dbuf,sizeof(dbuf),"%s/%d",p,ses[i].cl);
171
    snprintf(dbuf,sizeof(dbuf),"%s/%d",p,ses[i].cl);
171
    mkdirs2(dbuf);
172
    mkdirs2(dbuf);
172
    snprintf(buf,sizeof(buf),"%s.%02d:%02d %d\n",
173
    snprintf(buf,sizeof(buf),"%s.%02d:%02d %d\n",
173
         datestr,ses[i].start/60,ses[i].start%60,ses[i].cnt);
174
       datestr,ses[i].start/60,ses[i].start%60,ses[i].cnt);
174
    accessfile(buf,"a","%s/%s",dbuf,ses[i].u);
175
    accessfile(buf,"a","%s/%s",dbuf,ses[i].u);
175
    }
176
  }
176
    snprintf(dbuf,sizeof(dbuf),"%s/bydate/%.4s",p,datestr);
177
  snprintf(dbuf,sizeof(dbuf),"%s/bydate/%.4s",p,datestr);
177
    mkdirs2(dbuf);
178
  mkdirs2(dbuf);
178
    snprintf(dbuf+strlen(dbuf),sizeof(dbuf)-strlen(dbuf),"/%.2s",datestr+4);
179
  snprintf(dbuf+strlen(dbuf),sizeof(dbuf)-strlen(dbuf),"/%.2s",datestr+4);
179
    t=0;
180
  t=0;
180
    qsort(cls,clscnt,sizeof(cls[0]),clscmp);
181
  qsort(cls,clscnt,sizeof(cls[0]),clscmp);
181
    for(i=0;i<clscnt;i++) {
182
  for(i=0;i<clscnt;i++) {
182
    snprintf(buf,sizeof(buf),"%s %d\n",datestr,cls[i].cnt);
183
    snprintf(buf,sizeof(buf),"%s %d\n",datestr,cls[i].cnt);
183
    accessfile(buf,"a","%s/%d/.total",p,cls[i].cl);
184
    accessfile(buf,"a","%s/%d/.total",p,cls[i].cl);
184
    snprintf(buf,sizeof(buf),"%s %d %d\n",datestr+4,cls[i].cl,cls[i].cnt);
185
    snprintf(buf,sizeof(buf),"%s %d %d\n",datestr+4,cls[i].cl,cls[i].cnt);
185
    accessfile(buf,"a","%s",dbuf);
186
    accessfile(buf,"a","%s",dbuf);
186
    t+=cls[i].cnt;
187
    t+=cls[i].cnt;
187
    }
188
  }
188
    snprintf(buf,sizeof(buf),"%s %d %d\n",datestr,t,(t+30)/60);
189
  snprintf(buf,sizeof(buf),"%s %d %d\n",datestr,t,(t+30)/60);
189
    accessfile(buf,"a","%s/done",p);
190
  accessfile(buf,"a","%s/done",p);
190
}
191
}
191
 
192
 
192
int main(int argc, char *argv[])
193
int main(int argc, char *argv[])
193
{
194
{
194
    sescnt=0;
195
  sescnt=0;
195
    if(argc<2) return 1;
196
  if(argc<2) return 1;
196
    datestr=getenv("ccsum_date");
197
  datestr=getenv("ccsum_date");
197
    if(datestr==NULL || strlen(datestr)!=8) return 2;
198
  if(datestr==NULL || strlen(datestr)!=8) return 2;
198
    onefile(argv[1]);
199
  onefile(argv[1]);
199
    classaccount();
200
  classaccount();
200
    output();
201
  output();
201
    return 0;
202
  return 0;
202
}
203
}
203
 
204