evaalapi.py

   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
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
################################################################
## evaalapi.py
## a web API interface for running the IPIN competitions
## Copyright 2021 Francesco Potortì
## This program can be freely run, copied, modified and distributed
## under a GNU Affero General Public License v3.0 or later
## SPDX-License-Identifier: AGPL-3.0-or-later
################################################################

import os
import lzma
import time                             # sleep
from datetime import datetime, timezone # strftime with microseconds
import yaml
from parse import parse

from markdown import markdown
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter

from flask import Flask, abort, request, Request, Response, send_file
from markupsafe import escape


revision = "$Revision: 2.51 $"[11:-2]
source = "evaalapi.py"
sourcedir = "source/"
trialsdir = "trials/"
globinifn = "evaalapi.yaml"
privatefn = "private.yaml"
starttime =  datetime.now(timezone.utc).strftime("%FT%H:%M:%S")
guisource = "evaalapigui.py"


## The Flask app
app = Flask(__name__)

## This is crude, probably something should be done with the app.logger
debug = app.debug

## Useful for logging to get the originating IP when behind proxies
## In the Request object request, use X-Forwarded-For to set remote_addr
proxies = 1                             # number of proxies
if proxies > 0:
    try:                                # get remote ip when behind a proxy
        from werkzeug.middleware.proxy_fix import ProxyFix
        app.wsgi_app = ProxyFix(app.wsgi_app, proxies)
    except ImportError:
        pass

states = ('nonstarted','running','finished','timeout')
esthdr = "pts,c,h,s,pos"
estfmt = "{pts:.3f},{c:.3f},{h:.3f},{s:.3f},{pos}"
statefmt = "{trialts:.3f},{rem:.3f},{V:.3f},{S:.3f},{p:.3f},{h:.3f},{pts:.3f},{pos}"

trials = {}                             # the known trials
test_trial = {"test":                   # a trial used by the test suite
              {'name': "test",
               'datafile': "test.csv",  # text data file
               'commsep': '%',          # comment-starting string in data file
               'sepch': ',',            # field separator in data file
               'V': 1,                  # normally 3, but testsuite would be slower
               'S': 2,                  # normally 15, but testsuite would be slower
               'inipos': "10.422057,43.718278,1", # my office location in WGS84
               'reloadable': True}}

## OpenAPI description initialisation using the swagger library
##
## A bug in flasgger 0.9.51 keeps newlines in description even with MK_SANITIZER
## so we should remove the newlines from the string.  We do it by using yaml
## block scalars (see https://yaml-multiline.info/).  Note that the blank lines
## between paragraphs are indented: the number of space characters matters!
apidescription = """
    ## IPIN competition interface

    For official use at the [IPIN competition](http://evaal.aaloa.org) you need a trial code.
      
    For unofficial testing you can freely use the `demo` trial, either by
    writing your own tests or by running the [demo](/evaalapi/demo)
    program at your premises.  Calling `demo auto` produces [this output](/evaalapi/demo-auto.out)
    on your terminal.  Calling `demo interactive` allows one to choose the timing by
    pressing Return at the terminal.
      
    If you want to run `demo` with an API server at your premises, you need to download
    the API server source code and the [demo configuration](/evaalapi/evaalapi.yaml) in the
    same directory, plus the `Logfiles/01-Training/01a-Regular/T03_02.txt` file taken from
    [Indoorloc](http://indoorloc.uji.es/ipin2020track3/files/logfiles2020.zip), to be put
    under a `trials/` subdirectory.

    ### Documentation

    You can find a browsable, interactive [OpenAPI description](/evaalapi/apidocs/)
    and the API [complete documentation](/evaalapi/evaalapi.html) (Markdown [source](/evaalapi/evaalapi.md)).

    ### Source code

    Copyright 2021 Francesco Potortì
      
    The Python ["""+source+"""](/evaalapi/"""+source+""") source code is released under the
    [GNU Affero General Public License v3.0 or later](https://www.gnu.org/licenses/agpl-3.0.html).
"""
swagger_config = {
    ## See examples/changelog_090.py for additional customisations
    'title': "EvaalAPI",
    'uiversion': '3',                   # 3 is the default
    'swagger_ui': True,
    'hide_top_bar': True,               # hide the green top bar
    ## 'top_text': "<h1>A web API for the IPIN competition</h1><br><hr>",
    'footer_text': "<b>Copyright 2021 Francesco Potortì - GNU AGPL 3.0 or later",
    'headers': [],
    'specs': [{'endpoint': "openapispec",
               'route': "/evaalapi/openapispec.json",
               'rule_filter': lambda rule: True,  # all in
               'model_filter': lambda tag: True,  # all in
    }],
    'static_url_path': "/evaalapi/flasgger_static",
    'specs_route': "/evaalapi/apidocs/"
}
swagger_template = yaml.safe_load("""
info:
  title: EvaalAPI
  schemes:
   - https
  description: >
""" + apidescription + """
  contact:
   name: Francesco Potortì
   email: Potorti@isti.cnr.it
   url: http://fly.isti.cnr.it
  version: "1.0"
    ## The definition of Trialname is not conformant to OpenAPI 2.0: it should not
    ## contain the 'name' and 'in' properties, and references to it should be from
    ## inside a schema.  However, Swagger parses it right this way, and doing it
    ## the official way shows the description only in the Models section, not inside
    ## the endpoint description, which is annoying
definitions:
  Trialname:
    name: trialname
    in: path
    type: string
    description: >
      Trial name provided by IPIN.
        
      Competitors will individually receive one ore more scoring trial names.  Scoring trials
      are not reloadable, meaning they can be used only once and can not be restarted when finished.
      IPIN will also provide some test trials for testing and tuning which are reloadable
      and can be reset to nonstarted state by using the `reload` request.
        
      The `test` and `demo` trials, both reloadable, are always available.  The `test` trial
      can be used for quickly testing the interface and provides arbitrary strings as data, while
      the `demo` trial provides data from Logfiles/01-Training/01a-Regular/T03_02.txt taken
      from http://indoorloc.uji.es/ipin2020track3/files/logfiles2020.zip"""
)
try:
    from flasgger import Swagger, MK_SANITIZER
    Swagger(app, config=swagger_config, template=swagger_template, sanitizer=MK_SANITIZER)
