package edu.neusoft.project.util;

import com.csvreader.CsvReader;
import com.opencsv.CSVReaderBuilder;
import edu.neusoft.project.model.vo.Monite;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Iterator;

public class CSVReader {
    private String url;

    public CSVReader(String url) {
        this.url = url;
    }

    public void readlastLine() throws IOException {
        RandomAccessFile raf = new RandomAccessFile(url, "r");
        long len = raf.length();
        String lastLine = "";
        if (len != 0L) {
            long pos = len - 1;
            while (pos > 0) {
                pos--;
                raf.seek(pos);
                if (raf.readByte() == '\n') {
                    lastLine = raf.readLine();
                    break;
                }
            }
        }
        raf.close();
        System.out.println(lastLine);
    }
    public  Monite getElementLine () throws IOException {
        CsvReader r = new CsvReader(url, ',',Charset.forName("GBK"));
        String cpu= "";
        String memory="";
        String date="";
                 //读取表头
                 r.readHeaders();
                 //逐条读取记录，直至读完
                r.readRecord();
                 while (r.readRecord()) {
//                     memory=r.get("\\\\iZa3datzzyguwdZ\\Memory\\Available MBytes");
                     memory=r.get("\\\\OSA-01712041922\\Memory\\Available MBytes");

//                     cpu=r.get("\\\\iZa3datzzyguwdZ\\Processor(_Total)\\% Processor Time");
                     cpu=r.get("\\\\OSA-01712041922\\Processor(_Total)\\% Processor Time");
                 }
                 if (memory!=""){
                     int my = 32-((Integer.parseInt(memory)/1024)/32);
                     memory=String.valueOf(my);
                 }//OSA
//                 if (memory!=""){
//                     int my = 2-((Integer.parseInt(memory)/1024)/2);
//                     memory=String.valueOf(my);
//                 }//Aliyun

        r.close();
        Monite monite = new Monite(memory,cpu);
        return monite;
    }
}
