Skip to content

Commit

Permalink
Minimal updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Dokotela committed Nov 15, 2024
1 parent 5b934b8 commit d7e5c64
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 57 deletions.
90 changes: 90 additions & 0 deletions assets/mimic_iv/split_mimic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)

// SplitNDJSON splits a large NDJSON file into smaller chunks
func SplitNDJSON(inputFile string, outputDir string, linesPerFile int) error {
// Open the input NDJSON file
file, err := os.Open(inputFile)
if err != nil {
return fmt.Errorf("failed to open input file: %v", err)
}
defer file.Close()

// Create the output directory if it doesn't exist
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %v", err)
}

// Extract the base name of the input file (without extension)
baseFileName := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))

// Initialize reader
scanner := bufio.NewScanner(file)

// Variables to track the current chunk
var currentFile *os.File
var currentLineCount int
var fileIndex int

for scanner.Scan() {
// If starting a new chunk, create a new file
if currentLineCount == 0 {
outputFile := filepath.Join(outputDir, fmt.Sprintf("%s_%d.ndjson", baseFileName, fileIndex))
currentFile, err = os.Create(outputFile)
if err != nil {
return fmt.Errorf("failed to create output file: %v", err)
}
defer currentFile.Close()
log.Printf("Created new chunk: %s", outputFile)
}

// Write the current line to the file
_, err := currentFile.WriteString(scanner.Text() + "\n")
if err != nil {
return fmt.Errorf("failed to write to output file: %v", err)
}

// Increment the line count
currentLineCount++

// If we reach the limit, close the current file and start a new one
if currentLineCount >= linesPerFile {
currentFile.Close()
currentLineCount = 0
fileIndex++
}
}

if scanner.Err() != nil {
return fmt.Errorf("error reading file: %v", scanner.Err())
}

// Close the last file if it's still open
if currentFile != nil {
currentFile.Close()
}

return nil
}

func main() {
// Input file, output directory, and lines per file
inputFile := "ObservationChartevents.ndjson"
outputDir := "./"
linesPerFile := 20000 // Adjust based on your needs

err := SplitNDJSON(inputFile, outputDir, linesPerFile)
if err != nil {
log.Fatalf("Error splitting NDJSON file: %v", err)
}

log.Println("NDJSON file split successfully!")
}
4 changes: 4 additions & 0 deletions build/build_android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ if [ $? -ne 0 ]; then
echo "Android build failed!"
exit 1
fi
mv pocketfhir.aar ../fhir_ant/android/app/libs/pocketfhir.aar
rm pocketfhir-sources.jar

echo "PocketFHIR Android build completed successfully."

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/google/fhir/go v0.7.4
github.com/labstack/echo/v5 v5.0.0-20230722203903-ec5b858dab61
github.com/pocketbase/pocketbase v0.22.15
golang.org/x/net v0.31.0
google.golang.org/protobuf v1.34.2
)

Expand Down Expand Up @@ -80,7 +81,6 @@ require (
golang.org/x/image v0.22.0 // indirect
golang.org/x/mobile v0.0.0-20241108191957-fa514ef75a0f // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import "github.com/fhir-fli/pocketfhir/pocketfhir"

func main() {
// Use the string wrapper for local development
dataDir := "."
pocketfhir.RunServer(dataDir)
dataDir := "./assets"
hostname := "127.0.0.1"
port := "8080" // Changed from 443 to 8080 to avoid permission issues
getApiLogs := false

pocketfhir.RunServer(dataDir, hostname, port, getApiLogs)
}
44 changes: 0 additions & 44 deletions pocketfhir/fhir_helpers.go

This file was deleted.

10 changes: 5 additions & 5 deletions pocketfhir/initial_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func initializeCollections(app *pocketbase.PocketBase) error {
// Step 2: Check if FHIR spec has been initialized
if !isFhirSpecInitialized(app) {
// Load the FHIR spec into the database
if err := loadFhirSpecOnce(app); err != nil {
log.Printf("Failed to load FHIR spec: %v", err)
return err
}
// if err := loadFhirSpecOnce(app); err != nil {
// log.Printf("Failed to load FHIR spec: %v", err)
// return err
// }

// Set the FHIR spec as initialized
setFhirSpecInitialized(app)
// setFhirSpecInitialized(app)
} else {
log.Println("FHIR spec already initialized.")
}
Expand Down
36 changes: 36 additions & 0 deletions pocketfhir/load_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,39 @@ func loadNDJSONFile(app *pocketbase.PocketBase, filePath string) error {

return nil
}

// Check if the FHIR spec is already initialized
func isFhirSpecInitialized(app *pocketbase.PocketBase) bool {
metadataCollection, err := app.Dao().FindCollectionByNameOrId("metadata")
if err != nil {
log.Printf("Metadata collection not found: %v", err)
return false
}

// Check for a record indicating FHIR spec initialization
record, err := app.Dao().FindFirstRecordByData(metadataCollection.Id, "fhirSpecInitialized", true)
if err != nil || record == nil {
return false
}

return true
}

// Mark the FHIR spec as initialized in the "metadata" collection
func setFhirSpecInitialized(app *pocketbase.PocketBase) {
metadataCollection, err := app.Dao().FindCollectionByNameOrId("metadata")
if err != nil {
log.Printf("Metadata collection not found: %v", err)
return
}

// Create a new record in the metadata collection
record := models.NewRecord(metadataCollection)
record.Set("fhirSpecInitialized", true)

if err := app.Dao().SaveRecord(record); err != nil {
log.Printf("Failed to save metadata record: %v", err)
} else {
log.Println("FHIR spec initialization marked as complete.")
}
}
Loading

0 comments on commit d7e5c64

Please sign in to comment.