except ImportError:
    pass


## This class is used inside a trial to provide logging services
class Triallog:
    sep = " ================ "          # predefined string for messages
    # trialname: name of the containing trial
    # logfn: name of the log file
    # logf: the stream object for the log file

    ## Constructor
    def __init__ (self, trial):
        self.trialname = trial['name']  # name of this trial
        self.logfn = trial['logfn']
        openflag = 'a' if trial['reloadable'] else 'x'
        try:
            logf = lzma.open(self.logfn, openflag) # log file descriptor
        except IOError:
            abort(Response("Cannot write log for " + self.trialname, 500))
        self.logf = logf

    ## Create date string
    def __date (self):
        ## ISO format with milliseconds: YYYY-MM-DDTHH:MM:SS.mmm
        return datetime.now(timezone.utc).strftime("%FT%H:%M:%S.%f")[0:-3]

    ## Low-level logger
    def __log (self, entrystr):
        if debug: print("logging entry: " + entrystr)
        try:
            self.logf.write(bytes(entrystr + '\n', 'ascii'))
        except IOError:
            abort(Response("Cannot log new entry for " + self.trialname, 500))

    ## CLose log file
    def close (self):
        if self.logf is not None:       # if file opened
            self.logf.close()

    ## Close and reopen log file to make sure it's flushed to disk
    def reopen (self):
        if self.logf is not None:       # if file opened
            self.logf.close()
            try:
                self.logf = lzma.open(self.logfn, 'a')
            except IOError:
                self.logf = None        # log file not opened
                abort(Response("Cannot reopen log for " + self.trialname, 500))

    ## Log functions
    def log_msg (self, msg):
        self.__log(self.__date() + self.sep + msg)

    def log_request (self):
        self.__log("%s <-- %s, %s%s" % (self.__date(), request.remote_addr,
                                       request.host, request.full_path))
        
    def log_response (self, trial, response):
        self.__log("%s --> %d, trialts=%.3f s=%.3f" %
                   (self.__date(), response.status_code, trial['trialts'], trial['s']))
        self.__log(response.data.decode('ascii'))

            
## Return the timestamp (first numeric field) of a data line
def get_line_timestamp (line, sep):
    fields = line.strip().split(sep)    # split on commas
    for field in fields:                # look at fields starting from first
        try:                            # is this a numeric field?
            datats = float(field)       # try to interpret it as a timestamp
        except ValueError:              # not a numeric field
            continue                    # try next one
        else:                           # yes, we found it
            return datats
    return None                         # no numeric field found

## Compute remaining time for a running trial
def remaining_time (trial):
    assert trial['state'] == 'running'
    return round(trial['p'] + trial['V']*trial['h'] + trial['s'] - time.time(), 3)

