DBhelper.js 10.3 KB
// Define your models and their properties
import Realm from 'realm'

class Waste {}
Waste.schema = {
  name: 'Waste',
  properties: {
    weight: 'int',
    diff: 'int',
    date: 'float',
  }
};

class ID {}
ID.schema={
  name:'ID',
  properties:{
    id : 'string',
  }
};

export function registerID(_id){
  if(_id != ''){
    realm.write(()=>{
    let user = realm.create('ID',{id:_id});
    console.log('success')
  });
  } 
}

export function getId(){
  let user = realm.objects('ID');
  console.log(user.length);
  if(user.length>0)
    return true;
  return false;
}

export function deleteId(){
  let user = realm.objects('ID');
  realm.write(()=>{
    realm.delete(user);
    console.log('success')
  })
}

// Get the default Realm with support for our objects
let realm = new Realm({schema: [Waste, ID]});
export function add_data(responseJson){
   for(i = 0; i < responseJson.length; i++){
        realm.write(() => {
            // responseJson[0].ras_id.S
            let myWaste = realm.create('Waste', {
                weight: parseFloat(responseJson[i].weight.N),
                diff: parseFloat(responseJson[i].diff.N),
                date: parseFloat(responseJson[i].save_date.S),
            });
          });
        
        console.log(responseJson[i].save_date.S)
    }
    return responseJson.length;
}

export function get_now_weight(){
    let wastes = realm.objects('Waste');
    let recent = wastes.max('date');
    
    var recentWeight = wastes.filtered('date = $0', recent);
    console.log("now_weight",recentWeight)
    if(recentWeight.length != 0)
      return recentWeight[0].weight;
    else return 0;
}

export function get_last_update(){
    let recent = realm.objects('Waste').max('date');
    let recentWeight = wastes.filtered('date = %0', recent);
    console.log(recentWeight[0].weight)
    return recentWeight[0].weight;
}

export function get_recent_date(){
    
    return realm.objects('Waste').max("date");
}

// export function get_today_data(today){
//     let wastes = realm.objects('Waste');
//     var list = [];
//     for(var i =0; i<wastes.length; i++){
//       if(get_date(wastes[i].date) == get_date(today)){
//         temp = [waste[i].date, waste[i].diff, waste[i].weight]
//         list.append(temp);
//       } 
//     }
//     return list;
//   }

export function get_today_data(){
  var today_start = get_today_start();
  console.log("today_start: ",today_start)
  let wastes = realm.objects('Waste').filtered('date >= $0',today_start);
  var list = [];
  for(var i = 0; i < wastes.length; i++){
    var save_time = new Date(wastes[i].date);
    var temp = [save_time.getHours()+":"+save_time.getMinutes()+":"+save_time.getSeconds(), wastes[i].diff, wastes[i].weight+"g"]
    list.push(temp);
  }
  console.log(list);
  console.log("count:", wastes.length)
  return list;
}

export function get_week_data(){
  var today = new Date();
  var daybefore1 = new Date();
  daybefore1.setDate(today.getDate()-1);
  var daybefore2 = new Date();
  daybefore2.setDate(today.getDate()-2);
  var daybefore3 = new Date();
  daybefore3.setDate(today.getDate()-3);
  var daybefore4 = new Date();
  daybefore4.setDate(today.getDate()-4);
  var daybefore5 = new Date();
  daybefore5.setDate(today.getDate()-5);
  var daybefore6 = new Date();
  daybefore6.setDate(today.getDate()-6);
  var day = [daybefore6.getDay(), daybefore5.getDay(),daybefore4.getDay(),
    daybefore3.getDay(),daybefore2.getDay(),daybefore1.getDay(), today.getDay()]
  today = today.getTime();
  daybefore1 = daybefore1.getTime();
  daybefore2 = daybefore2.getTime();
  daybefore3 = daybefore3.getTime();
  daybefore4 = daybefore4.getTime();
  daybefore5 = daybefore5.getTime();
  daybefore6 = daybefore6.getTime();
 
  let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore6, daybefore5);
  let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore5, daybefore4);
  let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore4, daybefore3);
  let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore3, daybefore2);
  let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore2, daybefore1);
  let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore1, today);
  let wastes7 = realm.objects('Waste').filtered('date >= $0', today);
  var list = [];
  var total1 =0, total2=0, total3=0, total4=0, total5=0, total6=0, total7 = 0;
  
  for(var i = 0; i< wastes1.length; i++){
    total1 += wastes1[i].diff;
  }
  for(var i = 0; i< wastes2.length; i++){
    total2 += wastes2[i].diff;
  }
  for(var i = 0; i< wastes3.length; i++){
    total3 += wastes3[i].diff;
  }
  for(var i = 0; i< wastes4.length; i++){
    total4 += wastes4[i].diff;
  }
  for(var i = 0; i< wastes5.length; i++){
    total5 += wastes5[i].diff;
  }
  for(var i = 0; i< wastes6.length; i++){
    total6 += wastes6[i].diff;
  }
  for(var i = 0; i< wastes7.length; i++){
    total7 += wastes7[i].diff;
  }
  list = [[total1, total2, total3, total4, total5, total6, total7],day]
  return list;
}

