summaryrefslogtreecommitdiffstats
path: root/core/views/statistics.py
blob: f056ab1e6febaa9c8aa6bd5f37110d4b93c02c16 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from collections import OrderedDict
from pathlib import Path

from django.shortcuts import render

import troggle.settings as settings
from troggle.core.models.caves import Cave, Entrance
from troggle.core.models.logbooks import LogbookEntry
from troggle.core.models.survex import SurvexStation, SurvexPersonRole
from troggle.core.models.troggle import DataIssue, Expedition, Person, PersonExpedition
from troggle.core.models.wallets import Wallet
from troggle.parsers.people import GetPersonExpeditionNameLookup, foreign_friends

# from django.views.generic.list import ListView
"""Very simple report pages summarizing data about the whole set of expeditions and of
the status of data inconsistencies
"""

def svxfilewild(request, year=None):
    """Looks for survexfiles which do not have an associated
    wallet, per year
    """
    legsbyexpo = []
    addupsurvexlength = 0.0
    addupsurvexlegs = 0
 
    if not year:
        expos = Expedition.objects.all()
    else:
        expos = Expedition.objects.filter(year=year)
    
    for expedition in expos:
        survexblocks = expedition.survexblock_set.all()
        legsyear = 0
        survexleglength = 0.0
        for survexblock in survexblocks:
            survexleglength += survexblock.legslength
            legsyear += int(survexblock.legsall)
        addupsurvexlength += survexleglength
        addupsurvexlegs += legsyear
        legsbyexpo.append((expedition, {"nsurvexlegs": legsyear, "survexleglength": survexleglength}))
    legsbyexpo.reverse()

    svxwild = []
    wildlength = 0.0
    for expedition in expos:
        survexblocks = expedition.survexblock_set.all()
        for sb in survexblocks:
            # print(f"{sb=}")
            if sb.scanswallet == None:
                if sb.name != "rootblock":
                    svxwild.append(sb)
                    print(f"  WILD {sb.survexfile} {sb.date}")
                    wildlength += sb.legslength
                    sb.year = f"{expedition}"
                    people = SurvexPersonRole.objects.filter(survexblock=sb)
                    team = []
                    for p in people:
                        team.append(p.personname)
                    sb.team = team
    walletslength = 0.0
    if year:
        wallets = Wallet.objects.filter(walletyear__year=year)
        for w in wallets:
            for sb in w.survexblock_set.all():
                walletslength += sb.legslength
            
    return render(request, "survexfilewild.html", 
        {"addupsurvexlength": addupsurvexlength / 1000, 
        "legsbyexpo": legsbyexpo, 
        "nsurvexlegs": addupsurvexlegs,
        "walletslength": walletslength,
        "wildlength": wildlength,
        "svxwild": svxwild}
    )
    
def therionissues(request):
    """Page displaying contents of a file produced during data import"""
    logname = "therionrefs.log"
    logpath = Path(settings.PYTHON_PATH, logname)
    therionlog = []
    newlog = []

    if Path(logpath).is_file:
        try:
            with open(logpath, "r") as f:
                therionlog = f.readlines()
                print(f"{logpath} has {len(therionlog)} entries")
        except:
            msg = f"{logpath} EXCEPTION opening or reading therion logfile. Run a full reset."
            print(msg)
            return render(request, "therionreport.html", {"therionlog": newlog})
    else:
        print(f"{logpath} NOT FOUND: {len(therionlog)} entries")

    for line in therionlog:
        line = line.replace("! Un-parsed image filename:", "")
        newlog.append(line)
    return render(request, "therionreport.html", {"therionlog": newlog})


def surveximport(request):
    """Page displaying contents of a file produced during data import"""
    logname = "svxlinear.log"
    logpath = Path(settings.PYTHON_PATH, logname)

    if Path(logpath).is_file:
        with open(logpath, "r") as f:
            contents = f.read()
    else:
        print(f"{logpath} NOT FOUND {len(contents)}")
    return render(request, "survexreport.html", {"log": contents})


def survexdebug(request):
    """Page displaying contents of a file produced during data import"""
    logname = "svxblks.log"
    logpath = Path(settings.PYTHON_PATH, logname)

    if Path(logpath).is_file:
        with open(logpath, "r") as f:
            contents = f.read()
    else:
        print(f"{logpath} NOT FOUND {len(contents)}")
    return render(request, "survexdebug.html", {"log": contents})