## Get the trial TRIALNAME from the trials dict, or create it from config files
def get_trial (trialname):

    ## If we don't know about trialname, read the init files and the test trial
    initrials = {}
    if trialname not in trials:
        trialinifn = trialsdir + trialname + ".yaml"
        if trialname in test_trial:
            if debug: conforigin = "internal test"
            initrials = test_trial
        elif os.path.isfile(trialinifn):
            if debug: conforigin = "file " + trialinifn
            with open(trialinifn, 'r') as inif:
                initrials = yaml.safe_load(inif)
        elif os.path.isfile(globinifn):
            if debug: conforigin = "file " + globinifn
            with open(globinifn, 'r') as inif:
                initrials = yaml.safe_load(inif)

        ## Create trials from initrials, set state
        for name in initrials:
            initrial = initrials[name]
            if name in trials:
                state = trials[name]['state']
                if state != 'nonstarted':
                    if debug: print(f"Trial {name} not reloaded from init file {globinifn} because it is {state}")
                    continue
            ## Create trial
            trials[name] = {'name': name,
                            'addr': set(), # remote addresses accessing this trial
                            'logfn': trialsdir + name + ".log.xz",
                            'estfn': trialsdir + name + ".est",
                            'state': 'nonstarted'}
            try:
                for k in (              # selected keys from initrial
                    'datafile',         # text data file
                    'commsep',          # comment-starting string in data file
                    'sepch',            # field separator in data file
                    'V',                # trial speed increase factor
                    'S',                # timeout slack time (seconds)
                    'inipos',           # initial position
                    'reloadable'        # not an scoring trial
                    ):
                    trials[name][k] = initrial[k]; # copy from initrial
                assert isinstance(initrial['datafile'], str)
                assert isinstance(initrial['commsep'], str)
                assert isinstance(initrial['sepch'], str)
                assert isinstance(initrial['V'], (int, float)) and initrial['V'] >= 1
                assert isinstance(initrial['S'], (int, float)) and initrial['S'] >= 0
                assert isinstance(initrial['inipos'], str)
                assert isinstance(initrial['reloadable'], bool)
            except:
                abort(Response(f"Bad config for trial {name}", 500))
            if debug: print(f"Trial {name} loaded from {conforigin}")
            
    ## Return the trial, if it exists
    if trialname in trials:
        trial = trials[trialname]
        trial['addr'].add(request.remote_addr)
        return trial                    # got and initialised it
    else:
        time.sleep(0.1)                 # slow down brute-force attacks
        return None

## Close trial: dump estimates, close log and data file descriptors
def close_trial (trial):
    if 'est' in trial:
        try:
            with open(trial['estfn'], 'w') as estf:
                estf.write("\n".join(trial['est']))
        except IOError:
            abort(Response("Cannot write estimates", 500))
    if 'dataf' in trial:
        trial['dataf'].close()          # close the data file
        del trial['dataf']
    if 'log' in trial:
        trial['log'].close()            # close the Triallog
        del trial['log']

## Read the doc file
def get_docs ():
    docfn = sourcedir+"evaalapi.md"
    with open(docfn, 'r') as docf:
        return docf.read()



################################################################
#### Documentation endpoints
################################################################

htmlhead = """<!DOCTYPE html><html lang=en><head><meta charset="utf-8"/>
<title>{title}</title><link rel="icon" href="{favicon}"/></head><body>
""".format(title = "EvaalAPI",
           favicon = "https://evaal.aaloa.org/templates/evaal2012/favicon.ico")                 

@app.route("/evaalapi/")
def root ():
    ## Remove indentation from apidescription, least it be rendered literally
    lines = apidescription.splitlines() # separate lines
    sl = [i.lstrip() for i in lines]    # remove leftmost spaces
    desc = "Revision " + revision
    desc += '\n'.join(sl)                # join stripped lines
    return htmlhead + markdown(desc)

@app.route("/evaalapi/evaalapi.html")
def doc_html ():
    return htmlhead + markdown(get_docs())

@app.route("/evaalapi/evaalapi.md")
def doc_md ():
    lexer = get_lexer_by_name("md", stripall=True)
    formatter = HtmlFormatter(full=True, title="evaalapi.md", linenos='table', wrapcode=True)
    return highlight(get_docs(), lexer, formatter)

