DBhelper.js 9.75 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(var 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 add_temp_data(){
  realm.write(() => {
    let myWaste = realm.create('Waste', {
        weight: parseFloat(747),
        diff: parseFloat(257),
        date: parseFloat(1576691588000),
    });
  });
  realm.write(() => {
    let myWaste = realm.create('Waste', {
        weight: parseFloat(1466),
        diff: parseFloat(468),
        date: parseFloat(1576650859000),
    });
  });
}

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[recentWeight.length-1].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).sum('diff');
  let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore5, daybefore4).sum('diff');
  let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore4, daybefore3).sum('diff');
  let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore3, daybefore2).sum('diff');
  let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore2, daybefore1).sum('diff');
  let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore1, today).sum('diff');
  let wastes7 = realm.objects('Waste').filtered('date >= $0', today).sum('diff');

  
  var week = ['SUN','MON','TUE', 'WED', 'THU', 'FRI', 'SAT'];
  var data1 = [
    { label: week[day[0]], value: wastes1 },
    { label: week[day[1]], value: wastes2 },
    { label: week[day[2]], value: wastes3 },
    { label: week[day[3]], value: wastes4 },
    { label: week[day[4]], value: wastes5 },
    { label: week[day[5]], value: wastes6 },
    { label: week[day[6]], value: wastes7 }
  ]
  console.log(data1)
  return data1;
}

function getM_D(time){
  console.log("time",time)
  var day = new Date(time);
  console.log(day.getDate())
  return day.getMonth()+1+ "/" +day.getDate();
}

export function get_month_data(){
  var day7 = (24*60*60*1000) * 7;
  var today = new Date();
  today.setHours(0,0,0,0);
  var day = today.getDay();
  var diff = today.getDate() - day + (day == 0 ? -6:1);
  
  var sep5 = new Date(today.setDate(diff));
  var sep5 = sep5.getTime();
  var sep4 = sep5 - day7;
  var sep3 = sep4 - day7;
  var sep2 = sep3 - day7;
  var sep1 = sep2 - day7;
  console.log("first day of week: ", sep5, sep4, sep3, sep2, sep1);

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

  var data2 = [
    { label: getM_D(sep1), value: wastes1 },
    { label: getM_D(sep2), value: wastes2 },
    { label: getM_D(sep3), value: wastes3 },
    { label: getM_D(sep4), value: wastes4 },
    { label: getM_D(sep5), value: wastes5 }
  ]
  console.log(data2)
  return data2;
}

export function get_year_data(){
  var today = new Date();
  var mon1 = new Date(today.getFullYear(), 0, 1);
  var mon2 = new Date(today.getFullYear(), 1, 1);
  var mon3 = new Date(today.getFullYear(), 2, 1);
  var mon4 = new Date(today.getFullYear(), 3, 1);
  var mon5 = new Date(today.getFullYear(), 4, 1);
  var mon6 = new Date(today.getFullYear(), 5, 1);
  var mon7 = new Date(today.getFullYear(), 6, 1);
  var mon8 = new Date(today.getFullYear(), 7, 1);
  var mon9 = new Date(today.getFullYear(), 8, 1);
  var mon10 = new Date(today.getFullYear(), 9, 1);
  var mon11 = new Date(today.getFullYear(), 10, 1);
  var mon12 = new Date(today.getFullYear(), 11, 1);
  var nYear = new Date(today.getFullYear()+1, 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).sum('diff');
  let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon2, mon3).sum('diff');
  let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon3, mon4).sum('diff');
  let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon4, mon5).sum('diff');
  let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon5, mon6).sum('diff');
  let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon6, mon7).sum('diff');
  let wastes7 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon7, mon8).sum('diff');
  let wastes8 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon8, mon9).sum('diff');
  let wastes9 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon9, mon10).sum('diff');
  let wastes10 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon10, mon11).sum('diff');
  let wastes11 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon11, mon12).sum('diff');
  let wastes12 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon12, nYear).sum('diff');

  var data3 = [
    { label: 'Jan', value: wastes1 },
    { label: 'Feb', value: wastes2 },
    { label: 'Mar', value: wastes3 },
    { label: 'Apr', value: wastes4 },
    { label: 'May', value: wastes5 },
    { label: 'Jun', value: wastes6 },
    { label: 'Jul', value: wastes7 },
    { label: 'Aug', value: wastes8 },
    { label: 'Sep', value: wastes9 },
    { label: 'Oct', value: wastes10 },
    { label: 'Nov', value: wastes11 },
    { label: 'Dec', value: wastes12 }
]
console.log(data3)
  return data3;
}

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(yyyy,mm,dd).getTime();
  console.log(togettime)
  return togettime;
}