Skip to content

Commit

Permalink
Merge pull request #140 from unarxiv/dashboard
Browse files Browse the repository at this point in the history
Dashboard
  • Loading branch information
Xiaozhe Yao committed Dec 3, 2018
2 parents 8769a75 + d03e7fd commit 5a1b544
Show file tree
Hide file tree
Showing 31 changed files with 729 additions and 56 deletions.
15 changes: 15 additions & 0 deletions cli/config.go
Expand Up @@ -103,4 +103,19 @@ func validateConfig() {
log.Fatal(err)
}
}
// check if system log file exists
cvpmLogPath := filepath.Join(cvpmPath, "system.log")
exist, err := isPathExists(cvpmLogPath)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Fatal(err)
}
if !exist {
f, err := os.Create(cvpmLogPath)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Fatal(err)
}
defer f.Close()
}
}
168 changes: 160 additions & 8 deletions cli/shell.go
@@ -1,16 +1,37 @@
package main

import (
"bufio"
"fmt"
"github.com/getsentry/raven-go"
"io"
"log"
"os"
"os/exec"
"sync"
"time"
)

func pip(args []string) {
config := readConfig()
localPip := config.Local.Pip
_ = _execCommand(localPip, args)
// _ = _execCommand(localPip, args)
proc := NewProcess(localPip, args...)
out := proc.StreamOutput()
file, err := os.OpenFile("system.log", os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
go func() {
for out.Scan() {
_, err := file.WriteString(out.Text() + "\n")
if err != nil {
log.Fatalf("failed writing to file: %s", err)
}
defer file.Close()
log.Println(out.Text())
}
}()
proc.Start()
}

func python(args []string) {
Expand All @@ -21,13 +42,144 @@ func python(args []string) {

func _execCommand(commandName string, params []string) bool {
cmd := exec.Command(commandName, params...)
cmd.Stdout = os.Stdout
fmt.Println(cmd.Args)
output, err := cmd.CombinedOutput()
return true
}

type Process struct {
proc *exec.Cmd
cancellationSignal chan uint8
done chan error
returnCode chan error
started bool
stdOutRead *io.PipeReader
stdOutWrite *io.PipeWriter
inputWriter *io.PipeWriter
inputStreamSet bool
outputStreamSet bool
completed bool
timeout time.Duration
// Access to completed MUST capture the lock.
mutex sync.RWMutex
}

func NewProcess(command string, args ...string) *Process {
process := &Process{
exec.Command(command, args...),
make(chan uint8, 1),
make(chan error, 1),
make(chan error, 1),
false,
&io.PipeReader{},
&io.PipeWriter{},
&io.PipeWriter{},
false,
false,
false,
0,
sync.RWMutex{}}
return process
}

func (p *Process) Start() *Process {
if p.timeout > 0 {
log.Println("Its greater than 0")
timer := time.NewTimer(p.timeout)
go func() {
<-timer.C
p.Kill()
}()
}
p.started = true
//Call the other functions to stream stdin and stdout
err := p.proc.Start()
if err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Fatal(err)
return false
panic(err)
}
log.Printf(string(output))
return true
go p.awaitOutput()
go p.finishTimeOutOrDie()
return p
}

func (p *Process) SetTimeout(d time.Duration) {
if p.started {
panic("Can not set timeout after process started")
}
p.timeout = d
}

func (p *Process) Wait() error {
return <-p.returnCode
}

func (p *Process) awaitOutput() {
//send the exit code to the done channel to signify the command finished
p.done <- p.proc.Wait()
}

func (p *Process) Kill() {
p.mutex.Lock()
if !p.completed {
p.cancellationSignal <- 1
}
p.mutex.Unlock()
}

func (p *Process) OpenInputStream() (io.WriteCloser, error) {
if p.inputStreamSet {
panic("Input stream already set")
}
if p.started {
panic("process already started")
}
stdIn, err := p.proc.StdinPipe()
p.inputStreamSet = true
return stdIn, err

}
func (p *Process) StreamOutput() *bufio.Scanner {
//pipe both stdout and stderr into the same pipe
//panics if you do streamoutput after proccess starting or
//if the output stream is already set
if p.started {
panic("Cant set output stream after starting")
}
if p.outputStreamSet {
panic("output stream already set!")
}
p.stdOutRead, p.stdOutWrite = io.Pipe()
p.proc.Stdout = p.stdOutWrite
p.proc.Stderr = p.stdOutWrite
p.outputStreamSet = true
//return a scanner which they can read from till empty
return bufio.NewScanner(p.stdOutRead)
}

func (p *Process) finishTimeOutOrDie() {
defer p.cleanup()
var result error
select {
case result = <-p.done:
case <-p.cancellationSignal:
log.Println("received cancellationSignal")
//NOT PORTABLE TO WINDOWS
err := p.proc.Process.Kill()
if err != nil {
log.Println(err)
}
}
p.returnCode <- result
}

func (p *Process) cleanup() {
p.mutex.Lock()
p.completed = true
p.mutex.Unlock()
if p.outputStreamSet {
p.stdOutRead.Close()
p.stdOutWrite.Close()
}
close(p.done)
close(p.cancellationSignal)
}
3 changes: 0 additions & 3 deletions cli/user.go
Expand Up @@ -21,7 +21,6 @@ type User struct {
}

func (u *User) login() User {
fmt.Println(u.Username)
csrp, _ := cognitosrp.NewCognitoSRP(u.Username, u.Password, "us-east-1_IYJ3FvCKZ", "1jinmsd412vcs8pkhqg5u0gjd2", nil)
cfg, _ := external.LoadDefaultAWSConfig()
cfg.Region = endpoints.UsEast1RegionID
Expand All @@ -33,8 +32,6 @@ func (u *User) login() User {
AuthParameters: csrp.GetAuthParams(),
})
resp, _ := req.Send()
fmt.Println("123456")
fmt.Println(resp.ChallengeName)
if resp.ChallengeName == cip.ChallengeNameTypePasswordVerifier {
challengeInput, _ := csrp.PasswordVerifierChallenge(resp.ChallengeParameters, time.Now())
chal := svc.RespondToAuthChallengeRequest(challengeInput)
Expand Down
16 changes: 16 additions & 0 deletions cli/utils.go
@@ -0,0 +1,16 @@
package main

import (
"os"
)

func isExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
10 changes: 6 additions & 4 deletions dashboard/package.json
Expand Up @@ -14,10 +14,12 @@
"build": "node build/build.js"
},
"dependencies": {
"axios": "0.18.0",
"vue": "2.5.17",
"vue-router": "3.0.2",
"vuetify": "1.3.11"
"axios": "^0.18.0",
"vue": "^2.5.2",
"vue-i18n": "^8.3.2",
"vue-markdown": "^2.2.4",
"vue-router": "^3.0.1",
"vuetify": "^1.0.0"
},
"devDependencies": {
"autoprefixer": "7.2.6",
Expand Down
Empty file.
59 changes: 59 additions & 0 deletions dashboard/src/components/CVPM-News.vue
@@ -0,0 +1,59 @@
<template>
<v-card class="cvpm-news-card">
<v-card-title primary-title>
<h2>Latest News</h2>
</v-card-title>
<v-list two-line dense>
<template v-for="(item, index) in news">
<v-card-title primary-title v-if="item.title" :key="index">
<h4><a :href=item.url target="_blank">{{ item.title }} </a></h4>
</v-card-title>
<v-list-tile-content :key="item.id" class="cvpm-news-content">
<vue-markdown>{{item.body}}</vue-markdown>
</v-list-tile-content>
</template>
</v-list>
</v-card>
</template>

<script>
import { discovery } from '@/services/discovery'
import VueMarkdown from 'vue-markdown'
export default {
data: () => ({
news: []
}),
methods: {
getNews () {
let self = this
discovery.fetchNews().then(function (res) {
self.news = res.data.map(function (each) {
each.title = each.body.split('\n\n')[0]
each.url = 'https://write.as/autoai/' + each.slug
if (each.body.length > 300) {
each.body = each.body.substring(0, 300) + '...'
}
return each
})
console.log(self.news)
})
}
},
created () {
this.getNews()
},
components: {
'vue-markdown': VueMarkdown
}
}
</script>

<style scoped>
.cvpm-news-content {
padding-left: 2em;
padding-right: 1em;
}
.cvpm-news-card {
width: 90%;
}
</style>
42 changes: 42 additions & 0 deletions dashboard/src/components/CVPM-Status.vue
@@ -0,0 +1,42 @@
<template>
<v-card class="cvpm-status-card">
<v-card-title primary-title>
<h2>Status</h2>
</v-card-title>
<v-card-text >
<p class="cvpm-status-content">Installed: {{status.installed}}</p>
<p class="cvpm-status-content">Running: {{status.running}} </p>
<p class="cvpm-status-content">System Status: {{status.status}}</p>
</v-card-text>
</v-card>
</template>

<script>
import { systemService } from '@/services/system'
export default {
data: () => ({
status: {}
}),
methods: {
getStatus () {
this.status = systemService.getStatus()
console.log(this.status)
}
},
created () {
this.getStatus()
},
components: {
}
}
</script>

<style scoped>
.cvpm-status-card {
width: 95%;
}
.cvpm-status-content {
padding-left: 2em;
padding-right: 1em;
}
</style>
25 changes: 25 additions & 0 deletions dashboard/src/i18n/config.js
@@ -0,0 +1,25 @@
import i18n from '@/i18n'
import map from '@/i18n/map'

function setLang (lang) {
localStorage.setItem('lang', lang)
console.log(map[lang])
i18n.locale = map[lang]
}

function loadDefautlLang (lang) {
if (localStorage.getItem('lang') === null) {
localStorage.setItem('lang', 'English')
}
i18n.locale = map[localStorage.getItem('lang')]
}

function getLang () {
return localStorage.getItem('lang')
}

export {
getLang,
loadDefautlLang,
setLang
}
1 change: 1 addition & 0 deletions dashboard/src/i18n/en-US.json
@@ -0,0 +1 @@
{}

0 comments on commit 5a1b544

Please sign in to comment.