@app.route("/evaalapi/"+source)
def python_source ():
    lexer = get_lexer_by_name("python", stripall=True)
    formatter = HtmlFormatter(full=True, title=source, linenos='table', wrapcode=True)
    with open(sourcedir+source, 'r') as sf:
        code = sf.read()
    #return sf.read(), {"Content-Type": "text/plain"}
    return highlight(code, lexer, formatter)

@app.route("/evaalapi/evaalapi.yaml")
def conf_source ():
    lexer = get_lexer_by_name("yaml", stripall=True)
    formatter = HtmlFormatter(full=True, title="evaalapy.yaml", linenos='table', wrapcode=True)
    with open(sourcedir+"evaalapi.yaml", 'r') as sf:
        code = sf.read()
    #return sf.read(), {"Content-Type": "text/yaml"}
    return highlight(code, lexer, formatter)

@app.route("/evaalapi/demo")
def demo_source ():
    lexer = get_lexer_by_name("python", stripall=True)
    formatter = HtmlFormatter(full=True, title="evaalapi demo", linenos='table', wrapcode=True)
    with open(sourcedir+"demo", 'r') as sf:
        code = sf.read()
    #return sf.read(), {"Content-Type": "text/x-python"}
    return highlight(code, lexer, formatter)

@app.route("/evaalapi/demo-auto.out")
def demo_out ():
    lexer = get_lexer_by_name("text", stripall=True)
    formatter = HtmlFormatter(full=True, title="evaalapi demo", linenos='table', wrapcode=True)
    with open(sourcedir+"demo-auto.out", 'r') as sf:
        code = sf.read()
    #return sf.read(), {"Content-Type": "text/plain"}
    return highlight(code, lexer, formatter)



################################################################
#### Management endpoints
################################################################

@app.route("/evaalapi/revision")
def server_revision ():
    return { 'start_time': starttime, 'revision': revision }
    """Return revision and start time of the server.

    ---
    produces: application/json
    responses:
      200:
        description: Returns server revision and start time.
        schema:
          type: string
          example: {"revision":"2.47","start_time":"2021-11-13T20:17:16"}

    """

@app.route("/evaalapi/status/<op>/<key>")
@app.route("/evaalapi/status/<key>", defaults={'op': 'complete'})
def trial_status (op, key):
    key = escape(key)
    try:
        with open(privatefn, 'r') as inif:
            ini = yaml.safe_load(inif)
        masterkey = ini['masterkey']
    except (IOError, KeyError):
        return "Cannot get global status", 500

    if key != masterkey:
        time.sleep(1)                   # slow down brute-force attacks
        return revision()

    ts = {}
            
    #ts[t] = {k: v for k, v in trials[t].items() if k not in ('log', 'dataf')}
    if op == 'complete':
        for t in trials:
            ts[t] = {}
            for k, v in trials[t].items():
                if k in ('log', 'dataf'):
                    pass
                elif k == 'addr':
                    ts[t][k] = list(v)
                else:
                    ts[t][k] = v

    elif op == 'state':
        ## Return state for each trial
        for t in trials:
            ts[t] = trials[t]['state']

    elif op == 'running':
        ## Return remaining time for running trials
        for t in trials:
            if trials[t]['state'] == 'running':
                ts[t] = remaining_time(trials[t])

    return ts



################################################################
#### EvaalAPI endpoints
################################################################

@app.route("/evaalapi/<trialname>/reload")
def trial_reload (trialname):
    """Reload trial data, set nonstarted state
    Returns trial state after reload, as with the `state` request in nonstarted state.

    Works for a test trial or a nonstarted scoring trial.  Does not work for a scoring
    trial which has generated a log.

    ---
    externalDocs:
      description: See the complete documentation.
      url: /evaalapi.html
    parameters:
      - $ref: "#/definitions/Trialname"
      - name: keeplog
        in: query
        allowEmptyValue: true
        description: If present, keeps log and appends to it; if absent, deletes the log.
        required: false
    produces: text/csv
    responses:
      200:
        description: Returns the state of the nonstarted trial.
        schema:
          type: string
          example: 0.000,-1.000,3.000,15.000,0.000,0.000,0.000,10.422057;43.718278;1
      404:
        description: The trial does not exist.
      405:
        description: Trial is unstarted, no estimates are there.
      422:
        description: Trial is not reloadable.

    """
    trialname = escape(trialname)

    ## Check for existence of trial
    trial = get_trial(trialname)
    if trial is None: return "Trial does not exist", 404

    if 'log' in trial:                  # trial not closed
        state = trial['state']
        assert state == 'running', state
        trial['log'].log_request()
        trial['log'].reopen()           # flush the compressed data

    logfn = trial['logfn']
    estfn = trial['estfn']
    logfnexists = os.path.exists(logfn)
    if debug: print(f"File {logfn} does{'' if logfnexists else ' not'} exist")

    if not trial['reloadable']:
        if logfnexists:
            return (f"Trial {trialname} is not reloadable", 422)
        else:
            ## A non-reloadable trial can be reloaded if it has not yet produced any logs
            ## This opens a way to restart a non reloadable trial by first removing the log file
            if debug: print(f"Reloading non-reloadable trial {trialname}")
    
    close_trial(trial)
    del trials[trialname]               # trial will be recreated by trial_state
    if 'keeplog' not in request.args:
        if logfnexists: os.remove(logfn)
        if os.path.exists(estfn): os.remove(estfn)
        
    ## trial_state calls get_trial, which recreates the trial from conf
    return trial_state(trialname)


