Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate sds with launcher #49

Merged
merged 17 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions authentication/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type QuestionnaireSchema struct {
Metadata []Metadata `json:"metadata"`
SchemaName string `json:"schema_name"`
SurveyType string `json:"theme"`
SurveyId string `json:"survey_id"`
}

// Metadata is a representation of the metadata within the schema with an additional `Default` value
Expand Down Expand Up @@ -629,11 +630,10 @@ func GenerateTokenFromPost(postValues url.Values, launchVersion2 bool) (string,
return token, ""
}

// GetRequiredMetadata Gets the required metadata from a schema
func GetRequiredMetadata(launcherSchema surveys.LauncherSchema) ([]Metadata, string) {
func GetSurveyData(launcherSchema surveys.LauncherSchema) (QuestionnaireSchema, string) {
schema, error := getSchema(launcherSchema)
if error != "" {
return nil, fmt.Sprintf("getSchema failed err: %v", error)
return QuestionnaireSchema{}, fmt.Sprintf("getSchema failed err: %v", error)
}

defaults := GetDefaultValues()
Expand Down Expand Up @@ -664,7 +664,13 @@ func GetRequiredMetadata(launcherSchema surveys.LauncherSchema) ([]Metadata, str
schema.Metadata = append(schema.Metadata, v)
}

return schema.Metadata, ""
return schema, ""
}

// GetRequiredMetadata Gets the required metadata from a schema
func GetRequiredMetadata(launcherSchema surveys.LauncherSchema) ([]Metadata, string) {
surveyData, err := GetSurveyData(launcherSchema)
return surveyData.Metadata, err
}

func getSchema(launcherSchema surveys.LauncherSchema) (QuestionnaireSchema, string) {
Expand Down
27 changes: 21 additions & 6 deletions launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,40 @@ func postLaunchHandler(w http.ResponseWriter, r *http.Request) {
redirectURL(w, r)
}

func getMetadataHandler(w http.ResponseWriter, r *http.Request) {
func getSurveyDataHandler(w http.ResponseWriter, r *http.Request) {
schemaName := r.URL.Query().Get("schema_name")
schemaUrl := r.URL.Query().Get("schema_url")

launcherSchema := surveys.GetLauncherSchema(schemaName, schemaUrl)

metadata, err := authentication.GetRequiredMetadata(launcherSchema)
surveyData, err := authentication.GetSurveyData(launcherSchema)

if err != "" {
http.Error(w, fmt.Sprintf("GetRequiredMetadata err: %v", err), 500)
http.Error(w, fmt.Sprintf("GetSurveyData err: %v", err), 500)
return
}

metadataJSON, _ := json.Marshal(metadata)
surveyDataJSON, _ := json.Marshal(surveyData)

w.Write([]byte(metadataJSON))
w.Write([]byte(surveyDataJSON))

return
}

func getSupplementaryDataHandler(w http.ResponseWriter, r *http.Request) {
surveyId := r.URL.Query().Get("survey_id")
periodId := r.URL.Query().Get("period_id")

datasets, err := surveys.GetSupplementaryDataSets(surveyId, periodId)
if err != nil {
http.Error(w, fmt.Sprintf("GetSupplementaryDataSets err: %v", err), 500)
return
}
datasetJSON, _ := json.Marshal(datasets)

w.Write([]byte(datasetJSON))
}

func getAccountServiceURL(r *http.Request) string {
forwardedProtocol := r.Header.Get("X-Forwarded-Proto")

Expand Down Expand Up @@ -206,7 +220,8 @@ func main() {
// Launch handlers
r.HandleFunc("/", getLaunchHandler).Methods("GET")
r.HandleFunc("/", postLaunchHandler).Methods("POST")
r.HandleFunc("/metadata", getMetadataHandler).Methods("GET")
r.HandleFunc("/survey-data", getSurveyDataHandler).Methods("GET")
r.HandleFunc("/supplementary-data", getSupplementaryDataHandler).Methods("GET")

//Author Launcher with passed parameters in Url
r.HandleFunc("/quick-launch", quickLauncherHandler).Methods("GET")
Expand Down
1 change: 1 addition & 0 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
setSetting("SURVEY_RUNNER_SCHEMA_URL", Get("SURVEY_RUNNER_URL"))
setSetting("SCHEMA_VALIDATOR_URL", "")
setSetting("SURVEY_REGISTER_URL", "")
setSetting("SDS_API_URL", "http://localhost:5003")
setSetting("JWT_ENCRYPTION_KEY_PATH", "jwt-test-keys/sdc-user-authentication-encryption-sr-public-key.pem")
setSetting("JWT_SIGNING_KEY_PATH", "jwt-test-keys/sdc-user-authentication-signing-launcher-private-key.pem")
}
Expand Down
2 changes: 1 addition & 1 deletion static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ label { display:inline-block; font-weight: 600; margin-bottom: 0.25rem; width:20
position: relative;
}

.field-container--hidden, .metadata-fields--hidden, .button--hidden, .group-title--hidden {
.field-container--hidden, .metadata-fields--hidden, .button--hidden, .group-title--hidden, .supplementary-data--hidden {
display:none;
}

Expand Down
40 changes: 40 additions & 0 deletions surveys/surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package surveys

import (
"encoding/json"
"errors"
"log"
"regexp"

Expand All @@ -22,6 +23,19 @@ type LauncherSchema struct {
URL string
}

type DatasetMetadata struct {
SurveyID string `json:"survey_id"`
PeriodID string `json:"period_id"`
Title string `json:"title"`
SdsSchemaVersion int `json:"sds_schema_version"`
SdsPublishedAt string `json:"sds_published_at"`
TotalReportingUnits int `json:"total_reporting_units"`
SchemaVersion string `json:"schema_version"`
SdsDatasetVersion int `json:"sds_dataset_version"`
Filename string `json:"filename"`
DatasetID string `json:"dataset_id"`
}

// RegisterResponse is the response from the eq-survey-register request
type RegisterResponse struct {
jsonhal.Hal
Expand Down Expand Up @@ -172,6 +186,32 @@ func FindSurveyByName(name string) LauncherSchema {
panic("Schema not found")
}

func GetSupplementaryDataSets(surveyId string, periodId string) ([]DatasetMetadata, error) {
datasetList := []DatasetMetadata{}
hostURL := settings.Get("SDS_API_URL")
log.Printf("SDS Api URL: %s", hostURL)
url := fmt.Sprintf("%s/v1/dataset_metadata?survey_id=%s&period_id=%s", hostURL, surveyId, periodId)
resp, err := clients.GetHTTPClient().Get(url)

if err != nil || (resp.StatusCode != 200 && resp.StatusCode != 404){
MebinAbraham marked this conversation as resolved.
Show resolved Hide resolved
return datasetList, errors.New("unable to fetch supplementary data")
}
if resp.StatusCode == 404 {
return datasetList, nil
}
responseBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return datasetList, errors.New("unable to read response body of supplementary data")
}

if err := json.Unmarshal(responseBody, &datasetList); err != nil {
log.Print(err)
return datasetList, errors.New(fmt.Sprintf("%v", err))
}
return datasetList, nil
}

// Return a LauncherSchema instance by loading schema from name or URL
func GetLauncherSchema(schemaName string, schemaUrl string) LauncherSchema {
var launcherSchema LauncherSchema
Expand Down
Loading