Example #1
0
func SetupDatabase(wf *web.WebFace, db *database.StatTrackerDB) {
	webBuf := bytes.NewBufferString("Starting Server")
	fileCounter := 0
	numFiles := 0

	outBuf := io.MultiWriter(webBuf, os.Stdout)

	fmt.Fprintf(outBuf, "Hosting on %s \n", *addr)

	wf.RedirectHandler = func(rw http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(rw, "Files Processed: %4d/%d \n", fileCounter, numFiles)
		fmt.Fprint(rw, webBuf)
	}

	fmt.Fprintln(outBuf, "Fetching File List")

	cPage := make(chan int)

	var driveFilelist []*drive.File
	go func() {
		var errDrv error
		driveFilelist, errDrv = google.AllFiles("mimeType = 'application/vnd.google-apps.document'", cPage)
		if errDrv != nil {
			log.Fatalln("File List Error:", errDrv)
		}
	}()

	for i := <-cPage; i != -1; i = <-cPage {
		fmt.Fprintf(outBuf, "Getting Page: %d \n", i)
	}

	// Handle per file
	docStatList := []*stat.DocStat{}
	numFiles = len(driveFilelist)
	for ifile, file := range driveFilelist {
		fileCounter = ifile

		db.WriteFile(file)
		dStat := FilePullCalc(file, db)

		db.WriteFileStats(dStat)

		fmt.Fprintf(outBuf, "Stats File Generated: %s... %s %s\n", file.Id[:6], dStat.LastMod[:10], file.Title)
		docStatList = append(docStatList, dStat)
	}

	// Generate Daily Stat
	dates := stat.CreateDailyUserStat(docStatList)

	// Slower but good test (and get sorting from DB)
	for _, v := range dates {
		db.WriteDailyUserStats(&v)
	}

	wf.RedirectHandler = nil
}
Example #2
0
func main() {
	commandFuncs["clear"]()
	flag.Parse()
	if *debug {
		log.Println("Debug Active")
	}

	// Start Web Server
	log.Println("Start Web Server")
	wf := web.MakeWebFace(*addr, *staticFldr, *templateFldr)
	wf.RedirectHandler = func(rw http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(rw, "Starting Server on %s", *addr)
	}

	// Setup Database
	log.Println("Setup Database")
	db := database.OpenDB(*db)

	// Get Identity
	log.Println("Get Identity")
	userStat := db.LoadNextUser("")

	// First Time Load
	if userStat == nil {

		// Login
		log.Println("Login")
		Tok, cErr := google.Login(wf, google.GetClientScope())
		if cErr != nil {
			log.Fatalln("Login Error:", cErr)
		}

		log.Println("===== FRESH DATABSE SETUP =====")

		iTok, iErr := google.GetIdentity(Tok)
		if iErr != nil {
			log.Fatalln("Identity Error:", iErr)
		}

		b := new(bytes.Buffer)
		google.EncodeToken(Tok, b)

		userStat = &stat.UserStat{
			UpdateDate: time.Now().String(),
			Token:      b.Bytes(),
			Email:      iTok.Email,
			UserID:     iTok.UserId,
		}

		// Get & Write DB
		db.WriteUserStats(userStat)

		// Init DB
		SetupDatabase(wf, db)
	}

	// REBUILD DEBUG
	{

		// Generate Daily Stat
		docs := []*stat.DocStat{}
		for f := db.LoadNextFileStat(""); f != nil; f = db.LoadNextFileStat(f.FileId) {
			docs = append(docs, f)
		}

		dates := stat.CreateDailyUserStat(docs)

		// Slower but good test (and get sorting from DB)
		for _, v := range dates {
			db.WriteDailyUserStats(&v)
		}
	}

	log.Println("User", userStat.UserID, userStat.Email)

	// Setup Webface with Database
	log.Println("Setup Webface with Database")
	SetupWebFace(wf, db)
	wf.RedirectHandler = nil

	// Running Loop
	log.Println("Running Loop")
	commandLoop()

	// Clean up
	log.Println("Clean up")
	db.CloseDB()
}