################################################################

@app.route("/evaalapi/<trialname>/state")
def trial_state (trialname, dolog=True):
    """Get trial state
    Returns the trial state

    Useful to get the state of the trial for consistency checking and for getting
    the time remaining until timeout.
    ---
    externalDocs:
      description: See the complete documentation.
      url: /evaalapi.html
    parameters:
      - $ref: "#/definitions/Trialname"
    produces: text/csv
    responses:
      200:
        description: >
          Returns the state of the trial as a CSV line
            
          Returns a single data line in CSV format.  From Python, the returned data line can be parsed with
          `parse("{trialts:.3f},{rem:.3f},{V:.3f},{S:.3f},{p:.3f},{h:.3f},{pts:.3f},{pos}", line)`.
          See the complete documentation for the meaning of the returned values.          
        schema:
          type: string
          example: 0.000,-1.000,3.000,15.000,0.000,0.000,0.000,10.422057;43.718278;1
      404:
        description: The trial does not exist.
      405:
        description: Trial is unstarted, no estimates are there.
      422:
        description: This is not a reloadable trial.

    """
    trialname = escape(trialname)

    ## Check for existence of trial
    trial = get_trial(trialname)
    if trial is None: return "Trial does not exist", 404
    
    state = trial['state']
    V = trial['V']
    S = trial['S']

    if dolog and 'log' in trial:        # trial nonstarted or not closed
        trial['log'].log_request()

    if state == 'nonstarted':
        inipos = trial['inipos']
        data = statefmt.format(trialts=0,rem=-1,V=V,S=S,p=0,h=0,pts=0,pos=inipos)
        if debug: print(f"State is {state}: {data}")
        ## Do not log this response, because trial is not started
        return data, {"Content-Type": "text/csv"}
    
    p = trial['p']
    h = trial['h']
    s = trial['s']
    est = trial['est'][-1]
    if debug: print(f"Last estimate is: \"{est}\"")
    lastest = parse(estfmt, est)        # parse last estimate
    assert lastest is not None
    pts = lastest['pts']                # timestamp of last estimate
    pos = lastest['pos']                # position of last estimate
    if state == 'running':
        trialts = trial['trialts']
        rem = remaining_time(trial)     # time remaining until timeout
    elif state == 'finished':
        trialts = -1
        rem = 0
    elif state == 'timeout':
        trialts = -1
        rem = s
    else:
        assert False, f"Unknown state '{state}'"

    data = statefmt.format(trialts=trialts,rem=rem,V=V,S=S,p=p,h=h,pts=pts,pos=pos)
    r = Response(data, 200, {"Content-Type": "text/csv"})

    if dolog and 'log' in trial:        # trial nonstarted or not closed
        trial['log'].log_response(trial, r)
    if debug: print(f"State is {state}: {data}")
    return r


################################################################
    