export function get_month_data(){
  var today = new Date();
  var sep1 = new Date(today.getFullYear(), today.getMonth()-1, 1);
  var sep2 = new Date(today.getFullYear(), today.getMonth()-1, 7);
  var sep3 = new Date(today.getFullYear(), today.getMonth()-1, 14);
  var sep4 = new Date(today.getFullYear(), today.getMonth()-1, 21);
  var sep5 = new Date(today.getFullYear(), today.getMonth()-1, 28);
  var sep6 = new Date(today.getFullYear(), today.getMonth(), 1)

  sep1 = sep1.getTime();
  sep2 = sep2.getTime();
  sep3 = sep3.getTime();
  sep4 = sep4.getTime();
  sep5 = sep5.getTime();
  sep6 = sep6.getTime();
 
  let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep1, sep2);
  let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep2, sep3);
  let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep3, sep4);
  let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep4, sep5);
  let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep5, sep6);

  var list = [];
  var total1 =0, total2=0, total3=0, total4=0, total5=0;
  
  for(var i = 0; i< wastes1.length; i++){
    total1 += wastes1[i].diff;
  }
  for(var i = 0; i< wastes2.length; i++){
    total2 += wastes2[i].diff;
  }
  for(var i = 0; i< wastes3.length; i++){
    total3 += wastes3[i].diff;
  }
  for(var i = 0; i< wastes4.length; i++){
    total4 += wastes4[i].diff;
  }
  for(var i = 0; i< wastes5.length; i++){
    total5 += wastes5[i].diff;
  }
  list = [total1, total2, total3, total4, total5]
  return list;
}

export function get_year_data(){
  var today = new Date();
  var mon1 = new Date(today.getFullYear()-1, 0, 1);
  var mon2 = new Date(today.getFullYear()-1, 1, 1);
  var mon3 = new Date(today.getFullYear()-1, 2, 1);
  var mon4 = new Date(today.getFullYear()-1, 3, 1);
  var mon5 = new Date(today.getFullYear()-1, 4, 1);
  var mon6 = new Date(today.getFullYear()-1, 5, 1);
  var mon7 = new Date(today.getFullYear()-1, 6, 1);
  var mon8 = new Date(today.getFullYear()-1, 7, 1);
  var mon9 = new Date(today.getFullYear()-1, 8, 1);
  var mon10 = new Date(today.getFullYear()-1, 9, 1);
  var mon11 = new Date(today.getFullYear()-1, 10, 1);
  var mon12 = new Date(today.getFullYear()-1, 11, 1);
  var nYear = new Date(today.getFullYear(), 0, 1);

  mon1 = mon1.getTime();
  mon2 = mon2.getTime();
  mon3 = mon3.getTime();
  mon4 = mon4.getTime();
  mon5 = mon5.getTime();
  mon6 = mon6.getTime();
  mon7 = mon7.getTime();
  mon8 = mon8.getTime();
  mon9 = mon9.getTime();
  mon10 = mon10.getTime();
  mon11 = mon11.getTime();
  mon12 = mon12.getTime();
  nYear = nYear.getTime();

 
  let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon1, mon2);
  let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon2, mon3);
  let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon3, mon4);
  let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon4, mon5);
  let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon5, mon6);
  let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon6, mon7);
  let wastes7 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon7, mon8);
  let wastes8 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon8, mon9);
  let wastes9 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon9, mon10);
  let wastes10 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon10, mon11);
  let wastes11 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon11, mon12);
  let wastes12 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon12, nYear);

  var list = [];
  var total1 =0, total2=0, total3=0, total4=0, total5=0,
  total6 =0, total7=0, total8=0, total9=0, total10=0,
  total11 =0, total12=0;
  
  for(var i = 0; i< wastes1.length; i++){
    total1 += wastes1[i].diff;
  }
  for(var i = 0; i< wastes2.length; i++){
    total2 += wastes2[i].diff;
  }
  for(var i = 0; i< wastes3.length; i++){
    total3 += wastes3[i].diff;
  }
  for(var i = 0; i< wastes4.length; i++){
    total4 += wastes4[i].diff;
  }
  for(var i = 0; i< wastes5.length; i++){
    total5 += wastes5[i].diff;
  }
  for(var i = 0; i< wastes6.length; i++){
    total6 += wastes6[i].diff;
  }
  for(var i = 0; i< wastes7.length; i++){
    total7 += wastes7[i].diff;
  }
  for(var i = 0; i< wastes8.length; i++){
    total8 += wastes8[i].diff;
  }
  for(var i = 0; i< wastes9.length; i++){
    total9 += wastes9[i].diff;
  }
  for(var i = 0; i< wastes10.length; i++){
    total10 += wastes10[i].diff;
  }
  for(var i = 0; i< wastes11.length; i++){
    total11 += wastes11[i].diff;
  }
  for(var i = 0; i< wastes12.length; i++){
    total12 += wastes12[i].diff;
  }
  
  list = [total1, total2, total3, total4, total5,total6, total7, total8, total9, total10,total11, total12]
  return list;
}

export function delete_all_data(today){
    let all_wastes = realm.objects('Waste');
    realm.write(() => {
      realm.delete(all_wastes);
    })
    var wastes = realm.objects('Waste')
    console.log("delete, count: ", wastes.length)
}

function get_today_start(){
  var today = new Date();
  var dd = today. getDate();
  var mm = today.getMonth();
  var yyyy = today. getFullYear();

  var togettime = new Date(2019,11,13).getTime();
  console.log(togettime)
  return togettime;
}