
#
# NoPi generates around 2,500 PNG files per run (this number varies based on the signals
# analyzed, and satellites tracked). We analyze over 50 baselines each day as part of the
# RTX Network analysis. We've been doing this for around 2 years (and plan to continue).
# This has generated on the order of:
#
# 2 * 365 * 50 * 2,500 = 91,250,000 files!
#
# It is more efficient to store all data for a station/day in a single zip file (reducing 
# the number of files by a factor of ~2,500). However, without JS or backend code, a
# way page can't extract data from a zip file. This script gets around this. It is 
# designed to run on meson where the NoPi data is located. You can then access a file as follows:
#
# http://meson.eng.trimble.com:8888/IL2IN/2020/01/276.zip/NoPiDI_Top.html
#
# This gets the data for the "IL2IN" baseline, in 2020, from station "01" on day 276. It reads
# "NoPiDI_Top.html" from "276.zip" and returns the html to the browser. The browser will then 
# request js and png files all relative to the zip file. This script extracts the requested 
# data in memory from the zip and sends it to the browser. No change of the HTML from NoPi is 
# required! Just a zipped version of the web/ plots/ and NoPiDI_Top.html file (all in the same
# zip with the web/ and plots/ directory preserved.
#
# We expect (but don't enforce) that this script is exeucted from meson
#
# Copyright 2022 Trimble Inc.
#

from flask import Flask, make_response, send_from_directory
import zipfile
import os

webPort = 8888
app = Flask(__name__)

# Serve the favicon
@app.route('/favicon.ico')
def favicon():
  return send_from_directory(os.path.join(app.root_path, 'static'),
                             'favicon.ico', mimetype='image/vnd.microsoft.icon')


@app.route("/<string:linetype>/<int:year>/<string:station>/<string:nopiZip>/<path:filepath>")
def getZipFile(linetype,year,station,nopiZip,filepath):
  # The Trimble logo is the same for each run of NoPi, no need to save a 
  # copy in each zip file. Instead, this app has a copy
  if(filepath == 'TRMBlogoMed.png'):
    return send_from_directory(os.path.join(app.root_path, 'static'),
                             'TRMBlogoMed.png', mimetype='image/png')
  else:
    # Build the full path to the file
    datafile  = '/mnt/data_drive/TerrasatCycleSlips/NoPiAnalysis/Results/' + linetype + '/'
    datafile += str(year) + '/'
    datafile += station + '/'
    datafile += nopiZip

    # Get the requested file from the zip file and load into a memory buffer
    with zipfile.ZipFile(datafile) as z:
      filebuffer = z.read(filepath)
    
    # Generate the response. By default, the content type will be html. We also
    # have JS & PNG files, set the appropriate content type.
    response = make_response(filebuffer)
    if(filepath.endswith('png')):
      response.headers.set('Content-Type', 'image/png')
    elif(filepath.endswith('js')):
      response.headers.set('Content-Type', 'text/javascript; charset=utf-8')

    return response

if __name__ == "__main__":
  app.run(host='0.0.0.0',port=webPort,threaded=True)