@app.route("/evaalapi/<trialname>/nextdata")
def trial_nextdata (trialname):
    """Get data and set estimated position

    This is the main endpoint.
    For an scoring trial, this is the only necessary request to use.
    At each request, the competitor asks for next data, starting
    from the current trial timestamp
    through the requested `horizon` (default 0.5 seconds).  With the same request,
    the competitor can set a `position` estimate relative to the current timestamp.

    If the trial has not started yet, starts it and make it running.  If the trial is
    not finished, return the data for the requested `horizon`.
    If the trial is finished, returns the status as with the `state` request.

    See the complete documentation for detail and timeout management.
    ---
    externalDocs:
      description: See the complete documentation.
      url: /evaalapi.html
    parameters:
      - $ref: "#/definitions/Trialname"
      - name: horizon
        in: query
        type: float
        allowEmptyValue: true
        description: A non-negative number of seconds.
        required: false
        default: 0.5
        minimum: 0
        maximum: 3e10
      - name: position
        in: query
        type: string
        description: Estimated position at current trial timestamp. Format is trial-dependent.
        required: false
        default: none
    produces: text/csv
    responses:
      200:
        description: Data lines in the requested time horizon.
        schema:
          type: string
          example: |
            ACCE;0.015;17287.341;-2.64559;8.69813;2.78446;3
            GYRO;0.016;17287.337;0.27428;-1.10933;-0.18998;3
            MAGN;0.016;17287.342;7.56000;-26.28000;-39.84000;3
            GYRO;0.016;17287.342;0.17776;-1.35429;-0.19364;3
            ACCE;0.017;17287.346;-2.82995;8.87052;2.36547;3
            AHRS;0.017;17287.346;51.891617;31.498898;-54.021839;0.48601067;0.02620481;-0.28725025;3
            ACCE;0.017;17287.351;-2.96402;9.06684;2.32716;3
      404:
        description: The trial does not exist.
      405:
        description: >
          Trial has finished, whether normally or because of a timeout;
          the position estimate is ignored; the trial timestamp does not change.
          Returns the trial state, as with the `state` request in nonstarted state.
        schema:
          type: string
          example: -1.000,0.000,1.000,2.000,1630239724.795,66.000,7.600,10.422059,43.718278,1
      422:
        description: >
          Some parameters do not conform to the constraints;
          the position estimate is ignored;
          the trial timestamp does not change; no data are returned.

    """
    trialname = escape(trialname)

    ## Get already created trial or create it from conf
    trial = get_trial(trialname)
    if trial is None: return "Trial does not exist", 404

    ## Get state
    state = trial['state']
    assert state in states, state

    ## Log trial creation before logging request
    if state == 'nonstarted':
        log = trial['log'] = Triallog(trial) # first, init log
        r = trial_state(trialname, dolog=False)
        ## When nonstarted, trial_state returns a tuple rather than a Response
        ## so we do not use trial_state(trialname)[0].data.decode('ascii')
        log.log_msg(f"Creating trial '{trialname}'\nstate: {trial_state(trialname, dolog=False)[0]}")

    ## The Triallog is not there if the trial is closed
    if 'log' in trial:
        assert state in ('nonstarted', 'running'), state
        log = trial['log']
        log.log_request()
    else:
        assert state in ('finished', 'timeout'), state
        log = None

    ################################################################
    ## Return trial state if trial is finished
    if state in ('finished', 'timeout'):
        if debug: print(f"State is {state}")
        return trial_state(trialname, dolog=False), 405

    ################################################################
    ## Start the trial if not started
    if state in ('nonstarted'):
        if debug: print(f"State is {state}")
        
        # Open data file
        datafn = trialsdir + trial['datafile']
        linets = None
        try:
            dataf = open(datafn, 'r')
            ## Skip initial headers, comments and empty lines
            while linets is None:       # a line without a numeric field
                line = next(dataf)
                if line.lstrip().startswith(trial['commsep']):
                    continue            # skip comments
                linets = get_line_timestamp(line, trial['sepch'])
        except (IOError, StopIteration):
            return "Cannot open data file for " + trialname, 500

        ## Advance the state, set the remaining trial variables
        state = 'running'           # start the trial
        S = trial['S']
        inipos = trial['inipos']
        iniest = [esthdr,               # first line is header
                  estfmt.format(        # initial estimate
                      pts = 0,          # estimation timestamp
                      c = 0,            # estimation wall clock
                      h = 0,            # horizon requested at estimation
                      s = S,            # slack time at estimation
                      pos = inipos      # position estimate
                  )]
        trial.update({'state': state,   # running
                      'trialts': 0,     # trial timestamp, inited to 0              
                      'p': 0,           # wall clock of previous step             
                      'h': 0,           # horizon of previous step                
                      's': S,           # timeout slack (inited to S)                 
                      'line': line,     # first data line
                      'dataf': dataf,   # data file descriptor
                      'est': iniest     # initial estimate
                      })

    ################################################################
    ## Running state: advance to next horizon
    assert state == 'running'
    if debug: print(f"State is {state}")
    V = trial['V']
    S = trial['S']
    trialts = trial['trialts']
    p = trial['p']
    h = trial['h']
    s = trial['s']
    dataf = trial['dataf']
    sepch = trial['sepch']
    line = trial['line']

    ## Check for timeout
    assert s >= 0 and s <= S
    c = time.time()
    if p > 0:                           # p == 0 for first state request call
        ## Force not-faster-than-real-time behaviour
        # if V > 1 and (c-p) < h:          # client is going too fast
        #     time.sleep(h-(c-p))         # slow down to real time
        s += V*h - (c-p)                # remaining time
        if s > S:                       # remaining greater than slack time
            s = S
        elif s < 0:                     # no remaining time
            state = 'timeout'

    ## Check for finished trial
    if line is None:                    # no more data lines
        state = 'finished'

    ## Unless timed out, read horizon and update the trial estimates
    if state in ('running', 'finished'):

        ## Read and validate horizon
        if 'horizon' in request.args:
            horizon = request.args['horizon']
            try:
                h = float(horizon)
            except ValueError:
                r = Response(f"Horizon value not a number: \"{horizon}\" -- request ignored", 422)
                log.log_response(trial, r); return r
            if h < 0 or h == float('inf'): # negative or infinite
                r = Response(f"Horizon negative or inf: \"{horizon}\" -- request ignored", 422)
                log.log_response(trial, r); return r
        else:
            h = 0.5                     # default horizon (seconds)

        ## Update the trial estimates
        if (trialts > 0                 # position at 0 is the initial position
            and 'position' in request.args):

            pos = request.args['position']
            estimate = estfmt.format(
                pts = trialts,          # estimation timestamp
                c = c,                  # estimation wall clock
                h = h,                  # horizon requested at estimation
                s = s,                  # slack time at estimation
                pos = pos               # position estimate
            )
            if debug: print(f"Estimate is: {estimate}")
            trial['est'].append(estimate) # append to list of estimates

    ## If trial finished, stop here
    if state in ('finished', 'timeout'):
        trial['s'] = s                  # save slack time
        trial['state'] = state          # set finished or timeout state
        r = trial_state(trialname, dolog=False)
        r.status_code = 405;
        log.log_response(trial, r);
        log.log_msg(f"Trial finished {'normally' if state == 'finished' else 'by timeout'}\n")
        close_trial(trial)
        return r

    ## Get data for the requested horizon
    assert state == 'running'
    linets = get_line_timestamp(line, sepch)
    assert linets is not None
    assert linets >= trialts
    data = ""                           # data to return
    datats = 0                          # last data timestamp
    while linets < trialts+h:           # while line in horizon
        data += line                    # append line to data
        datats = linets                 # only used for consistency check
        try:
            line = next(dataf)          # read next line of data file
        except IOError:
            r = Response("IO error while reading data file for " + trialname, 500)
            log.log_response(trial, r); return r
        except StopIteration:           # no more lines in data file
            line = None
            break
        ## Get new line timestamp and check it
        linets = get_line_timestamp(line, sepch)
        if linets is None:
            r = Response("No timestamp in data file for " + trialname, 500)
            log.log_response(trial, r); return r
        if linets < datats:             # invalid timestamp
            r = Response("Decreasing timestamp in data file for " + trialname, 500)
            log.log_response(trial, r); return r
    if debug:
        nlc = data.count('\n')
        print(f"Read {nlc} lines from data file for trial {trialname}")

    ## Advance trial timestamp, update trial variables
    trial.update({'state': state,
                  'trialts': trialts + h, # trial timestamp
                  'p': c,                 # wall clock of last step             
                  'h': h,                 # horizon of last step                
                  's': s,                 # timeout slack
                  'line': line,           # first line beyond horizon
                  })

    ## Return data in the requested horizon
    if debug:
        nlc = data.count('\n')
        print(f"Sending {nlc} lines of data")
    r = Response(data, 200, {"Content-Type": "text/csv"});
    log.log_response(trial, r);
    return r