def pathsreport(request):
    """The CONSTANTs declared in the settings and localsettings and how they have
    been evaluated for this specific installation - live"""
    pathsdict = OrderedDict()
    try:
        pathsdict = {
            #       "BOGUS" : str( settings.BOGUS),
            "JSLIB_URL": str(settings.JSLIB_URL),
            "JSLIB_ROOT": str(settings.JSLIB_ROOT),
            #       "CSSLIB_URL" : str( settings.CSSLIB_URL),
            "CAVEDESCRIPTIONS": str(settings.CAVEDESCRIPTIONS),
            "DIR_ROOT": str(settings.DIR_ROOT),
            "ENTRANCEDESCRIPTIONS": str(settings.ENTRANCEDESCRIPTIONS),
            "EXPOUSER_EMAIL": str(settings.EXPOUSER_EMAIL),
            "EXPOUSERPASS": str("<redacted>"),
            "EXPOUSER": str(settings.EXPOUSER),
            "EXPOWEB": str(settings.EXPOWEB),
            # "EXPOWEB_URL": str(settings.EXPOWEB_URL),
            #       "FILES" : str( settings.FILES),
            "LIBDIR": str(settings.LIBDIR),
            "LOGFILE": str(settings.LOGFILE),
            "LOGIN_REDIRECT_URL": str(settings.LOGIN_REDIRECT_URL),
            "MEDIA_ROOT": str(settings.MEDIA_ROOT),
            "MEDIA_URL": str(settings.MEDIA_URL),
            "PHOTOS_URL": str(settings.PHOTOS_URL),
            "PHOTOS_ROOT": str(settings.PHOTOS_ROOT),
            "PYTHON_PATH": str(settings.PYTHON_PATH),
            "REPOS_ROOT_PATH": str(settings.REPOS_ROOT_PATH),
            "ROOT_URLCONF": str(settings.ROOT_URLCONF),
            "STATIC_URL": str(settings.STATIC_URL),
            "SURVEX_DATA": str(settings.SURVEX_DATA),
            "SCANS_ROOT": str(settings.SCANS_ROOT),
            #       "SURVEYS" : str( settings.SURVEYS),
            #       "SCANS_URL" : str( settings.SCANS_URL),
            "SURVEXPORT": str(settings.SURVEXPORT),
            "DRAWINGS_DATA": str(settings.DRAWINGS_DATA),
            "URL_ROOT": str(settings.URL_ROOT),
        }
    except:
        pathsdict["! EXCEPTION !"] = "missing or exta string constant in troggle/settings"

    pathstype = OrderedDict()
    try:
        pathstype = {
            #       "BOGUS" : type(settings.BOGUS),
            "JSLIB_URL": type(settings.JSLIB_URL),
            "JSLIB_ROOT": type(settings.JSLIB_ROOT),
            #       "CSSLIB_URL" : type(settings.CSSLIB_URL),
            "CAVEDESCRIPTIONS": type(settings.CAVEDESCRIPTIONS),
            "DIR_ROOT": type(settings.DIR_ROOT),
            "ENTRANCEDESCRIPTIONS": type(settings.ENTRANCEDESCRIPTIONS),
            "EXPOUSER_EMAIL": type(settings.EXPOUSER_EMAIL),
            "EXPOUSERPASS": type(settings.EXPOUSERPASS),
            "EXPOUSER": type(settings.EXPOUSER),
            "EXPOWEB": type(settings.EXPOWEB),
            # "EXPOWEB_URL": type(settings.EXPOWEB_URL),
            #       "FILES" : type(settings.FILES),
            "LIBDIR": type(settings.LIBDIR),
            "LOGFILE": type(settings.LOGFILE),
            "LOGIN_REDIRECT_URL": type(settings.LOGIN_REDIRECT_URL),
            "MEDIA_ROOT": type(settings.MEDIA_ROOT),
            "MEDIA_URL": type(settings.MEDIA_URL),
            "PHOTOS_ROOT": type(settings.PHOTOS_ROOT),
            "PHOTOS_URL": type(settings.PHOTOS_URL),
            "PYTHON_PATH": type(settings.PYTHON_PATH),
            "REPOS_ROOT_PATH": type(settings.REPOS_ROOT_PATH),
            "ROOT_URLCONF": type(settings.ROOT_URLCONF),
            "STATIC_URL": type(settings.STATIC_URL),
            "SURVEX_DATA": type(settings.SURVEX_DATA),
            "SCANS_ROOT": type(settings.SCANS_ROOT),
            #       "SURVEYS" : type(settings.SURVEYS),
            #       "SCANS_URL" : type(settings.SCANS_URL),
            "SURVEXPORT": type(settings.SURVEXPORT),
            "DRAWINGS_DATA": type(settings.DRAWINGS_DATA),
            "URL_ROOT": type(settings.URL_ROOT),
        }
    except:
        pathstype["! EXCEPTION !"] = "missing or exta string constant in troggle/settings"
        raise

    # settings are unique by paths are not
    ncodes = len(pathsdict)
    bycodeslist = sorted(pathsdict.items())  # a list of tuples
    bycodeslist2 = []

    for k, p in bycodeslist:
        bycodeslist2.append((k, p, str(pathstype[k])))

    bypaths = sorted(pathsdict.values())  # a list
    bypathslist = []

    for p in bypaths:
        for k in pathsdict.keys():
            if pathsdict[k] == p:
                bypathslist.append((p, k, str(pathstype[k])))
                del pathsdict[k]
                break

    return render(
        request,
        "pathsreport.html",
        {"pathsdict": pathsdict, "bycodeslist": bycodeslist2, "bypathslist": bypathslist, "ncodes": ncodes},
    )


