-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial version of the user interface
- Loading branch information
Showing
9 changed files
with
394 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"runtime" | ||
"syscall" | ||
) | ||
|
||
func getRootPaths() (drives []string) { | ||
switch runtime.GOOS { | ||
case "windows": | ||
kernel32, _ := syscall.LoadLibrary("kernel32.dll") | ||
getLogicalDrivesHandle, _ := syscall.GetProcAddress(kernel32, "GetLogicalDrives") | ||
|
||
if ret, _, callErr := syscall.Syscall(uintptr(getLogicalDrivesHandle), 0, 0, 0, 0); callErr != 0 { | ||
drives = append(drives, "C:") | ||
} else { | ||
drives = bitsToDrives(uint32(ret)) | ||
} | ||
default: | ||
drives = append(drives, subdirectoriesOf("/")...) | ||
} | ||
return | ||
} | ||
|
||
func bitsToDrives(bitMap uint32) (drives []string) { | ||
availableDrives := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} | ||
|
||
for i := range availableDrives { | ||
if bitMap&1 == 1 { | ||
drives = append(drives, availableDrives[i]+":") | ||
} | ||
bitMap >>= 1 | ||
} | ||
|
||
return | ||
} | ||
|
||
type Explorer struct { | ||
} | ||
|
||
// func (fe *Explorer) GetDirectoryContents(path string) ([]FileDescriptor, error) { | ||
// var dirs []FileDescriptor | ||
// if len(path) == 0 { | ||
// for _, v := range getRootPaths() { | ||
// dirs = append(dirs, FileDescriptor{Path: v, IsDir: true}) | ||
// } | ||
// } else { | ||
// files, err := ioutil.ReadDir(path) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// for _, f := range files { | ||
// if f.IsDir() { | ||
// dirs = append(dirs, FileDescriptor{BasePath: path, Path: f.Name(), IsDir: true}) | ||
// } | ||
// } | ||
// } | ||
// return dirs, nil | ||
// } | ||
|
||
func (fe *Explorer) GetDirectory(path string) (dirs []Directory) { | ||
var subDirs []string | ||
if len(path) == 0 { | ||
subDirs = getRootPaths() | ||
} else { | ||
subDirs = subdirectoriesOf(path) | ||
} | ||
for _, v := range subDirs { | ||
dirs = append(dirs, Directory{Parent: path, Name: v, SubDirectories: subdirectoriesOf(path + v + "/")}) | ||
} | ||
return dirs | ||
} | ||
|
||
func subdirectoriesOf(path string) []string { | ||
dirs := make([]string, 0) | ||
fmt.Printf("Looking for directories under %v\n", path) | ||
files, err := ioutil.ReadDir(filepath.Clean(path)) | ||
if err != nil { | ||
fmt.Printf("Error occured %v\n", err) | ||
return dirs | ||
} | ||
for _, f := range files { | ||
if f.IsDir() { | ||
dirs = append(dirs, f.Name()) | ||
} | ||
} | ||
return dirs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package file | ||
|
||
type FileDescriptor struct { | ||
BasePath string `json:"basePath"` | ||
Path string `json:"path"` | ||
IsDir bool `json:"isDir"` | ||
} | ||
|
||
type Service interface { | ||
GetDirectory(dirPath string) []Directory | ||
} | ||
|
||
type Directory struct { | ||
Parent string `json:"parent"` | ||
Name string `json:"name"` | ||
SubDirectories []string `json:"subdirectories"` | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
rice "github.com/GeertJohan/go.rice" | ||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
|
||
"github.com/ocrease/vboxanalyser/pkg/file" | ||
"github.com/ocrease/vboxanalyser/pkg/vbo" | ||
) | ||
|
||
type Server struct { | ||
router *mux.Router | ||
fs file.Service | ||
analyser *vbo.Analyser | ||
} | ||
|
||
func NewServer() *Server { | ||
s := Server{router: mux.NewRouter(), fs: new(file.Explorer), analyser: new(vbo.Analyser)} | ||
|
||
s.router.HandleFunc("/api/directory", s.directoryList).Methods("GET") | ||
s.router.HandleFunc("/api/analyse", s.analyseDirectory).Methods("GET") | ||
s.router.PathPrefix("/").Handler(http.FileServer(rice.MustFindBox("../../ui").HTTPBox())) | ||
|
||
return &s | ||
} | ||
|
||
func (s *Server) Start() { | ||
log.Println("Listening on port 8080") | ||
log.Fatal(http.ListenAndServe(":8080", handlers.CORS()(s.router))) | ||
} | ||
|
||
func (s *Server) directoryList(w http.ResponseWriter, r *http.Request) { | ||
path := r.FormValue("path") | ||
json.NewEncoder(w).Encode(s.fs.GetDirectory(path)) | ||
} | ||
|
||
func (s *Server) analyseDirectory(w http.ResponseWriter, r *http.Request) { | ||
path := r.FormValue("path") | ||
json.NewEncoder(w).Encode(s.analyser.AnalyseDirectory(path)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package vbo | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Analyser struct{} | ||
|
||
const VboxExtension = ".vbo" | ||
|
||
func (a *Analyser) AnalyseDirectory(path string) []FileSummary { | ||
summaries := make([]FileSummary, 0) | ||
fmt.Printf("Analysing .vbo files in: %v\n", path) | ||
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() { | ||
|
||
if filepath.Ext(path) == VboxExtension { | ||
fmt.Printf("Analysing %v\n", path) | ||
|
||
file := ParseFile(path) | ||
rpm, err := file.MaxValue("LOT_Engine_Spd") | ||
if err != nil { | ||
rpm = 0 | ||
} | ||
vel, err := file.MaxValue("velocity") | ||
if err != nil { | ||
vel = 0 | ||
} | ||
summaries = append(summaries, FileSummary{Path: path, NumLaps: NumLaps(&file), MaxVelocity: vel, MaxRpm: rpm}) | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
fmt.Printf("Error processing directory %v - %v\n", path, err) | ||
} | ||
fmt.Printf("Analysing %v .vbo files in: %v\n", len(summaries), path) | ||
return summaries | ||
} | ||
|
||
// func createFileProcessor(summaries *[]FileSummary) func(string, os.FileInfo, error) error { | ||
// return func(path string, info os.FileInfo, err error) error { | ||
// if err != nil { | ||
// return err | ||
// } | ||
// if !info.IsDir() { | ||
|
||
// if filepath.Ext(path) == VboxExtension { | ||
// fmt.Printf("Analysing %v\n", path) | ||
|
||
// file := ParseFile(path) | ||
// rpm, err := file.MaxValue("rpm") | ||
// if err != nil { | ||
// rpm = 0 | ||
// } | ||
// vel, err := file.MaxValue("velocity") | ||
// if err != nil { | ||
// vel = 0 | ||
// } | ||
// summaries = append(summaries, FileSummary{Path: path, NumLaps: NumLaps(&file), MaxVelocity: vel, MaxRpm: rpm}) | ||
// } | ||
// } | ||
// return nil | ||
// } | ||
// } | ||
|
||
type FileSummary struct { | ||
Path string `json:"path"` | ||
NumLaps uint32 `json:"numlaps"` | ||
MaxVelocity float64 `json:"maxvelocity"` | ||
MaxRpm float64 `json:"maxrpm"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.