################################################################

@app.route("/evaalapi/<trialname>/estimates")
def trial_estimates (trialname):
    """Get estimates
    Returns a list of the estimates in CSV format with header.

    An estimate is an estimated position sent via the `nextdata` request preceded by
    timestamp and other relevant parameters.  Right after starting the trial, a
    header and the initial estimate are returned.  While running and after finished,
    all the estimates sent so far via the `nextdata` request are returned, one per
    data line.
    ---
    externalDocs:
      description: See the complete documentation.
      url: /evaalapi.html
    parameters:
      - $ref: "#/definitions/Trialname"
    produces: text/csv
    responses:
      200:
        description: >
          CSV list of estimates.
            
          The first line of data is a header.  Subsequent lines all have the same format.
          From Python, a data line after the header can be parsed with
          `parse("{pts:.3f},{c:.3f},{h:.3f},{s:.3f},{pos}", line)`.
          See the complete documentation for the meaning of the returned values.          
        schema:
          type: string
          example: |
            pts,c,h,s,pos
            0.000,0.000,0.000,15.000,10.422057;43.718278;1
            1.000,1630165968.001,0.500,7.475,10.422059;43.718278;1
            1.500,1630165969.517,0.500,7.459,10.422063;43.718270;1
            2.000,1630165971.013,0.500,7.462,10.422071;43.718268;1
            2.500,1630165972.516,0.500,7.460,10.422072;43.718267;1
            3.000,1630165974.023,5.000,7.452,10.422069;43.718266;1
      404:
        description: The trial does not exist.
      405:
        description: Trial is unstarted, no estimates are there.

    """
    trialname = escape(trialname)

    ## Check for existence of trial
    trial = get_trial(trialname)
    if trial is None: return "Trial does not exist", 404

    ## If unstarted, no estimates exist
    if trial['state'] == 'nonstarted':
        return "Trial is not started", 405
    
    data = "\n".join(trial['est'])      # estimates separated by newlines
    estfn = trial['estfn']
    return data, {"Content-Type": "text/csv",
                  "Content-Disposition": "attachment; filename=" + estfn}