def stats(request):
    """Calculates number of survey blocks, the number of survey legs and the survey length for each year.
    This is only underground survey legs, but includes ARGE as well as Expo survex files.
    """
    statsDict = {}
    statsDict["expoCount"] = f"{Expedition.objects.count():,}"
    statsDict["caveCount"] = f"{Cave.objects.count():,}"
    statsDict["personCount"] = f"{Person.objects.count():,}"
    statsDict["logbookEntryCount"] = f"{LogbookEntry.objects.count():,}"

    legsbyexpo = []
    addupsurvexlength = 0.0
    addupsurvexlegs = 0
    for expedition in Expedition.objects.all():
        survexblocks = expedition.survexblock_set.all()
        legsyear = 0
        survexleglength = 0.0
        for survexblock in survexblocks:
            survexleglength += survexblock.legslength
            legsyear += int(survexblock.legsall)
        addupsurvexlength += survexleglength
        addupsurvexlegs += legsyear
        legsbyexpo.append((expedition, {"nsurvexlegs": legsyear, "survexleglength": survexleglength}))
    legsbyexpo.reverse()

    renderDict = {
        **statsDict,
        **{"addupsurvexlength": addupsurvexlength / 1000, "legsbyexpo": legsbyexpo, "nsurvexlegs": addupsurvexlegs},
    }  # new syntax
    return render(request, "statistics.html", renderDict)


def dataissues(request):
    """Each issue has a parser, a message and a url linking to the offending object after loading"""

    def myFunc(di):
        return di.parser.lower() + di.message.lower()

    dilist = list(DataIssue.objects.all())
    dilist.sort(key=myFunc)

    return render(request, "dataissues.html", {"didict": dilist})


def eastings(request):
    """report each Northing/Easting pair wherever recorded"""
    ents = []
    entrances = Entrance.objects.all()
    for e in entrances:
        if e.easting or e.northing:
            ents.append(e)
        if e.lat_wgs84 or e.long_wgs84:
            ents.append(e)

    stations = SurvexStation.objects.all()

    return render(request, "eastings.html", {"ents": ents, "stations": stations})


def aliases(request, year):
    """Page which displays a list of all the person aliases in a specific year"""

    if not year:
        year = 1998
    expo = Expedition.objects.filter(year=year)[0]  # returns a set, even though we know there is only one
    personexpeditions = PersonExpedition.objects.filter(expedition=expo)
    persons = list(Person.objects.all().order_by("last_name"))

    aliases = GetPersonExpeditionNameLookup(expo)

    aliasdict = {}
    for i in sorted(aliases):
        aliasdict[i] = aliases[i]
    invert = {}

    return render(
        request,
        "aliases.html",
        {
            "year": year,
            "aliasdict": aliasdict,
            "foreign_friends": foreign_friends,
            "invert": invert,
            "personexpeditions": personexpeditions,
            "persons": persons,
        },
    )