Skip to content

Commit

Permalink
setup json_adder to test taking date from filename locally
Browse files Browse the repository at this point in the history
also some formatting changes
  • Loading branch information
extua committed Aug 5, 2024
1 parent 1e9f8b3 commit 43e02d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion graphql_server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const graphqlHTTP = require('express-graphql').graphqlHTTP;
const graphql = require('graphql');
const { MongoClient } = require('mongodb');

const context = () => MongoClient.connect('mongodb+srv://databaseReader:[email protected]/').then(client => client.db('communities'));
const context = () => MongoClient.connect('mongodb://ADMIN:PASSWORD@localhost:27017').then(client => client.db('communities'));

const schema = require('./schema.js');

Expand Down
22 changes: 13 additions & 9 deletions json_adder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use chrono::{NaiveDateTime, Utc};
use mongodb::{bson, Collection};
use serde_json::Value;
use std::fs;
mod setup_db;
mod models;
mod setup_db;

const DATA_DIRECTORY: &str = "../../api.freifunk.net/data/history/";

Expand All @@ -14,7 +14,9 @@ async fn main() -> mongodb::error::Result<()> {

for file in fs::read_dir(DATA_DIRECTORY).unwrap() {
// File path for sample json file, change this later
let file_path = file.unwrap().path();
let file_path: std::path::PathBuf = file.unwrap().path();

println!("{:?}", file_path.file_name().expect("filename not found"));

// Convert JSON to string, then to value, then to bson
let contents: String = fs::read_to_string(file_path).expect("couldn't read file");
Expand Down Expand Up @@ -50,14 +52,16 @@ async fn main() -> mongodb::error::Result<()> {
communities_in_snapshot.push(community);
}

// print!("{:?}",communities_in_snapshot);

// Insert lots of documents in one go
let insert_many_result = snapshot_collection
.insert_many(communities_in_snapshot, None)
.await?;
println!("Inserted documents with _ids:");
for (_key, value) in &insert_many_result.inserted_ids {
println!("{}", value);
}
// let insert_many_result = snapshot_collection
// .insert_many(communities_in_snapshot, None)
// .await?;
// println!("Inserted documents with _ids:");
// for (_key, value) in &insert_many_result.inserted_ids {
// println!("{}", value);
// }
}

Ok(())
Expand Down
23 changes: 14 additions & 9 deletions json_adder/src/setup_db.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use crate::models;
use bson::doc;
use mongodb::{
bson, options::{
bson,
options::{
ClientOptions, CreateCollectionOptions, ServerApi, ServerApiVersion, TimeseriesGranularity,
TimeseriesOptions,
}, Client, Collection
},
Client, Collection,
};
use crate::models;

pub async fn get_collection() -> Collection<models::Community> {

// boilerplate connection code
// changed this to a const, test it?
// local database
const URI: &str = "mongodb://ADMIN:PASSWORD@localhost:27017";
// remote database
// const URL: &str = "mongodb+srv://pierremarshall:${password}@freifunktest.zsfzlav.mongodb.net/";

let mut client_options = ClientOptions::parse_async(URI).await.unwrap();

// Set the server_api field of the client_options object to Stable API version 1
Expand All @@ -24,7 +28,7 @@ pub async fn get_collection() -> Collection<models::Community> {
let db_list: Vec<String> = client.list_database_names(doc! {}, None).await.unwrap();
println!("List databases: {:?}", db_list);
let db = client.database("communities");

// List collections in the database
let coll_list: Vec<String> = db.list_collection_names(doc! {}).await.unwrap();
println!("List collections in database: {:?}", coll_list);
Expand All @@ -42,11 +46,12 @@ pub async fn get_collection() -> Collection<models::Community> {
let coll_opts = CreateCollectionOptions::builder()
.timeseries(ts_opts)
.build();
db.create_collection("hourly_snapshot", coll_opts).await.unwrap();
db.create_collection("hourly_snapshot", coll_opts)
.await
.unwrap();
};

// Return the collection
let snapshot_collection: Collection<models::Community> = db.collection("hourly_snapshot");
return snapshot_collection
return snapshot_collection;
}

0 comments on commit 43e02d4

Please sign in to comment.