################################################################

@app.route("/evaalapi/<trialname>/log")
def trial_log (trialname):
    """Get the trial log
    Returns the log in text format of timestamped `state` and `nextdata`
    requests and responses.  `state` requests are logged only while the trial is running,
    that is after the first `nextdata` request and before it is finished.

    The log file exists only after the trial has started, which happens after the first
    `nextdata` request.  It is deleted after a successful `reload` request, unless used 
    with the `keeplog` parameter.
    ---
    externalDocs:
      description: See the complete documentation.
      url: /evaalapi.html
    parameters:
      - $ref: "#/definitions/Trialname"
      - name: xzcompr
        in: query
        allowEmptyValue: true
        description: If present, log is returned in xz compressed form.
        required: false
    produces: text/plain or application/x-xz
    responses:
      200:
        description: The complete trial log.
        schema:
          type: string
          example: |
            2021-08-28T15:52:37.455 ================ Creating trial 'test'
            state: 0.000,-1.000,3.000,15.000,0.000,0.000,0.000,10.422057;43.718278;1
            2021-08-28T15:52:37.456 <-- evaal.aaloa.org/evaalapi/demo/nextdata?horizon=1
            2021-08-28T15:52:37.516 --> 200, trialts=1.000 s=15.000
            ACCE;0.015;17287.341;-2.64559;8.69813;2.78446;3
            GYRO;0.016;17287.337;0.27428;-1.10933;-0.18998;3
            MAGN;0.016;17287.342;7.56000;-26.28000;-39.84000;3
      404:
        description: The trial does not exist.
      405:
        description: The trial log does not exist because the trial has not started.

    """
    trialname = escape(trialname)

    ## Check for existence of trial
    trial = get_trial(trialname)
    if trial is None: return "Trial does not exist", 404

    logfn = trial['logfn']
    if not os.path.exists(logfn):
        return "Log file does not exist", 405

    if 'log' in trial:                  # log file is still open
        if debug: print(f"Reopening log file {logfn}")
        trial['log'].reopen()           # flush the compressed data

    ## Send the log
    try:
        if 'xzcompr' in request.args:
            ## Send the log file as it is
            return send_file(logfn)
        else:
            ## Create a byte stream and close the file before returning
            # with lzma.open(logfn, 'r') as logf:
            #     stream = io.BytesIO(logf.read())
            ## Send the log decompressed the easy (and hopefully correct) way
            stream = lzma.open(logfn, 'r')
            return send_file(stream, mimetype="text/plain", as_attachment=True,
                             attachment_filename=trialname+".log")
    except IOError:
        return "Cannot read log file for " + trialname, 500


## Main code
if os.path.isfile(guisource):
    with open(guisource, 'r') as guif:
        exec(compile(guif.read()+'\n', guisource, 'exec'))
    extrafiles = [guisource]
else:
    extrafiles = []

if __name__ == "__main__":
    app.run(extrafiles)
    

# Local Variables:
# comment-column: 40
# fill-column: 100
# End: