-
Notifications
You must be signed in to change notification settings - Fork 3
/
db-statistics.rkt
142 lines (119 loc) · 4.16 KB
/
db-statistics.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#lang racket/base
; db-statistics.rkt
(require racket/class
racket/format
racket/gui/base
racket/list
"base.rkt"
"db.rkt"
"files.rkt")
(provide stats-frame update-stats)
(define stats-frame (new frame%
[label "Ivy Statistics"]
[width 700]
[height 100]))
(unless macosx?
(void (send stats-frame set-icon logo-bmp)))
; total images
(define total-images-hpanel
(new horizontal-panel%
[parent stats-frame]))
(define total-images-text
(new horizontal-panel%
[parent total-images-hpanel]
[alignment '(left center)]))
(define total-images-value
(new horizontal-panel%
[parent total-images-hpanel]
[alignment '(right center)]))
; total tags
(define total-tags-hpanel
(new horizontal-panel%
[parent stats-frame]))
(define total-tags-text
(new horizontal-panel%
[parent total-tags-hpanel]
[alignment '(left center)]))
(define total-tags-value
(new horizontal-panel%
[parent total-tags-hpanel]
[alignment '(right center)]))
; tags per image
(define tags-per-img-hpanel
(new horizontal-panel%
[parent stats-frame]))
(define tags-per-img-text
(new horizontal-panel%
[parent tags-per-img-hpanel]
[alignment '(left center)]))
(define tags-per-img-value
(new horizontal-panel%
[parent tags-per-img-hpanel]
[alignment '(right center)]))
; vertical-panel for the rest
(define stats-vpanel
(new vertical-panel%
[parent stats-frame]
[alignment '(left center)]))
; obtain the largest ocurrence `num' of tag `name'
; return values of num and name
(define (greater lst [num 0] [name ""])
(cond [(empty? lst) (values num name)]
[else
(define len (length (second (first lst))))
(if (> len num)
(greater (rest lst) len (first (first lst)))
(greater (rest lst) num name))]))
; give an up-to-date reading of the database each time
(define (create-children)
(define imgs-pairs (table-pairs 'images))
(define tags-pairs (table-pairs 'tags))
; total images
(new message%
[parent total-images-text]
[label "Total images:"])
(new message%
[parent total-images-value]
[label (format "~a" (length imgs-pairs))])
; total tags
(new message%
[parent total-tags-text]
[label "Total tags:"])
(new message%
[parent total-tags-value]
[label (format "~a" (length tags-pairs))])
; tags per image
(let ([avg (/ (for/sum ([ip (in-list imgs-pairs)])
(length (second ip)))
(length imgs-pairs))])
(new message%
[parent tags-per-img-text]
[label "Average tags per image:"])
(new message%
[parent tags-per-img-value]
[label (format "~a" (~r (exact->inexact avg) #:precision 3))]))
(new message%
[parent stats-vpanel]
[label
(let-values ([(num name) (greater imgs-pairs)])
; make sure formatted string does not exceed label-string? max-length
(define truncation (- +label-max+ 39 (string-length (number->string num))))
(format "Largest number of tags on an image: ~a (~a)"
num (string-truncate (path->string name) truncation)))])
(new message%
[parent stats-vpanel]
[label
(let-values ([(num name) (greater tags-pairs)])
(define truncation (- +label-max+ 47 (string-length (number->string num))))
(format "Largest number of images in a tag category: ~a (~a)"
num (string-truncate name truncation)))])
(void))
(define (update-stats)
(remove-children total-images-text (send total-images-text get-children))
(remove-children total-images-value (send total-images-value get-children))
(remove-children total-tags-text (send total-tags-text get-children))
(remove-children total-tags-value (send total-tags-value get-children))
(remove-children tags-per-img-text (send tags-per-img-text get-children))
(remove-children tags-per-img-value (send tags-per-img-value get-children))
(remove-children stats-vpanel (send stats-vpanel get-children))
(create-children))